56 #ifndef _MADARA_KNOWLEDGE_KNOWLEDGE_CAST_H_
57 #define _MADARA_KNOWLEDGE_KNOWLEDGE_CAST_H_
61 #include <type_traits>
81 typename std::enable_if<
82 std::is_constructible<O, const KnowledgeRecord&>::value, O>
::type
92 return static_cast<O
>(in.to_double());
101 return static_cast<O
>(in.to_integer());
126 typename std::enable_if<std::is_integral<T>::value, std::vector<T>>
::type
128 auto vec = in.to_integers();
129 return {std::begin(vec), std::end(vec)};
142 typename std::enable_if<std::is_floating_point<T>::value,
143 std::vector<T>>::type
145 auto vec = in.to_doubles();
146 return {std::begin(vec), std::end(vec)};
154 inline auto get_size(
const T& c) -> decltype(c.size())
159 template<
typename T,
size_t N>
169 *std::begin(std::declval<T&>()))>
::type>::value,
170 decltype(in.share_integers())>
::type
172 return in.share_integers();
178 *std::begin(std::declval<T&>()))>
::type>::value,
179 decltype(in.to_integers())>
::type
181 return in.to_integers();
187 *std::begin(std::declval<T&>()))>
::type>::value,
188 decltype(in.share_doubles())>
::type
190 return in.share_doubles();
196 *std::begin(std::declval<T&>()))>::type>::value,
197 decltype(in.to_doubles())>::type
199 return in.to_doubles();
247 template<
typename CharT,
typename Traits,
typename Allocator>
255 template<
typename CharT,
typename Traits,
typename Allocator>
257 const KnowledgeRecord& in, std::basic_string<CharT, Traits, Allocator>& out)
260 std::basic_string<CharT, Traits, Allocator>>::value,
261 "knowledge_cast only supports std::string, "
262 "not other std::basic_string types");
299 typename std::decay<decltype(
KnowledgeRecord{std::forward<O>(in)})>::type
312 unsigned char>::value &&
313 !std::is_same<T, std::vector<int64_t>>::value,
322 inline auto knowledge_cast(
const T& in) ->
typename std::enable_if<
324 !std::is_same<T, std::vector<double>>::value,
333 inline auto knowledge_cast(
const T& in) ->
typename std::enable_if<
334 std::is_same<
typename std::decay<decltype(in[0])>::type,
char>::value &&
335 !std::is_same<T, std::string>::value,
337 tags::string, std::begin(in), std::end(in)})>::type>::type
344 inline auto knowledge_cast(
const T& in) ->
typename std::enable_if<
345 std::is_same<
typename std::decay<decltype(in[0])>::type,
346 unsigned char>::value &&
347 !std::is_same<T, std::vector<unsigned char>>::value,
349 tags::binary, std::begin(in), std::end(in)})>::type>::type
363 template<
typename Container>
367 using out_type =
typename Container::value_type;
392 #define MADARA_KNOWLEDGE_COMPARE_OP(op) \
393 template<typename T, typename std::enable_if< \
394 !std::is_convertible<T, KnowledgeRecord>::value && \
395 std::is_fundamental<T>::value, \
396 void*>::type = nullptr> \
397 inline auto operator op(const KnowledgeRecord& l, const T& r) \
398 ->decltype(knowledge_cast<T>(l) op r) \
400 return knowledge_cast<T>(l) op r; \
403 template<typename T, typename std::enable_if< \
404 !std::is_convertible<T, KnowledgeRecord>::value && \
405 std::is_fundamental<T>::value, \
406 void*>::type = nullptr> \
407 inline auto operator op(const T& l, const KnowledgeRecord& r) \
408 ->decltype(l op knowledge_cast<T>(r)) \
410 return l op knowledge_cast<T>(r); \
413 inline bool operator op(const KnowledgeRecord& l, std::nullptr_t) \
418 inline bool operator op(const KnowledgeRecord& l, const char* r) \
420 auto s = l.share_string(); \
423 return s->compare(r) op 0; \
427 return l.to_string().compare(r) op 0; \
431 inline bool operator op(const char* l, const KnowledgeRecord& r) \
433 auto s = r.share_string(); \
436 return std::strcmp(l, s->c_str()) op 0; \
440 return std::strcmp(l, r.to_string().c_str()) op 0; \
444 inline bool operator op(const KnowledgeRecord& l, const std::string& r) \
446 auto s = l.share_string(); \
449 return s->compare(r) op 0; \
453 return l.to_string().compare(r) op 0; \
457 inline bool operator op(const std::string& l, const KnowledgeRecord& r) \
459 auto s = r.share_string(); \
462 return l.compare(*s) op 0; \
466 return l.compare(r.to_string()) op 0; \
479 #define MADARA_KNOWLEDGE_BINARY_OP(op) \
480 template<typename T, typename std::enable_if< \
481 !std::is_convertible<T, KnowledgeRecord>::value && \
482 std::is_fundamental<T>::value, \
483 void*>::type = nullptr> \
484 inline auto operator op(const KnowledgeRecord& l, const T& r) \
485 ->decltype(l op knowledge_cast(r)) \
487 return l op knowledge_cast(r); \
490 template<typename T, typename std::enable_if< \
491 !std::is_convertible<T, KnowledgeRecord>::value && \
492 std::is_fundamental<T>::value, \
493 void*>::type = nullptr> \
494 inline auto operator op(const T& l, const KnowledgeRecord& r) \
495 ->decltype(knowledge_cast(l) op r) \
497 return knowledge_cast(l) op r; \
508 #define MADARA_KNOWLEDGE_COMPOSITE_OP(op) \
509 template<typename T, typename std::enable_if< \
510 !std::is_convertible<T, KnowledgeRecord>::value && \
511 std::is_fundamental<T>::value, \
512 void*>::type = nullptr> \
513 inline auto operator op(KnowledgeRecord& l, const T& r) \
514 ->decltype(l op knowledge_cast(r)) \
516 return l op knowledge_cast(r); \
525 template<typename OutputIterator>
527 OutputIterator out,
size_t index,
size_t count)
const
529 return for_history_range(
#define MADARA_KNOWLEDGE_COMPOSITE_OP(op)
Generates composite assignment operators for KnowledgeRecords, in combination with types that can be ...
#define MADARA_KNOWLEDGE_COMPARE_OP(op)
Generates comparison operators for KnowledgeRecords, in combination with types that can be knowledge_...
#define MADARA_KNOWLEDGE_BINARY_OP(op)
Generates binary math operators for KnowledgeRecords, in combination with types that can be knowledge...
This class encapsulates an entry in a KnowledgeBase.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
bool is_true(void) const
Checks to see if the record is true.
std::vector< double > to_doubles(void) const
converts the value to a vector of doubles
auto to_array(const KnowledgeRecord &in) -> typename std::enable_if< std::is_integral< typename std::decay< decltype(*std::begin(std::declval< T & >()))>::type >::value, decltype(in.to_integers())>::type
auto share_array(const KnowledgeRecord &in) -> typename std::enable_if< std::is_integral< typename std::decay< decltype(*std::begin(std::declval< T & >()))>::type >::value, decltype(in.share_integers())>::type
simple_span< T > make_span(T *ptr, size_t size)
auto get_size(const T &c) -> decltype(c.size())
Provides functions and classes for the distributed knowledge base.
auto knowledge_cast(type< O >, const KnowledgeRecord &in) -> typename std::enable_if< std::is_constructible< O, const KnowledgeRecord & >::value, O >::type
By default, call constructor of target class; for other semantics, define specializations.
typename std::enable_if< Pred, T >::type enable_if_
Less verbose synonym for std::enable_if.
constexpr bool is_same()
Less verbose equivalent for std::is_same.
constexpr bool is_integral()
Less verbose equivalent for std::is_integral.
typename std::decay< T >::type decay_
Less verbose synonym for std::decay.
constexpr bool is_floating_point()
Less verbose equivalent for std::is_floating_point.
Copyright(c) 2020 Galois.
T & operator[](size_t i) const