dd_value.h
Go to the documentation of this file.
1 //
2 // Created by Noam Dori on 19/06/18.
3 //
4 #include <string>
5 #include <sstream>
6 #include <cstdio>
7 #include <boost/functional/hash.hpp>
8 
9 #ifndef DDYNAMIC_RECONFIGURE_DD_VALUE_H
10 #define DDYNAMIC_RECONFIGURE_DD_VALUE_H
11 using namespace std;
12 using namespace boost;
13 namespace ddynamic_reconfigure {
29  class Value {
30  private:
31  int int_val_;
32  string str_val_, type_;
33  double double_val_;
34  bool bool_val_;
35  public:
40  inline explicit Value(int val) : double_val_(0.0), bool_val_(false) {
41  type_ = "int";
42  int_val_ = val;
43  }
44 
49  inline explicit Value(double val) : int_val_(0), bool_val_(false) {
50  type_ = "double";
51  double_val_ = val;
52  }
53 
58  inline explicit Value(const string &val) : int_val_(0), double_val_(0.0), bool_val_(false) {
59  type_ = "string";
60  str_val_ = val;
61  }
62 
67  inline explicit Value(const char* val) : int_val_(0), double_val_(0.0), bool_val_(false) {
68  *this = Value(string(val));
69  }
70 
75  inline explicit Value(bool val) : int_val_(0), double_val_(0.0) {
76  type_ = "bool";
77  bool_val_ = val;
78  }
79 
84  inline string getType() const {
85  return type_;
86  }
87 
97  inline int toInt() const {
98  if (type_ == "string") {
99  int i;
100  if (sscanf(str_val_.c_str(), "%d", &i) == 0) {
101  return (int) boost::hash<string>()(str_val_);
102  }
103  return i;
104  } else if (type_ == "bool") { return bool_val_ ? 1 : 0; }
105  else if (type_ == "double") { return (int) double_val_; }
106  else { return int_val_; }
107  }
108 
116  inline string toString() const {
117  stringstream ss;
118  if(type_ == "string") { return str_val_;}
119  else if(type_ == "bool") {return bool_val_ ? "true" : "false";}
120  else if(type_ == "double") {ss << double_val_; return ss.str();}
121  else {ss << int_val_; return ss.str();}
122  }
123 
133  inline double toDouble() const {
134  if(type_ == "string") {
135  double f;
136  if(sscanf(str_val_.c_str(), "%lf", &f) == 0) {
137  return boost::hash<string>()(str_val_);
138  }
139  return f;
140  } else if(type_ == "bool") {return bool_val_ ? 1.0 : 0.0;}
141  else if(type_ == "double") {return double_val_;}
142  else {return int_val_;}
143  }
144 
152  inline bool toBool() const {
153  if(type_ == "string") { return str_val_ == "true";}
154  else if(type_ == "bool") {return bool_val_;}
155  else if(type_ == "double") {return double_val_ > 0.0;}
156  else {return int_val_ > 0;}
157  }
158  };
159 }
160 
161 #endif //DDYNAMIC_RECONFIGURE_DD_VALUE_H
Value(const string &val)
creates a string value wrapper
Definition: dd_value.h:58
Value(double val)
creates an double value wrapper
Definition: dd_value.h:49
f
Value(bool val)
creates an integer value wrapper
Definition: dd_value.h:75
int toInt() const
converts the stored value into an integer.
Definition: dd_value.h:97
Value(int val)
creates an integer value wrapper
Definition: dd_value.h:40
The Value class is used to wrap all basic data-types (bool,int,double,string) in something generic...
Definition: dd_value.h:29
bool toBool() const
converts the stored value into a boolean.
Definition: dd_value.h:152
double toDouble() const
converts the stored value into a double.
Definition: dd_value.h:133
string toString() const
converts the stored value into a string.
Definition: dd_value.h:116
string getType() const
gets the type this value wrapper stores
Definition: dd_value.h:84
Value(const char *val)
creates a c-string value wrapper, though it is considered a regular string.
Definition: dd_value.h:67


ddynamic_reconfigure
Author(s): Noam Dori
autogenerated on Thu May 16 2019 02:46:37