MADARA  3.1.8
knowledge_cast.h
Go to the documentation of this file.
1 
56 #ifndef INCL_KNOWLEDGE_CAST_HPP
57 #define INCL_KNOWLEDGE_CAST_HPP
58 
59 #include <string>
60 #include <stdbool.h>
62 
63 namespace madara
64 {
65 
66  namespace knowledge
67  {
68 
71  template<class O>
72  inline O knowledge_cast(const KnowledgeRecord &in)
73  {
74  return O(in);
75  }
76 
77  template<>
78  inline float knowledge_cast<float>(const KnowledgeRecord &in)
79  {
80  return static_cast<float>(in.to_double());
81  }
82 
83  template<>
84  inline double knowledge_cast<double>(const KnowledgeRecord &in)
85  {
86  return static_cast<double>(in.to_double());
87  }
88 
89  template<>
90  inline long double knowledge_cast<long double>(const KnowledgeRecord &in)
91  {
92  return static_cast<long double>(in.to_double());
93  }
94 
95  template<>
96  inline bool knowledge_cast<bool>(const KnowledgeRecord &in)
97  {
98  return in.to_integer() ? true : false;
99  }
100 
101  template<>
102  inline char knowledge_cast<char>(const KnowledgeRecord &in)
103  {
104  return static_cast<char>(in.to_integer());
105  }
106 
107  template<>
108  inline unsigned char knowledge_cast<unsigned char>(const KnowledgeRecord &in)
109  {
110  return static_cast<unsigned char>(in.to_integer());
111  }
112 
113  template<>
114  inline short knowledge_cast<short>(const KnowledgeRecord &in)
115  {
116  return static_cast<short>(in.to_integer());
117  }
118 
119  template<>
120  inline unsigned short knowledge_cast<unsigned short>(const KnowledgeRecord &in)
121  {
122  return static_cast<unsigned short>(in.to_integer());
123  }
124 
125  template<>
126  inline int knowledge_cast<int>(const KnowledgeRecord &in)
127  {
128  return static_cast<int>(in.to_integer());
129  }
130 
131  template<>
132  inline unsigned int knowledge_cast<unsigned int>(const KnowledgeRecord &in)
133  {
134  return static_cast<unsigned int>(in.to_integer());
135  }
136 
137  template<>
138  inline long int knowledge_cast<long int>(const KnowledgeRecord &in)
139  {
140  return static_cast<long int>(in.to_integer());
141  }
142 
143  template<>
144  inline unsigned long int knowledge_cast<unsigned long int>(const KnowledgeRecord &in)
145  {
146  return static_cast<unsigned long int>(in.to_integer());
147  }
148 
149  template<>
150  inline long long int knowledge_cast<long long int>(const KnowledgeRecord &in)
151  {
152  return static_cast<long long int>(in.to_integer());
153  }
154 
155  template<>
156  inline unsigned long long int knowledge_cast<unsigned long long int>(const KnowledgeRecord &in)
157  {
158  return static_cast<unsigned long long int>(in.to_integer());
159  }
160 
161  template<>
163  {
164  return in.to_string();
165  }
166 
167  template<>
168  inline std::vector<int64_t> knowledge_cast<std::vector<int64_t>>(const KnowledgeRecord &in)
169  {
170  return in.to_integers();
171  }
172 
173  template<>
174  inline std::vector<double> knowledge_cast<std::vector<double>>(const KnowledgeRecord &in)
175  {
176  return in.to_doubles();
177  }
178 
179  template<>
181  {
182  return in;
183  }
184 
185  inline KnowledgeRecord knowledge_cast(const int &in)
186  {
188  }
189 
190  inline KnowledgeRecord knowledge_cast(const unsigned int &in)
191  {
193  }
194 
195  inline KnowledgeRecord knowledge_cast(const long int &in)
196  {
198  }
199 
200  inline KnowledgeRecord knowledge_cast(const unsigned long int &in)
201  {
203  }
204 
205  inline KnowledgeRecord knowledge_cast(const long long int &in)
206  {
208  }
209 
210  inline KnowledgeRecord knowledge_cast(const unsigned long long int &in)
211  {
213  }
214 
215  inline KnowledgeRecord knowledge_cast(const short &in)
216  {
218  }
219 
220  inline KnowledgeRecord knowledge_cast(const unsigned short &in)
221  {
223  }
224 
225  inline KnowledgeRecord knowledge_cast(const char &in)
226  {
228  }
229 
230  inline KnowledgeRecord knowledge_cast(const unsigned char &in)
231  {
233  }
234 
235  inline KnowledgeRecord knowledge_cast(const bool &in)
236  {
237  return KnowledgeRecord(KnowledgeRecord::Integer(in ? 1 : 0));
238  }
239 
240  inline KnowledgeRecord knowledge_cast(const float &in)
241  {
242  return KnowledgeRecord(static_cast<double>(in));
243  }
244 
245  inline KnowledgeRecord knowledge_cast(const double &in)
246  {
247  return KnowledgeRecord(in);
248  }
249 
250  inline KnowledgeRecord knowledge_cast(const long double &in)
251  {
252  return KnowledgeRecord(static_cast<double>(in));
253  }
254 
256  {
257  return KnowledgeRecord(in);
258  }
259 
260  inline KnowledgeRecord knowledge_cast(const std::vector<int64_t> &in)
261  {
262  return KnowledgeRecord(in);
263  }
264 
265  inline KnowledgeRecord knowledge_cast(const std::vector<double> &in)
266  {
267  return KnowledgeRecord(in);
268  }
269 
270  template<size_t N>
271  inline KnowledgeRecord knowledge_cast(const uint64_t (&in)[N])
272  {
273  return KnowledgeRecord(&in, in + N);
274  }
275 
276  template<size_t N>
277  inline KnowledgeRecord knowledge_cast(const double (&in)[N])
278  {
279  return KnowledgeRecord(&in, in + N);
280  }
281 
283  {
284  return in;
285  }
286 
287  inline const KnowledgeRecord &knowledge_cast(const KnowledgeRecord &in)
288  {
289  return in;
290  }
291  }
292 }
293 
294 #endif
This class encapsulates an entry in a KnowledgeBase.
madara::knowledge::KnowledgeRecord KnowledgeRecord
O knowledge_cast(const KnowledgeRecord &in)
By default, call constructor of target class; for other semantics, define specializations.
static constexpr struct madara::knowledge::tags::string_t string
Provides functions and classes for the distributed knowledge base.
Copyright (c) 2015 Carnegie Mellon University.