00001 #ifdef __clang__
00002 #pragma clang diagnostic push
00003 #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
00004 #pragma clang diagnostic ignored "-Wunknown-pragmas"
00005 #pragma ide diagnostic ignored "modernize-loop-convert"
00006 #pragma ide diagnostic ignored "modernize-use-auto"
00007 #endif
00008
00009
00010
00011
00012 #include <ddynamic_reconfigure/param/dd_enum_param.h>
00013
00014 #include "ddynamic_reconfigure/param/dd_enum_param.h"
00015
00016 map<string,pair<int,string> > fillGaps(map<string,int> old_map) {
00017 map<string,pair<int,string> > ret;
00018 for(map<string,int>::const_iterator it = old_map.begin(); it != old_map.end(); it++) {
00019 ret[it->first] = pair<int,string>(it->second,"");
00020 };
00021 return ret;
00022 };
00023
00024 namespace ddynamic_reconfigure {
00025
00026 void DDEnum::prepGroup(Group &group) {
00027 ParamDescription desc;
00028 desc.name = name_;
00029 desc.level = level_;
00030 desc.description = desc_;
00031 desc.type = "int";
00032 desc.edit_method = makeEditMethod();
00033 group.parameters.push_back(desc);
00034 }
00035
00036 bool DDEnum::sameType(Value val) {
00037 return val.getType() == "int" || val.getType() == "string";
00038 }
00039
00040 bool DDEnum::sameValue(Value val) {
00041 if(val.getType() == "string" && dict_.find(val.toString())->second.first == val_) {
00042 return true;
00043 } else {
00044 return val.toInt() == val_;
00045 }
00046 }
00047
00048 void DDEnum::setValue(Value val) {
00049 if(val.getType() == "string" && dict_.find(val.toString()) != dict_.end()) {
00050 val_ = lookup(val);
00051 } else {
00052 val_ = val.toInt();
00053 }
00054 }
00055
00056 int DDEnum::lookup(Value val) {
00057 if(val.getType() == "string" && dict_.find(val.toString()) != dict_.end()) {
00058 return dict_.find(val.toString())->second.first;
00059 } else {
00060 return val.toInt();
00061 }
00062 }
00063
00064 DDEnum::DDEnum(const string &name, unsigned int level, const string &description,
00065 int def, const map<string, int> &dictionary) :
00066 DDInt(name,level,description,def),
00067 dict_(fillGaps(dictionary)) {
00068 max_ = def;
00069 min_ = def;
00070 for(map<string,int>::const_iterator it = dictionary.begin(); it != dictionary.end(); it++) {
00071 if(it->second > max_) {max_ = it->second;}
00072 if(it->second < min_) {min_ = it->second;}
00073 };
00074 }
00075
00076 DDEnum::DDEnum(const string &name, unsigned int level, const string &description,
00077 const string &def, const map<string, int> &dictionary) :
00078 DDInt(name,level,description,dictionary.find(def)->second),
00079 dict_(fillGaps(dictionary)) {
00080 max_ = def_;
00081 min_ = def_;
00082 for(map<string,int>::const_iterator it = dictionary.begin(); it != dictionary.end(); it++) {
00083 if(it->second > max_) {max_ = it->second;}
00084 if(it->second < min_) {min_ = it->second;}
00085 };
00086 }
00087
00088 DDEnum::DDEnum(const string &name, unsigned int level, const string &description, int def,
00089 const pair<EnumMap, string> &dictionary) :
00090 DDInt(name,level,description,def),
00091 dict_(dictionary.first) {
00092 max_ = def;
00093 min_ = def;
00094 for(EnumMap::const_iterator it = dict_.begin(); it != dict_.end(); it++) {
00095 if(it->second.first > max_) {max_ = it->second.first;}
00096 if(it->second.first < min_) {min_ = it->second.first;}
00097 };
00098 enum_description_ = dictionary.second;
00099 }
00100
00101 DDEnum::DDEnum(const string &name, unsigned int level, const string &description, const string &def,
00102 const pair<EnumMap,string> &dictionary) :
00103 DDInt(name,level,description,dictionary.first.find(def)->second.first),
00104 dict_(dictionary.first) {
00105 max_ = def_;
00106 min_ = def_;
00107 for(EnumMap::const_iterator it = dict_.begin(); it != dict_.end(); it++) {
00108 if(it->second.first > max_) {max_ = it->second.first;}
00109 if(it->second.first < min_) {min_ = it->second.first;}
00110 };
00111 enum_description_ = dictionary.second;
00112 }
00113
00114 string DDEnum::makeEditMethod() {
00115 stringstream ret;
00116 ret << "{";
00117 {
00118 ret << "'enum_description': '" << enum_description_ << "', ";
00119 ret << "'enum': [";
00120 {
00121 EnumMap::const_iterator it = dict_.begin();
00122 ret << makeConst(it->first, it->second.first, it->second.second);
00123 for(it++; it != dict_.end(); it++) {
00124 ret << ", " << makeConst(it->first, it->second.first, it->second.second);
00125 };
00126 }
00127 ret << "]";
00128 }
00129 ret << "}";
00130 return ret.str();
00131 }
00132
00133 string DDEnum::makeConst(string name, int value, string desc) {
00134 stringstream ret;
00135 ret << "{";
00136 {
00137 ret << "'srcline': 0, ";
00138 ret << "'description': '" << desc << "', ";
00139 ret << "'srcfile': '/does/this/really/matter.cfg', ";
00140 ret << "'cconsttype': 'const int', ";
00141 ret << "'value': " << value << ", ";
00142 ret << "'ctype': 'int', ";
00143 ret << "'type': 'int', ";
00144 ret << "'name': '" << name << "'";
00145 }
00146 ret << "}";
00147 return ret.str();
00148 }
00149 }
00150 #ifdef __clang__
00151 #pragma clang diagnostic pop
00152 #endif