27#include <SimoxUtility/meta/type_name.h>
40 if (
type.getMaybe() != type::Maybe::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__,
50 std::to_string((int)type.getMaybe()));
54 std::vector<CppFieldPtr>
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;
65 std::make_shared<CppField>(
"using",
66 std::string(IMPL_ENUM),
67 simox::alg::to_lower(className) +
"_details::Enum",
68 "Legacy typedef of enum"));
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())
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));
82 enum_to_name <<
"\t\t{" << enumKeyWithNamespace <<
", \"" << key <<
"\"}," << std::endl;
83 name_to_enum <<
"\t\t{\"" << key <<
"\", " << enumKeyWithNamespace <<
"}," << std::endl;
85 enum_to_value <<
"\t\t{" << enumKeyWithNamespace <<
", " << value <<
"}," << std::endl;
86 value_to_enum <<
"\t\t{" << value <<
", " << enumKeyWithNamespace <<
"}," << std::endl;
89 enum_to_name <<
"\t}";
90 name_to_enum <<
"\t}";
91 enum_to_value <<
"\t}";
92 value_to_enum <<
"\t}";
94 fields.push_back(std::make_shared<CppField>(
"static inline const std::map<" +
95 std::string(IMPL_ENUM) +
", std::string>",
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) +
">",
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>",
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) +
">",
113 "Mapping int values to a enum"));
115 fields.push_back(std::make_shared<CppField>(std::string(IMPL_ENUM),
117 type.getDefaultValueName(),
118 "The current value of the enum object"));
126 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
127 block_if_data->addLine(
"value = {};");
128 return block_if_data;
134 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
135 block_if_data->addLine(
"value = {};");
136 return block_if_data;
149 std::vector<std::string> map_initializer;
150 for (
const auto& [key, value] :
type.getAcceptedValueMap())
152 map_initializer.push_back(
"{\"" + key +
"\", " + std::to_string(value) +
"}");
154 b->addLine(
"std::map<std::string, int> " + INT_ENUM_VALUE_MAP +
" = {" +
155 simox::alg::to_string(map_initializer,
", ") +
"};");
157 "\", " + INT_ENUM_VALUE_MAP +
", " +
"\"" +
type.getDefaultValueName() +
"\", " +
165 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
170 return block_if_data;
178 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
179 block_if_data->addLine(
"int " + INT_ENUM_TMP_VALUE +
";");
181 ", " + INT_ENUM_TMP_VALUE +
"); // of top level enum " +
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(" +
191 block_if_data->addLine(
"value = valueToEnumMap_iterator->second;");
192 return block_if_data;
197 const std::string& otherInstanceAccessor)
const
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;
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>());
224 std::vector<CppEnumFieldPtr>
227 std::vector<CppEnumFieldPtr> e;
228 for (
const auto& [key, value] :
type.getAcceptedValueMap())
230 e.push_back(std::make_shared<CppEnumField>(key));
238 std::stringstream doc;
239 doc <<
"@brief int() - Converts the internally stored value to int representation \n";
240 doc <<
"@return - the int representation";
244 b->addLine(
"return EnumToValueMap.at(value);");
252 std::stringstream doc;
253 doc <<
"@brief operator=() - Assignment operator for copy \n";
254 doc <<
"@return - nothing";
261 b->addLine(
"value = c.value;");
262 b->addLine(
"return *this;");
270 std::stringstream doc;
271 doc <<
"@brief operator=() - Assignment operator for the internally defined enum \n";
272 doc <<
"@return - nothing";
278 b->addLine(
"value = v;");
279 b->addLine(
"return *this;");
287 std::stringstream doc;
288 doc <<
"@brief operator=() - Assignment operator for the internally defined enum \n";
289 doc <<
"@return - nothing";
294 b->addLine(
"if (auto it = ValueToEnumMap.find(v); it == ValueToEnumMap.end())");
296 b2->addLine(
"throw armarx::LocalException(\"The input int is not valid. Could net set the "
297 "enum to value '\" + std::to_string(v) + \"'\");");
301 b3->addLine(
"value = it->second;");
303 b->addLine(
"return *this;");
311 std::stringstream doc;
312 doc <<
"@brief toString() - Converts the internally stored value to string \n";
313 doc <<
"@return - the name of the enum";
317 b->addLine(
"return EnumToStringMap.at(value);");
325 std::stringstream doc;
326 doc <<
"@brief fromString() - sets the internally stored value to the corrsponding enum of "
328 doc <<
"@return - nothing";
333 b->addLine(
"if (auto it = StringToEnumMap.find(str); it == StringToEnumMap.end())");
335 b2->addLine(
"throw armarx::LocalException(\"The input name is not valid. Could net set the "
336 "enum to value '\" + str + \"'\");");
340 b3->addLine(
"value = it->second;");
static const std::string ARON_READER_ACCESSOR
std::string getInstantiatedCppTypename() const
static const std::string ARON_PATH_ACCESSOR
std::string getFullClassCppTypename() const
std::string getFullInstantiatedCppTypename() const
static const std::string ARON_VARIABLE_PREFIX
static const std::string ARON_WRITER_ACCESSOR
static const std::string ARON_MAYBE_TYPE_ACCESSOR
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
CppMethodPtr toCopyAssignmentMethod() const
CppBlockPtr getEqualsBlock(const std::string &, const std::string &) const final
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
CppMethodPtr toEnumAssignmentMethod() const
CppMethodPtr toFromStringMethod() const
std::vector< CppEnumFieldPtr > toEnumFields() const
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
CppMethodPtr toIntMethod() const
CppMethodPtr toIntAssignmentMethod() const
CppCtorPtr toEnumCtor(const std::string &) const
std::vector< CppFieldPtr > getPublicVariableDeclarations(const std::string &) const final
IntEnumClass(const type::IntEnum &)
CppMethodPtr toToStringMethod() const
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)
#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
std::shared_ptr< CppCtor > CppCtorPtr
std::shared_ptr< CppMethod > CppMethodPtr