3 #ifndef _MADARA_KNOWLEDGE_RECORD_H_ 4 #define _MADARA_KNOWLEDGE_RECORD_H_ 17 #include <type_traits> 18 #include "madara/MadaraExport.h" 29 class ThreadSafeContext;
84 UNKNOWN_FILE_TYPE = 8,
90 ALL_ARRAYS = INTEGER_ARRAY | DOUBLE_ARRAY,
91 ALL_INTEGERS = INTEGER | INTEGER_ARRAY,
92 ALL_DOUBLES = DOUBLE | DOUBLE_ARRAY,
93 ALL_PRIMITIVE_TYPES = INTEGER | STRING | DOUBLE |
94 INTEGER_ARRAY | DOUBLE_ARRAY,
95 ALL_FILE_TYPES = UNKNOWN_FILE_TYPE | XML | TEXT_FILE | IMAGE_JPEG,
96 ALL_IMAGES = IMAGE_JPEG,
97 ALL_TEXT_FORMATS = XML | TEXT_FILE | STRING,
98 ALL_TYPES = ALL_PRIMITIVE_TYPES | ALL_FILE_TYPES,
99 ALL_CLEARABLES = ALL_ARRAYS | ALL_TEXT_FORMATS | ALL_FILE_TYPES
117 uint32_t quality = 0;
122 uint32_t write_quality = 0;
131 Integer int_value_ = 0;
143 uint32_t type_ = EMPTY;
149 mutable bool shared_ = OWNED;
160 typename std::enable_if<std::is_integral<T>::value,
167 typename std::enable_if<std::is_floating_point<T>::value,
168 void*>::type =
nullptr>
181 explicit KnowledgeRecord (std::shared_ptr<std::vector <Integer>> value,
214 std::shared_ptr<std::vector<unsigned char>> value,
236 template<
typename... Args>
238 emplace_shared_vec<
Integer, INTEGER_ARRAY,
240 std::forward<Args>(args)...);
253 template<
typename... Args>
255 emplace_shared_integers(std::forward<Args>(args)...);
263 template<
typename... Args>
265 emplace_vec<
Integer, INTEGER_ARRAY,
267 std::forward<Args>(args)...);
275 template<
typename... Args>
277 emplace_integers(std::forward<Args>(args)...);
289 template<
typename... Args>
291 emplace_shared_vec<double, DOUBLE_ARRAY,
293 std::forward<Args>(args)...);
306 template<
typename... Args>
308 emplace_shared_doubles(std::forward<Args>(args)...);
316 template<
typename... Args>
318 emplace_vec<double, DOUBLE_ARRAY,
320 std::forward<Args>(args)...);
328 template<
typename... Args>
330 emplace_doubles(std::forward<Args>(args)...);
341 template<
typename... Args>
345 std::forward<Args>(args)...);
357 template<
typename... Args>
359 emplace_shared_string(std::forward<Args>(args)...);
367 template<
typename... Args>
371 std::forward<Args>(args)...);
379 template<
typename... Args>
381 emplace_string(std::forward<Args>(args)...);
393 template<
typename... Args>
395 emplace_shared_vec<
unsigned char, UNKNOWN_FILE_TYPE,
397 std::forward<Args>(args)...);
410 template<
typename... Args>
412 emplace_shared_binary(std::forward<Args>(args)...);
420 template<
typename... Args>
422 emplace_vec<
unsigned char, UNKNOWN_FILE_TYPE,
424 std::forward<Args>(args)...);
432 template<
typename... Args>
434 emplace_binary(std::forward<Args>(args)...);
442 template<
typename... Args>
444 : int_array_ (
std::make_shared<
std::vector<Integer>> (
445 std::forward<Args>(args)...)),
446 type_ (INTEGER_ARRAY) {}
453 template<
typename... Args>
455 : int_array_ {std::forward<Args>(args)...},
456 type_ (INTEGER_ARRAY), shared_ (SHARED) {}
463 template<
typename... Args>
465 : double_array_ (
std::make_shared<
std::vector<double>> (
466 std::forward<Args>(args)...)),
467 type_ (DOUBLE_ARRAY) {}
474 template<
typename... Args>
476 : double_array_ {std::forward<Args>(args)...},
477 type_ (DOUBLE_ARRAY), shared_ (SHARED) {}
487 template<
typename... Args>
490 std::forward<Args>(args)...)),
502 template<
typename... Args>
504 : str_value_ {std::forward<Args>(args)...},
505 type_ (STRING), shared_ (SHARED) {}
512 template<
typename... Args>
514 : file_value_ (
std::make_shared<
std::vector<unsigned char>> (
515 std::forward<Args>(args)...)),
516 type_ (UNKNOWN_FILE_TYPE) {}
524 template<
typename... Args>
526 : file_value_ {std::forward<Args>(args)...},
527 type_ (UNKNOWN_FILE_TYPE), shared_ (SHARED) {}
567 typename std::enable_if<std::is_integral<T>::value,
568 void*>::type =
nullptr>
569 void set_index (
size_t index, T value);
579 typename std::enable_if<std::is_floating_point<T>::value,
580 void*>::type =
nullptr>
581 void set_index (
size_t index, T value);
595 std::shared_ptr<std::string> share_string()
const;
602 std::shared_ptr<std::string> take_string();
608 ssize_t to_file (
const std::string & filename)
const;
629 Integer to_integer (
void)
const;
639 double to_double (
void)
const;
645 std::vector <Integer> to_integers (
void)
const;
651 std::shared_ptr<std::vector<Integer>> share_integers()
const;
658 std::shared_ptr<std::vector<Integer>> take_integers();
664 std::vector <double> to_doubles (
void)
const;
670 std::shared_ptr<std::vector<double>> share_doubles()
const;
677 std::shared_ptr<std::vector<double>> take_doubles();
683 std::shared_ptr<std::vector<unsigned char>> share_binary()
const;
690 std::shared_ptr<std::vector<unsigned char>> take_binary();
706 unsigned char * to_unmanaged_buffer (
size_t & size)
const;
735 typename std::enable_if<std::is_integral<T>::value,
736 void*>::type =
nullptr>
744 void set_value (
const Integer * new_value, uint32_t size);
750 void set_value (std::vector <Integer> && new_value);
756 void set_value (
const std::vector <Integer> & new_value);
762 void set_value (std::shared_ptr<std::vector <Integer>> new_value);
769 void set_value (
const char * new_value, uint32_t size);
787 void set_value (std::shared_ptr<std::string> new_value);
794 typename std::enable_if<std::is_floating_point<T>::value,
795 void*>::type =
nullptr>
803 void set_value (
const double * new_value, uint32_t size);
809 void set_value (std::vector <double> && new_value);
815 void set_value (
const std::vector <double> & new_value);
821 void set_value (std::shared_ptr<std::vector <double>> new_value);
825 typename std::enable_if<!std::is_convertible<T, KnowledgeRecord>::value,
826 decltype(this->
set_value(std::forward<T>(t)), *
this)>::type
837 void set_xml (
const char * new_value,
size_t size);
855 void set_xml (std::shared_ptr<std::string> new_value);
862 void set_text (
const char * new_value,
size_t size);
880 void set_text (std::shared_ptr<std::string> new_value);
889 static void set_precision (
int new_precision);
894 static void set_scientific (
void);
899 static void set_fixed (
void);
905 static int get_precision (
void);
912 void set_jpeg (
const unsigned char * new_value,
size_t size);
918 void set_jpeg (std::vector <unsigned char> && new_value);
924 void set_jpeg (
const std::vector <unsigned char> & new_value);
930 void set_jpeg (std::shared_ptr<std::vector <unsigned char>> new_value);
937 void set_file (
const unsigned char * new_value,
size_t size);
943 void set_file (std::vector <unsigned char> && new_value);
949 void set_file (
const std::vector <unsigned char> & new_value);
955 void set_file (std::shared_ptr<std::vector <unsigned char>> new_value);
979 int status (
void)
const;
984 void set_modified (
void);
990 void resize (
size_t new_size);
1005 void reset_value (
void) noexcept;
1008 void clear_union (
void) noexcept;
1016 void clear_value (
void) noexcept;
1021 uint32_t size (
void)
const;
1026 int32_t
type (
void)
const;
1039 bool set_type (int32_t type);
1045 bool is_ref_counted (
void)
const;
1052 static bool is_ref_counted (uint32_t type);
1058 bool is_string_type (
void)
const;
1065 static bool is_string_type (uint32_t type);
1071 bool is_double_type (
void)
const;
1078 static bool is_double_type (uint32_t type);
1084 bool is_integer_type (
void)
const;
1091 static bool is_integer_type (uint32_t type);
1098 bool is_array_type (
void)
const;
1105 static bool is_array_type (uint32_t type);
1122 bool is_image_type (
void)
const;
1129 static bool is_image_type (uint32_t type);
1135 bool is_file_type (
void)
const;
1142 static bool is_file_type (uint32_t type);
1148 bool is_binary_file_type (
void)
const;
1155 static bool is_binary_file_type (uint32_t type);
1176 template<
typename T,
1177 typename std::enable_if<std::is_integral<T>::value,
1178 void*>::type =
nullptr>
1184 template<
typename T,
1185 typename std::enable_if<std::is_floating_point<T>::value,
1186 void*>::type =
nullptr>
1290 explicit operator bool (
void)
const;
1310 const char * read (
const char * buffer, int64_t & buffer_remaining);
1321 const char * read (
const char * buffer,
std::string & key,
1322 int64_t & buffer_remaining);
1333 const char * read (
const char * buffer, uint32_t & key_id,
1334 int64_t & buffer_remaining);
1352 char * write (
char * buffer, int64_t & buffer_remaining)
const;
1373 char * write (
char * buffer,
const std::string & key,
1374 int64_t & buffer_remaining)
const;
1397 char * write (
char * buffer, uint32_t key_id,
1398 int64_t & buffer_remaining)
const;
1404 const std::string & key,
unsigned int quality, uint64_t clock,
1412 bool is_true (
void)
const;
1419 bool is_false (
void)
const;
1428 return status() != UNCREATED;
1436 int64_t get_encoded_size (
const std::string & key)
const;
1443 int64_t get_encoded_size (
void)
const;
1446 template<
typename T>
1449 template<
typename T, u
int32_t Type, MemberType<T> Member,
typename... Args>
1453 return *
new(&(this->*Member)) std::shared_ptr<T>(
1454 std::forward<Args>(args)...);
1457 template<
typename T, u
int32_t Type, MemberType<T> Member,
typename... Args>
1459 return emplace_shared_val<T, Type, Member> (std::move(
1460 std::make_shared<T> (
1461 std::forward<Args>(args)...)));
1464 template<
typename T, u
int32_t Type, MemberType<std::vector<T>> Member,
1467 return emplace_shared_val<std::vector<T>, Type, Member> (
1468 std::forward<Args>(args)...);
1471 template<
typename T, u
int32_t Type, MemberType<std::vector<T>> Member,
1474 return emplace_val<std::vector<T>, Type, Member> (
1475 std::forward<Args>(args)...);
1493 uint32_t
max_quality (
const KnowledgeRecords & records);
1500 uint32_t
max_quality (
const KnowledgeMap & records);
1513 #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.
void emplace_shared_file(Args &&...args)
Construct a shared_ptr to a file (vector of unsigned char) within this KnowledgeRecord.
auto operator+=(KnowledgeRecord &l, const T &r) -> decltype(l+=knowledge_cast(r))
void emplace(tags::string_t, Args &&...args)
Construct a string within this KnowledgeRecord.
auto operator>=(const KnowledgeRecord &l, const T &r) -> decltype(knowledge_cast< T >(l) >=r)
void emplace_string(Args &&...args)
Construct a string within this KnowledgeRecord.
auto operator>(const KnowledgeRecord &l, const T &r) -> decltype(knowledge_cast< T >(l) >r)
auto operator*=(KnowledgeRecord &l, const T &r) -> decltype(l *=knowledge_cast(r))
KnowledgeRecord(tags::string_t, Args &&...args)
Forwarding constructor for strings Each argument past the first will be forwarded to construct a std:...
helper type for specifying template type parameters using a function argument instead of inside expli...
auto operator==(const KnowledgeRecord &l, const T &r) -> decltype(knowledge_cast< T >(l)==r)
void emplace_integers(Args &&...args)
Construct a vector of integers within this KnowledgeRecord.
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.
int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char)
Reads a file into a provided void pointer.
KnowledgeRecord(tags::integers_t, Args &&...args)
Forwarding constructor for integer arrays Each argument past the first will be forwarded to construct...
void emplace(tags::shared_t< tags::binary_t >, Args &&...args)
Construct a shared_ptr to binary (vector of unsigned char) within this KnowledgeRecord.
auto operator+(const KnowledgeRecord &l, const T &r) -> decltype(l+knowledge_cast(r))
auto operator/(const KnowledgeRecord &l, const T &r) -> decltype(l/knowledge_cast(r))
auto operator-(const KnowledgeRecord &l, const T &r) -> decltype(l-knowledge_cast(r))
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_
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.
auto operator%(const KnowledgeRecord &l, const T &r) -> decltype(l%knowledge_cast(r))
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.
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.
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 KnowledgeRecord &l, const T &r) -> decltype(knowledge_cast< T >(l)< r)
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
auto operator=(T &&t) -> typename std::enable_if<!std::is_convertible< T, KnowledgeRecord >::value, decltype(this->set_value(std::forward< T >(t)),*this)>::type
KnowledgeRecord KnowledgeValue
auto operator*(const KnowledgeRecord &l, const T &r) -> decltype(l *knowledge_cast(r))
std::shared_ptr< T > & emplace_val(Args &&...args)
std::shared_ptr< std::vector< double > > double_array_
auto operator-=(KnowledgeRecord &l, const T &r) -> decltype(l-=knowledge_cast(r))
auto operator/=(KnowledgeRecord &l, const T &r) -> decltype(l/=knowledge_cast(r))
auto operator<=(const KnowledgeRecord &l, const T &r) -> decltype(knowledge_cast< T >(l)<=r)
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_
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...
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.
auto operator!=(const KnowledgeRecord &l, const T &r) -> decltype(knowledge_cast< T >(l)!=r)
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.
auto operator%=(KnowledgeRecord &l, const T &r) -> decltype(l%=knowledge_cast(r))
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++(Tracked< T > &lhs) -> decltype(++lhs.get_mut())
::std::vector< std::string > StringVector
std::shared_ptr< std::string > str_value_