30#include <SimoxUtility/algorithm/string.h>
31#include <SimoxUtility/meta/type_name.h>
57 const static std::map<std::string, std::string> ESCAPE_ACCESSORS = {{
"->",
"_ptr_"},
65 std::string escaped_accessor = accessor;
66 for (
const auto& [key, value] : ESCAPE_ACCESSORS)
68 escaped_accessor = simox::alg::replace_all(escaped_accessor, key, value);
70 return escaped_accessor;
76 if (!value.has_value())
78 return "std::nullopt";
81 std::ostringstream stream;
82 stream << std::setprecision(7) << *value;
83 auto literal = stream.str();
84 if (literal.find_first_of(
".eE") == std::string::npos)
89 return "std::optional<float>{" + literal +
"}";
97 return cpp->getInstantiatedCppTypename();
100 std::vector<std::string>
103 std::vector<std::string> ret;
104 for (
const auto& v : n)
111 std::unique_ptr<Generator>
114 return FACTORY->create(n, n.getPath());
119 const std::string& classCppTypename,
120 const std::string& aronDataTypename,
121 const std::string& aronTypeTypename) :
122 instantiatedCppTypename(instantiatedCppTypename),
123 classCppTypename(classCppTypename),
124 aronDataTypename(aronDataTypename),
125 aronTypeTypename(aronTypeTypename)
133 return instantiatedCppTypename;
141 case type::Maybe::NONE:
143 case type::Maybe::OPTIONAL:
145 case type::Maybe::RAW_PTR:
147 case type::Maybe::SHARED_PTR:
149 case type::Maybe::UNIQUE_PTR:
153 "Received unknown maybe enum",
154 std::to_string((
int)
getType().getMaybe()),
164 case type::Maybe::NONE:
166 case type::Maybe::OPTIONAL:
168 case type::Maybe::RAW_PTR:
170 case type::Maybe::SHARED_PTR:
172 case type::Maybe::UNIQUE_PTR:
176 "Received unknown maybe enum",
177 std::to_string((
int)
getType().getMaybe()),
185 return classCppTypename;
193 case type::Maybe::NONE:
195 case type::Maybe::OPTIONAL:
197 case type::Maybe::RAW_PTR:
199 case type::Maybe::SHARED_PTR:
201 case type::Maybe::UNIQUE_PTR:
205 "Received unknown maybe enum",
206 std::to_string((
int)
getType().getMaybe()),
217 if (b->size() > 0 || initList.second)
220 c->addInitListEntries(initList.first);
235 if (b->size() > 0 || initList.second)
239 c->addInitListEntries(initList.first);
258 m->setEnforceBlockGeneration(
true);
263 m->setEnforceBlockGeneration(
false);
270 std::stringstream doc;
271 doc <<
"@brief resetSoft() - This method resets all member variables with respect to the "
272 "current parameterization. \n";
273 doc <<
"@return - nothing";
280 m->setEnforceBlockGeneration(
true);
287 std::stringstream doc;
288 doc <<
"@brief resetHard() - This method resets member variables according to the XML type "
290 doc <<
"@return - nothing";
297 m->setEnforceBlockGeneration(
true);
304 std::stringstream doc;
305 doc <<
"@brief writeType() - This method returns a new type from the class structure using "
306 "a type writer implementation. This function is static. \n";
307 doc <<
"@return - the result of the writer implementation";
310 "template<class WriterT>\nstatic typename WriterT::ReturnType writeType(WriterT& " +
314 " = ::armarx::aron::type::Maybe::NONE, const ::armarx::aron::Path& " +
318 b->addLine(
"using _Aron_T [[maybe_unused]] = typename WriterT::ReturnType;");
324 m->setEnforceBlockGeneration(
true);
331 std::stringstream doc;
332 doc <<
"@brief write() - This method returns a new type from the member data types using a "
333 "data writer implementation. \n";
334 doc <<
"@param w - The writer implementation\n";
335 doc <<
"@return - the result of the writer implementation";
338 new CppMethod(
"template<class WriterT>\ntypename WriterT::ReturnType write(WriterT& " +
343 b->addLine(
"using _Aron_T [[maybe_unused]] = typename WriterT::ReturnType;");
349 b->addLineAsBlock(
"throw ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
350 "std::string(\"An error occured during the write method of an aron "
351 "generated class. The full error log was:\\n\") + " +
355 m->setEnforceBlockGeneration(
true);
362 std::stringstream doc;
363 doc <<
"@brief read() - This method sets the struct members to new values given in a data "
364 "reader implementation. \n";
365 doc <<
"@param r - The reader implementation\n";
366 doc <<
"@return - nothing";
370 ", typename ReaderT::InputType& input)",
373 b->addLine(
"using _Aron_T [[maybe_unused]] = typename ReaderT::InputType;");
374 b->addLine(
"using _Aron_TNonConst [[maybe_unused]] = typename ReaderT::InputTypeNonConst;");
376 b->addLine(
"this->resetSoft();");
378 ".readNull(input), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
379 "\"The input to the read method must not be null.\"));");
384 b->addLineAsBlock(
"throw ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
385 "std::string(\"An error occured during the read method of an aron "
386 "generated class. The full error log was:\\n\") + " +
390 m->setEnforceBlockGeneration(
true);
397 std::stringstream doc;
399 <<
"() - This method returns a new data from the member data types using a writer "
400 "implementation. \n";
401 doc <<
"@return - the result of the writer implementation";
411 m->setEnforceBlockGeneration(
true);
418 std::stringstream doc;
420 <<
" - This method sets the struct members to new values given in a reader "
421 "implementation. \n";
422 doc <<
"@return - nothing";
426 "& input)" + (info.
override ?
" override" :
""),
432 m->setEnforceBlockGeneration(
true);
439 std::stringstream doc;
441 <<
"() - This method sets the struct members to new values given in a reader "
442 "implementation. \n";
443 doc <<
"@return - nothing";
450 m->addLine(
"t.fromAron(input);");
451 m->addLine(
"return t;");
453 m->setEnforceBlockGeneration(
true);
460 std::stringstream doc;
462 <<
"() - This method returns a new type from the member data types using a writer "
463 "implementation. \n";
464 doc <<
"@return - the result of the writer implementation";
472 m->setEnforceBlockGeneration(
true);
479 std::stringstream doc;
480 doc <<
"@brief operator==() - This method checks whether all values equal another "
482 doc <<
"@param i - The other instance\n";
483 doc <<
"@return - true, if all members are the same, false otherwise";
488 b->addLine(
"return true;");
491 m->setEnforceBlockGeneration(
true);
496 std::vector<std::string>
502 std::vector<CppFieldPtr>
509 std::pair<std::vector<std::pair<std::string, std::string>>,
bool>
518 return std::make_shared<CppBlock>();
521 std::pair<std::vector<std::pair<std::string, std::string>>,
bool>
525 if (t.getMaybe() == type::Maybe::UNIQUE_PTR)
540 return std::make_shared<CppBlock>();
546 return std::make_shared<CppBlock>();
552 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
561 auto block_if_data = std::make_shared<CppBlock>();
569 const std::string& otherInstanceAccessor)
const
571 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
573 std::string other_instance_resolved_accessor =
576 block_if_data->addLine(
"if (not (" + resolved_accessor +
577 " == " + other_instance_resolved_accessor +
"))");
578 block_if_data->addLineAsBlock(
"return false;");
587 if (t.getMaybe() == type::Maybe::OPTIONAL)
589 return s +
".value()";
591 if (t.getMaybe() == type::Maybe::RAW_PTR || t.getMaybe() == type::Maybe::SHARED_PTR ||
592 t.getMaybe() == type::Maybe::UNIQUE_PTR)
603 if (t.getMaybe() == type::Maybe::OPTIONAL)
607 if (t.getMaybe() == type::Maybe::RAW_PTR)
611 if (t.getMaybe() == type::Maybe::SHARED_PTR)
615 if (t.getMaybe() == type::Maybe::UNIQUE_PTR)
628 return s +
" = " + gen +
";";
637 switch (
type.getMaybe())
639 case type::Maybe::NONE:
641 case type::Maybe::OPTIONAL:
642 case type::Maybe::RAW_PTR:
643 case type::Maybe::SHARED_PTR:
644 case type::Maybe::UNIQUE_PTR:
648 "Received unknown maybe enum",
649 std::to_string((
int)
type.getMaybe()),
657 switch (
type.getMaybe())
659 case type::Maybe::RAW_PTR:
661 case type::Maybe::SHARED_PTR:
662 case type::Maybe::UNIQUE_PTR:
663 return cppAccessor +
".get()";
665 case type::Maybe::NONE:
666 case type::Maybe::OPTIONAL:
670 "Received invalid maybe enum (not a pointer?)",
671 std::to_string((
int)
type.getMaybe()),
677 const std::string& cppAccessor)
const
680 if (
type.getMaybe() == type::Maybe::OPTIONAL)
683 b->addLine(cppAccessor +
" = std::nullopt;");
686 else if (
type.getMaybe() == type::Maybe::RAW_PTR ||
687 type.getMaybe() == type::Maybe::SHARED_PTR ||
688 type.getMaybe() == type::Maybe::UNIQUE_PTR)
691 b->addLine(cppAccessor +
" = nullptr;");
694 return block_if_data;
699 const std::string& cppAccessor)
const
702 if (
type.getMaybe() == type::Maybe::OPTIONAL ||
type.getMaybe() == type::Maybe::RAW_PTR ||
703 type.getMaybe() == type::Maybe::SHARED_PTR ||
704 type.getMaybe() == type::Maybe::UNIQUE_PTR)
707 b->addLine(
"if (" + cppAccessor +
") // if " + cppAccessor +
" contains data");
708 b->addBlock(block_if_data);
711 return block_if_data;
716 const std::string& cppAccessor)
const
719 if (
type.getMaybe() == type::Maybe::OPTIONAL ||
type.getMaybe() == type::Maybe::RAW_PTR ||
720 type.getMaybe() == type::Maybe::SHARED_PTR ||
721 type.getMaybe() == type::Maybe::UNIQUE_PTR)
724 b->addLine(
"if (" + cppAccessor +
") // if " + cppAccessor +
" contains data");
725 b->addBlock(block_if_data);
728 return block_if_data;
733 const std::string& cppAccessor,
734 const std::string& variantAccessor)
const
737 if (
type.getMaybe() == type::Maybe::OPTIONAL ||
type.getMaybe() == type::Maybe::RAW_PTR ||
738 type.getMaybe() == type::Maybe::SHARED_PTR ||
739 type.getMaybe() == type::Maybe::UNIQUE_PTR)
743 "))) // if aron contains data");
747 ifb->appendBlock(block_if_data);
752 return block_if_data;
757 const std::string& accessor,
758 const std::string& otherInstanceAccessor)
const
761 if (
type.getMaybe() == type::Maybe::RAW_PTR ||
type.getMaybe() == type::Maybe::SHARED_PTR ||
762 type.getMaybe() == type::Maybe::UNIQUE_PTR)
765 b->addLine(
"if (not (static_cast<bool>(" + accessor +
") == static_cast<bool>(" +
766 otherInstanceAccessor +
"))) // check if both contain data");
767 b->addLineAsBlock(
"return false;");
768 b->addLine(
"if (static_cast<bool>(" + accessor +
") && static_cast<bool>(" +
769 otherInstanceAccessor +
"))");
770 b->addBlock(block_if_data);
774 if (
type.getMaybe() == type::Maybe::OPTIONAL)
777 b->addLine(
"if (not ( " + accessor +
".has_value() == " + otherInstanceAccessor +
778 ".has_value())) // check if both contain data");
779 b->addLineAsBlock(
"return false;");
780 b->addLine(
"if ( " + accessor +
".has_value() && " + otherInstanceAccessor +
782 b->addBlock(block_if_data);
785 return block_if_data;
virtual std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCtorInitializers(const std::string &) const
static const std::string ARON_OTHER_ACCESSOR
virtual CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const =0
CppCtorPtr toCopyCtor(const std::string &) const
virtual std::vector< CppFieldPtr > getPublicVariableDeclarations(const std::string &) const
std::string resolveMaybeAccessor(const std::string &) const
CppMethodPtr toSpecializedDataReaderMethod(const ReaderInfo &info) const
CppBlockPtr resolveMaybeResetHardBlock(const CppBlockPtr &, const std::string &) const
static std::string OptionalFloatLiteral(const std::optional< float > &)
CppBlockPtr resolveMaybeEqualsBlock(const CppBlockPtr &, const std::string &, const std::string &) const
CppMethodPtr toWriteTypeMethod() const
CppMethodPtr toResetHardMethod() const
CppBlockPtr resolveMaybeReadBlock(const CppBlockPtr &, const std::string &, const std::string &) const
std::string resolveMaybeGeneratorWithSetter(const std::string &, const std::string &args="") const
static std::string EscapeAccessor(const std::string &)
virtual CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const =0
std::string getFullInstantiatedCppTypenameGenerator() const
virtual CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const
CppBlockPtr resolveMaybeResetSoftBlock(const CppBlockPtr &, const std::string &) const
CppMethodPtr toReadMethod() const
CppCtorPtr toCtor(const std::string &) const
virtual const type::Variant & getType() const =0
virtual std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCopyCtorInitializers(const std::string &) const
virtual CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const =0
std::string toPointerAccessor(const std::string &) const
CppMethodPtr toWriteMethod() const
std::string nextEl() const
static std::string ExtractCppTypename(const type::Variant &)
static const std::string ARON_READER_ACCESSOR
std::string resolveMaybeGenerator(const std::string &args="") const
virtual std::vector< std::string > getRequiredIncludes() const
virtual CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const
virtual CppBlockPtr getEqualsBlock(const std::string &cppAccessorThis, const std::string &cppAccessorOther) const
CppMethodPtr toDtor(const std::string &) const
static std::vector< std::string > ExtractCppTypenames(const std::vector< type::VariantPtr > &)
std::string getClassCppTypename() const
CppMethodPtr toSpecializedDataWriterMethod(const WriterInfo &info) const
std::string getInstantiatedCppTypename() const
static const std::string ARON_PATH_ACCESSOR
virtual CppBlockPtr getCopyCtorBlock(const std::string &) const
virtual CppBlockPtr getDtorBlock(const std::string &) const
std::string getFullClassCppTypename() const
CppMethodPtr toEqualsMethod() const
std::string getFullInstantiatedCppTypename() const
static const std::string ARON_VARIABLE_PREFIX
static const std::string ARON_WRITER_ACCESSOR
CppMethodPtr toSpecializedTypeWriterMethod(const WriterInfo &info) const
CppMethodPtr toSpecializedStaticDataReaderMethod(const StaticReaderInfo &info) const
virtual CppBlockPtr getCtorBlock(const std::string &) const
CppMethodPtr toResetSoftMethod() const
static std::unique_ptr< Generator > FromAronType(const type::Variant &)
static const std::string ARON_VARIANT_RETURN_ACCESSOR
static const std::string ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR
static const std::string ARON_MAYBE_TYPE_ACCESSOR
CppBlockPtr resolveMaybeWriteBlock(const CppBlockPtr &, const std::string &) const
The ValueNotValidException class.
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
std::shared_ptr< GeneratorFactory > SerializerFactoryPtr
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
std::string readerClassType
std::string enforceMemberAccess
std::string enforceConversion
std::string writerClassType
std::string enforceMemberAccess
std::string enforceConversion