3 #ifndef _MADARA_KNOWLEDGE_RECORD_H_ 4 #define _MADARA_KNOWLEDGE_RECORD_H_ 17 #include <type_traits> 18 #include "madara/MADARA_export.h" 21 #include "ace/os_include/sys/os_types.h" 30 class ThreadSafeContext;
85 UNKNOWN_FILE_TYPE = 8,
91 ALL_ARRAYS = INTEGER_ARRAY | DOUBLE_ARRAY,
92 ALL_INTEGERS = INTEGER | INTEGER_ARRAY,
93 ALL_DOUBLES = DOUBLE | DOUBLE_ARRAY,
94 ALL_PRIMITIVE_TYPES = INTEGER | STRING | DOUBLE |
95 INTEGER_ARRAY | DOUBLE_ARRAY,
96 ALL_FILE_TYPES = UNKNOWN_FILE_TYPE | XML | TEXT_FILE | IMAGE_JPEG,
97 ALL_IMAGES = IMAGE_JPEG,
98 ALL_TEXT_FORMATS = XML | TEXT_FILE | STRING,
99 ALL_TYPES = ALL_PRIMITIVE_TYPES | ALL_FILE_TYPES,
100 ALL_CLEARABLES = ALL_ARRAYS | ALL_TEXT_FORMATS | ALL_FILE_TYPES
118 uint32_t quality = 0;
123 uint32_t write_quality = 0;
132 Integer int_value_ = 0;
144 uint32_t type_ = EMPTY;
150 mutable bool shared_ = OWNED;
161 typename std::enable_if<std::is_integral<T>::value,
162 void*>::type =
nullptr>
168 typename std::enable_if<std::is_floating_point<T>::value,
169 void*>::type =
nullptr>
182 explicit KnowledgeRecord (std::shared_ptr<std::vector <Integer>> value,
215 std::shared_ptr<std::vector<unsigned char>> value,
231 template<
typename T, u
int32_t Type, MemberType<T> Member,
typename... Args>
235 return *
new(&(this->*Member)) std::shared_ptr<T>(
236 std::forward<Args>(args)...);
239 template<
typename T, u
int32_t Type, MemberType<T> Member,
typename... Args>
241 return emplace_shared_val<T, Type, Member> (std::move(
242 std::make_shared<T> (
243 std::forward<Args>(args)...)));
246 template<
typename T, u
int32_t Type, MemberType<std::vector<T>> Member,
249 return emplace_shared_val<std::vector<T>, Type, Member> (
250 std::forward<Args>(args)...);
253 template<
typename T, u
int32_t Type, MemberType<std::vector<T>> Member,
256 return emplace_val<std::vector<T>, Type, Member> (
257 std::forward<Args>(args)...);
270 template<
typename... Args>
272 emplace_shared_vec<
Integer, INTEGER_ARRAY,
274 std::forward<Args>(args)...);
287 template<
typename... Args>
289 emplace_shared_integers(std::forward<Args>(args)...);
297 template<
typename... Args>
299 emplace_vec<
Integer, INTEGER_ARRAY,
301 std::forward<Args>(args)...);
309 template<
typename... Args>
311 emplace_integers(std::forward<Args>(args)...);
323 template<
typename... Args>
325 emplace_shared_vec<double, DOUBLE_ARRAY,
327 std::forward<Args>(args)...);
340 template<
typename... Args>
342 emplace_shared_doubles(std::forward<Args>(args)...);
350 template<
typename... Args>
352 emplace_vec<double, DOUBLE_ARRAY,
354 std::forward<Args>(args)...);
362 template<
typename... Args>
364 emplace_doubles(std::forward<Args>(args)...);
375 template<
typename... Args>
379 std::forward<Args>(args)...);
391 template<
typename... Args>
393 emplace_shared_string(std::forward<Args>(args)...);
401 template<
typename... Args>
405 std::forward<Args>(args)...);
413 template<
typename... Args>
415 emplace_string(std::forward<Args>(args)...);
427 template<
typename... Args>
429 emplace_shared_vec<
unsigned char, UNKNOWN_FILE_TYPE,
431 std::forward<Args>(args)...);
444 template<
typename... Args>
446 emplace_shared_binary(std::forward<Args>(args)...);
454 template<
typename... Args>
456 emplace_vec<
unsigned char, UNKNOWN_FILE_TYPE,
458 std::forward<Args>(args)...);
466 template<
typename... Args>
468 emplace_binary(std::forward<Args>(args)...);
476 template<
typename... Args>
478 : int_array_ (
std::make_shared<
std::vector<Integer>> (
479 std::forward<Args>(args)...)),
480 type_ (INTEGER_ARRAY) {}
487 template<
typename... Args>
489 : int_array_ {std::forward<Args>(args)...},
490 type_ (INTEGER_ARRAY), shared_ (SHARED) {}
497 template<
typename... Args>
499 : double_array_ (
std::make_shared<
std::vector<double>> (
500 std::forward<Args>(args)...)),
501 type_ (DOUBLE_ARRAY) {}
508 template<
typename... Args>
510 : double_array_ {std::forward<Args>(args)...},
511 type_ (DOUBLE_ARRAY), shared_ (SHARED) {}
521 template<
typename... Args>
524 std::forward<Args>(args)...)),
536 template<
typename... Args>
538 : str_value_ {std::forward<Args>(args)...},
539 type_ (STRING), shared_ (SHARED) {}
546 template<
typename... Args>
548 : file_value_ (
std::make_shared<
std::vector<unsigned char>> (
549 std::forward<Args>(args)...)),
550 type_ (UNKNOWN_FILE_TYPE) {}
558 template<
typename... Args>
560 : file_value_ {std::forward<Args>(args)...},
561 type_ (UNKNOWN_FILE_TYPE), shared_ (SHARED) {}
601 typename std::enable_if<std::is_integral<T>::value,
602 void*>::type =
nullptr>
603 void set_index (
size_t index, T value);
613 typename std::enable_if<std::is_floating_point<T>::value,
614 void*>::type =
nullptr>
615 void set_index (
size_t index, T value);
629 std::shared_ptr<std::string> share_string()
const;
635 std::shared_ptr<std::string> take_string();
641 ssize_t to_file (
const std::string & filename)
const;
657 Integer to_integer (
void)
const;
662 double to_double (
void)
const;
668 std::vector <Integer> to_integers (
void)
const;
674 std::shared_ptr<std::vector<Integer>> share_integers()
const;
680 std::shared_ptr<std::vector<Integer>> take_integers();
686 std::vector <double> to_doubles (
void)
const;
692 std::shared_ptr<std::vector<double>> share_doubles()
const;
698 std::shared_ptr<std::vector<double>> take_doubles();
704 std::shared_ptr<std::vector<unsigned char>> share_binary()
const;
710 std::shared_ptr<std::vector<unsigned char>> take_binary();
726 unsigned char * to_unmanaged_buffer (
size_t & size)
const;
757 typename std::enable_if<std::is_integral<T>::value,
758 void*>::type =
nullptr>
766 void set_value (
const Integer * new_value, uint32_t size);
772 void set_value (std::vector <Integer> && new_value);
778 void set_value (
const std::vector <Integer> & new_value);
784 void set_value (std::shared_ptr<std::vector <Integer>> new_value);
791 void set_value (
const char * new_value, uint32_t size);
809 void set_value (std::shared_ptr<std::string> new_value);
816 typename std::enable_if<std::is_floating_point<T>::value,
817 void*>::type =
nullptr>
825 void set_value (
const double * new_value, uint32_t size);
831 void set_value (std::vector <double> && new_value);
837 void set_value (
const std::vector <double> & new_value);
843 void set_value (std::shared_ptr<std::vector <double>> new_value);
850 void set_xml (
const char * new_value,
size_t size);
868 void set_xml (std::shared_ptr<std::string> new_value);
875 void set_text (
const char * new_value,
size_t size);
893 void set_text (std::shared_ptr<std::string> new_value);
902 static void set_precision (
int new_precision);
907 static void set_scientific (
void);
912 static void set_fixed (
void);
918 static int get_precision (
void);
925 void set_jpeg (
const unsigned char * new_value,
size_t size);
931 void set_jpeg (std::vector <unsigned char> && new_value);
937 void set_jpeg (
const std::vector <unsigned char> & new_value);
943 void set_jpeg (std::shared_ptr<std::vector <unsigned char>> new_value);
950 void set_file (
const unsigned char * new_value,
size_t size);
956 void set_file (std::vector <unsigned char> && new_value);
962 void set_file (
const std::vector <unsigned char> & new_value);
968 void set_file (std::shared_ptr<std::vector <unsigned char>> new_value);
992 int status (
void)
const;
997 void set_modified (
void);
1003 void resize (
size_t new_size);
1018 void reset_value (
void) noexcept;
1021 void clear_union (
void) noexcept;
1029 void clear_value (
void) noexcept;
1034 uint32_t size (
void)
const;
1039 int32_t type (
void)
const;
1052 bool set_type (int32_t type);
1058 bool is_ref_counted (
void)
const;
1065 static bool is_ref_counted (uint32_t type);
1071 bool is_string_type (
void)
const;
1078 static bool is_string_type (uint32_t type);
1084 bool is_double_type (
void)
const;
1091 static bool is_double_type (uint32_t type);
1097 bool is_integer_type (
void)
const;
1104 static bool is_integer_type (uint32_t type);
1111 bool is_array_type (
void)
const;
1118 static bool is_array_type (uint32_t type);
1135 bool is_image_type (
void)
const;
1142 static bool is_image_type (uint32_t type);
1148 bool is_file_type (
void)
const;
1155 static bool is_file_type (uint32_t type);
1161 bool is_binary_file_type (
void)
const;
1168 static bool is_binary_file_type (uint32_t type);
1188 template<
typename T,
1189 typename std::enable_if<std::is_integral<T>::value,
1190 void*>::type =
nullptr>
1196 template<
typename T,
1197 typename std::enable_if<std::is_floating_point<T>::value,
1198 void*>::type =
nullptr>
1301 explicit operator bool (
void)
const;
1321 const char * read (
const char * buffer, int64_t & buffer_remaining);
1332 const char * read (
const char * buffer,
std::string & key,
1333 int64_t & buffer_remaining);
1344 const char * read (
const char * buffer, uint32_t & key_id,
1345 int64_t & buffer_remaining);
1363 char * write (
char * buffer, int64_t & buffer_remaining)
const;
1384 char * write (
char * buffer,
const std::string & key,
1385 int64_t & buffer_remaining)
const;
1408 char * write (
char * buffer, uint32_t key_id,
1409 int64_t & buffer_remaining)
const;
1415 const std::string & key,
unsigned int quality, uint64_t clock,
1423 bool is_true (
void)
const;
1430 bool is_false (
void)
const;
1439 return status() != UNCREATED;
1447 int64_t get_encoded_size (
const std::string & key)
const;
1454 int64_t get_encoded_size (
void)
const;
1471 uint32_t
max_quality (
const KnowledgeRecords & records);
1478 uint32_t
max_quality (
const KnowledgeMap & records);
1492 #endif // _MADARA_KNOWLEDGE_RECORD_H_ This class encapsulates an entry in a KnowledgeBase.
uint32_t max_quality(const KnowledgeRecords &records)
Returns the maximum quality within the records.
auto operator>=(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get() >=rhs.get())
void emplace_shared_file(Args &&...args)
Construct a shared_ptr to a file (vector of unsigned char) within this KnowledgeRecord.
auto operator/=(Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get_mut()/=rhs.get())
auto operator>(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get() >rhs.get())
void emplace(tags::string_t, Args &&...args)
Construct a string within this KnowledgeRecord.
auto operator*=(Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get_mut()*=rhs.get())
void emplace_string(Args &&...args)
Construct a string within this KnowledgeRecord.
KnowledgeRecord(tags::string_t, Args &&...args)
Forwarding constructor for strings Each argument past the first will be forwarded to construct a std:...
auto operator<=(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()<=rhs.get())
void emplace_integers(Args &&...args)
Construct a vector of integers within this KnowledgeRecord.
auto operator<(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()< rhs.get())
std::shared_ptr< std::vector< T > > & emplace_vec(Args &&...args)
::std::vector< std::string > KnowledgeRules
auto operator--(Tracked< T > &lhs) -> decltype(--lhs.get_mut())
madara::knowledge::KnowledgeRecord KnowledgeRecord
bool is_valid(void) const
Checks to see if the record is valid.
void emplace(tags::shared_t< tags::integers_t >, Args &&...args)
Construct a shared_ptr to vector of integers within this KnowledgeRecord.
void emplace_file(Args &&...args)
Construct a file (vector of unsigned char) within this KnowledgeRecord.
KnowledgeRecord(tags::integers_t, Args &&...args)
Forwarding constructor for integer arrays Each argument past the first will be forwarded to construct...
auto operator/(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()/rhs.get())
void emplace(tags::shared_t< tags::binary_t >, Args &&...args)
Construct a shared_ptr to binary (vector of unsigned char) within this KnowledgeRecord.
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.
auto operator%(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()%rhs.get())
auto operator+=(Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get_mut()+=rhs.get())
This class stores variables and their values for use by any entity needing state information in a thr...
std::shared_ptr< T > KnowledgeRecord::* MemberType
std::shared_ptr< std::vector< Integer > > int_array_
auto operator-(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()-rhs.get())
void emplace(tags::integers_t, Args &&...args)
Construct a vector of integers within this KnowledgeRecord.
MADARA_Export utility::Refcounter< logger::Logger > global_logger
void emplace_shared_integers(Args &&...args)
Construct a shared_ptr to vector of integers within this KnowledgeRecord.
void emplace(tags::shared_t< tags::doubles_t >, Args &&...args)
Construct a shared_ptr to vector of doubles within this KnowledgeRecord.
Provides knowledge logging services to files and terminals.
void emplace(tags::binary_t, Args &&...args)
Construct a binary (vector of unsigned char) within this KnowledgeRecord.
void emplace_shared_doubles(Args &&...args)
Construct a shared_ptr to vector of doubles within this KnowledgeRecord.
auto operator%=(Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get_mut()%=rhs.get())
std::shared_ptr< T > & emplace_shared_val(Args &&...args)
void emplace_shared_string(Args &&...args)
Construct a shared_ptr to a string within this KnowledgeRecord.
auto operator*(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()*rhs.get())
A multi-threaded logger for logging to one or more destinations.
::std::vector< KnowledgeRecord > KnowledgeVector
void emplace(tags::shared_t< tags::string_t >, Args &&...args)
Construct a shared_ptr to string within this KnowledgeRecord.
auto operator!(const Tracked< T > &lhs) -> decltype(!lhs.get())
madara::knowledge::KnowledgeRecord::Integer Integer
::std::map< std::string, KnowledgeRecord * > KnowledgeRecords
::std::map< std::string, KnowledgeRecord > KnowledgeMap
KnowledgeRecord KnowledgeValue
std::shared_ptr< T > & emplace_val(Args &&...args)
std::shared_ptr< std::vector< double > > double_array_
auto operator-=(Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get_mut()-=rhs.get())
KnowledgeRecord(tags::shared_t< tags::string_t >, Args &&...args)
Forwarding constructor for double arrays shared_ptr Each argument past the first will be forwarded to...
std::shared_ptr< std::vector< unsigned char > > file_value_
auto operator==(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()==rhs.get())
KnowledgeRecord(tags::shared_t< tags::binary_t >, Args &&...args)
Forwarding constructor for binary file (blob) shared_ptr Each argument past the first will be forward...
auto operator!=(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()!=rhs.get())
std::shared_ptr< std::vector< T > > & emplace_shared_vec(Args &&...args)
KnowledgeRecord(tags::shared_t< tags::doubles_t >, Args &&...args)
Forwarding constructor for double arrays shared_ptr Each argument past the first will be forwarded to...
MADARA_Export bool exists(const char *originator, uint64_t clock, uint32_t update_number, OriginatorFragmentMap &map)
Checks if a fragment already exists within a fragment map.
KnowledgeRecord(tags::binary_t, Args &&...args)
Forwarding constructor for binary files (blobs) Each argument past the first will be forwarded to con...
KnowledgeRecord() noexcept
Provides functions and classes for the distributed knowledge base.
void emplace(tags::doubles_t, Args &&...args)
Construct a vector of doubles within this KnowledgeRecord.
void emplace_doubles(Args &&...args)
Construct a vector of doubles within this KnowledgeRecord.
void set_value(T &t, const T &v)
Fallback definition of set_value; simply passes through the value.
KnowledgeRecord(tags::shared_t< tags::integers_t >, Args &&...args)
Forwarding constructor for integer arrays shared_ptr Each argument past the first will be forwarded t...
Copyright (c) 2015 Carnegie Mellon University.
KnowledgeRecord(tags::doubles_t, Args &&...args)
Forwarding constructor for double arrays Each argument past the first will be forwarded to construct ...
auto operator+(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()+rhs.get())
auto operator++(Tracked< T > &lhs) -> decltype(++lhs.get_mut())
::std::vector< std::string > StringVector
std::shared_ptr< std::string > str_value_