IntEnumClass.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5 * Karlsruhe Institute of Technology (KIT), all rights reserved.
6 *
7 * ArmarX is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * ArmarX is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24// Header
25#include "IntEnumClass.h"
26
27#include <SimoxUtility/meta/type_name.h>
28
30{
31 // constructors
34 n.getEnumName(),
35 n.getEnumName(),
36 simox::meta::get_type_name<data::dto::NDArray>(),
37 simox::meta::get_type_name<type::dto::IntEnum>(),
38 n)
39 {
40 if (type.getMaybe() != type::Maybe::NONE)
41 {
42 //due to this being a specialized variant type the getMaybe() function will not actually check the xml
43 //the maybe flag is always set to the default value of NONE
44 std::string message = "Somehow the maybe flag of a top level int enum "
45 "declaration is set. This is not valid!\n"
46 "Maybe value is: " + std::to_string((int)type.getMaybe()) + "\n"
47 "Path is: " + type.getPath().toString();
48 throw error::ValueNotValidException(__PRETTY_FUNCTION__,
49 message,
50 std::to_string((int)type.getMaybe()));
51 }
52 }
53
54 std::vector<CppFieldPtr>
55 IntEnumClass::getPublicVariableDeclarations(const std::string& className) const
56 {
57 std::vector<CppFieldPtr> fields;
58 std::stringstream enum_to_name;
59 std::stringstream name_to_enum;
60 std::stringstream enum_to_value;
61 std::stringstream value_to_enum;
62
63 // add legacy typedef
64 fields.push_back(
65 std::make_shared<CppField>("using",
66 std::string(IMPL_ENUM),
67 simox::alg::to_lower(className) + "_details::Enum",
68 "Legacy typedef of enum"));
69
70 ARMARX_CHECK(type.getAcceptedValueMap().size() > 0);
71
72 enum_to_name << "{" << std::endl;
73 name_to_enum << "{" << std::endl;
74 enum_to_value << "{" << std::endl;
75 value_to_enum << "{" << std::endl;
76 for (const auto& [key, value] : type.getAcceptedValueMap())
77 {
78 std::string enumKeyWithNamespace = std::string(IMPL_ENUM) + "::" + key;
79 fields.push_back(std::make_shared<CppField>(
80 "static constexpr " + std::string(IMPL_ENUM), key + " = " + enumKeyWithNamespace));
81
82 enum_to_name << "\t\t{" << enumKeyWithNamespace << ", \"" << key << "\"}," << std::endl;
83 name_to_enum << "\t\t{\"" << key << "\", " << enumKeyWithNamespace << "}," << std::endl;
84
85 enum_to_value << "\t\t{" << enumKeyWithNamespace << ", " << value << "}," << std::endl;
86 value_to_enum << "\t\t{" << value << ", " << enumKeyWithNamespace << "}," << std::endl;
87 }
88
89 enum_to_name << "\t}";
90 name_to_enum << "\t}";
91 enum_to_value << "\t}";
92 value_to_enum << "\t}";
93
94 fields.push_back(std::make_shared<CppField>("static inline const std::map<" +
95 std::string(IMPL_ENUM) + ", std::string>",
96 "EnumToStringMap",
97 enum_to_name.str(),
98 "Mapping enum values to readable strings"));
99 fields.push_back(std::make_shared<CppField>("static inline const std::map<std::string, " +
100 std::string(IMPL_ENUM) + ">",
101 "StringToEnumMap",
102 name_to_enum.str(),
103 "Mapping readable strings to enum values"));
104 fields.push_back(std::make_shared<CppField>("static inline const std::map<" +
105 std::string(IMPL_ENUM) + ", int>",
106 "EnumToValueMap",
107 enum_to_value.str(),
108 "Mapping enum values to a int value"));
109 fields.push_back(std::make_shared<CppField>("static inline const std::map<int, " +
110 std::string(IMPL_ENUM) + ">",
111 "ValueToEnumMap",
112 value_to_enum.str(),
113 "Mapping int values to a enum"));
114
115 fields.push_back(std::make_shared<CppField>(std::string(IMPL_ENUM),
116 "value",
117 type.getDefaultValueName(),
118 "The current value of the enum object"));
119
120 return fields;
121 }
122
124 IntEnumClass::getResetSoftBlock(const std::string& accessor) const
125 {
126 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
127 block_if_data->addLine("value = {};");
128 return block_if_data;
129 }
130
132 IntEnumClass::getResetHardBlock(const std::string& accessor) const
133 {
134 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
135 block_if_data->addLine("value = {};");
136 return block_if_data;
137 }
138
141 const std::string&,
142 const Path& p,
143 std::string&) const
144 {
145 static const std::string INT_ENUM_VALUE_MAP = ARON_VARIABLE_PREFIX + "_str2ValueMap";
146
147 CppBlockPtr b = std::make_shared<CppBlock>();
148
149 std::vector<std::string> map_initializer;
150 for (const auto& [key, value] : type.getAcceptedValueMap())
151 {
152 map_initializer.push_back("{\"" + key + "\", " + std::to_string(value) + "}");
153 }
154 b->addLine("std::map<std::string, int> " + INT_ENUM_VALUE_MAP + " = {" +
155 simox::alg::to_string(map_initializer, ", ") + "};");
156 b->addLine("return " + ARON_WRITER_ACCESSOR + ".writeIntEnum(\"" + type.getEnumName() +
157 "\", " + INT_ENUM_VALUE_MAP + ", " + "\"" + type.getDefaultValueName() + "\", " +
159 return b;
160 }
161
163 IntEnumClass::getWriteBlock(const std::string&, const Path& p, std::string&) const
164 {
165 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
166
167 block_if_data->addLine("return " + ARON_WRITER_ACCESSOR +
168 ".writePrimitive(EnumToValueMap.at(value), " + ARON_PATH_ACCESSOR +
169 "); // of top level enum " + getInstantiatedCppTypename());
170 return block_if_data;
171 }
172
174 IntEnumClass::getReadBlock(const std::string&, const std::string& variantAccessor) const
175 {
176 static const std::string INT_ENUM_TMP_VALUE = ARON_VARIABLE_PREFIX + "_tmpValue";
177
178 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
179 block_if_data->addLine("int " + INT_ENUM_TMP_VALUE + ";");
180 block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readPrimitive(" + variantAccessor +
181 ", " + INT_ENUM_TMP_VALUE + "); // of top level enum " +
183
184 block_if_data->addLine("auto valueToEnumMap_iterator = ValueToEnumMap.find(" +
185 INT_ENUM_TMP_VALUE + ");");
186 block_if_data->addLine(
187 "ARMARX_CHECK_AND_THROW(valueToEnumMap_iterator != ValueToEnumMap.end(), "
188 "::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Missing enum for value "
189 "'\" + std::to_string(" +
190 INT_ENUM_TMP_VALUE + ") + \"' in aron enum '" + getFullClassCppTypename() + "'.\"))");
191 block_if_data->addLine("value = valueToEnumMap_iterator->second;");
192 return block_if_data;
193 }
194
196 IntEnumClass::getEqualsBlock(const std::string& accessor,
197 const std::string& otherInstanceAccessor) const
198 {
199 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
200 block_if_data->addLine("if (not (value == " + otherInstanceAccessor + ".value))");
201 block_if_data->addLineAsBlock("return false;");
202 return block_if_data;
203 }
204
205 /*CppCtorPtr IntEnumClass::toCopyCtor(const std::string& name) const
206 {
207 CppCtorPtr c = std::make_shared<CppCtor>(name + "(const " + getFullInstantiatedCppTypename() + "& i)");
208 std::vector<std::pair<std::string, std::string>> initList = {{"value", "i.value"}};
209 c->addInitListEntries(initList);
210 c->setBlock(std::make_shared<CppBlock>());
211 return c;
212 }*/
213
215 IntEnumClass::toEnumCtor(const std::string& name) const
216 {
217 CppCtorPtr c = std::make_shared<CppCtor>(name + "(const " + std::string(IMPL_ENUM) + " e)");
218 std::vector<std::pair<std::string, std::string>> initList = {{"value", "e"}};
219 c->addInitListEntries(initList);
220 c->setBlock(std::make_shared<CppBlock>());
221 return c;
222 }
223
224 std::vector<CppEnumFieldPtr>
226 {
227 std::vector<CppEnumFieldPtr> e;
228 for (const auto& [key, value] : type.getAcceptedValueMap())
229 {
230 e.push_back(std::make_shared<CppEnumField>(key));
231 }
232 return e;
233 }
234
237 {
238 std::stringstream doc;
239 doc << "@brief int() - Converts the internally stored value to int representation \n";
240 doc << "@return - the int representation";
241
242 CppMethodPtr m = CppMethodPtr(new CppMethod("operator int() const", doc.str()));
243 CppBlockPtr b = std::make_shared<CppBlock>();
244 b->addLine("return EnumToValueMap.at(value);");
245 m->setBlock(b);
246 return m;
247 }
248
251 {
252 std::stringstream doc;
253 doc << "@brief operator=() - Assignment operator for copy \n";
254 doc << "@return - nothing";
255
256 CppMethodPtr m =
257 CppMethodPtr(new CppMethod(getFullInstantiatedCppTypename() + "& operator=(const " +
259 doc.str()));
260 CppBlockPtr b = std::make_shared<CppBlock>();
261 b->addLine("value = c.value;");
262 b->addLine("return *this;");
263 m->setBlock(b);
264 return m;
265 }
266
269 {
270 std::stringstream doc;
271 doc << "@brief operator=() - Assignment operator for the internally defined enum \n";
272 doc << "@return - nothing";
273
275 getFullInstantiatedCppTypename() + "& operator=(" + std::string(IMPL_ENUM) + " v)",
276 doc.str()));
277 CppBlockPtr b = std::make_shared<CppBlock>();
278 b->addLine("value = v;");
279 b->addLine("return *this;");
280 m->setBlock(b);
281 return m;
282 }
283
286 {
287 std::stringstream doc;
288 doc << "@brief operator=() - Assignment operator for the internally defined enum \n";
289 doc << "@return - nothing";
290
292 new CppMethod(getFullInstantiatedCppTypename() + "& operator=(int v)", doc.str()));
293 CppBlockPtr b = std::make_shared<CppBlock>();
294 b->addLine("if (auto it = ValueToEnumMap.find(v); it == ValueToEnumMap.end())");
295 CppBlockPtr b2 = std::make_shared<CppBlock>();
296 b2->addLine("throw armarx::LocalException(\"The input int is not valid. Could net set the "
297 "enum to value '\" + std::to_string(v) + \"'\");");
298 b->addBlock(b2);
299 b->addLine("else");
300 CppBlockPtr b3 = std::make_shared<CppBlock>();
301 b3->addLine("value = it->second;");
302 b->addBlock(b3);
303 b->addLine("return *this;");
304 m->setBlock(b);
305 return m;
306 }
307
310 {
311 std::stringstream doc;
312 doc << "@brief toString() - Converts the internally stored value to string \n";
313 doc << "@return - the name of the enum";
314
315 CppMethodPtr m = CppMethodPtr(new CppMethod("std::string toString() const", doc.str()));
316 CppBlockPtr b = std::make_shared<CppBlock>();
317 b->addLine("return EnumToStringMap.at(value);");
318 m->setBlock(b);
319 return m;
320 }
321
324 {
325 std::stringstream doc;
326 doc << "@brief fromString() - sets the internally stored value to the corrsponding enum of "
327 "the input str \n";
328 doc << "@return - nothing";
329
330 CppMethodPtr m =
331 CppMethodPtr(new CppMethod("void fromString(const std::string& str)", doc.str()));
332 CppBlockPtr b = std::make_shared<CppBlock>();
333 b->addLine("if (auto it = StringToEnumMap.find(str); it == StringToEnumMap.end())");
334 CppBlockPtr b2 = std::make_shared<CppBlock>();
335 b2->addLine("throw armarx::LocalException(\"The input name is not valid. Could net set the "
336 "enum to value '\" + str + \"'\");");
337 b->addBlock(b2);
338 b->addLine("else");
339 CppBlockPtr b3 = std::make_shared<CppBlock>();
340 b3->addLine("value = it->second;");
341 b->addBlock(b3);
342
343 m->setBlock(b);
344 return m;
345 }
346} // namespace armarx::aron::codegenerator::cpp::generator
constexpr T c
The Path class.
Definition Path.h:36
static const std::string ARON_READER_ACCESSOR
Definition Generator.h:164
static const std::string ARON_PATH_ACCESSOR
Definition Generator.h:163
static const std::string ARON_VARIABLE_PREFIX
Definition Generator.h:160
static const std::string ARON_WRITER_ACCESSOR
Definition Generator.h:165
static const std::string ARON_MAYBE_TYPE_ACCESSOR
Definition Generator.h:162
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
CppBlockPtr getEqualsBlock(const std::string &, const std::string &) const final
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
std::vector< CppEnumFieldPtr > toEnumFields() const
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
std::vector< CppFieldPtr > getPublicVariableDeclarations(const std::string &) const final
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
SpecializedGeneratorBase(const std::string &instantiatedCppTypename, const std::string &classCppTypename, const std::string &aronDataTypename, const std::string &aronTypeTypename, const type::IntEnum &t)
The IntEnum class.
Definition IntEnum.h:43
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< CppBlock > CppBlockPtr
Definition CppBlock.h:37
std::shared_ptr< CppCtor > CppCtorPtr
Definition CppCtor.h:31
std::shared_ptr< CppMethod > CppMethodPtr
Definition CppMethod.h:31
Definition Impl.cpp:41