1 #ifndef _KNOWLEDGE_RECORD_CPP_ 2 #define _KNOWLEDGE_RECORD_CPP_ 16 int madara_double_precision (-1);
18 bool madara_use_scientific (
false);
26 return madara_double_precision;
33 "KnowledgeRecord::set_precision:" \
34 " setting precision to %d\n", madara_double_precision);
36 madara_double_precision = new_precision;
43 "KnowledgeRecord::set_fixed:" \
44 " setting output format to std::fixed\n");
46 madara_use_scientific =
false;
53 "KnowledgeRecord::set_scientific:" \
54 " setting output format to std::scientific\n");
56 madara_use_scientific =
true;
61 const std::string & filename, uint32_t read_as_type)
65 bool add_zero_char =
false;
70 std::string::size_type position = filename.rfind (
'.');
72 filename.size () - position);
77 extension ==
".txt" || extension ==
".xml")
87 || extension ==
".txt" || extension ==
".xml")
90 str_value_ = std::make_shared<std::string> ((
char *)buffer, size);
94 else if (extension ==
".xml")
101 unsigned char *ucbuf = (
unsigned char *)buffer;
104 if (extension ==
".jpg" || read_as_type ==
IMAGE_JPEG)
137 (
void *)buffer.c_str (), buffer.size ());
153 std::stringstream buffer;
180 std::stringstream buffer;
196 std::vector <KnowledgeRecord::Integer>
202 integers.push_back(0);
206 unsigned int size = (
unsigned int)this->size ();
207 integers.resize (size);
215 const Integer * ptr_temp = &(*int_array_)[0];
217 for (
unsigned int i = 0; i <
size; ++i)
218 integers[i] = ptr_temp[i];
224 const double * ptr_temp = &(*double_array_)[0];
226 for (
unsigned int i = 0; i <
size; ++i)
227 integers[i] =
Integer (ptr_temp[i]);
233 for (
unsigned int i = 0; i <
size; ++i)
234 integers[i] =
Integer (ptr_temp[i]);
238 const unsigned char * ptr_temp = &(*file_value_)[0];
240 for (
unsigned int i = 0; i <
size; ++i)
241 integers[i] =
Integer (ptr_temp[i]);
253 doubles.push_back(0);
257 unsigned int size = (
unsigned int)this->size ();
258 doubles.resize (size);
264 const Integer * ptr_temp = &(*int_array_)[0];
266 for (
unsigned int i = 0; i <
size; ++i)
267 doubles[i] =
double (ptr_temp[i]);
273 const double * ptr_temp = &(*double_array_)[0];
275 for (
unsigned int i = 0; i <
size; ++i)
276 doubles[i] = ptr_temp[i];
282 for (
unsigned int i = 0; i <
size; ++i)
283 doubles[i] =
double (ptr_temp[i]);
287 const unsigned char * ptr_temp = &(*file_value_)[0];
289 for (
unsigned int i = 0; i <
size; ++i)
290 doubles[i] =
double (ptr_temp[i]);
307 " type_ is %d\n",
type_);
309 std::stringstream buffer;
315 const Integer * ptr_temp = &(*int_array_)[0];
323 for (uint32_t i = 1; i <
size; ++i, ++ptr_temp)
324 buffer << delimiter << *ptr_temp;
329 if (!madara_use_scientific)
332 "KnowledgeRecord::to_string: using fixed format\n");
334 buffer << std::fixed;
339 "KnowledgeRecord::to_string: using scientific format\n");
341 buffer << std::scientific;
344 if (madara_double_precision >= 0)
347 buffer << std::setprecision (madara_double_precision);
350 " precision set to %d\n", madara_double_precision);
355 " precision set to default\n", madara_double_precision);
363 if (!madara_use_scientific)
366 "KnowledgeRecord::to_string: using fixed format\n");
368 buffer << std::fixed;
373 "KnowledgeRecord::to_string: using scientific format\n");
375 buffer << std::scientific;
378 if (madara_double_precision >= 0)
380 buffer << std::setprecision (madara_double_precision);
383 " precision set to %d\n", madara_double_precision);
388 " precision set to default\n", madara_double_precision);
391 const double * ptr_temp = &(*double_array_)[0];
399 for (uint32_t i = 1; i <
size; ++i, ++ptr_temp)
400 buffer << delimiter << *ptr_temp;
404 buffer <<
"binary:size=";
407 return buffer.str ();
422 buffer =
new char [
size];
428 buffer =
new char [
size];
434 buffer =
new char [
size];
439 size =
sizeof(double);
440 buffer =
new char [
size];
446 buffer =
new char [
size];
452 buffer =
new char [
size];
459 return (
unsigned char *)buffer;
473 last = std::min <unsigned int> (last, size - 1);
476 char * new_buffer =
new char [last - first + 2];
478 memcpy (new_buffer,
str_value_->c_str () + first, last - first + 1);
479 new_buffer[last-first + 1] = 0;
488 last = std::min <unsigned int> (last, size - 1);
491 uint32_t bufsize = last - first + 1;
492 unsigned char * new_buffer =
new unsigned char [bufsize];
494 memcpy (new_buffer, &(*
file_value_)[0] + first, last - first + 1);
504 last = std::min <unsigned int> (last, size - 1);
505 uint32_t bufsize = last - first + 1;
508 integers.resize (bufsize);
509 Integer * ptr_temp = &(*int_array_)[0];
511 for (
unsigned int i = first; i <= last; ++i, ++ptr_temp)
512 integers[i] = *ptr_temp;
521 last = std::min <unsigned int> (last, size - 1);
522 uint32_t bufsize = last - first + 1;
525 doubles.resize (bufsize);
526 double * ptr_temp = &(*double_array_)[0];
528 for (
unsigned int i = first; i <= last; ++i, ++ptr_temp)
529 doubles[i] = *ptr_temp;
552 result = lhs < other;
558 result = lhs < other;
581 result = temp < other;
592 result = temp < other;
609 result = lhs < other;
616 result = lhs < other;
638 result = lhs <= other;
644 result = lhs <= other;
667 result = temp <= other;
678 result = temp <= other;
695 result = lhs <= other;
702 result = lhs <= other;
733 result = lhs == other;
739 result = lhs == other;
762 result = temp == other;
779 result = temp == other;
796 result = lhs == other;
804 result = lhs == other;
825 result = lhs > other;
831 result = lhs > other;
855 result = lhs > other;
866 result = lhs > other;
883 result = lhs > other;
891 result = lhs > other;
912 result = lhs >= other;
918 result = lhs >= other;
942 result = lhs >= other;
953 result = lhs >= other;
970 result = lhs >= other;
978 result = lhs >= other;
1026 std::vector<Integer> tmp(index + 1);
1052 std::vector<Integer> tmp(index + 1);
1060 size_t cur_size =
size ();
1062 if (cur_size == new_size) {
1068 if (new_size > cur_size)
1098 const std::string & key,
unsigned int , uint64_t ,
1103 if (key.length () > 0)
1106 " attempting to set %s=%s\n", key.c_str (),
to_string ().c_str ());
1133 " received data[%s]=%s.\n",
1137 else if (result == 0)
1140 " discarded data[%s]=%s as the value was already set.\n",
1143 else if (result == -1)
1146 " discarded data due to null key.\n");
1148 else if (result == -2)
1151 " discarded data[%s]=%s due to lower quality.\n",
1154 else if (result == -3)
1157 " discarded data[%s]=%" PRId64
" due to older timestamp.\n",
1168 " checking if record is non-zero.\n");
1175 return value < 0 || value > 0;
1193 #endif // _KNOWLEDGE_RECORD_CPP_ This class encapsulates an entry in a KnowledgeBase.
int apply(madara::knowledge::ThreadSafeContext &context, const std::string &key, unsigned int quality, uint64_t clock, bool perform_lock)
Apply the knowledge record to a context, given some quality and clock.
bool is_true(void) const
Checks to see if the record is true.
int update_record_from_external(const std::string &key, const knowledge::KnowledgeRecord &rhs, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true))
Atomically sets if the variable value meets update conditions.
static void set_fixed(void)
Sets the output format for doubles to std::fixed.
void emplace_integers(Args &&...args)
Construct a vector of integers within this KnowledgeRecord.
madara::knowledge::KnowledgeRecord KnowledgeRecord
void emplace_file(Args &&...args)
Construct a file (vector of unsigned char) within this KnowledgeRecord.
double to_double(void) const
converts the value to a float/double
KnowledgeRecord inc_index(size_t index)
increments the value at the index to the specified value.
void set_file(const unsigned char *new_value, size_t size)
sets the value to an unknown file type
MADARA_Export int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char=false)
Reads a file into a provided void pointer.
void lock(void) const
Locks the mutex on this context.
void clear_value(void) noexcept
clears any dynamic values.
bool is_binary_file_type(void) const
returns true if the knowledge record has a binary file type
ssize_t to_file(const std::string &filename) const
writes the value to a file
This class stores variables and their values for use by any entity needing state information in a thr...
KnowledgeRecord fragment(unsigned int first, unsigned int last)
returns a record containing a fragment of the character buffer.
std::shared_ptr< std::vector< Integer > > int_array_
MADARA_Export std::string & lower(std::string &input)
Converts the string to lower.
MADARA_Export utility::Refcounter< logger::Logger > global_logger
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
bool is_double_type(void) const
returns if the record is a double type (DOUBLE, DOUBLE_ARRAY)
logger::Logger * logger_
the logger used for any internal debugging information
bool operator>(const KnowledgeRecord &rhs) const
Greater than.
bool operator>=(const KnowledgeRecord &rhs) const
Greater than or equal to.
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
MADARA_Export ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
bool operator==(const KnowledgeRecord &rhs) const
Equal to.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
void unlock(void) const
Unlocks the mutex on this context.
std::vector< double > to_doubles(void) const
converts the value to a vector of doubles
static void set_precision(int new_precision)
Sets the double precision of a double record when using to_string ().
std::shared_ptr< std::vector< double > > double_array_
void set_index(size_t index, T value)
sets the value at the index to the specified value.
Integer to_integer(void) const
converts the value to an integer
static const KnowledgeUpdateSettings GLOBAL_AS_LOCAL_NO_EXPAND
std::shared_ptr< std::vector< unsigned char > > file_value_
void set_changed(void)
Force a change to be registered, waking up anyone waiting on entry.
bool operator<(const KnowledgeRecord &rhs) const
Less than.
static void set_scientific(void)
Sets the output format for doubles to std::scientific.
void unshare(void)
If this record holds a shared_ptr, make a copy of the underlying value so it has an exclusive copy...
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
Provides functions and classes for the distributed knowledge base.
uint32_t type_
type of variable (INTEGER, DOUBLE, STRING, FILE, IMAGE)
KnowledgeRecord dec_index(size_t index)
decrements the value at the index to the specified value.
bool operator<=(const KnowledgeRecord &rhs) const
Less than or equal to.
Copyright (c) 2015 Carnegie Mellon University.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
int read_file(const std::string &filename, uint32_t read_as_type=0)
reads a file and sets the type appropriately according to the extension
bool is_integer_type(void) const
returns if the record is a integer type (INTEGER, INTEGER_ARRAY)
bool is_false(void) const
Checks to see if the record is false.
bool is_string_type(void) const
returns true if the record is a string type (STRING, XML, TEXT_FILE)
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
void resize(size_t new_size)
resizes an array to a new size
uint32_t size(void) const
returns the size of the value
std::shared_ptr< std::string > str_value_
unsigned char * to_unmanaged_buffer(size_t &size) const
returns an unmanaged buffer that the user will have to take care of (this is a copy of the internal v...
static int get_precision(void)
Gets the current double precision for double to string conversion.