dd_enum_param.cpp
Go to the documentation of this file.
1 #ifdef __clang__
2 #pragma clang diagnostic push
3 #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
4 #pragma clang diagnostic ignored "-Wunknown-pragmas"
5 #pragma ide diagnostic ignored "modernize-loop-convert"
6 #pragma ide diagnostic ignored "modernize-use-auto"
7 #endif
8 //
9 // Created by Noam Dori on 20/06/18.
10 //
11 
13 
15 
16 map<string,pair<int,string> > fillGaps(map<string,int> old_map) {
17  map<string,pair<int,string> > ret;
18  for(map<string,int>::const_iterator it = old_map.begin(); it != old_map.end(); it++) {
19  ret[it->first] = pair<int,string>(it->second,"");
20  };
21  return ret;
22 };
23 
24 namespace ddynamic_reconfigure {
25 
26  void DDEnum::prepGroup(Group &group) {
27  ParamDescription desc;
28  desc.name = name_;
29  desc.level = level_;
30  desc.description = desc_;
31  desc.type = "int";
32  desc.edit_method = makeEditMethod();
33  group.parameters.push_back(desc);
34  }
35 
36  bool DDEnum::sameType(Value val) {
37  return val.getType() == "int" || val.getType() == "string";
38  }
39 
41  if(val.getType() == "string" && dict_.find(val.toString())->second.first == val_) {
42  return true;
43  } else {
44  return val.toInt() == val_;
45  }
46  }
47 
48  void DDEnum::setValue(Value val) {
49  if(val.getType() == "string" && dict_.find(val.toString()) != dict_.end()) {
50  val_ = lookup(val);
51  } else {
52  val_ = val.toInt();
53  }
54  }
55 
56  int DDEnum::lookup(Value val) {
57  if(val.getType() == "string" && dict_.find(val.toString()) != dict_.end()) {
58  return dict_.find(val.toString())->second.first;
59  } else {
60  return val.toInt();
61  }
62  }
63 
64  DDEnum::DDEnum(const string &name, unsigned int level, const string &description,
65  int def, const map<string, int> &dictionary) :
66  DDInt(name,level,description,def),
67  dict_(fillGaps(dictionary)) {
68  max_ = def;
69  min_ = def;
70  for(map<string,int>::const_iterator it = dictionary.begin(); it != dictionary.end(); it++) {
71  if(it->second > max_) {max_ = it->second;}
72  if(it->second < min_) {min_ = it->second;}
73  };
74  }
75 
76  DDEnum::DDEnum(const string &name, unsigned int level, const string &description,
77  const string &def, const map<string, int> &dictionary) :
78  DDInt(name,level,description,dictionary.find(def)->second),
79  dict_(fillGaps(dictionary)) {
80  max_ = def_;
81  min_ = def_;
82  for(map<string,int>::const_iterator it = dictionary.begin(); it != dictionary.end(); it++) {
83  if(it->second > max_) {max_ = it->second;}
84  if(it->second < min_) {min_ = it->second;}
85  };
86  }
87 
88  DDEnum::DDEnum(const string &name, unsigned int level, const string &description, int def,
89  const pair<EnumMap, string> &dictionary) :
90  DDInt(name,level,description,def),
91  dict_(dictionary.first) {
92  max_ = def;
93  min_ = def;
94  for(EnumMap::const_iterator it = dict_.begin(); it != dict_.end(); it++) {
95  if(it->second.first > max_) {max_ = it->second.first;}
96  if(it->second.first < min_) {min_ = it->second.first;}
97  };
98  enum_description_ = dictionary.second;
99  }
100 
101  DDEnum::DDEnum(const string &name, unsigned int level, const string &description, const string &def,
102  const pair<EnumMap,string> &dictionary) :
103  DDInt(name,level,description,dictionary.first.find(def)->second.first),
104  dict_(dictionary.first) {
105  max_ = def_;
106  min_ = def_;
107  for(EnumMap::const_iterator it = dict_.begin(); it != dict_.end(); it++) {
108  if(it->second.first > max_) {max_ = it->second.first;}
109  if(it->second.first < min_) {min_ = it->second.first;}
110  };
111  enum_description_ = dictionary.second;
112  }
113 
115  stringstream ret;
116  ret << "{";
117  {
118  ret << "'enum_description': '" << enum_description_ << "', ";
119  ret << "'enum': [";
120  {
121  EnumMap::const_iterator it = dict_.begin();
122  ret << makeConst(it->first, it->second.first, it->second.second);
123  for(it++; it != dict_.end(); it++) {
124  ret << ", " << makeConst(it->first, it->second.first, it->second.second);
125  };
126  }
127  ret << "]";
128  }
129  ret << "}";
130  return ret.str();
131  }
132 
133  string DDEnum::makeConst(string name, int value, string desc) {
134  stringstream ret;
135  ret << "{";
136  {
137  ret << "'srcline': 0, "; // the sole reason this is here is because dynamic placed it in its enum JSON.
138  ret << "'description': '" << desc << "', ";
139  ret << "'srcfile': '/does/this/really/matter.cfg', "; // the answer is no. This is useless.
140  ret << "'cconsttype': 'const int', ";
141  ret << "'value': " << value << ", ";
142  ret << "'ctype': 'int', ";
143  ret << "'type': 'int', ";
144  ret << "'name': '" << name << "'";
145  }
146  ret << "}";
147  return ret.str();
148  }
149 }
150 #ifdef __clang__
151 #pragma clang diagnostic pop
152 #endif
bool sameType(Value val)
checks whether or not the raw value stored in the value is compatible with the given parameter...
int def_
the default value (def_), the current value (val_), the minimum allowed value (min_), and the maximum allowed value (max_)
Definition: dd_int_param.h:69
unsigned int level_
the level of the parameter: the degree in which things need to be shut down if this param changes ...
Definition: dd_int_param.h:62
DDEnum(const string &name, unsigned int level, const string &description, int def, const map< string, int > &dictionary)
creates a new int-enum param
int toInt() const
converts the stored value into an integer.
Definition: dd_value.h:97
map< string, pair< int, string > > fillGaps(map< string, int > old_map)
void setValue(Value val)
sets the value of this parameter as this one.
void prepGroup(Group &group)
updates a group message according to this param&#39;s info.
string name_
the name of the parameter (name_), and its description (desc_)
Definition: dd_int_param.h:74
bool sameValue(Value val)
checks whether or not the value stored in the value object, when converted to the type of the interna...
const EnumMap dict_
A dictionary from the string aliases to their integer counterparts. This method of storage allows int...
Definition: dd_enum_param.h:92
string enum_description_
this holds the physical enum&#39;s description. why is this here? because 1D-reconfigure.
Definition: dd_enum_param.h:96
The Value class is used to wrap all basic data-types (bool,int,double,string) in something generic...
Definition: dd_value.h:29
string toString() const
converts the stored value into a string.
Definition: dd_value.h:116
string makeConst(string name, int value, string desc)
string getType() const
gets the type this value wrapper stores
Definition: dd_value.h:84
an integer implementation of the parameter. This is used to 32 bit signed integral numbers...
Definition: dd_int_param.h:17


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