56 #ifndef _MADARA_KNOWLEDGE_KNOWLEDGE_CAST_H_ 57 #define _MADARA_KNOWLEDGE_KNOWLEDGE_CAST_H_ 61 #include <type_traits> 157 typename std::enable_if<std::is_constructible<O,
166 typename std::enable_if<std::is_floating_point<O>::value, O>
::type 174 typename std::enable_if<std::is_integral<O>::value, O>
::type 201 typename std::enable_if<std::is_integral<T>::value, std::vector<T>>
::type 204 return {std::begin(vec), std::end(vec)};
216 typename std::enable_if<std::is_floating_point<T>::value, std::vector<T>>
::type 219 return {std::begin(vec), std::end(vec)};
226 inline auto get_size(
const T &c) -> decltype(c.size()) {
230 template<
typename T,
size_t N>
231 inline size_t get_size(
const T (&arr)[N]) {
239 inline auto resize_or_clear(T &c,
size_t n) ->
240 typename std::enable_if<!supports_resize<T&>::value,
241 decltype(c[0], get_size(c),
size_t{})>::
type 243 using elem_type =
typename std::decay<decltype(c[0])>::type;
244 size_t curn = get_size(c);
245 for (; n < curn; ++n) {
252 inline auto resize_or_clear(T &c,
size_t n) ->
253 typename std::enable_if<supports_resize<T&>::value,
size_t>
::type 261 typename std::enable_if<std::is_integral<
262 typename std::decay<decltype(*std::begin(std::declval<T&>()))>::
type>::value,
270 typename std::enable_if<std::is_integral<
271 typename std::decay<decltype(*std::begin(std::declval<T&>()))>::
type>::value,
279 typename std::enable_if<std::is_floating_point<
280 typename std::decay<decltype(*std::begin(std::declval<T&>()))>::
type>::value,
288 typename std::enable_if<std::is_floating_point<
289 typename std::decay<decltype(*std::begin(std::declval<T&>()))>::
type>::value,
301 T *begin()
const {
return ptr; }
302 const T *cbegin()
const {
return ptr; }
303 T *end()
const {
return ptr + len; }
304 const T *cend()
const {
return ptr + len; }
306 T &operator[](
size_t i)
const {
return ptr[i]; }
308 size_t size()
const {
return len; }
312 simple_span<T> make_span(T* ptr,
size_t size) {
317 (std::is_arithmetic<
typename std::decay<
318 decltype(*std::begin(x))>::
type>::value,
326 typename std::enable_if<!impl::supports_target_container<T&>::value,
333 template <
typename T>
334 struct is_basic_string : std::false_type {};
336 template<
typename CharT,
typename Traits,
typename Allocator>
337 struct is_basic_string<std::basic_string<CharT, Traits, Allocator>> :
342 template<
typename CharT,
typename Traits,
typename Allocator>
343 inline std::basic_string<CharT, Traits, Allocator> &
knowledge_cast(
345 std::basic_string<CharT, Traits, Allocator> & out)
348 std::basic_string<CharT, Traits, Allocator>>::value,
349 "knowledge_cast only supports std::string, " 350 "not other std::basic_string types");
358 typename std::enable_if<
359 impl::supports_target_container<T&>::value &&
360 !impl::is_basic_string<T>::value,
363 auto ints = impl::share_array<T>(in);
365 size_t count = ints->size();
366 count = std::min(impl::resize_or_clear(out, count), count);
367 std::copy_n (std::begin(*ints), count, std::begin(out));
369 auto ints_arr = impl::to_array<T>(in);
370 size_t count = ints_arr.size();
371 count = std::min(impl::resize_or_clear(out, count), count);
372 std::copy_n (std::begin(ints_arr), count, std::begin(out));
381 -> decltype(
knowledge_cast(in, std::declval<impl::simple_span<T>&>()),
384 auto span = impl::make_span(out, size);
417 typename std::enable_if<
418 std::is_integral<typename std::decay<decltype(in[0])>::type>::value &&
419 !std::is_same<typename std::decay<decltype(in[0])>::type,
char>::value &&
420 !std::is_same<typename std::decay<decltype(in[0])>::type,
421 unsigned char>::value &&
422 !std::is_same<T, std::vector<int64_t>>::value,
432 typename std::enable_if<
433 std::is_floating_point<typename std::decay<decltype(in[0])>::type>::value &&
434 !std::is_same<T, std::vector<double>>::value,
444 typename std::enable_if<
445 std::is_same<typename std::decay<decltype(in[0])>::type,
char>::value &&
446 !std::is_same<T, std::string>::value,
456 typename std::enable_if<
457 std::is_same<typename std::decay<decltype(in[0])>::type,
unsigned char>::value &&
458 !std::is_same<T, std::vector<unsigned char>>::value,
481 #define MADARA_KNOWLEDGE_COMPARE_OP(op) \ 482 template<typename T, \ 483 typename std::enable_if<!std::is_convertible<T, KnowledgeRecord>::value && \ 484 std::is_fundamental<T>::value, void *>::type = nullptr> \ 485 inline auto operator op (const KnowledgeRecord &l, const T &r) -> \ 486 decltype(knowledge_cast<T>(l) op r) \ 488 return knowledge_cast<T>(l) op r; \ 491 template<typename T, \ 492 typename std::enable_if<!std::is_convertible<T, KnowledgeRecord>::value && \ 493 std::is_fundamental<T>::value, void *>::type = nullptr> \ 494 inline auto operator op (const T &l, const KnowledgeRecord &r) -> \ 495 decltype(l op knowledge_cast<T>(r)) \ 497 return l op knowledge_cast<T>(r); \ 500 inline bool operator op (const KnowledgeRecord &l, std::nullptr_t) \ 505 inline bool operator op (const KnowledgeRecord &l, const char *r) \ 507 auto s = l.share_string(); \ 509 return s->compare(r) op 0; \ 511 return l.to_string().compare(r) op 0; \ 515 inline bool operator op (const char *l, const KnowledgeRecord &r) \ 517 auto s = r.share_string(); \ 519 return std::strcmp(l, s->c_str()) op 0; \ 521 return std::strcmp(l, r.to_string().c_str()) op 0; \ 525 inline bool operator op (const KnowledgeRecord &l, const std::string &r) \ 527 auto s = l.share_string(); \ 529 return s->compare(r) op 0; \ 531 return l.to_string().compare(r) op 0; \ 535 inline bool operator op (const std::string &l, const KnowledgeRecord &r) \ 537 auto s = r.share_string(); \ 539 return l.compare(*s) op 0; \ 541 return l.compare(r.to_string()) op 0; \ 554 #define MADARA_KNOWLEDGE_BINARY_OP(op) \ 555 template<typename T, \ 556 typename std::enable_if<!std::is_convertible<T, KnowledgeRecord>::value && \ 557 std::is_fundamental<T>::value, void *>::type = nullptr> \ 558 inline auto operator op (const KnowledgeRecord &l, const T &r) -> \ 559 decltype(l op knowledge_cast(r)) \ 561 return l op knowledge_cast(r); \ 564 template<typename T, \ 565 typename std::enable_if<!std::is_convertible<T, KnowledgeRecord>::value && \ 566 std::is_fundamental<T>::value, void *>::type = nullptr> \ 567 inline auto operator op (const T &l, const KnowledgeRecord &r) -> \ 568 decltype(knowledge_cast(l) op r) \ 570 return knowledge_cast(l) op r; \ 581 #define MADARA_KNOWLEDGE_COMPOSITE_OP(op) \ 582 template<typename T, \ 583 typename std::enable_if<!std::is_convertible<T, KnowledgeRecord>::value && \ 584 std::is_fundamental<T>::value, void *>::type = nullptr> \ 585 inline auto operator op (KnowledgeRecord &l, const T &r) -> \ 586 decltype(l op knowledge_cast(r)) \ 588 return l op knowledge_cast(r); \ This class encapsulates an entry in a KnowledgeBase.
bool is_true(void) const
Checks to see if the record is true.
#define MADARA_KNOWLEDGE_BINARY_OP(op)
Generates binary math operators for KnowledgeRecords, in combination with types that can be knowledge...
std::shared_ptr< std::vector< double > > share_doubles() const
helper type for specifying template type parameters using a function argument instead of inside expli...
double to_double(void) const
converts the value to a float/double.
std::shared_ptr< std::vector< Integer > > share_integers() const
std::vector< double > to_doubles(void) const
converts the value to a vector of doubles
Integer to_integer(void) const
converts the value to an integer.
#define MADARA_MAKE_VAL_SUPPORT_TEST(name, var, expr)
Macro which generates feature testing traits, to allow enabling features based on what a given type s...
#define MADARA_KNOWLEDGE_COMPARE_OP(op)
Generates comparison operators for KnowledgeRecords, in combination with types that can be knowledge_...
#define MADARA_KNOWLEDGE_COMPOSITE_OP(op)
Generates composite assignment operators for KnowledgeRecords, in combination with types that can be ...
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
Provides functions and classes for the distributed knowledge base.
Out knowledge_cast(const KnowledgeRecord &in)
Convert a KnowledgeRecord into a specified type.
Copyright (c) 2015 Carnegie Mellon University.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.