3 #ifndef _KNOWLEDGE_RECORD_INL_ 4 #define _KNOWLEDGE_RECORD_INL_ 27 typename std::enable_if<std::is_integral<T>::value,
void*>::type>
30 : logger_ (&
logger), int_value_ ((
Integer)value), type_ (INTEGER)
49 std::shared_ptr<std::vector<Integer>> value,
57 typename std::enable_if<std::is_floating_point<T>::value,
void*>
::type>
65 const std::vector <double> & value,
73 std::vector <double> && value,
81 std::shared_ptr<std::vector<double>> value,
103 std::shared_ptr<std::string> value,
118 std::shared_ptr<std::vector<unsigned char>> value,
160 if (rhs.type_ ==
EMPTY)
166 new (&
int_array_) std::shared_ptr<std::vector<Integer>>(std::move(rhs.int_array_));
167 else if (rhs.type_ ==
DOUBLE)
170 new (&
double_array_) std::shared_ptr<std::vector<double>>(std::move(rhs.double_array_));
171 else if (rhs.is_string_type ())
172 new (&
str_value_) std::shared_ptr<std::string>(std::move(rhs.str_value_));
173 else if (rhs.is_binary_file_type ())
174 new (&
file_value_) std::shared_ptr<std::vector<unsigned char>>(std::move(rhs.file_value_));
226 if (rhs.type_ ==
EMPTY)
239 new (&
int_array_) std::shared_ptr<std::vector<Integer>>(std::move(rhs.int_array_));
240 else if (rhs.type_ ==
DOUBLE)
243 new (&
double_array_) std::shared_ptr<std::vector<double>>(std::move(rhs.double_array_));
244 else if (rhs.is_string_type ())
245 new (&
str_value_) std::shared_ptr<std::string>(std::move(rhs.str_value_));
246 else if (rhs.is_binary_file_type ())
247 new (&
file_value_) std::shared_ptr<std::vector<unsigned char>>(std::move(rhs.file_value_));
253 typename std::enable_if<std::is_integral<T>::value,
void*>
::type>
272 typename std::enable_if<std::is_floating_point<T>::value,
void*>
::type>
300 return to_string ().compare (value) == 0;
306 return !(*
this == rhs);
494 return ret_value *= rhs;
506 return ret_value /= rhs;
518 return ret_value %= rhs;
530 return ret_value += rhs;
566 return ret_value -= rhs;
665 if ((uint32_t)type ==
type_ ||
678 int64_t buffer_size (
sizeof (uint32_t) * 2);
681 buffer_size +=
sizeof (
Integer);
685 buffer_size +=
sizeof (double);
711 int64_t buffer_size (
sizeof (uint32_t) * 1);
712 buffer_size += (key.size () + 1);
824 for (KnowledgeRecords::const_iterator i = records.begin ();
825 i != records.end (); ++i)
827 max = std::max <uint32_t> (i->second->quality, max);
838 for (KnowledgeMap::const_iterator i = records.begin ();
839 i != records.end (); ++i)
841 max = std::max <uint32_t> (i->second.quality, max);
878 int64_t & buffer_remaining)
882 uint32_t buff_value_size (0);
887 if (buffer_remaining >= (int64_t)
sizeof (
type))
889 memcpy (&
type, buffer,
sizeof (
type));
891 buffer +=
sizeof (
type);
893 buffer_remaining -=
sizeof (
type);
896 if (buffer_remaining >= (int64_t)
sizeof (
size))
898 memcpy (&size, buffer,
sizeof (size));
902 buff_value_size = size *
sizeof (
Integer);
904 buff_value_size = size *
sizeof (
double);
906 buff_value_size =
size;
908 buffer +=
sizeof (buff_value_size);
910 buffer_remaining = -1;
913 buffer_remaining -=
sizeof (buff_value_size);
916 if (buffer_remaining >= int64_t (buff_value_size))
920 if (buff_value_size >= 1) {
930 memcpy (&tmp, buffer,
sizeof (tmp));
936 std::vector<Integer> tmp;
939 for (uint32_t i = 0; i <
size; ++i)
942 memcpy (&cur, buffer + i *
sizeof(cur),
sizeof(cur));
952 memcpy (&tmp, buffer,
sizeof (tmp));
958 std::vector<double> tmp;
961 for (uint32_t i = 0; i <
size; ++i)
964 memcpy (&cur, buffer + i *
sizeof(cur),
sizeof(cur));
973 const unsigned char *b = (
const unsigned char *)buffer;
978 buffer_remaining = -1;
982 buffer += buff_value_size;
983 buffer_remaining -=
sizeof (char) * buff_value_size;
993 int64_t & buffer_remaining)
997 uint32_t key_size (0);
1000 if (buffer_remaining >= (int64_t)
sizeof (key_size))
1002 memcpy (&key_size, buffer,
sizeof (key_size));
1004 buffer +=
sizeof (key_size);
1006 buffer_remaining -=
sizeof (key_size);
1009 if (buffer_remaining >= int64_t (
sizeof (
char) * int64_t (key_size)))
1013 key.assign (buffer, key_size - 1);
1018 buffer +=
sizeof (char) * key_size;
1020 buffer_remaining -=
sizeof (char) * int64_t (key_size);
1023 buffer =
read (buffer, buffer_remaining);
1030 int64_t & buffer_remaining)
1035 if (buffer_remaining >= (int64_t)
sizeof (key_id))
1037 memcpy (&key_id, buffer,
sizeof (key_id));
1039 buffer +=
sizeof (key_id);
1040 buffer_remaining -=
sizeof (key_id);
1043 buffer =
read (buffer, buffer_remaining);
1067 this->clock =
clock;
1078 *
this = std::move(new_value);
1079 this->clock =
clock;
1205 std::shared_ptr<std::vector <unsigned char>> new_value)
1236 std::shared_ptr<std::vector <unsigned char>> new_value)
1243 template<
typename T,
1244 typename std::enable_if<std::is_integral<T>::value,
void*>
::type>
1284 template<
typename T,
1285 typename std::enable_if<std::is_floating_point<T>::value,
void*>
::type>
1329 template<
typename T,
1330 typename std::enable_if<std::is_integral<T>::value,
void*>
::type>
1362 template<
typename T,
1363 typename std::enable_if<std::is_floating_point<T>::value,
void*>
::type>
1389 inline std::shared_ptr<std::string>
1399 inline std::shared_ptr<std::string>
1403 std::shared_ptr<std::string> ret;
1415 inline std::shared_ptr<std::vector<KnowledgeRecord::Integer>>
1425 inline std::shared_ptr<std::vector<KnowledgeRecord::Integer>>
1429 std::shared_ptr<std::vector<Integer>> ret;
1441 inline std::shared_ptr<std::vector<double>>
1451 inline std::shared_ptr<std::vector<double>>
1455 std::shared_ptr<std::vector<double>> ret;
1467 inline std::shared_ptr<std::vector<unsigned char>>
1477 inline std::shared_ptr<std::vector<unsigned char>>
1481 std::shared_ptr<std::vector<unsigned char>> ret;
1494 KnowledgeRecord::operator bool (
void)
const 1501 int64_t & buffer_remaining)
const 1505 char * size_location = 0;
1506 uint32_t size_intermediate = 0;
1507 uint32_t uint32_temp;
1514 if (buffer_remaining >= encoded_size)
1517 " encoding %" PRId64
" byte message\n", encoded_size);
1520 if (buffer_remaining >= (int64_t)
sizeof (
type_))
1523 memcpy (buffer, &tmp,
sizeof (tmp));
1524 buffer +=
sizeof (tmp);
1526 buffer_remaining -=
sizeof (
type_);
1529 if (buffer_remaining >= (int64_t)
sizeof (
size))
1533 size_location = buffer;
1534 size_intermediate =
size;
1537 memcpy (buffer, &uint32_temp,
sizeof (uint32_temp));
1542 buffer +=
sizeof (
size);
1544 buffer_remaining -=
sizeof (
size);
1550 if (buffer_remaining >= int64_t (size))
1557 if (buffer_remaining >= int64_t (
sizeof (
Integer)))
1560 memcpy (buffer, &integer_temp,
sizeof (integer_temp));
1562 size_intermediate =
sizeof (
Integer);
1567 if (buffer_remaining >= int64_t (size *
sizeof (
Integer)))
1570 const Integer * ptr_temp = &(*int_array_)[0];
1573 for (uint32_t i = 0; i <
size; ++i, ++ptr_temp, ++target_buffer)
1576 memcpy (target_buffer, &integer_temp,
sizeof (
Integer));
1579 size_intermediate = size *
sizeof (
Integer);
1584 if (buffer_remaining >= int64_t (
sizeof (
double)))
1587 memcpy (buffer, &double_temp,
sizeof (
double));
1589 size_intermediate =
sizeof (double);
1594 if (buffer_remaining >= int64_t (size *
sizeof (
double)))
1597 const double * ptr_temp = &(*double_array_)[0];
1598 double * target_buffer = (
double *)buffer;
1600 for (uint32_t i = 0; i <
size; ++i, ++ptr_temp, ++target_buffer)
1603 memcpy (target_buffer, &double_temp,
sizeof (double_temp));
1606 size_intermediate = size *
sizeof (double);
1623 if (buffer_remaining >= size)
1631 buffer_remaining -= size_intermediate;
1632 buffer += size_intermediate;
1638 " %" PRId64
" byte buffer cannot contain %" PRId64
" byte message\n",
1639 buffer_remaining, encoded_size);
1646 int64_t & buffer_remaining)
const 1651 uint32_t key_size = uint32_t (key.size () + 1);
1652 uint32_t uint32_temp;
1656 if (buffer_remaining >= encoded_size)
1659 " encoding %" PRId64
" byte message\n", encoded_size);
1662 if (buffer_remaining >= (int64_t)
sizeof (key_size))
1665 memcpy (buffer, &uint32_temp,
sizeof (uint32_temp));
1666 buffer +=
sizeof (key_size);
1668 buffer_remaining -=
sizeof (key_size);
1671 if (buffer_remaining >= (int64_t)
sizeof (char) * key_size)
1674 strncpy (buffer, key.c_str (), key_size - 1);
1675 buffer[key_size - 1] = 0;
1677 buffer +=
sizeof (char) * key_size;
1679 buffer_remaining -=
sizeof (char) * key_size;
1682 buffer =
write (buffer, buffer_remaining);
1687 " %" PRId64
" byte buffer cannot contain %" PRId64
" byte message\n",
1688 buffer_remaining, encoded_size);
1695 int64_t & buffer_remaining)
const 1699 uint32_t uint32_temp;
1703 if (buffer_remaining >= encoded_size)
1706 " encoding %" PRId64
" byte message\n", encoded_size);
1709 if (buffer_remaining >= (int64_t)
sizeof (key_id))
1712 memcpy (buffer, &uint32_temp,
sizeof (uint32_temp));
1713 buffer +=
sizeof (key_id);
1715 buffer_remaining -=
sizeof (key_id);
1718 buffer =
write (buffer, buffer_remaining);
1723 " %" PRId64
" byte buffer cannot contain %" PRId64
" byte message\n",
1724 buffer_remaining, encoded_size);
1731 #endif // _KNOWLEDGE_RECORD_INL_ This class encapsulates an entry in a KnowledgeBase.
uint32_t max_quality(const KnowledgeRecords &records)
Returns the maximum quality within the records.
void emplace_shared_file(Args &&...args)
Construct a shared_ptr to a file (vector of unsigned char) within this KnowledgeRecord.
bool is_true(void) const
Checks to see if the record is true.
void emplace_string(Args &&...args)
Construct a string within this KnowledgeRecord.
std::shared_ptr< std::vector< double > > share_doubles() const
Returns a shared_ptr, sharing with the internal one.
void reset_value(void) noexcept
resets the variable to an integer
int32_t type(void) const
returns the size of the value
std::shared_ptr< std::vector< unsigned char > > share_binary() const
Returns a shared_ptr, sharing with the internal one.
bool is_image_type(void) const
returns true if the knowledge record has an image type
std::shared_ptr< std::string > share_string() const
Returns a shared_ptr, sharing with the internal one.
void emplace_integers(Args &&...args)
Construct a vector of integers within this KnowledgeRecord.
uint32_t quality
priority of the update
KnowledgeRecord deep_copy() const
Creates a deep copy of this knowledge record.
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
const char * read(const char *buffer, int64_t &buffer_remaining)
Reads a KnowledgeRecord instance from a buffer and updates the amount of buffer room remaining...
std::shared_ptr< std::vector< Integer > > share_integers() const
Returns a shared_ptr, sharing with the internal one.
void set_file(const unsigned char *new_value, size_t size)
sets the value to an unknown file type
void clear_value(void) noexcept
clears any dynamic values.
KnowledgeRecord operator-(void) const
Negate.
bool is_file_type(void) const
returns true if the knowledge record has a file type
bool is_binary_file_type(void) const
returns true if the knowledge record has a binary file type
void set_text(const char *new_value, size_t size)
sets the value to a plaintext string
std::shared_ptr< std::vector< Integer > > int_array_
void emplace_shared_integers(Args &&...args)
Construct a shared_ptr to vector of integers within this KnowledgeRecord.
int64_t get_encoded_size(void) const
Returns the encoded size of the record.
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)
void set_xml(const char *new_value, size_t size)
sets the value to an xml string
logger::Logger * logger_
the logger used for any internal debugging information
Provides knowledge logging services to files and terminals.
void emplace_shared_doubles(Args &&...args)
Construct a shared_ptr to vector of doubles within this KnowledgeRecord.
bool set_type(int32_t type)
Modify the type, but only if it's compatible with current type without changing any actual data store...
void emplace_shared_string(Args &&...args)
Construct a shared_ptr to a string within this KnowledgeRecord.
A multi-threaded logger for logging to one or more destinations.
~KnowledgeRecord() noexcept
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
std::shared_ptr< std::vector< double > > take_doubles()
Returns a shared_ptr, while resetting this record to empty.
KnowledgeRecord & operator=(const KnowledgeRecord &rhs)
Assignment.
int status(void) const
returns the status of the record.
void set_jpeg(const unsigned char *new_value, size_t size)
sets the value to a jpeg
KnowledgeRecord operator*(const KnowledgeRecord &rhs) const
Times operator.
KnowledgeRecord & operator*=(const KnowledgeRecord &rhs)
In-place multiplication operator.
KnowledgeRecord & operator%=(const KnowledgeRecord &rhs)
In-place modulus operator.
MADARA_Export uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
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.
::std::map< std::string, KnowledgeRecord * > KnowledgeRecords
KnowledgeRecord & operator++(void)
Preincrement operator.
std::shared_ptr< std::vector< unsigned char > > take_binary()
Returns a shared_ptr, while resetting this record to empty.
::std::map< std::string, KnowledgeRecord > KnowledgeMap
std::shared_ptr< std::vector< double > > double_array_
KnowledgeRecord operator%(const KnowledgeRecord &rhs) const
Modulus operator.
std::shared_ptr< std::vector< Integer > > take_integers()
Returns a shared_ptr, while resetting this record to empty.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
void set_modified(void)
sets the status to modified
Integer to_integer(void) const
converts the value to an integer
KnowledgeRecord & operator-=(const KnowledgeRecord &rhs)
In-place subtraction operator.
std::shared_ptr< std::vector< unsigned char > > file_value_
bool operator!(void) const
Logical not.
KnowledgeRecord operator+(const KnowledgeRecord &rhs) const
Plus operator.
KnowledgeRecord * clone(void) const
clones the record.
bool shared_
is this knowledge record's shared_ptr, if any, exposed to outside holders?
void unshare(void)
If this record holds a shared_ptr, make a copy of the underlying value so it has an exclusive copy...
KnowledgeRecord() noexcept
Provides functions and classes for the distributed knowledge base.
std::ostream & operator<<(std::ostream &stream, const KnowledgeRecord &rhs)
output stream buffering
void emplace_doubles(Args &&...args)
Construct a vector of doubles within this KnowledgeRecord.
uint32_t write_quality
write priority for any local updates
uint32_t type_
type of variable (INTEGER, DOUBLE, STRING, FILE, IMAGE)
KnowledgeRecord & operator--(void)
Predecrement operator.
KnowledgeRecord & operator+=(const KnowledgeRecord &rhs)
In-place addition operator.
bool operator!=(const KnowledgeRecord &rhs) const
Unequal to.
bool is_array_type(void) const
returns if the record is an array type (DOUBLE_ARRAY, INTEGER_ARRAY)
Copyright (c) 2015 Carnegie Mellon University.
bool is_ref_counted(void) const
returns if the record has a reference-counted type
void clear_union(void) noexcept
KnowledgeRecord & operator/=(const KnowledgeRecord &rhs)
In-place division operator.
char * write(char *buffer, int64_t &buffer_remaining) const
Writes a KnowledgeRecord instance to a buffer and updates the amount of buffer room remaining...
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.
std::shared_ptr< std::string > take_string()
Returns a shared_ptr, while resetting this record to empty.
uint32_t size(void) const
returns the size of the value
KnowledgeRecord operator/(const KnowledgeRecord &rhs) const
Divides operator.
uint64_t clock
last modification time
std::shared_ptr< std::string > str_value_