C API Documentation
Variant.h
1 /*
2  * Variant.h
3  *
4  * Created on: Apr 26, 2014
5  * Author: andy
6  */
7 
8 #ifndef VARIANT_H_
9 #define VARIANT_H_
10 
11 #include "rrExporter.h"
12 #include <typeinfo>
13 #include <string>
14 #include <vector>
15 
16 
17 namespace rr
18 {
19 
74 class RR_DECLSPEC Variant
75 {
76 public:
77 
81  enum TypeId
82  {
83  STRING, BOOL, INT32, UINT32, INT64, UINT64, FLOAT, DOUBLE, CHAR, UCHAR, EMPTY, DOUBLEVECTOR
84  };
85 
89  TypeId type() const;
90 
95 
104  template <typename T>
105  Variant(const T& val) : self(0)
106  {
107  const std::type_info &info = typeid(T);
108  alloc();
109  assign(info, &val);
110  }
111 
112  Variant(const char* str) : self(0)
113  {
114  const std::type_info &info = typeid(std::string);
115  std::string val(str);
116  alloc();
117  assign(info, &val);
118  }
119 
123  Variant(const Variant& other);
124 
130  template <typename T>
131  Variant& operator = (const T& value)
132  {
133  const std::type_info &info = typeid(T);
134  assign(info, &value);
135  return *this;
136  }
137 
141  Variant& operator = (const Variant& other);
142 
146  virtual ~Variant();
147 
163  const std::type_info& typeInfo() const;
164 
177  template <typename T>
178  T convert() const
179  {
180  T value;
181  convert_to(typeid(T), &value);
182  return value;
183  }
184 
190  #define VARIANT_IMPLICIT_CONVERT(type) \
191  operator type() const \
192  { \
193  return convert<type>(); \
194  }
195 
196  VARIANT_IMPLICIT_CONVERT(std::string);
197 
198  VARIANT_IMPLICIT_CONVERT(long);
199 
200  VARIANT_IMPLICIT_CONVERT(bool);
201 
202  VARIANT_IMPLICIT_CONVERT(float);
203 
204  VARIANT_IMPLICIT_CONVERT(double);
205 
206  VARIANT_IMPLICIT_CONVERT(unsigned long);
207 
208  VARIANT_IMPLICIT_CONVERT(int);
209 
210  VARIANT_IMPLICIT_CONVERT(unsigned int);
211 
212  VARIANT_IMPLICIT_CONVERT(char);
213 
214  VARIANT_IMPLICIT_CONVERT(unsigned char);
215 
216  VARIANT_IMPLICIT_CONVERT(std::vector<double>);
217 
225  static Variant parse(const std::string& val);
226 
230  std::string toString() const;
231 
236  std::string pythonRepr() const;
237 
241  bool isString() const;
242 
246  bool isInteger() const;
247 
251  bool isNumeric() const;
252 
256  bool isBool() const;
257 
261  bool isEmpty() const;
262 
266  bool isSigned() const;
267 
268  /*
269  * true if this is a vector of doubles
270  * */
271  bool isDoubleVector() const;
272 
273 private:
279  struct VariantImpl *self;
280  /*
281  * Allocates this Variant's VariantImpl on the free store
282  */
283  void alloc();
284  /*
285  * Assigns this variant object to the value pointed to by value
286  * of the type specified by info
287  *
288  * Pre: value must point to valid data for the type specified by info
289  *
290  * Throws std::invalid_argument if the type specified by info is not
291  * supported by rr::Variant
292  */
293  void assign(const std::type_info& info, const void* value);
294  /*
295  * Converts this variant object to the type specified by info and puts
296  * the result at value.
297  *
298  * Pre: the type specified by info is supported by rr::Variant
299  *
300  * Performs type coercions supported by Poco::Dynamic::Var (https://pocoproject.org/docs/Poco.Dynamic.Var.html)
301  *
302  * Throws std::invalid_argument if the type given is not supported by rr:Variant
303  *
304  * Throws std::logic_error if the conversion cannot be performed
305  */
306  void convert_to(const std::type_info& info, void* value) const;
307 };
308 
309 } /* namespace rr */
310 
311 #endif /* VARIANT_H_ */
Definition: Variant.h:75
bool isString() const
virtual ~Variant()
TypeId
Definition: Variant.h:82
bool isSigned() const
const std::type_info & typeInfo() const
T convert() const
Definition: Variant.h:178
Variant(const T &val)
Definition: Variant.h:105
Variant(const Variant &other)
std::string pythonRepr() const
Convert to Python-compatible representation.
std::string toString() const
bool isBool() const
bool isInteger() const
static Variant parse(const std::string &val)
bool isEmpty() const
TypeId type() const
bool isNumeric() const