Go to the documentation of this file.00001
00002
00003
00004 #include <string>
00005 #include <sstream>
00006 #include <cstdio>
00007 #include <boost/functional/hash.hpp>
00008
00009 #ifndef DDYNAMIC_RECONFIGURE_DD_VALUE_H
00010 #define DDYNAMIC_RECONFIGURE_DD_VALUE_H
00011 using namespace std;
00012 using namespace boost;
00013 namespace ddynamic_reconfigure {
00029 class Value {
00030 private:
00031 int int_val_;
00032 string str_val_, type_;
00033 double double_val_;
00034 bool bool_val_;
00035 public:
00040 inline explicit Value(int val) : double_val_(0.0), bool_val_(false) {
00041 type_ = "int";
00042 int_val_ = val;
00043 }
00044
00049 inline explicit Value(double val) : int_val_(0), bool_val_(false) {
00050 type_ = "double";
00051 double_val_ = val;
00052 }
00053
00058 inline explicit Value(const string &val) : int_val_(0), double_val_(0.0), bool_val_(false) {
00059 type_ = "string";
00060 str_val_ = val;
00061 }
00062
00067 inline explicit Value(const char* val) : int_val_(0), double_val_(0.0), bool_val_(false) {
00068 *this = Value(string(val));
00069 }
00070
00075 inline explicit Value(bool val) : int_val_(0), double_val_(0.0) {
00076 type_ = "bool";
00077 bool_val_ = val;
00078 }
00079
00084 inline string getType() const {
00085 return type_;
00086 }
00087
00097 inline int toInt() const {
00098 if (type_ == "string") {
00099 int i;
00100 if (sscanf(str_val_.c_str(), "%d", &i) == 0) {
00101 return (int) boost::hash<string>()(str_val_);
00102 }
00103 return i;
00104 } else if (type_ == "bool") { return bool_val_ ? 1 : 0; }
00105 else if (type_ == "double") { return (int) double_val_; }
00106 else { return int_val_; }
00107 }
00108
00116 inline string toString() const {
00117 stringstream ss;
00118 if(type_ == "string") { return str_val_;}
00119 else if(type_ == "bool") {return bool_val_ ? "true" : "false";}
00120 else if(type_ == "double") {ss << double_val_; return ss.str();}
00121 else {ss << int_val_; return ss.str();}
00122 }
00123
00133 inline double toDouble() const {
00134 if(type_ == "string") {
00135 double f;
00136 if(sscanf(str_val_.c_str(), "%lf", &f) == 0) {
00137 return boost::hash<string>()(str_val_);
00138 }
00139 return f;
00140 } else if(type_ == "bool") {return bool_val_ ? 1.0 : 0.0;}
00141 else if(type_ == "double") {return double_val_;}
00142 else {return int_val_;}
00143 }
00144
00152 inline bool toBool() const {
00153 if(type_ == "string") { return str_val_ == "true";}
00154 else if(type_ == "bool") {return bool_val_;}
00155 else if(type_ == "double") {return double_val_ > 0.0;}
00156 else {return int_val_ > 0;}
00157 }
00158 };
00159 }
00160
00161 #endif //DDYNAMIC_RECONFIGURE_DD_VALUE_H