1 #ifndef _KNOWLEDGE_RECORD_CPP_
2 #define _KNOWLEDGE_RECORD_CPP_
18 int madara_double_precision(-1);
20 bool madara_use_scientific(
false);
29 return madara_double_precision;
35 "KnowledgeRecord::set_precision:"
36 " setting precision to %d\n",
37 madara_double_precision);
39 madara_double_precision = new_precision;
45 "KnowledgeRecord::set_fixed:"
46 " setting output format to std::fixed\n");
48 madara_use_scientific =
false;
54 "KnowledgeRecord::set_scientific:"
55 " setting output format to std::scientific\n");
57 madara_use_scientific =
true;
65 bool add_zero_char =
false;
70 int ret = tmp.
read_file(filename, read_as_type);
78 std::string::size_type position = filename.rfind(
'.');
79 std::string extension = filename.substr(position, filename.size() - position);
86 add_zero_char =
false;
100 type_ = read_as_type;
101 else if (extension ==
".xml")
108 unsigned char* ucbuf = (
unsigned char*)buffer;
111 if (extension ==
".jpg" || read_as_type ==
IMAGE_JPEG)
142 buf_->emplace_back();
151 filename, (
void*)buffer.c_str(), buffer.size());
172 std::stringstream buffer;
205 std::stringstream buffer;
235 unsigned int size = (
unsigned int)this->
size();
236 integers.resize(
size);
244 const Integer* ptr_temp = &(*int_array_)[0];
246 for (
unsigned int i = 0; i <
size; ++i)
253 const double* ptr_temp = &(*double_array_)[0];
255 for (
unsigned int i = 0; i <
size; ++i)
262 for (
unsigned int i = 0; i <
size; ++i)
267 const unsigned char* ptr_temp = &(*file_value_)[0];
269 for (
unsigned int i = 0; i <
size; ++i)
290 unsigned int size = (
unsigned int)this->
size();
291 doubles.resize(
size);
297 const Integer* ptr_temp = &(*int_array_)[0];
299 for (
unsigned int i = 0; i <
size; ++i)
300 doubles[i] =
double(ptr_temp[i]);
306 const double* ptr_temp = &(*double_array_)[0];
308 for (
unsigned int i = 0; i <
size; ++i)
315 for (
unsigned int i = 0; i <
size; ++i)
316 doubles[i] =
double(ptr_temp[i]);
320 const unsigned char* ptr_temp = &(*file_value_)[0];
322 for (
unsigned int i = 0; i <
size; ++i)
323 doubles[i] =
double(ptr_temp[i]);
332 size_t len = std::min(buf_size, contents.length() + 1);
340 size_t actual_size = 0;
344 actual_size = std::min(
str_value_->size(), buf_size);
345 memcpy(buffer,
str_value_->c_str(), actual_size);
349 actual_size = std::min(
file_value_->size(), buf_size);
354 if (
sizeof(
Integer) <= buf_size)
356 actual_size = std::min(
sizeof(
Integer), buf_size);
362 if (
sizeof(
Integer) <= buf_size)
364 actual_size = std::min(
sizeof(
double), buf_size);
373 memcpy(buffer, &(*
int_array_)[0], actual_size);
378 if (
sizeof(
double) *
int_array_->size() <= buf_size)
380 actual_size = std::min(
sizeof(
double) *
int_array_->size(), buf_size);
416 "KnowledgeRecord::to_string:"
420 std::stringstream buffer;
426 const Integer* ptr_temp = &(*int_array_)[0];
434 for (uint32_t i = 1; i <
size; ++i, ++ptr_temp)
435 buffer << delimiter << *ptr_temp;
440 if (!madara_use_scientific)
443 "KnowledgeRecord::to_string: using fixed format\n");
445 buffer << std::fixed;
450 "KnowledgeRecord::to_string: using scientific format\n");
452 buffer << std::scientific;
455 if (madara_double_precision >= 0)
458 buffer << std::setprecision(madara_double_precision);
461 "KnowledgeRecord::to_string:"
462 " precision set to %d\n",
463 madara_double_precision);
468 "KnowledgeRecord::to_string:"
469 " precision set to default\n",
470 madara_double_precision);
478 if (!madara_use_scientific)
481 "KnowledgeRecord::to_string: using fixed format\n");
483 buffer << std::fixed;
488 "KnowledgeRecord::to_string: using scientific format\n");
490 buffer << std::scientific;
493 if (madara_double_precision >= 0)
495 buffer << std::setprecision(madara_double_precision);
498 "KnowledgeRecord::to_string:"
499 " precision set to %d\n",
500 madara_double_precision);
505 "KnowledgeRecord::to_string:"
506 " precision set to default\n",
507 madara_double_precision);
510 const double* ptr_temp = &(*double_array_)[0];
518 for (uint32_t i = 1; i <
size; ++i, ++ptr_temp)
519 buffer << delimiter << *ptr_temp;
523 buffer <<
"binary:size=";
540 buffer =
new char[
size];
546 buffer =
new char[
size];
552 buffer =
new char[
size];
557 size =
sizeof(double);
558 buffer =
new char[
size];
564 buffer =
new char[
size];
570 buffer =
new char[
size];
583 return (
unsigned char*)buffer;
605 last = std::min<unsigned int>(last,
size - 1);
608 char* new_buffer =
new char[last - first + 2];
610 memcpy(new_buffer,
str_value_->c_str() + first, last - first + 1);
611 new_buffer[last - first + 1] = 0;
620 last = std::min<unsigned int>(last, (
unsigned int)
size - 1);
623 uint32_t bufsize = last - first + 1;
624 unsigned char* new_buffer =
new unsigned char[bufsize];
626 memcpy(new_buffer, &(*
file_value_)[0] + first, last - first + 1);
636 last = std::min<unsigned int>(last,
size - 1);
637 uint32_t bufsize = last - first + 1;
641 Integer* ptr_temp = &(*int_array_)[0];
643 for (
unsigned int i = first; i <= last; ++i, ++ptr_temp)
653 last = std::min<unsigned int>(last,
size - 1);
654 uint32_t bufsize = last - first + 1;
658 double* ptr_temp = &(*double_array_)[0];
660 for (
unsigned int i = first; i <= last; ++i, ++ptr_temp)
694 result = lhs < other;
700 result = lhs < other;
722 result = temp < other;
733 result = temp < other;
750 result = lhs < other;
757 result = lhs < other;
789 result = lhs <= other;
795 result = lhs <= other;
817 result = temp <= other;
828 result = temp <= other;
845 result = lhs <= other;
852 result = lhs <= other;
935 this->str_value_->at(0) <=
'9')
959 result = lhs == other;
967 result = lhs == other;
999 result = lhs > other;
1005 result = lhs > other;
1027 result = lhs > other;
1038 result = lhs > other;
1055 result = lhs > other;
1063 result = lhs > other;
1095 result = lhs >= other;
1101 result = lhs >= other;
1123 result = lhs >= other;
1134 result = lhs >= other;
1151 result = lhs >= other;
1159 result = lhs >= other;
1217 std::vector<Integer> tmp(index + 1);
1251 std::vector<Integer> tmp(index + 1);
1266 size_t cur_size =
size();
1268 if (cur_size == new_size)
1275 if (new_size > cur_size)
1309 unsigned int , uint64_t ,
bool perform_lock)
1313 if (key.length() > 0)
1316 "KnowledgeRecord::apply:"
1317 " attempting to set %s=%s\n",
1345 "KnowledgeRecord::apply:"
1346 " received data[%s]=%s.\n",
1350 else if (result == 0)
1353 "KnowledgeRecord::apply:"
1354 " discarded data[%s]=%s as the value was already set.\n",
1357 else if (result == -1)
1360 "KnowledgeRecord::apply:"
1361 " discarded data due to null key.\n");
1363 else if (result == -2)
1366 "KnowledgeRecord::apply:"
1367 " discarded data[%s]=%s due to lower quality.\n",
1370 else if (result == -3)
1373 "KnowledgeRecord::apply:"
1374 " discarded data[%s]=%" PRId64
" due to older timestamp.\n",
1384 "KnowledgeRecord::apply:"
1385 " checking if record is non-zero.\n");
1392 return value < 0 || value > 0;
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
madara::knowledge::KnowledgeRecord KnowledgeRecord
This class encapsulates an entry in a KnowledgeBase.
size_t to_managed_buffer(char *buffer, size_t buf_size) const
converts the value to a c string.
bool has_history() const
Return true if this record has a circular buffer history.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
std::shared_ptr< std::string > str_value_
void emplace_file(Args &&... args)
Construct a file (vector of unsigned char) within this KnowledgeRecord.
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
std::shared_ptr< std::vector< unsigned char > > file_value_
KnowledgeRecord & ref_newest()
bool is_double_type(void) const
returns if the record is a double type (DOUBLE, DOUBLE_ARRAY)
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.
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy toi, clock, and write_quality.
double to_double(void) const
converts the value to a float/double.
bool operator<=(const KnowledgeRecord &rhs) const
Less than or equal to.
bool is_true(void) const
Checks to see if the record is true.
bool operator>=(const KnowledgeRecord &rhs) const
Greater than or equal to.
KnowledgeRecord fragment(unsigned int first, unsigned int last)
returns a record containing a fragment of the character buffer.
static void set_fixed(void)
Sets the output format for doubles to std::fixed.
KnowledgeRecord inc_index(size_t index)
increments the value at the index to the specified value.
KnowledgeRecord dec_index(size_t index)
decrements the value at the index to the specified value.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
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.
uint32_t size(void) const
returns the size of the value
std::shared_ptr< std::vector< Integer > > int_array_
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
bool is_integer_type(void) const
returns if the record is a integer type (INTEGER, INTEGER_ARRAY)
bool is_binary_file_type(void) const
returns true if the knowledge record has a binary file type
static int get_precision(void)
Gets the current double precision for double to string conversion.
uint32_t type_
type of variable (INTEGER, DOUBLE, STRING, FILE, IMAGE)
Integer to_integer(void) const
converts the value to an integer.
bool is_string_type(void) const
returns true if the record is a string type (STRING, XML, TEXT_FILE)
bool operator<(const KnowledgeRecord &rhs) const
Less than.
size_t to_managed_string(char *buffer, size_t buf_size) const
converts the value to a c string.
bool operator==(const KnowledgeRecord &rhs) const
Equal to.
KnowledgeRecord get_newest() const
Return the newest stored history entry of this record.
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
void emplace_string(Args &&... args)
Construct a string within this KnowledgeRecord.
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...
ssize_t to_file(const std::string &filename) const
writes the value to a file
bool operator>(const KnowledgeRecord &rhs) const
Greater than.
void clear_value(void) noexcept
clears any dynamic values.
void emplace_integers(Args &&... args)
Construct a vector of integers within this KnowledgeRecord.
std::shared_ptr< CircBuf > buf_
logger::Logger * logger_
the logger used for any internal debugging information
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 ().
void set_file(const unsigned char *new_value, size_t size)
sets the value to an unknown file type
static void set_scientific(void)
Sets the output format for doubles to std::scientific.
bool is_false(void) const
Checks to see if the record is false.
void unshare(void)
If this record holds a shared_ptr, make a copy of the underlying value so it has an exclusive copy.
void resize(size_t new_size)
resizes an array to a new size
static const KnowledgeUpdateSettings GLOBAL_AS_LOCAL_NO_EXPAND
This class stores variables and their values for use by any entity needing state information in a thr...
void unlock(void) const
Unlocks the mutex on this context.
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.
void set_changed(void)
Force a change to be registered, waking up anyone waiting on entry.
void lock(void) const
Locks the mutex on this context.
Provides functions and classes for the distributed knowledge base.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
void safe_clear(KnowledgeMap &map)
::std::map< std::string, KnowledgeRecord > KnowledgeMap
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
std::string & lower(std::string &input)
Converts the string to lower.
MADARA_EXPORT void strncpy_safe(char *dst, const char *src, size_t dst_size)
Performs a strncpy in a way that will compile without warnings.
int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char)
Reads a file into a provided void pointer.
Copyright(c) 2020 Galois.