ed
io/variant.h
Go to the documentation of this file.
1 #ifndef ERA_TUE_CONFIGURATION_VARIANT_H_
2 #define ERA_TUE_CONFIGURATION_VARIANT_H_
3 
4 #include <vector>
5 #include <map>
6 #include <string>
7 #include <iostream>
8 
9 namespace ed
10 {
11 
12 namespace io
13 {
14 
15 class Variant
16 {
17 
18 public:
19 
20  Variant() : type_('?') {}
21 
22  Variant(const double& d) : type_('d'), d_(d) {}
23  Variant(int i) : type_('i'), i_(i) {}
24  Variant(const std::string& s) : type_('s'), s_(s) {}
25  Variant(const char* s) : type_('s'), s_(s) {}
26 
27  bool getValue(int& v) const { return checkAndGet(i_, 'i', v); }
28  bool getValue(double& v) const { return checkAndGet(d_, 'd', v) || checkAndGet((double)i_, 'i', v); }
29  bool getValue(float& v) const { return checkAndGet((float)d_, 'd', v) || checkAndGet((float)i_, 'i', v); }
30  bool getValue(std::string& v) const { return checkAndGet(s_, 's', v); }
31 
32  bool getValue(bool& v) const
33  {
34  int i;
35  if (!checkAndGet(i_, 'i', i))
36  return false;
37  v = (i == 1);
38  return true;
39  }
40 
41  bool isString() const { return type_ == 's'; }
42 
43  bool inline valid() const { return type_ != '?'; }
44 
45 private:
46 
47  char type_;
48 
49  union {
50  int i_;
51  double d_;
52  };
53 
55 
56  template<typename T>
57  inline bool checkAndGet(const T& v, char type, T& out) const
58  {
59  if (type != type_)
60  return false;
61  out = v;
62  return true;
63  }
64 
65  friend std::ostream& operator<< (std::ostream& out, const Variant& v)
66  {
67  switch (v.type_)
68  {
69  case 'i': out << v.i_;
70  break;
71  case 'd': out << v.d_;
72  break;
73  case 's': out << v.s_;
74  break;
75  default: out << "?";
76  break;
77  }
78 
79  return out;
80  }
81 
82 };
83 
84 }
85 
86 }
87 
88 #endif
std::string
vector
ed::io::Variant::Variant
Variant()
Definition: io/variant.h:20
ed::io::Variant::d_
double d_
Definition: io/variant.h:51
ed::io::Variant::Variant
Variant(const std::string &s)
Definition: io/variant.h:24
ed::io::Variant::valid
bool valid() const
Definition: io/variant.h:43
ed::io::Variant::type_
char type_
Definition: io/variant.h:47
iostream
ed::io::Variant::getValue
bool getValue(int &v) const
Definition: io/variant.h:27
ed::io::Variant::checkAndGet
bool checkAndGet(const T &v, char type, T &out) const
Definition: io/variant.h:57
ed::io::Variant::i_
int i_
Definition: io/variant.h:50
ed::io::Variant::getValue
bool getValue(float &v) const
Definition: io/variant.h:29
std::ostream
ed::io::Variant::isString
bool isString() const
Definition: io/variant.h:41
map
ed::io::Variant::s_
std::string s_
Definition: io/variant.h:54
ed::io::Variant
Definition: io/variant.h:15
ed::io::Variant::getValue
bool getValue(bool &v) const
Definition: io/variant.h:32
ed::io::Variant::Variant
Variant(const double &d)
Definition: io/variant.h:22
ed::io::Variant::getValue
bool getValue(double &v) const
Definition: io/variant.h:28
ed
Definition: convex_hull.h:8
ed::io::Variant::Variant
Variant(const char *s)
Definition: io/variant.h:25
ed::io::Variant::getValue
bool getValue(std::string &v) const
Definition: io/variant.h:30
ed::io::Variant::operator<<
friend std::ostream & operator<<(std::ostream &out, const Variant &v)
Definition: io/variant.h:65
ed::io::Variant::Variant
Variant(int i)
Definition: io/variant.h:23
string