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
33  detail::SpecializedGeneratorBase<type::IntEnum, IntEnumClass>(
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  {
41  {
42  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
43  "Somehow the maybe flag of a top level int enum "
44  "declaration is set. This is not valid!",
46  type.getPath());
47  }
48  }
49 
50  std::vector<CppFieldPtr>
51  IntEnumClass::getPublicVariableDeclarations(const std::string& className) const
52  {
53  std::vector<CppFieldPtr> fields;
54  std::stringstream enum_to_name;
55  std::stringstream name_to_enum;
56  std::stringstream enum_to_value;
57  std::stringstream value_to_enum;
58 
59  // add legacy typedef
60  fields.push_back(
61  std::make_shared<CppField>("using",
62  std::string(IMPL_ENUM),
63  simox::alg::to_lower(className) + "_details::Enum",
64  "Legacy typedef of enum"));
65 
67 
68  enum_to_name << "{" << std::endl;
69  name_to_enum << "{" << std::endl;
70  enum_to_value << "{" << std::endl;
71  value_to_enum << "{" << std::endl;
72  for (const auto& [key, value] : type.getAcceptedValueMap())
73  {
74  std::string enumKeyWithNamespace = std::string(IMPL_ENUM) + "::" + key;
75  fields.push_back(std::make_shared<CppField>(
76  "static constexpr " + std::string(IMPL_ENUM), key + " = " + enumKeyWithNamespace));
77 
78  enum_to_name << "\t\t{" << enumKeyWithNamespace << ", \"" << key << "\"}," << std::endl;
79  name_to_enum << "\t\t{\"" << key << "\", " << enumKeyWithNamespace << "}," << std::endl;
80 
81  enum_to_value << "\t\t{" << enumKeyWithNamespace << ", " << value << "}," << std::endl;
82  value_to_enum << "\t\t{" << value << ", " << enumKeyWithNamespace << "}," << std::endl;
83  }
84 
85  enum_to_name << "\t}";
86  name_to_enum << "\t}";
87  enum_to_value << "\t}";
88  value_to_enum << "\t}";
89 
90  fields.push_back(std::make_shared<CppField>("static inline const std::map<" +
91  std::string(IMPL_ENUM) + ", std::string>",
92  "EnumToStringMap",
93  enum_to_name.str(),
94  "Mapping enum values to readable strings"));
95  fields.push_back(std::make_shared<CppField>("static inline const std::map<std::string, " +
96  std::string(IMPL_ENUM) + ">",
97  "StringToEnumMap",
98  name_to_enum.str(),
99  "Mapping readable strings to enum values"));
100  fields.push_back(std::make_shared<CppField>("static inline const std::map<" +
101  std::string(IMPL_ENUM) + ", int>",
102  "EnumToValueMap",
103  enum_to_value.str(),
104  "Mapping enum values to a int value"));
105  fields.push_back(std::make_shared<CppField>("static inline const std::map<int, " +
106  std::string(IMPL_ENUM) + ">",
107  "ValueToEnumMap",
108  value_to_enum.str(),
109  "Mapping int values to a enum"));
110 
111  fields.push_back(std::make_shared<CppField>(std::string(IMPL_ENUM),
112  "value",
114  "The current value of the enum object"));
115 
116  return fields;
117  }
118 
120  IntEnumClass::getResetSoftBlock(const std::string& accessor) const
121  {
122  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
123  block_if_data->addLine("value = {};");
124  return block_if_data;
125  }
126 
128  IntEnumClass::getResetHardBlock(const std::string& accessor) const
129  {
130  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
131  block_if_data->addLine("value = {};");
132  return block_if_data;
133  }
134 
137  const std::string&,
138  const Path& p,
139  std::string&) const
140  {
141  static const std::string INT_ENUM_VALUE_MAP = ARON_VARIABLE_PREFIX + "_str2ValueMap";
142 
143  CppBlockPtr b = std::make_shared<CppBlock>();
144 
145  std::vector<std::string> map_initializer;
146  for (const auto& [key, value] : type.getAcceptedValueMap())
147  {
148  map_initializer.push_back("{\"" + key + "\", " + std::to_string(value) + "}");
149  }
150  b->addLine("std::map<std::string, int> " + INT_ENUM_VALUE_MAP + " = {" +
151  simox::alg::to_string(map_initializer, ", ") + "};");
152  b->addLine("return " + ARON_WRITER_ACCESSOR + ".writeIntEnum(\"" + type.getEnumName() +
153  "\", " + INT_ENUM_VALUE_MAP + ", " + "\"" + type.getDefaultValueName() + "\", " +
155  return b;
156  }
157 
159  IntEnumClass::getWriteBlock(const std::string&, const Path& p, std::string&) const
160  {
161  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
162 
163  block_if_data->addLine("return " + ARON_WRITER_ACCESSOR +
164  ".writePrimitive(EnumToValueMap.at(value), " + ARON_PATH_ACCESSOR +
165  "); // of top level enum " + getInstantiatedCppTypename());
166  return block_if_data;
167  }
168 
170  IntEnumClass::getReadBlock(const std::string&, const std::string& variantAccessor) const
171  {
172  static const std::string INT_ENUM_TMP_VALUE = ARON_VARIABLE_PREFIX + "_tmpValue";
173 
174  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
175  block_if_data->addLine("int " + INT_ENUM_TMP_VALUE + ";");
176  block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readPrimitive(" + variantAccessor +
177  ", " + INT_ENUM_TMP_VALUE + "); // of top level enum " +
179 
180  block_if_data->addLine("auto valueToEnumMap_iterator = ValueToEnumMap.find(" +
181  INT_ENUM_TMP_VALUE + ");");
182  block_if_data->addLine(
183  "ARMARX_CHECK_AND_THROW(valueToEnumMap_iterator != ValueToEnumMap.end(), "
184  "::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Missing enum for value "
185  "'\" + std::to_string(" +
186  INT_ENUM_TMP_VALUE + ") + \"' in aron enum '" + getFullClassCppTypename() + "'.\"))");
187  block_if_data->addLine("value = valueToEnumMap_iterator->second;");
188  return block_if_data;
189  }
190 
192  IntEnumClass::getEqualsBlock(const std::string& accessor,
193  const std::string& otherInstanceAccessor) const
194  {
195  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
196  block_if_data->addLine("if (not (value == " + otherInstanceAccessor + ".value))");
197  block_if_data->addLineAsBlock("return false;");
198  return block_if_data;
199  }
200 
201  /*CppCtorPtr IntEnumClass::toCopyCtor(const std::string& name) const
202  {
203  CppCtorPtr c = std::make_shared<CppCtor>(name + "(const " + getFullInstantiatedCppTypename() + "& i)");
204  std::vector<std::pair<std::string, std::string>> initList = {{"value", "i.value"}};
205  c->addInitListEntries(initList);
206  c->setBlock(std::make_shared<CppBlock>());
207  return c;
208  }*/
209 
210  CppCtorPtr
211  IntEnumClass::toEnumCtor(const std::string& name) const
212  {
213  CppCtorPtr c = std::make_shared<CppCtor>(name + "(const " + std::string(IMPL_ENUM) + " e)");
214  std::vector<std::pair<std::string, std::string>> initList = {{"value", "e"}};
215  c->addInitListEntries(initList);
216  c->setBlock(std::make_shared<CppBlock>());
217  return c;
218  }
219 
220  std::vector<CppEnumFieldPtr>
222  {
223  std::vector<CppEnumFieldPtr> e;
224  for (const auto& [key, value] : type.getAcceptedValueMap())
225  {
226  e.push_back(std::make_shared<CppEnumField>(key));
227  }
228  return e;
229  }
230 
233  {
234  std::stringstream doc;
235  doc << "@brief int() - Converts the internally stored value to int representation \n";
236  doc << "@return - the int representation";
237 
238  CppMethodPtr m = CppMethodPtr(new CppMethod("operator int() const", doc.str()));
239  CppBlockPtr b = std::make_shared<CppBlock>();
240  b->addLine("return EnumToValueMap.at(value);");
241  m->setBlock(b);
242  return m;
243  }
244 
247  {
248  std::stringstream doc;
249  doc << "@brief operator=() - Assignment operator for copy \n";
250  doc << "@return - nothing";
251 
252  CppMethodPtr m =
253  CppMethodPtr(new CppMethod(getFullInstantiatedCppTypename() + "& operator=(const " +
255  doc.str()));
256  CppBlockPtr b = std::make_shared<CppBlock>();
257  b->addLine("value = c.value;");
258  b->addLine("return *this;");
259  m->setBlock(b);
260  return m;
261  }
262 
265  {
266  std::stringstream doc;
267  doc << "@brief operator=() - Assignment operator for the internally defined enum \n";
268  doc << "@return - nothing";
269 
271  getFullInstantiatedCppTypename() + "& operator=(" + std::string(IMPL_ENUM) + " v)",
272  doc.str()));
273  CppBlockPtr b = std::make_shared<CppBlock>();
274  b->addLine("value = v;");
275  b->addLine("return *this;");
276  m->setBlock(b);
277  return m;
278  }
279 
282  {
283  std::stringstream doc;
284  doc << "@brief operator=() - Assignment operator for the internally defined enum \n";
285  doc << "@return - nothing";
286 
288  new CppMethod(getFullInstantiatedCppTypename() + "& operator=(int v)", doc.str()));
289  CppBlockPtr b = std::make_shared<CppBlock>();
290  b->addLine("if (auto it = ValueToEnumMap.find(v); it == ValueToEnumMap.end())");
291  CppBlockPtr b2 = std::make_shared<CppBlock>();
292  b2->addLine("throw armarx::LocalException(\"The input int is not valid. Could net set the "
293  "enum to value '\" + std::to_string(v) + \"'\");");
294  b->addBlock(b2);
295  b->addLine("else");
296  CppBlockPtr b3 = std::make_shared<CppBlock>();
297  b3->addLine("value = it->second;");
298  b->addBlock(b3);
299  b->addLine("return *this;");
300  m->setBlock(b);
301  return m;
302  }
303 
306  {
307  std::stringstream doc;
308  doc << "@brief toString() - Converts the internally stored value to string \n";
309  doc << "@return - the name of the enum";
310 
311  CppMethodPtr m = CppMethodPtr(new CppMethod("std::string toString() const", doc.str()));
312  CppBlockPtr b = std::make_shared<CppBlock>();
313  b->addLine("return EnumToStringMap.at(value);");
314  m->setBlock(b);
315  return m;
316  }
317 
320  {
321  std::stringstream doc;
322  doc << "@brief fromString() - sets the internally stored value to the corrsponding enum of "
323  "the input str \n";
324  doc << "@return - nothing";
325 
326  CppMethodPtr m =
327  CppMethodPtr(new CppMethod("void fromString(const std::string& str)", doc.str()));
328  CppBlockPtr b = std::make_shared<CppBlock>();
329  b->addLine("if (auto it = StringToEnumMap.find(str); it == StringToEnumMap.end())");
330  CppBlockPtr b2 = std::make_shared<CppBlock>();
331  b2->addLine("throw armarx::LocalException(\"The input name is not valid. Could net set the "
332  "enum to value '\" + str + \"'\");");
333  b->addBlock(b2);
334  b->addLine("else");
335  CppBlockPtr b3 = std::make_shared<CppBlock>();
336  b3->addLine("value = it->second;");
337  b->addBlock(b3);
338 
339  m->setBlock(b);
340  return m;
341  }
342 } // namespace armarx::aron::codegenerator::cpp::generator
armarx::aron::codegenerator::cpp::Generator::getFullInstantiatedCppTypename
std::string getFullInstantiatedCppTypename() const
Definition: Generator.cpp:116
armarx::aron::codegenerator::cpp::Generator::ARON_MAYBE_TYPE_ACCESSOR
static const std::string ARON_MAYBE_TYPE_ACCESSOR
Definition: Generator.h:149
armarx::aron::codegenerator::cpp::Generator::getFullClassCppTypename
std::string getFullClassCppTypename() const
Definition: Generator.cpp:168
armarx::aron::type::detail::EnumVariant::getAcceptedValueNames
std::vector< std::string > getAcceptedValueNames() const
Definition: EnumVariant.h:85
armarx::CppCtorPtr
std::shared_ptr< CppCtor > CppCtorPtr
Definition: CppCtor.h:31
armarx::aron::type::detail::EnumVariant::getEnumName
std::string getEnumName() const
Definition: EnumVariant.h:129
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:92
armarx::aron::codegenerator::cpp::generator::IntEnumClass::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: IntEnumClass.cpp:120
armarx::aron::codegenerator::cpp::generator::IntEnumClass::toFromStringMethod
CppMethodPtr toFromStringMethod() const
Definition: IntEnumClass.cpp:319
armarx::aron::codegenerator::cpp::generator::IntEnumClass::toEnumCtor
CppCtorPtr toEnumCtor(const std::string &) const
Definition: IntEnumClass.cpp:211
armarx::aron::codegenerator::cpp::Generator::ARON_VARIABLE_PREFIX
static const std::string ARON_VARIABLE_PREFIX
Definition: Generator.h:147
detail
Definition: OpenCVUtil.cpp:127
armarx::CppBlockPtr
std::shared_ptr< CppBlock > CppBlockPtr
Definition: CppBlock.h:35
armarx::aron::codegenerator::cpp::Generator::ARON_WRITER_ACCESSOR
static const std::string ARON_WRITER_ACCESSOR
Definition: Generator.h:152
armarx::aron::codegenerator::cpp::generator::IntEnumClass::toIntMethod
CppMethodPtr toIntMethod() const
Definition: IntEnumClass.cpp:232
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::aron::codegenerator::cpp::generator::IntEnumClass::getPublicVariableDeclarations
std::vector< CppFieldPtr > getPublicVariableDeclarations(const std::string &) const final
Definition: IntEnumClass.cpp:51
armarx::aron::codegenerator::cpp::generator::IntEnumClass::toToStringMethod
CppMethodPtr toToStringMethod() const
Definition: IntEnumClass.cpp:305
IntEnumClass.h
armarx::aron::codegenerator::cpp::generator::IntEnumClass::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: IntEnumClass.cpp:159
armarx::aron::type::IntEnum
The IntEnum class.
Definition: IntEnum.h:42
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::aron::type::detail::EnumVariant::getDefaultValueName
std::string getDefaultValueName() const
Definition: EnumVariant.h:147
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::codegenerator::cpp::generator::IntEnumClass::toIntAssignmentMethod
CppMethodPtr toIntAssignmentMethod() const
Definition: IntEnumClass.cpp:281
armarx::aron::codegenerator::cpp::generator::IntEnumClass::getEqualsBlock
CppBlockPtr getEqualsBlock(const std::string &, const std::string &) const final
Definition: IntEnumClass.cpp:192
armarx::aron::codegenerator::cpp::generator::NDArray
Definition: NDArray.h:33
armarx::aron::codegenerator::cpp::generator::IntEnumClass::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: IntEnumClass.cpp:170
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::codegenerator::cpp::generator::IntEnumClass::toEnumFields
std::vector< CppEnumFieldPtr > toEnumFields() const
Definition: IntEnumClass.cpp:221
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::IntEnum, IntEnumClass >::type
type::IntEnum type
Definition: SpecializedGenerator.h:52
armarx::CppMethodPtr
std::shared_ptr< CppMethod > CppMethodPtr
Definition: CppMethod.h:32
armarx::aron::codegenerator::cpp::generator::IntEnum
Definition: IntEnum.h:37
armarx::aron::codegenerator::cpp::generator::IntEnumClass::toCopyAssignmentMethod
CppMethodPtr toCopyAssignmentMethod() const
Definition: IntEnumClass.cpp:246
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::codegenerator::cpp::generator
Definition: AnyObject.cpp:29
armarx::aron::codegenerator::cpp::generator::IntEnumClass::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: IntEnumClass.cpp:136
armarx::aron::codegenerator::cpp::generator::IntEnumClass::getResetHardBlock
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
Definition: IntEnumClass.cpp:128
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:11
armarx::aron::codegenerator::cpp::Generator::ARON_READER_ACCESSOR
static const std::string ARON_READER_ACCESSOR
Definition: Generator.h:151
armarx::CppMethod
Definition: CppMethod.h:34
armarx::aron::codegenerator::cpp::Generator::getInstantiatedCppTypename
std::string getInstantiatedCppTypename() const
Definition: Generator.cpp:110
armarx::aron::codegenerator::cpp::generator::IntEnumClass::toEnumAssignmentMethod
CppMethodPtr toEnumAssignmentMethod() const
Definition: IntEnumClass.cpp:264
armarx::aron::codegenerator::cpp::generator::IntEnumClass
Definition: IntEnumClass.h:34
simox
Definition: Impl.cpp:40
armarx::aron::type::detail::EnumVariant::getAcceptedValueMap
std::map< std::string, int > getAcceptedValueMap() const
Definition: EnumVariant.h:79
armarx::aron::codegenerator::cpp::generator::IntEnumClass::IntEnumClass
IntEnumClass(const type::IntEnum &)
Definition: IntEnumClass.cpp:32
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.h:99