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;");
375 b->addLine(
"using _Aron_TElement [[maybe_unused]] = typename ReaderT::ElementType;");
377 b->addLine(
"this->resetSoft();");
379 ".readNull(input), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
380 "\"The input to the read method must not be null.\"));");
385 b->addLineAsBlock(
"throw ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
386 "std::string(\"An error occured during the read method of an aron "
387 "generated class. The full error log was:\\n\") + " +
391 m->setEnforceBlockGeneration(
true);
398 std::stringstream doc;
400 <<
"() - This method returns a new data from the member data types using a writer "
401 "implementation. \n";
402 doc <<
"@return - the result of the writer implementation";
412 m->setEnforceBlockGeneration(
true);
419 std::stringstream doc;
421 <<
" - This method sets the struct members to new values given in a reader "
422 "implementation. \n";
423 doc <<
"@return - nothing";
427 "& input)" + (info.
override ?
" override" :
""),
433 m->setEnforceBlockGeneration(
true);
440 std::stringstream doc;
442 <<
"() - This method sets the struct members to new values given in a reader "
443 "implementation. \n";
444 doc <<
"@return - nothing";
451 m->addLine(
"t.fromAron(input);");
452 m->addLine(
"return t;");
454 m->setEnforceBlockGeneration(
true);
461 std::stringstream doc;
463 <<
"() - This method returns a new type from the member data types using a writer "
464 "implementation. \n";
465 doc <<
"@return - the result of the writer implementation";
473 m->setEnforceBlockGeneration(
true);
480 std::stringstream doc;
481 doc <<
"@brief operator==() - This method checks whether all values equal another "
483 doc <<
"@param i - The other instance\n";
484 doc <<
"@return - true, if all members are the same, false otherwise";
489 b->addLine(
"return true;");
492 m->setEnforceBlockGeneration(
true);
497 std::vector<std::string>
503 std::vector<CppFieldPtr>
510 std::pair<std::vector<std::pair<std::string, std::string>>,
bool>
519 return std::make_shared<CppBlock>();
522 std::pair<std::vector<std::pair<std::string, std::string>>,
bool>
526 if (t.getMaybe() == type::Maybe::UNIQUE_PTR)
541 return std::make_shared<CppBlock>();
547 return std::make_shared<CppBlock>();
553 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
562 auto block_if_data = std::make_shared<CppBlock>();
570 const std::string& otherInstanceAccessor)
const
572 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
574 std::string other_instance_resolved_accessor =
577 block_if_data->addLine(
"if (not (" + resolved_accessor +
578 " == " + other_instance_resolved_accessor +
"))");
579 block_if_data->addLineAsBlock(
"return false;");
588 if (t.getMaybe() == type::Maybe::OPTIONAL)
590 return s +
".value()";
592 if (t.getMaybe() == type::Maybe::RAW_PTR || t.getMaybe() == type::Maybe::SHARED_PTR ||
593 t.getMaybe() == type::Maybe::UNIQUE_PTR)
604 if (t.getMaybe() == type::Maybe::OPTIONAL)
608 if (t.getMaybe() == type::Maybe::RAW_PTR)
612 if (t.getMaybe() == type::Maybe::SHARED_PTR)
616 if (t.getMaybe() == type::Maybe::UNIQUE_PTR)
629 return s +
" = " + gen +
";";
638 switch (
type.getMaybe())
640 case type::Maybe::NONE:
642 case type::Maybe::OPTIONAL:
643 case type::Maybe::RAW_PTR:
644 case type::Maybe::SHARED_PTR:
645 case type::Maybe::UNIQUE_PTR:
649 "Received unknown maybe enum",
650 std::to_string((
int)
type.getMaybe()),
658 switch (
type.getMaybe())
660 case type::Maybe::RAW_PTR:
662 case type::Maybe::SHARED_PTR:
663 case type::Maybe::UNIQUE_PTR:
664 return cppAccessor +
".get()";
666 case type::Maybe::NONE:
667 case type::Maybe::OPTIONAL:
671 "Received invalid maybe enum (not a pointer?)",
672 std::to_string((
int)
type.getMaybe()),
678 const std::string& cppAccessor)
const
681 if (
type.getMaybe() == type::Maybe::OPTIONAL)
684 b->addLine(cppAccessor +
" = std::nullopt;");
687 else if (
type.getMaybe() == type::Maybe::RAW_PTR ||
688 type.getMaybe() == type::Maybe::SHARED_PTR ||
689 type.getMaybe() == type::Maybe::UNIQUE_PTR)
692 b->addLine(cppAccessor +
" = nullptr;");
695 return block_if_data;
700 const std::string& cppAccessor)
const
703 if (
type.getMaybe() == type::Maybe::OPTIONAL ||
type.getMaybe() == type::Maybe::RAW_PTR ||
704 type.getMaybe() == type::Maybe::SHARED_PTR ||
705 type.getMaybe() == type::Maybe::UNIQUE_PTR)
708 b->addLine(
"if (" + cppAccessor +
") // if " + cppAccessor +
" contains data");
709 b->addBlock(block_if_data);
712 return block_if_data;
717 const std::string& cppAccessor)
const
720 if (
type.getMaybe() == type::Maybe::OPTIONAL ||
type.getMaybe() == type::Maybe::RAW_PTR ||
721 type.getMaybe() == type::Maybe::SHARED_PTR ||
722 type.getMaybe() == type::Maybe::UNIQUE_PTR)
725 b->addLine(
"if (" + cppAccessor +
") // if " + cppAccessor +
" contains data");
726 b->addBlock(block_if_data);
729 return block_if_data;
734 const std::string& cppAccessor,
735 const std::string& variantAccessor)
const
738 if (
type.getMaybe() == type::Maybe::OPTIONAL ||
type.getMaybe() == type::Maybe::RAW_PTR ||
739 type.getMaybe() == type::Maybe::SHARED_PTR ||
740 type.getMaybe() == type::Maybe::UNIQUE_PTR)
744 "))) // if aron contains data");
748 ifb->appendBlock(block_if_data);
753 return block_if_data;
758 const std::string& accessor,
759 const std::string& otherInstanceAccessor)
const
762 if (
type.getMaybe() == type::Maybe::RAW_PTR ||
type.getMaybe() == type::Maybe::SHARED_PTR ||
763 type.getMaybe() == type::Maybe::UNIQUE_PTR)
766 b->addLine(
"if (not (static_cast<bool>(" + accessor +
") == static_cast<bool>(" +
767 otherInstanceAccessor +
"))) // check if both contain data");
768 b->addLineAsBlock(
"return false;");
769 b->addLine(
"if (static_cast<bool>(" + accessor +
") && static_cast<bool>(" +
770 otherInstanceAccessor +
"))");
771 b->addBlock(block_if_data);
775 if (
type.getMaybe() == type::Maybe::OPTIONAL)
778 b->addLine(
"if (not ( " + accessor +
".has_value() == " + otherInstanceAccessor +
779 ".has_value())) // check if both contain data");
780 b->addLineAsBlock(
"return false;");
781 b->addLine(
"if ( " + accessor +
".has_value() && " + otherInstanceAccessor +
783 b->addBlock(block_if_data);
786 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