MADARA  3.4.1
GetRecord.h
Go to the documentation of this file.
1 
9 #ifndef _MADARA_KNOWLEDGE_GET_RECORD_H_
10 #define _MADARA_KNOWLEDGE_GET_RECORD_H_
11 
15 
17 
18 namespace madara
19 {
20 namespace exceptions
21 {
26 {
27 public:
28  using MadaraException::MadaraException;
29 };
30 
35 {
36 public:
37  using InvalidGetException::InvalidGetException;
38 };
39 
45 {
46 public:
47  using InvalidGetException::InvalidGetException;
48 };
49 }
50 }
51 
52 namespace madara
53 {
54 namespace knowledge
55 {
56 #ifdef DOXYGEN
57 
65 template<typename T>
66 inline bool type_match(type<T> t, const KnowledgeRecord& kr);
67 #else
68 
69 template<typename T>
70 inline auto type_match(type<T>, const KnowledgeRecord& kr) ->
71  typename std::enable_if<std::is_integral<T>::value, bool>::type
72 {
73  return kr.type() == KnowledgeRecord::INTEGER;
74 }
75 
76 template<typename T>
77 inline auto type_match(type<T>, const KnowledgeRecord& kr) ->
78  typename std::enable_if<std::is_floating_point<T>::value, bool>::type
79 {
80  return kr.type() == KnowledgeRecord::DOUBLE;
81 }
82 
83 inline bool type_match(type<std::string>, const KnowledgeRecord& kr)
84 {
85  return kr.type() == KnowledgeRecord::STRING;
86 }
87 
88 template<typename T>
89 inline auto type_match(type<std::vector<T> >, const KnowledgeRecord& kr) ->
90  typename std::enable_if<std::is_integral<T>::value, bool>::type
91 {
92  return kr.type() == KnowledgeRecord::INTEGER_ARRAY;
93 }
94 
95 template<typename T>
96 inline auto type_match(type<std::vector<T> >, const KnowledgeRecord& kr) ->
97  typename std::enable_if<std::is_floating_point<T>::value, bool>::type
98 {
99  return kr.type() == KnowledgeRecord::DOUBLE_ARRAY;
100 }
101 
102 #endif // DOXYGEN
103 
120 template<typename T>
121 inline T get(const KnowledgeRecord& kr)
122 {
123  if (!kr.exists())
124  {
125  throw exceptions::MissingValueException("madara::knowledge::get: "
126  "KnowledgeRecord does not exist");
127  }
128  if (!type_match(type<T>{}, kr))
129  {
131  "madara::knowledge::get: "
132  "Expected type does not match the KnowledgeRecord's type (" +
133  std::to_string(kr.type()) + ")");
134  }
135 
136  return knowledge_cast<T>(kr);
137 }
138 
139 } // namespace knowledge
140 } // namespace madara
141 
142 #endif // _MADARA_KNOWLEDGE_GET_RECORD_H_
A base class for exceptions thrown by knowledge::get.
Definition: GetRecord.h:26
Base exception for all custom Madara exceptions.
Exception thrown when attempting to retrieve a value from a record of the wrong type.
Definition: GetRecord.h:45
Exception thrown when a attempting to retrieve a value from an empty record.
Definition: GetRecord.h:35
This class encapsulates an entry in a KnowledgeBase.
uint32_t type(void) const
returns the type of the value
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
Provides functions and classes for the distributed knowledge base.
bool type_match(type< T > t, const KnowledgeRecord &kr)
Check if a KnowledgeRecord type matches a specified type.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
Copyright(c) 2020 Galois.