From 8aa5d6d2b296b8f87962b377f526e117add803da Mon Sep 17 00:00:00 2001 From: Fabian Peller-Konrad <fabian.peller-konrad@kit.edu> Date: Mon, 7 Feb 2022 09:27:30 +0100 Subject: [PATCH] added any type and templates --- source/RobotAPI/interface/aron/Aron.ice | 11 +- .../armem/server/test/ArMemMemoryTest.cpp | 2 +- source/RobotAPI/libraries/armem/util/util.h | 8 +- .../libraries/armem_gui/test/ArMemGuiTest.cpp | 2 +- .../client/common/RobotReader.cpp | 2 +- .../converter/json/NLohmannJSONConverter.h | 8 +- .../libraries/aron/core/CMakeLists.txt | 12 +- .../RobotAPI/libraries/aron/core/Descriptor.h | 6 +- .../{AronCppClass.h => AronGeneratedClass.h} | 9 +- .../codewriter/cpp/generator/All.h | 1 + .../codewriter/cpp/generator/Factory.cpp | 1 + .../codewriter/cpp/generator/Generator.cpp | 105 ++++++++++++------ .../codewriter/cpp/generator/Generator.h | 15 ++- .../codewriter/cpp/generator/any/All.h | 7 ++ .../cpp/generator/any/AnyObject.cpp | 57 ++++++++++ .../codewriter/cpp/generator/any/AnyObject.h | 43 +++++++ .../cpp/generator/container/Dict.cpp | 10 +- .../cpp/generator/container/List.cpp | 10 +- .../cpp/generator/container/Object.cpp | 17 ++- .../cpp/generator/container/Pair.cpp | 10 +- .../cpp/generator/container/Tuple.cpp | 8 +- .../cpp/generator/detail/AnyGenerator.cpp | 25 +++++ .../cpp/generator/detail/AnyGenerator.h | 60 ++++++++++ .../generator/detail/SpecializedGenerator.h | 4 +- .../codewriter/cpp/generator/enum/IntEnum.cpp | 11 +- .../cpp/generator/ndarray/Image.cpp | 8 +- .../cpp/generator/ndarray/Matrix.cpp | 6 +- .../cpp/generator/ndarray/NDArray.cpp | 8 +- .../cpp/generator/ndarray/Orientation.cpp | 6 +- .../cpp/generator/ndarray/PointCloud.cpp | 5 +- .../codewriter/cpp/generator/ndarray/Pose.cpp | 1 + .../cpp/generator/ndarray/Position.cpp | 1 + .../cpp/generator/ndarray/Quaternion.cpp | 1 + .../cpp/generator/primitive/Bool.cpp | 6 +- .../cpp/generator/primitive/Double.cpp | 1 + .../cpp/generator/primitive/Float.cpp | 1 + .../cpp/generator/primitive/Int.cpp | 1 + .../cpp/generator/primitive/Long.cpp | 1 + .../cpp/generator/primitive/String.cpp | 1 + .../cpp/generator/primitive/Time.cpp | 1 + .../cpp/generator/toplevel/IntEnumClass.cpp | 13 ++- .../cpp/generator/toplevel/ObjectClass.cpp | 49 +++++--- .../aron/core/data/converter/Converter.h | 14 +++ .../nlohmannJSON/NlohmannJSONConverter.h | 36 +++--- .../data/converter/variant/VariantConverter.h | 36 +++--- .../libraries/aron/core/data/rw/Reader.h | 2 + .../libraries/aron/core/data/rw/Writer.h | 3 + .../nlohmannJSON/NlohmannJSONReader.cpp | 7 +- .../reader/nlohmannJSON/NlohmannJSONReader.h | 14 +++ .../data/rw/reader/variant/VariantReader.cpp | 9 +- .../data/rw/reader/variant/VariantReader.h | 32 ++++-- .../nlohmannJSON/NlohmannJSONWriter.cpp | 6 + .../writer/nlohmannJSON/NlohmannJSONWriter.h | 2 + .../data/rw/writer/variant/VariantWriter.cpp | 7 +- .../data/rw/writer/variant/VariantWriter.h | 2 + .../aron/core/data/variant/container/Dict.cpp | 13 ++- .../aron/core/data/variant/container/Dict.h | 1 + .../aron/core/data/visitor/RecursiveVisitor.h | 3 + .../aron/core/data/visitor/Visitor.h | 3 + .../libraries/aron/core/test/CMakeLists.txt | 4 +- .../libraries/aron/core/test/Randomizer.h | 4 +- .../libraries/aron/core/test/aron/AnyTest.xml | 12 ++ .../aron/core/test/aron/BaseClassTest.xml | 4 +- .../aron/core/test/aron/TemplateTest.xml | 40 +++++++ .../aron/core/test/aronCodeGenerationTest.cpp | 79 ++++++++++++- .../aron/core/test/aronExtendsTest.cpp | 4 +- .../aron/core/test/aronRandomizedTest.cpp | 12 +- .../aron/core/type/converter/Converter.h | 20 +++- .../nlohmannJSON/NlohmannJSONConverter.h | 36 +++--- .../type/converter/variant/VariantConverter.h | 37 +++--- .../libraries/aron/core/type/rw/Reader.h | 7 +- .../libraries/aron/core/type/rw/Writer.h | 8 +- .../libraries/aron/core/type/rw/json/Data.h | 6 +- .../nlohmannJSON/NlohmannJSONReader.cpp | 24 +++- .../reader/nlohmannJSON/NlohmannJSONReader.h | 6 +- .../type/rw/reader/variant/VariantReader.cpp | 21 +++- .../type/rw/reader/variant/VariantReader.h | 6 +- .../nlohmannJSON/NlohmannJSONWriter.cpp | 24 +++- .../writer/nlohmannJSON/NlohmannJSONWriter.h | 6 +- .../type/rw/writer/variant/VariantWriter.cpp | 20 +++- .../type/rw/writer/variant/VariantWriter.h | 6 +- .../libraries/aron/core/type/variant/All.h | 1 + .../aron/core/type/variant/any/All.h | 11 ++ .../aron/core/type/variant/any/AnyObject.cpp | 61 ++++++++++ .../aron/core/type/variant/any/AnyObject.h | 51 +++++++++ .../core/type/variant/container/Object.cpp | 63 ++++++++++- .../aron/core/type/variant/container/Object.h | 10 +- .../core/type/variant/detail/AnyVariant.cpp | 25 +++++ .../core/type/variant/detail/AnyVariant.h | 63 +++++++++++ .../aron/core/type/visitor/RecursiveVisitor.h | 3 + .../aron/core/type/visitor/Visitor.h | 3 + .../core/typereader/helper/GenerateInfo.h | 33 ++++++ .../libraries/aron/core/typereader/xml/Data.h | 2 + .../aron/core/typereader/xml/Factory.cpp | 82 +++++++++++--- .../aron/core/typereader/xml/Factory.h | 13 ++- .../aron/core/typereader/xml/Reader.cpp | 16 ++- 96 files changed, 1296 insertions(+), 271 deletions(-) rename source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/{AronCppClass.h => AronGeneratedClass.h} (89%) create mode 100644 source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/All.h create mode 100644 source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/AnyObject.cpp create mode 100644 source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/AnyObject.h create mode 100644 source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.cpp create mode 100644 source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.h create mode 100644 source/RobotAPI/libraries/aron/core/test/aron/AnyTest.xml create mode 100644 source/RobotAPI/libraries/aron/core/test/aron/TemplateTest.xml create mode 100644 source/RobotAPI/libraries/aron/core/type/variant/any/All.h create mode 100644 source/RobotAPI/libraries/aron/core/type/variant/any/AnyObject.cpp create mode 100644 source/RobotAPI/libraries/aron/core/type/variant/any/AnyObject.h create mode 100644 source/RobotAPI/libraries/aron/core/type/variant/detail/AnyVariant.cpp create mode 100644 source/RobotAPI/libraries/aron/core/type/variant/detail/AnyVariant.h diff --git a/source/RobotAPI/interface/aron/Aron.ice b/source/RobotAPI/interface/aron/Aron.ice index 4eb7b587c..979c15ac0 100644 --- a/source/RobotAPI/interface/aron/Aron.ice +++ b/source/RobotAPI/interface/aron/Aron.ice @@ -1,7 +1,9 @@ #pragma once - -#define ARON_VERSION "beta 0.2.3" +#define ARON_MAJOR "1" +#define ARON_MINOR "1" +#define ARON_PATCH "0" +#define ARON_VERSION "1.1.0" // allow templates + any types module armarx { @@ -105,7 +107,7 @@ module armarx class List extends GenericType { GenericType acceptedType; } class Tuple extends GenericType { GenericTypeSeq elementTypes; } class Pair extends GenericType { GenericType acceptedType1; GenericType acceptedType2; } - class AronObject extends GenericType { AronObject parent; string objectName; GenericTypeDict elementTypes; } + class AronObject extends GenericType { AronObject parent; AronStringSeq templates; string objectName; AronStringSeq templateInstantiations; GenericTypeDict elementTypes; } class Dict extends GenericType { GenericType acceptedType; } /* ***** Complex Types (serialize to ndarray) ***** */ @@ -129,6 +131,9 @@ module armarx class AronString extends GenericType { }; class AronBool extends GenericType { }; class AronTime extends GenericType { }; + + /* ***** Any Types ***** */ + class AnyObject extends GenericType {}; }; }; diff --git a/source/RobotAPI/libraries/armem/server/test/ArMemMemoryTest.cpp b/source/RobotAPI/libraries/armem/server/test/ArMemMemoryTest.cpp index 1fb8c6341..3ec604f35 100644 --- a/source/RobotAPI/libraries/armem/server/test/ArMemMemoryTest.cpp +++ b/source/RobotAPI/libraries/armem/server/test/ArMemMemoryTest.cpp @@ -665,7 +665,7 @@ struct CopyMoveCtorsOpsTest : public CopyMoveCtorsOpsTestBase if constexpr(std::is_base_of_v <armarx::armem::base::detail::AronTyped, T>) { - in.aronType() = std::make_shared<aron::type::Object>("some_object", std::map<std::string, aron::type::VariantPtr>()); + in.aronType() = std::make_shared<aron::type::Object>("some_object"); BOOST_CHECK(in.aronType()); } } diff --git a/source/RobotAPI/libraries/armem/util/util.h b/source/RobotAPI/libraries/armem/util/util.h index a51776792..a337bfafb 100644 --- a/source/RobotAPI/libraries/armem/util/util.h +++ b/source/RobotAPI/libraries/armem/util/util.h @@ -27,7 +27,7 @@ #include <ArmarXCore/core/logging/Logging.h> #include <RobotAPI/libraries/armem/core/wm/memory_definitions.h> -#include <RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/AronCppClass.h> +#include <RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/AronGeneratedClass.h> namespace armarx::armem @@ -43,7 +43,7 @@ namespace armarx::armem template <typename AronClass> std::optional<AronClass> tryCast(const wm::EntityInstance& item) { - static_assert(std::is_base_of<armarx::aron::codegenerator::cpp::AronCppClass, + static_assert(std::is_base_of<armarx::aron::codegenerator::cpp::AronGeneratedClass, AronClass>::value); @@ -81,7 +81,7 @@ namespace armarx::armem std::vector<AronClass> allOfType(const std::map<std::string, wm::Entity>& entities) { - static_assert(std::is_base_of<armarx::aron::codegenerator::cpp::AronCppClass, + static_assert(std::is_base_of<armarx::aron::codegenerator::cpp::AronGeneratedClass, AronClass>::value); std::vector<AronClass> outV; @@ -128,7 +128,7 @@ namespace armarx::armem auto transformAllOfType(const std::map<std::string, wm::Entity>& entities, auto pred) -> std::vector<decltype(pred(AronClass()))> { - static_assert(std::is_base_of<armarx::aron::codegenerator::cpp::AronCppClass, + static_assert(std::is_base_of<armarx::aron::codegenerator::cpp::AronGeneratedClass, AronClass>::value); std::vector<decltype(pred(AronClass()))> outV; diff --git a/source/RobotAPI/libraries/armem_gui/test/ArMemGuiTest.cpp b/source/RobotAPI/libraries/armem_gui/test/ArMemGuiTest.cpp index 557cb075d..af5f91496 100644 --- a/source/RobotAPI/libraries/armem_gui/test/ArMemGuiTest.cpp +++ b/source/RobotAPI/libraries/armem_gui/test/ArMemGuiTest.cpp @@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE(test_sanitizeTypeName_list) BOOST_AUTO_TEST_CASE(test_sanitizeTypeName_object) { - tn::Object obj("namespace::MyObjectName", std::map<std::string, tn::VariantPtr>()); + tn::Object obj("namespace::MyObjectName"); test_sanitize(obj.getFullName(), "MyObjectName (namespace)"); } diff --git a/source/RobotAPI/libraries/armem_robot_state/client/common/RobotReader.cpp b/source/RobotAPI/libraries/armem_robot_state/client/common/RobotReader.cpp index bdeeb0bd6..6610085a4 100644 --- a/source/RobotAPI/libraries/armem_robot_state/client/common/RobotReader.cpp +++ b/source/RobotAPI/libraries/armem_robot_state/client/common/RobotReader.cpp @@ -323,7 +323,7 @@ namespace armarx::armem::robot_state tryCast(const wm::EntityInstance& item) { static_assert( - std::is_base_of<armarx::aron::codegenerator::cpp::AronCppClass, AronClass>::value); + std::is_base_of<armarx::aron::codegenerator::cpp::AronGeneratedClass, AronClass>::value); try { diff --git a/source/RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.h b/source/RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.h index 1dc529348..0a4b2a5c3 100644 --- a/source/RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.h +++ b/source/RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.h @@ -31,22 +31,22 @@ namespace armarx::aron::converter { private: struct DataVariant2NlohmannJSONConverterHelper : - public data::VariantConverter<data::writer::NlohmannJSONWriter, DataVariant2NlohmannJSONConverterHelper> + public data::FromVariantConverter<data::writer::NlohmannJSONWriter> { }; struct NlohmannJSON2DataVariantConverterHelper : - public data::NlohmannJSONConverter<data::writer::VariantWriter, NlohmannJSON2DataVariantConverterHelper> + public data::FromNlohmannJSONConverter<data::writer::VariantWriter> { }; struct TypeVariant2NlohmannJSONConverterHelper : - public type::VariantConverter<type::writer::NlohmannJSONWriter, TypeVariant2NlohmannJSONConverterHelper> + public type::FromVariantConverter<type::writer::NlohmannJSONWriter> { }; struct NlohmannJSON2TypeVariantConverterHelper : - public type::NlohmannJSONConverter<type::writer::VariantWriter, NlohmannJSON2TypeVariantConverterHelper> + public type::FromNlohmannJSONConverter<type::writer::VariantWriter> { }; diff --git a/source/RobotAPI/libraries/aron/core/CMakeLists.txt b/source/RobotAPI/libraries/aron/core/CMakeLists.txt index 3e173f52e..1c4ae2923 100644 --- a/source/RobotAPI/libraries/aron/core/CMakeLists.txt +++ b/source/RobotAPI/libraries/aron/core/CMakeLists.txt @@ -41,6 +41,7 @@ set(LIB_FILES type/variant/detail/NDArrayVariant.cpp type/variant/detail/EnumVariant.cpp type/variant/detail/PrimitiveVariant.cpp + type/variant/detail/AnyVariant.cpp type/variant/container/Object.cpp type/variant/container/List.cpp type/variant/container/Dict.cpp @@ -62,6 +63,7 @@ set(LIB_FILES type/variant/primitive/String.cpp type/variant/primitive/Bool.cpp type/variant/primitive/Time.cpp + type/variant/any/AnyObject.cpp type/variant/Factory.cpp data/rw/Writer.cpp @@ -107,6 +109,7 @@ set(LIB_FILES codegenerator/codewriter/cpp/generator/detail/ContainerGenerator.cpp codegenerator/codewriter/cpp/generator/detail/NDArrayGenerator.cpp codegenerator/codewriter/cpp/generator/detail/PrimitiveGenerator.cpp + codegenerator/codewriter/cpp/generator/detail/AnyGenerator.cpp codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp codegenerator/codewriter/cpp/generator/container/Dict.cpp @@ -130,6 +133,7 @@ set(LIB_FILES codegenerator/codewriter/cpp/generator/primitive/String.cpp codegenerator/codewriter/cpp/generator/primitive/Bool.cpp codegenerator/codewriter/cpp/generator/primitive/Time.cpp + codegenerator/codewriter/cpp/generator/any/AnyObject.cpp codegenerator/codewriter/cpp/generator/Factory.cpp ) @@ -166,6 +170,7 @@ set(LIB_HEADERS type/variant/detail/NDArrayVariant.h type/variant/detail/EnumVariant.h type/variant/detail/PrimitiveVariant.h + type/variant/detail/AnyVariant.h type/variant/container/Object.h type/variant/container/List.h type/variant/container/Dict.h @@ -191,6 +196,8 @@ set(LIB_HEADERS type/variant/primitive/Bool.h type/variant/primitive/Time.h type/variant/primitive/All.h + type/variant/any/AnyObject.h + type/variant/any/All.h type/variant/All.h type/variant/Factory.h type/variant/forward_declarations.h @@ -247,13 +254,14 @@ set(LIB_HEADERS codegenerator/codewriter/CodeWriter.h codegenerator/codewriter/Factory.h - codegenerator/codewriter/cpp/AronCppClass.h + codegenerator/codewriter/cpp/AronGeneratedClass.h codegenerator/codewriter/cpp/Writer.h codegenerator/codewriter/cpp/generator/Generator.h codegenerator/codewriter/cpp/generator/detail/SpecializedGenerator.h codegenerator/codewriter/cpp/generator/detail/ContainerGenerator.h codegenerator/codewriter/cpp/generator/detail/NDArrayGenerator.h codegenerator/codewriter/cpp/generator/detail/PrimitiveGenerator.h + codegenerator/codewriter/cpp/generator/detail/AnyGenerator.h codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.h codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.h codegenerator/codewriter/cpp/generator/toplevel/All.h @@ -282,6 +290,8 @@ set(LIB_HEADERS codegenerator/codewriter/cpp/generator/primitive/Bool.h codegenerator/codewriter/cpp/generator/primitive/Time.h codegenerator/codewriter/cpp/generator/primitive/All.h + codegenerator/codewriter/cpp/generator/any/AnyObject.h + codegenerator/codewriter/cpp/generator/any/All.h codegenerator/codewriter/cpp/generator/All.h codegenerator/codewriter/cpp/generator/Factory.h ) diff --git a/source/RobotAPI/libraries/aron/core/Descriptor.h b/source/RobotAPI/libraries/aron/core/Descriptor.h index ba1b70b4d..0d6bba5a4 100644 --- a/source/RobotAPI/libraries/aron/core/Descriptor.h +++ b/source/RobotAPI/libraries/aron/core/Descriptor.h @@ -139,6 +139,7 @@ namespace armarx::aron::type eString, eBool, eTime, + eAnyObject, eUnknown = -1 }; @@ -165,6 +166,7 @@ namespace armarx::aron::type Descriptor::eString, Descriptor::eBool, Descriptor::eTime, + Descriptor::eAnyObject, Descriptor::eUnknown }; @@ -205,6 +207,7 @@ namespace armarx::aron::type {Descriptor::eString, "armarx::aron::type::Descriptor::eString"}, {Descriptor::eBool, "armarx::aron::type::Descriptor::eBool"}, {Descriptor::eTime, "armarx::aron::type::Descriptor::eTime"}, + {Descriptor::eAnyObject, "armarx::aron::type::Descriptor::eAnyObject"}, {Descriptor::eUnknown, "armarx::aron::type::Descriptor::eUnknown"} }; } @@ -233,7 +236,8 @@ namespace armarx::aron::type {typeid(aron::type::dto::AronDouble).hash_code(), Descriptor::eDouble}, {typeid(aron::type::dto::AronString).hash_code(), Descriptor::eString}, {typeid(aron::type::dto::AronBool).hash_code(), Descriptor::eBool}, - {typeid(aron::type::dto::AronTime).hash_code(), Descriptor::eTime} + {typeid(aron::type::dto::AronTime).hash_code(), Descriptor::eTime}, + {typeid(aron::type::dto::AnyObject).hash_code(), Descriptor::eAnyObject} }; } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/AronCppClass.h b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/AronGeneratedClass.h similarity index 89% rename from source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/AronCppClass.h rename to source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/AronGeneratedClass.h index 2e2ad3d99..eb4d5da66 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/AronCppClass.h +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/AronGeneratedClass.h @@ -40,11 +40,11 @@ namespace armarx::aron::codegenerator::cpp { - class AronCppClass + class AronGeneratedClass { public: - AronCppClass() = default; - virtual ~AronCppClass() = default; + AronGeneratedClass() = default; + virtual ~AronGeneratedClass() = default; /// Reset all member values of this class to default (as stated in the XML). This may mean that maybe types are null or false and images may be created as headers_only virtual void resetHard() = 0; @@ -52,4 +52,7 @@ namespace armarx::aron::codegenerator::cpp /// Reset all member values of this class softly, meaning if a maybe type has a value, we reset only the value (not the full maybe type) and if an image has data (width, height) we keep the data and width and height and only reset teh pixel values virtual void resetSoft() = 0; }; + + template <class T> + concept isAronGeneratedClass = std::is_base_of<AronGeneratedClass, T>::value; } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/All.h b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/All.h index 9bf1afd6b..36169d4a2 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/All.h +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/All.h @@ -5,6 +5,7 @@ #include "ndarray/All.h" #include "enum/All.h" #include "primitive/All.h" +#include "any/All.h" namespace { diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Factory.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Factory.cpp index 073d285e1..d3e9264f9 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Factory.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Factory.cpp @@ -59,6 +59,7 @@ namespace armarx::aron::codegenerator::cpp case type::Descriptor::eString: return std::make_unique<generator::String>(dynamic_cast<const type::String&>(n)); case type::Descriptor::eBool: return std::make_unique<generator::Bool>(dynamic_cast<const type::Bool&>(n)); case type::Descriptor::eTime: return std::make_unique<generator::Time>(dynamic_cast<const type::Time&>(n)); + case type::Descriptor::eAnyObject: return std::make_unique<generator::AnyObject>(dynamic_cast<const type::AnyObject&>(n)); default: throw error::ValueNotValidException(__PRETTY_FUNCTION__, "Cannot create a generator", std::to_string((int) desc), path);; } } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Generator.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Generator.cpp index 7e9930b64..4eee83a08 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Generator.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Generator.cpp @@ -38,6 +38,7 @@ namespace armarx::aron::codegenerator::cpp const std::string Generator::ARON_MAYBE_TYPE_ACCESSOR = Generator::ARON_VARIABLE_PREFIX + "_maybeType"; const std::string Generator::ARON_READER_ACCESSOR = Generator::ARON_VARIABLE_PREFIX + "_r"; const std::string Generator::ARON_WRITER_ACCESSOR = Generator::ARON_VARIABLE_PREFIX + "_w"; + const std::string Generator::ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR = Generator::ARON_VARIABLE_PREFIX + "_tmpls"; const std::string Generator::ARON_VARIANT_RETURN_ACCESSOR = Generator::ARON_VARIABLE_PREFIX + "_variant"; const std::string Generator::ARON_PATH_ACCESSOR = Generator::ARON_VARIABLE_PREFIX + "_p"; @@ -69,7 +70,7 @@ namespace armarx::aron::codegenerator::cpp { auto cpp = Generator::FromAronType(n); ARMARX_CHECK_NOT_NULL(cpp); - return cpp->getCoreCppTypename(); + return cpp->getInstantiatedCppTypename(); } std::vector<std::string> Generator::ExtractCppTypenames(const std::vector<type::VariantPtr>& n) { @@ -88,8 +89,9 @@ namespace armarx::aron::codegenerator::cpp // constructors - Generator::Generator(const std::string& cppName, const std::string& aronDataTypename, const std::string& aronTypeTypename) : - cppTypename(cppName), + Generator::Generator(const std::string& instantiatedCppTypename, const std::string& classCppTypename, const std::string& aronDataTypename, const std::string& aronTypeTypename) : + instantiatedCppTypename(instantiatedCppTypename), + classCppTypename(classCppTypename), aronDataTypename(aronDataTypename), aronTypeTypename(aronTypeTypename) { @@ -97,44 +99,68 @@ namespace armarx::aron::codegenerator::cpp } // public methods - std::string Generator::getCoreCppTypename() const + std::string Generator::getInstantiatedCppTypename() const { - return cppTypename; + return instantiatedCppTypename; } - std::string Generator::getFullCppTypename() const + std::string Generator::getFullInstantiatedCppTypename() const { switch (getType().getMaybe()) { case type::Maybe::eNone: - return getCoreCppTypename(); + return getInstantiatedCppTypename(); case type::Maybe::eOptional: - return "std::optional<" + getCoreCppTypename() + ">"; + return "std::optional<" + getInstantiatedCppTypename() + ">"; case type::Maybe::eRawPointer: - return getCoreCppTypename() + "*"; + return getInstantiatedCppTypename() + "*"; case type::Maybe::eSharedPointer: - return "std::shared_ptr<" + getCoreCppTypename() + ">"; + return "std::shared_ptr<" + getInstantiatedCppTypename() + ">"; case type::Maybe::eUniquePointer: - return "std::unique_ptr<" + getCoreCppTypename() + ">"; + return "std::unique_ptr<" + getInstantiatedCppTypename() + ">"; default: throw error::ValueNotValidException(__PRETTY_FUNCTION__, "Received unknown maybe enum", std::to_string((int) getType().getMaybe()), getType().getPath()); } } - std::string Generator::getFullCppTypenameGenerator() const + std::string Generator::getFullInstantiatedCppTypenameGenerator() const { switch (getType().getMaybe()) { case type::Maybe::eNone: - return getCoreCppTypename(); + return getInstantiatedCppTypename(); case type::Maybe::eOptional: - return "std::make_optional<" + getCoreCppTypename() + ">"; + return "std::make_optional<" + getInstantiatedCppTypename() + ">"; case type::Maybe::eRawPointer: - return getCoreCppTypename() + "*"; + return getInstantiatedCppTypename() + "*"; case type::Maybe::eSharedPointer: - return "std::make_shared<" + getCoreCppTypename() + ">"; + return "std::make_shared<" + getInstantiatedCppTypename() + ">"; case type::Maybe::eUniquePointer: - return "std::make_unique<" + getCoreCppTypename() + ">"; + return "std::make_unique<" + getInstantiatedCppTypename() + ">"; + default: + throw error::ValueNotValidException(__PRETTY_FUNCTION__, "Received unknown maybe enum", std::to_string((int) getType().getMaybe()), getType().getPath()); + } + } + + std::string Generator::getClassCppTypename() const + { + return classCppTypename; + } + + std::string Generator::getFullClassCppTypename() const + { + switch (getType().getMaybe()) + { + case type::Maybe::eNone: + return getClassCppTypename(); + case type::Maybe::eOptional: + return "std::optional<" + getClassCppTypename() + ">"; + case type::Maybe::eRawPointer: + return getClassCppTypename() + "*"; + case type::Maybe::eSharedPointer: + return "std::shared_ptr<" + getClassCppTypename() + ">"; + case type::Maybe::eUniquePointer: + return "std::unique_ptr<" + getClassCppTypename() + ">"; default: throw error::ValueNotValidException(__PRETTY_FUNCTION__, "Received unknown maybe enum", std::to_string((int) getType().getMaybe()), getType().getPath()); } @@ -183,9 +209,12 @@ namespace armarx::aron::codegenerator::cpp doc << "@brief writeType() - This method returns a new type from the class structure using a type writer implementation. This function is static. \n"; doc << "@return - the result of the writer implementation"; - CppMethodPtr m = CppMethodPtr(new CppMethod("template<class T>\nstatic T writeType(armarx::aron::type::WriterInterface<T>& " + ARON_WRITER_ACCESSOR + ", armarx::aron::type::Maybe "+ ARON_MAYBE_TYPE_ACCESSOR +" = armarx::aron::type::Maybe::eNone, const armarx::aron::Path& "+ARON_PATH_ACCESSOR+" = armarx::aron::Path())", doc.str())); + CppMethodPtr m = CppMethodPtr(new CppMethod("template<class WriterT>\nstatic typename WriterT::ReturnType writeType(WriterT& " + ARON_WRITER_ACCESSOR + ", std::vector<std::string> " + ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR + " = {}, armarx::aron::type::Maybe "+ ARON_MAYBE_TYPE_ACCESSOR +" = armarx::aron::type::Maybe::eNone, const armarx::aron::Path& "+ARON_PATH_ACCESSOR+" = armarx::aron::Path())", doc.str())); + CppBlockPtr b = std::make_shared<CppBlock>(); + b->addLine("using T = typename WriterT::ReturnType;"); + std::string dummy; - CppBlockPtr b = this->getWriteTypeBlock("", "", Path(), dummy); + b->appendBlock(this->getWriteTypeBlock("", "", Path(), dummy)); m->setBlock(b); return m; } @@ -197,9 +226,11 @@ namespace armarx::aron::codegenerator::cpp doc << "@param w - The writer implementation\n"; doc << "@return - the result of the writer implementation"; - CppMethodPtr m = CppMethodPtr(new CppMethod("template<class T>\nT write(armarx::aron::data::WriterInterface<T>& " + ARON_WRITER_ACCESSOR + ", const armarx::aron::Path& "+ARON_PATH_ACCESSOR+" = armarx::aron::Path()) const", doc.str())); - std::string dummy; + CppMethodPtr m = CppMethodPtr(new CppMethod("template<class WriterT>\ntypename WriterT::ReturnType write(WriterT& " + ARON_WRITER_ACCESSOR + ", const armarx::aron::Path& "+ARON_PATH_ACCESSOR+" = armarx::aron::Path()) const", doc.str())); CppBlockPtr b = std::make_shared<CppBlock>(); + b->addLine("using T = typename WriterT::ReturnType;"); + + std::string dummy; b->appendBlock(this->getWriteBlock("", Path(), dummy)); m->setBlock(b); return m; @@ -212,15 +243,19 @@ namespace armarx::aron::codegenerator::cpp doc << "@param r - The reader implementation\n"; doc << "@return - nothing"; - CppMethodPtr m = CppMethodPtr(new CppMethod("template<class T>\nvoid read(armarx::aron::data::ReaderInterface<T>& " + ARON_READER_ACCESSOR + ", T& input)", doc.str())); + CppMethodPtr m = CppMethodPtr(new CppMethod("template<class ReaderT>\nvoid read(ReaderT& " + ARON_READER_ACCESSOR + ", typename ReaderT::InputType& input)", doc.str())); CppBlockPtr b = std::make_shared<CppBlock>(); - b->addLine("using TNonConst = typename std::remove_const<T>::type;"); + b->addLine("using T = typename ReaderT::InputType;"); + b->addLine("using TNonConst = typename ReaderT::InputTypeNonConst;"); + // TODO: Remove me and make nice auto makeSuppressUnusedBlock = []() { auto block = std::make_shared<CppBlock>(); - block->addLine("const TNonConst* _suppressUnusedWarning;"); - block->addLine("(void) _suppressUnusedWarning;"); + block->addLine("const T* _suppressUnusedWarning1;"); + block->addLine("const TNonConst* _suppressUnusedWarning2;"); + block->addLine("(void) _suppressUnusedWarning1;"); + block->addLine("(void) _suppressUnusedWarning2;"); return block; }; b->addBlock(makeSuppressUnusedBlock()); @@ -253,7 +288,7 @@ namespace armarx::aron::codegenerator::cpp CppMethodPtr m = CppMethodPtr(new CppMethod("void " + info.methodName + "(const " + info.argumentType + "& input)", doc.str())); m->addLine(info.readerClassType + " reader;"); - m->addLine("this->read<" + info.readerClassType + "::InputType>(reader, " + info.enforceConversion + "(input));"); + m->addLine("this->read(reader, " + info.enforceConversion + "(input));"); return m; } @@ -289,7 +324,7 @@ namespace armarx::aron::codegenerator::cpp doc << "@param i - The other instance\n"; doc << "@return - true, if all members are the same, false otherwise"; - CppMethodPtr m = CppMethodPtr(new CppMethod("bool operator==(const " + this->getFullCppTypename() + "& i) const", doc.str())); + CppMethodPtr m = CppMethodPtr(new CppMethod("bool operator==(const " + this->getFullClassCppTypename() + "& i) const", doc.str())); CppBlockPtr b = this->getEqualsBlock("", "i"); b->addLine("return true;"); m->setBlock(b); @@ -299,7 +334,7 @@ namespace armarx::aron::codegenerator::cpp // defaulted implementations of the blocks std::vector<CppFieldPtr> Generator::getPublicVariableDeclarations(const std::string& name) const { - auto field = std::make_shared<CppField>(this->getFullCppTypename(), name); + auto field = std::make_shared<CppField>(this->getFullInstantiatedCppTypename(), name); return {field}; } @@ -316,14 +351,14 @@ namespace armarx::aron::codegenerator::cpp CppBlockPtr Generator::getResetHardBlock(const std::string& cppAccessor) const { CppBlockPtr block_if_data = std::make_shared<CppBlock>(); - block_if_data->addLine(cppAccessor + " = " + this->getFullCppTypename() + "();"); + block_if_data->addLine(cppAccessor + " = " + this->getFullInstantiatedCppTypename() + "();"); return resolveMaybeResetHardBlock(block_if_data, cppAccessor); } CppBlockPtr Generator::getResetSoftBlock(const std::string& cppAccessor) const { auto block_if_data = std::make_shared<CppBlock>(); - block_if_data->addLine(cppAccessor + " = " + this->getFullCppTypename() + "();"); + block_if_data->addLine(cppAccessor + " = " + this->getFullInstantiatedCppTypename() + "();"); return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor); } @@ -359,19 +394,19 @@ namespace armarx::aron::codegenerator::cpp const auto& t = getType(); if (t.getMaybe() == type::Maybe::eOptional) { - return s + " = std::make_optional<" + getCoreCppTypename() + ">();"; + return s + " = std::make_optional<" + getInstantiatedCppTypename() + ">();"; } if (t.getMaybe() == type::Maybe::eRawPointer) { - return s + " = new " + getCoreCppTypename() + "();"; + return s + " = new " + getInstantiatedCppTypename() + "();"; } if (t.getMaybe() == type::Maybe::eSharedPointer) { - return s + " = std::make_shared<" + getCoreCppTypename() + ">();"; + return s + " = std::make_shared<" + getInstantiatedCppTypename() + ">();"; } if (t.getMaybe() == type::Maybe::eUniquePointer) { - return s + " = std::make_unique<" + getCoreCppTypename() + ">();"; + return s + " = std::make_unique<" + getInstantiatedCppTypename() + ">();"; } return ""; } @@ -461,7 +496,7 @@ namespace armarx::aron::codegenerator::cpp b->addLine("if (not (" + ARON_READER_ACCESSOR + ".readNull(" + variantAccessor + "))) // if aron contains data"); { CppBlockPtr ifb = std::make_shared<CppBlock>(); - ifb->addLine(cppAccessor + " = " + getCoreCppTypename() + "();"); + ifb->addLine(resolveMaybeGenerator(cppAccessor)); ifb->appendBlock(block_if_data); b->addBlock(ifb); } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Generator.h b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Generator.h index c17405ce7..998c5c2e1 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Generator.h +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/Generator.h @@ -102,13 +102,16 @@ namespace armarx::aron::codegenerator::cpp public: // constructors Generator() = delete; - Generator(const std::string& cppName, const std::string& aronDataTypename, const std::string& aronTypeTypename); + Generator(const std::string& instantiatedCppTypename, const std::string& classCppTypename, const std::string& aronDataTypename, const std::string& aronTypeTypename); virtual ~Generator() = default; // public member methods - std::string getCoreCppTypename() const; - std::string getFullCppTypename() const; - std::string getFullCppTypenameGenerator() const; + std::string getInstantiatedCppTypename() const; + std::string getFullInstantiatedCppTypename() const; + std::string getFullInstantiatedCppTypenameGenerator() const; + + std::string getClassCppTypename() const; + std::string getFullClassCppTypename() const; CppMethodPtr toSpecializedDataWriterMethod(const WriterInfo& info) const; CppMethodPtr toSpecializedDataReaderMethod(const ReaderInfo& info) const; @@ -170,12 +173,14 @@ namespace armarx::aron::codegenerator::cpp static const std::string ARON_PATH_ACCESSOR; static const std::string ARON_READER_ACCESSOR; static const std::string ARON_WRITER_ACCESSOR; + static const std::string ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR; static const std::string ARON_VARIANT_RETURN_ACCESSOR; private: static const SerializerFactoryPtr FACTORY; - std::string cppTypename; + std::string instantiatedCppTypename; + std::string classCppTypename; std::string aronDataTypename; std::string aronTypeTypename; }; diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/All.h b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/All.h new file mode 100644 index 000000000..9a11aa6b9 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/All.h @@ -0,0 +1,7 @@ +#pragma once + +#include "AnyObject.h" + +namespace { + +} diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/AnyObject.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/AnyObject.cpp new file mode 100644 index 000000000..46a5f23a0 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/AnyObject.cpp @@ -0,0 +1,57 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +#include "AnyObject.h" + +#include <SimoxUtility/meta/type_name.h> + + +namespace armarx::aron::codegenerator::cpp::generator +{ + /* constructors */ + AnyObject::AnyObject(const type::AnyObject& e) : + detail::AnyGenerator<type::AnyObject, AnyObject>( + "armarx::aron::data::Dict", + "armarx::aron::data::Dict", + simox::meta::get_type_name<data::dto::Dict>(), + simox::meta::get_type_name<type::dto::AnyObject>(), + e) + { + if (type.getMaybe() != type::Maybe::eSharedPointer) + { + throw error::ValueNotValidException(__PRETTY_FUNCTION__, "At the momen the any object type must be a shared_ptr! Got ", std::to_string((int) type.getMaybe()) + " aka " + type::defaultconversion::string::Maybe2String.at(type.getMaybe()), type.getPath()); + } + } + + /* virtual implementations */ + CppBlockPtr AnyObject::getWriteTypeBlock(const std::string& typeAccessor, const std::string& accessor, const Path& p, std::string& variantAccessor) const + { + CppBlockPtr b = std::make_shared<CppBlock>(); + std::string escaped_accessor = EscapeAccessor(accessor); + variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor; + + b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeAnyObject(" + conversion::Maybe2CppString.at(type.getMaybe()) + ", " + + "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + typeAccessor); + return b; + } +} diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/AnyObject.h b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/AnyObject.h new file mode 100644 index 000000000..5d777de42 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/any/AnyObject.h @@ -0,0 +1,43 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +#pragma once + +#include <RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.h> +#include <RobotAPI/libraries/aron/core/type/variant/any/AnyObject.h> + + +namespace armarx::aron::codegenerator::cpp::generator +{ + class AnyObject : + public detail::AnyGenerator<type::AnyObject, AnyObject> + { + public: + /* constructors */ + AnyObject(const type::AnyObject& e); + virtual ~AnyObject() = default; + + /* virtual implementations */ + CppBlockPtr getWriteTypeBlock(const std::string& typeAccessor, const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const override; + }; +} diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Dict.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Dict.cpp index b42286150..16d73e061 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Dict.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Dict.cpp @@ -31,7 +31,11 @@ namespace armarx::aron::codegenerator::cpp::generator { // constructors Dict::Dict(const type::Dict& e) : - detail::ContainerGenerator<type::Dict, Dict>("std::map<std::string, " + FromAronType(*e.getAcceptedType())->getFullCppTypename() + ">", simox::meta::get_type_name<data::dto::Dict>(), simox::meta::get_type_name<type::dto::Dict>(), e) + detail::ContainerGenerator<type::Dict, Dict>( + "std::map<std::string, " + FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">", + "std::map<std::string, " + FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">", + simox::meta::get_type_name<data::dto::Dict>(), + simox::meta::get_type_name<type::dto::Dict>(), e) { } @@ -52,7 +56,7 @@ namespace armarx::aron::codegenerator::cpp::generator auto type_s = FromAronType(*type.getAcceptedType()); std::string nextVariantAccessor; Path nextPath = p.withAcceptedType(true); - b->appendBlock(type_s->getWriteTypeBlock(type_s->getCoreCppTypename(), cppAccessor + nextEl() + "accepted_type", nextPath, nextVariantAccessor)); + b->appendBlock(type_s->getWriteTypeBlock(type_s->getInstantiatedCppTypename(), cppAccessor + nextEl() + "accepted_type", nextPath, nextVariantAccessor)); b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeDict(" + nextVariantAccessor + ", " + conversion::Maybe2CppString.at(type.getMaybe()) + ", " + @@ -110,7 +114,7 @@ namespace armarx::aron::codegenerator::cpp::generator auto type_s = FromAronType(*type.getAcceptedType()); CppBlockPtr for_loop = std::make_shared<CppBlock>(); std::string accessor_iterator_tmp = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictTmp"; - for_loop->addLine(type_s->getFullCppTypename() + " " + accessor_iterator_tmp +";"); + for_loop->addLine(type_s->getFullInstantiatedCppTypename() + " " + accessor_iterator_tmp +";"); for_loop->appendBlock(type_s->getReadBlock(accessor_iterator_tmp, accessor_iterator_value)); for_loop->addLine(cppAccessor + nextEl() + "insert({" + accessor_iterator_key + ", " + accessor_iterator_tmp + "});"); block_if_data->addBlock(for_loop); diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/List.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/List.cpp index a7fbcd3ad..a69f17675 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/List.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/List.cpp @@ -31,7 +31,11 @@ namespace armarx::aron::codegenerator::cpp::generator { // constructors List::List(const type::List& e) : - detail::ContainerGenerator<type::List, List>("std::vector<" + FromAronType(*e.getAcceptedType())->getFullCppTypename() + ">", simox::meta::get_type_name<data::dto::List>(), simox::meta::get_type_name<type::dto::List>(), e) + detail::ContainerGenerator<type::List, List>( + "std::vector<" + FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">", + "std::vector<" + FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">", + simox::meta::get_type_name<data::dto::List>(), + simox::meta::get_type_name<type::dto::List>(), e) { } @@ -51,7 +55,7 @@ namespace armarx::aron::codegenerator::cpp::generator auto type_s = FromAronType(*type.getAcceptedType()); std::string nextVariantAccessor; Path nextPath = p.withAcceptedType(true); - CppBlockPtr b2 = type_s->getWriteTypeBlock(type_s->getCoreCppTypename(), cppAccessor + nextEl() + "accepted_type", nextPath, nextVariantAccessor); + CppBlockPtr b2 = type_s->getWriteTypeBlock(type_s->getInstantiatedCppTypename(), cppAccessor + nextEl() + "accepted_type", nextPath, nextVariantAccessor); b->appendBlock(b2); b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeList(" + nextVariantAccessor + ", " + @@ -107,7 +111,7 @@ namespace armarx::aron::codegenerator::cpp::generator auto type_s = FromAronType(*type.getAcceptedType()); std::string accessor_iterator_tmp = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_listTmp"; - for_loop->addLine(type_s->getFullCppTypename() + " " + accessor_iterator_tmp + ";"); + for_loop->addLine(type_s->getFullInstantiatedCppTypename() + " " + accessor_iterator_tmp + ";"); for_loop->appendBlock(type_s->getReadBlock(accessor_iterator_tmp, accessor_iterator_value)); for_loop->addLine(cppAccessor + nextEl() + "push_back(" + accessor_iterator_tmp + ");"); block_if_data->addBlock(for_loop); diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Object.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Object.cpp index faf7d6f2d..3b44ac06d 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Object.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Object.cpp @@ -31,7 +31,11 @@ namespace armarx::aron::codegenerator::cpp::generator { // constructors Object::Object(const type::Object& e) : - detail::ContainerGenerator<type::Object, Object>(e.getObjectName(), simox::meta::get_type_name<data::dto::Dict>(), simox::meta::get_type_name<type::dto::AronObject>(), e) + detail::ContainerGenerator<type::Object, Object>( + e.getObjectNameWithTemplateInstantiations(), + e.getObjectNameWithTemplates(), + simox::meta::get_type_name<data::dto::Dict>(), + simox::meta::get_type_name<type::dto::AronObject>(), e) { } @@ -49,7 +53,14 @@ namespace armarx::aron::codegenerator::cpp::generator std::string escaped_accessor = EscapeAccessor(cppAccessor); variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor; - b->addLine("auto " + variantAccessor + " = " + getCoreCppTypename() + "::writeType(" + ARON_WRITER_ACCESSOR + ", " + + std::vector<std::string> templatesQuoted; + for (const auto& t : type.getTemplateInstantiations()) + { + templatesQuoted.push_back("\"" + t + "\""); + } + + b->addLine("auto " + variantAccessor + " = " + getInstantiatedCppTypename() + "::writeType(" + ARON_WRITER_ACCESSOR + ", " + + "{" + simox::alg::join(templatesQuoted, ", ") + "}, " + conversion::Maybe2CppString.at(type.getMaybe()) + ", " + "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor); return b; @@ -73,7 +84,7 @@ namespace armarx::aron::codegenerator::cpp::generator { block_if_data->addLine(reset); } - block_if_data->addLine(cppAccessor + nextEl() + "read<T>(" + ARON_READER_ACCESSOR + ", " + variantAccessor + "); // of " + cppAccessor); + block_if_data->addLine(cppAccessor + nextEl() + "read(" + ARON_READER_ACCESSOR + ", " + variantAccessor + "); // of " + cppAccessor); return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor); } } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Pair.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Pair.cpp index d0fe5fc47..4f7cde8f0 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Pair.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Pair.cpp @@ -30,7 +30,11 @@ namespace armarx::aron::codegenerator::cpp::generator { Pair::Pair(const type::Pair& e) : - detail::ContainerGenerator<type::Pair, Pair>("std::pair<" + ExtractCppTypename(*e.getFirstAcceptedType()) + ", " + ExtractCppTypename(*e.getSecondAcceptedType()) + ">", simox::meta::get_type_name<data::dto::List>(), simox::meta::get_type_name<type::dto::Pair>(), e) + detail::ContainerGenerator<type::Pair, Pair>( + "std::pair<" + ExtractCppTypename(*e.getFirstAcceptedType()) + ", " + ExtractCppTypename(*e.getSecondAcceptedType()) + ">", + "std::pair<" + ExtractCppTypename(*e.getFirstAcceptedType()) + ", " + ExtractCppTypename(*e.getSecondAcceptedType()) + ">", + simox::meta::get_type_name<data::dto::List>(), + simox::meta::get_type_name<type::dto::Pair>(), e) { } @@ -59,14 +63,14 @@ namespace armarx::aron::codegenerator::cpp::generator std::string accessor_iterator1 = cppAccessor + nextEl() + "first"; std::string firstVariantAccessor; Path firstPath = p.withAcceptedTypeIndex(0); - CppBlockPtr b21 = child_s1->getWriteTypeBlock(child_s1->getCoreCppTypename(), accessor_iterator1, firstPath, firstVariantAccessor); + CppBlockPtr b21 = child_s1->getWriteTypeBlock(child_s1->getInstantiatedCppTypename(), accessor_iterator1, firstPath, firstVariantAccessor); block_if_data->appendBlock(b21); auto child_s2 = FromAronType(*type.getSecondAcceptedType()); std::string accessor_iterator2 = cppAccessor + nextEl() + "second"; std::string secondVariantAccessor; Path secondPath = p.withAcceptedTypeIndex(1); - CppBlockPtr b22 = child_s2->getWriteTypeBlock(child_s2->getCoreCppTypename(), accessor_iterator2, secondPath, secondVariantAccessor); + CppBlockPtr b22 = child_s2->getWriteTypeBlock(child_s2->getInstantiatedCppTypename(), accessor_iterator2, secondPath, secondVariantAccessor); block_if_data->appendBlock(b22); block_if_data->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writePair(" + firstVariantAccessor + ", " + diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Tuple.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Tuple.cpp index ce09ac505..5f2c241cb 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Tuple.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/container/Tuple.cpp @@ -29,7 +29,11 @@ namespace armarx::aron::codegenerator::cpp::generator { Tuple::Tuple(const type::Tuple& e) : - detail::ContainerGenerator<type::Tuple, Tuple>("std::tuple<" + simox::alg::join(ExtractCppTypenames(e.getAcceptedTypes()), ", ") + ">", simox::meta::get_type_name<data::dto::List>(), simox::meta::get_type_name<type::dto::Tuple>(), e) + detail::ContainerGenerator<type::Tuple, Tuple>( + "std::tuple<" + simox::alg::join(ExtractCppTypenames(e.getAcceptedTypes()), ", ") + ">", + "std::tuple<" + simox::alg::join(ExtractCppTypenames(e.getAcceptedTypes()), ", ") + ">", + simox::meta::get_type_name<data::dto::List>(), + simox::meta::get_type_name<type::dto::Tuple>(), e) { } @@ -65,7 +69,7 @@ namespace armarx::aron::codegenerator::cpp::generator auto type_s = FromAronType(*type); std::string nextVariantAccessor; Path nextPath = p.withAcceptedTypeIndex(i++); - CppBlockPtr b2 = type_s->getWriteTypeBlock(type_s->getCoreCppTypename(), accessor_iterator, nextPath, nextVariantAccessor); + CppBlockPtr b2 = type_s->getWriteTypeBlock(type_s->getInstantiatedCppTypename(), accessor_iterator, nextPath, nextVariantAccessor); block_if_data->appendBlock(b2); block_if_data->addLine(acceptedTypesAccessor + ".push_back(" + nextVariantAccessor + ");"); } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.cpp new file mode 100644 index 000000000..09378aa67 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.cpp @@ -0,0 +1,25 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +// Header +#include "AnyGenerator.h" diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.h b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.h new file mode 100644 index 000000000..8e02a66ef --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/AnyGenerator.h @@ -0,0 +1,60 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +#pragma once + +#include "SpecializedGenerator.h" + +#include <string> + + +namespace armarx::aron::codegenerator::cpp::generator::detail +{ + template<typename typeT, typename DerivedT> + class AnyGenerator : + public SpecializedGeneratorBase<typeT, DerivedT> + { + public: + using SpecializedGeneratorBase<typeT, DerivedT>::SpecializedGeneratorBase; + virtual ~AnyGenerator() = default; + + CppBlockPtr getWriteBlock(const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const override + { + auto block_if_data = std::make_shared<CppBlock>(); + std::string escaped_accessor = this->EscapeAccessor(cppAccessor); + variantAccessor = Generator::ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor; + + block_if_data->addLine(variantAccessor + " = armarx::aron::data::readAndWrite<armarx::aron::data::FromVariantConverter<WriterT>>(" + cppAccessor + "); // of " + cppAccessor); + return block_if_data; + } + + CppBlockPtr getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const override + { + auto block_if_data = std::make_shared<CppBlock>(); + std::string escaped_accessor = this->EscapeAccessor(cppAccessor); + + block_if_data->addLine(cppAccessor + " = armarx::aron::data::Dict::DynamicCastAndCheck(armarx::aron::data::readAndWrite<aron::data::ToVariantConverter<ReaderT>>(" + variantAccessor + ")); // of " + cppAccessor); + return block_if_data; + } + }; +} diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/SpecializedGenerator.h b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/SpecializedGenerator.h index 701ea582e..8df5c01fc 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/SpecializedGenerator.h +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/detail/SpecializedGenerator.h @@ -36,8 +36,8 @@ namespace armarx::aron::codegenerator::cpp::generator::detail public codegenerator::cpp::Generator { public: - SpecializedGeneratorBase(const std::string& cppName, const std::string& aronDataTypename, const std::string& aronTypeTypename, const TypeT& t) : - Generator(cppName, aronDataTypename, aronTypeTypename), + SpecializedGeneratorBase(const std::string& instantiatedCppTypename, const std::string& classCppTypename, const std::string& aronDataTypename, const std::string& aronTypeTypename, const TypeT& t) : + Generator(instantiatedCppTypename, classCppTypename, aronDataTypename, aronTypeTypename), type(t) { } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/enum/IntEnum.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/enum/IntEnum.cpp index a36899f72..f27c0cd24 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/enum/IntEnum.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/enum/IntEnum.cpp @@ -31,7 +31,11 @@ namespace armarx::aron::codegenerator::cpp::generator { // constructors IntEnum::IntEnum(const type::IntEnum& e) : - detail::SpecializedGeneratorBase<type::IntEnum, IntEnum>(e.getEnumName(), simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::IntEnum>(), e) + detail::SpecializedGeneratorBase<type::IntEnum, IntEnum>( + e.getEnumName(), + e.getEnumName(), + simox::meta::get_type_name<data::dto::NDArray>(), + simox::meta::get_type_name<type::dto::IntEnum>(), e) { } @@ -62,7 +66,8 @@ namespace armarx::aron::codegenerator::cpp::generator std::string escaped_accessor = EscapeAccessor(cppAccessor); variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor; - b->addLine("auto "+variantAccessor+" = " +getCoreCppTypename() + "::writeType(" + ARON_WRITER_ACCESSOR + ", " + + b->addLine("auto "+variantAccessor+" = " +getInstantiatedCppTypename() + "::writeType(" + ARON_WRITER_ACCESSOR + ", " + + "{}, " + conversion::Maybe2CppString.at(type.getMaybe()) + ", " + "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor); return b; @@ -87,7 +92,7 @@ namespace armarx::aron::codegenerator::cpp::generator { block_if_data->addLine(reset); } - block_if_data->addLine(cppAccessor + nextEl() + "read<T>(" + ARON_READER_ACCESSOR + ", " + variantAccessor + ");"); + block_if_data->addLine(cppAccessor + nextEl() + "read(" + ARON_READER_ACCESSOR + ", " + variantAccessor + ");"); return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor); } } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Image.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Image.cpp index 466f4deb5..096ac6aee 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Image.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Image.cpp @@ -38,7 +38,11 @@ namespace armarx::aron::codegenerator::cpp::generator // constructors Image::Image(const type::Image& n) : - detail::NDArrayGenerator<type::Image, Image>("cv::Mat", simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::Image>(), n) + detail::NDArrayGenerator<type::Image, Image>( + "cv::Mat", + "cv::Mat", + simox::meta::get_type_name<data::dto::NDArray>(), + simox::meta::get_type_name<type::dto::Image>(), n) { } @@ -101,7 +105,7 @@ namespace armarx::aron::codegenerator::cpp::generator block_if_data->addLine("std::vector<int> " + dims + ";"); block_if_data->addLine("std::vector<unsigned char> " + data + ";"); block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readNDArray("+variantAccessor+", "+dims+", "+type+", "+data+"); // of " + cppAccessor); - block_if_data->addLine(cppAccessor + " = " + getCoreCppTypename() + "(std::vector<int>({" + dims + ".begin(), std::prev(" + dims + ".end())}), std::stoi(" + type + "));"); + block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "(std::vector<int>({" + dims + ".begin(), std::prev(" + dims + ".end())}), std::stoi(" + type + "));"); block_if_data->addLine("std::memcpy(reinterpret_cast<unsigned char*>(" + cppAccessor + nextEl() + "data), "+data+".data(), "+data+".size());"); return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor); } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp index d65318386..6be43a1ab 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp @@ -41,7 +41,11 @@ namespace armarx::aron::codegenerator::cpp::generator // constructors Matrix::Matrix(const type::Matrix& n) : - detail::NDArrayGenerator<type::Matrix, Matrix>("Eigen::Matrix<" + ElementType2Cpp.at(n.getElementType()).first + ", " + std::to_string(n.getRows()) + ", " + std::to_string(n.getCols()) + ">", simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::Matrix>(), n) + detail::NDArrayGenerator<type::Matrix, Matrix>( + "Eigen::Matrix<" + ElementType2Cpp.at(n.getElementType()).first + ", " + std::to_string(n.getRows()) + ", " + std::to_string(n.getCols()) + ">", + "Eigen::Matrix<" + ElementType2Cpp.at(n.getElementType()).first + ", " + std::to_string(n.getRows()) + ", " + std::to_string(n.getCols()) + ">", + simox::meta::get_type_name<data::dto::NDArray>(), + simox::meta::get_type_name<type::dto::Matrix>(), n) { } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/NDArray.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/NDArray.cpp index 440aa5197..4198f74d4 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/NDArray.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/NDArray.cpp @@ -31,14 +31,18 @@ namespace armarx::aron::codegenerator::cpp::generator { // constructors NDArray::NDArray(const type::NDArray& n) : - detail::NDArrayGenerator<type::NDArray, NDArray>("NDArray", simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::NDArray>(), n) + detail::NDArrayGenerator<type::NDArray, NDArray>( + "NDArray", + "NDArray", + simox::meta::get_type_name<data::dto::NDArray>(), + simox::meta::get_type_name<type::dto::NDArray>(), n) { } CppBlockPtr NDArray::getResetSoftBlock(const std::string& cppAccessor) const { CppBlockPtr block_if_data = std::make_shared<CppBlock>(); - block_if_data->addLine(cppAccessor + " = " + this->getFullCppTypename() + "();"); + block_if_data->addLine(cppAccessor + " = " + this->getFullInstantiatedCppTypename() + "();"); return resolveMaybeResetSoftBlock(block_if_data, cppAccessor); } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Orientation.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Orientation.cpp index e65016224..e8c5dfc55 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Orientation.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Orientation.cpp @@ -31,7 +31,11 @@ namespace armarx::aron::codegenerator::cpp::generator { // constructors Orientation::Orientation(const type::Orientation& n) : - detail::NDArrayGenerator<type::Orientation, Orientation>("Eigen::Quaternion<float>", simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::Orientation>(), n) + detail::NDArrayGenerator<type::Orientation, Orientation>( + "Eigen::Quaternion<float>", + "Eigen::Quaternion<float>", + simox::meta::get_type_name<data::dto::NDArray>(), + simox::meta::get_type_name<type::dto::Orientation>(), n) { } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/PointCloud.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/PointCloud.cpp index 95aafae3d..aac65ba17 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/PointCloud.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/PointCloud.cpp @@ -45,6 +45,7 @@ namespace armarx::aron::codegenerator::cpp::generator // constructors PointCloud::PointCloud(const type::PointCloud& n) : detail::NDArrayGenerator<type::PointCloud, PointCloud>( + "pcl::PointCloud<" + VoxelType2Cpp.at(n.getVoxelType()).first + ">", "pcl::PointCloud<" + VoxelType2Cpp.at(n.getVoxelType()).first + ">", simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::PointCloud>(), @@ -55,7 +56,7 @@ namespace armarx::aron::codegenerator::cpp::generator CppBlockPtr PointCloud::getResetSoftBlock(const std::string& cppAccessor) const { CppBlockPtr block_if_data = std::make_shared<CppBlock>(); - block_if_data->addLine(cppAccessor + " = " + getFullCppTypenameGenerator() + "(" + cppAccessor + nextEl() + "width, " + cppAccessor + nextEl() + "height);"); + block_if_data->addLine(cppAccessor + " = " + getFullInstantiatedCppTypenameGenerator() + "(" + cppAccessor + nextEl() + "width, " + cppAccessor + nextEl() + "height);"); return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor); } @@ -97,7 +98,7 @@ namespace armarx::aron::codegenerator::cpp::generator block_if_data->addLine("std::vector<int> " + dims + ";"); block_if_data->addLine("std::vector<unsigned char> " + data + ";"); block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readNDArray("+variantAccessor+", "+dims+", "+type+", "+data+"); // of " + cppAccessor); - block_if_data->addLine(cppAccessor + " = " + getCoreCppTypename() + "(" + dims + "[0], " + dims + "[1]);"); + block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "(" + dims + "[0], " + dims + "[1]);"); block_if_data->addLine("std::memcpy(reinterpret_cast<unsigned char*>(" + cppAccessor + nextEl() + "points.data()), "+data+".data(), "+data+".size());"); return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor); } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Pose.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Pose.cpp index c6116c2f8..ce130c44a 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Pose.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Pose.cpp @@ -32,6 +32,7 @@ namespace armarx::aron::codegenerator::cpp::generator // constructors Pose::Pose(const type::Pose& n) : detail::NDArrayGenerator<type::Pose, Pose>( + "Eigen::Matrix<float, 4, 4>", "Eigen::Matrix<float, 4, 4>", simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::Pose>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Position.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Position.cpp index 919e9f14e..eec0330eb 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Position.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Position.cpp @@ -32,6 +32,7 @@ namespace armarx::aron::codegenerator::cpp::generator // constructors Position::Position(const type::Position& n) : detail::NDArrayGenerator<type::Position, Position>( + "Eigen::Matrix<float, 3, 1>", "Eigen::Matrix<float, 3, 1>", simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::Position>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Quaternion.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Quaternion.cpp index e5b0aeb6a..c7e9ba4b9 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Quaternion.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/ndarray/Quaternion.cpp @@ -39,6 +39,7 @@ namespace armarx::aron::codegenerator::cpp::generator // constructors Quaternion::Quaternion(const type::Quaternion& n) : detail::NDArrayGenerator<type::Quaternion, Quaternion>( + "Eigen::Quaternion<" + ElementType2Cpp.at(n.getElementType()).first + ">", "Eigen::Quaternion<" + ElementType2Cpp.at(n.getElementType()).first + ">", simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::Quaternion>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Bool.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Bool.cpp index 96a072070..fb3572e26 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Bool.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Bool.cpp @@ -30,7 +30,11 @@ namespace armarx::aron::codegenerator::cpp::generator { /* constructors */ Bool::Bool(const type::Bool& e) : - detail::PrimitiveGenerator<type::Bool, Bool>("bool", simox::meta::get_type_name<data::dto::AronBool>(), simox::meta::get_type_name<type::dto::AronBool>(), e) + detail::PrimitiveGenerator<type::Bool, Bool>( + "bool", + "bool", + simox::meta::get_type_name<data::dto::AronBool>(), + simox::meta::get_type_name<type::dto::AronBool>(), e) { } diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Double.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Double.cpp index ba2a91499..707679455 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Double.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Double.cpp @@ -31,6 +31,7 @@ namespace armarx::aron::codegenerator::cpp::generator /* constructors */ Double::Double(const type::Double& e) : detail::PrimitiveGenerator<type::Double, Double>( + "double", "double", simox::meta::get_type_name<data::dto::AronDouble>(), simox::meta::get_type_name<type::dto::AronDouble>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Float.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Float.cpp index a0161aa67..6c740a792 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Float.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Float.cpp @@ -31,6 +31,7 @@ namespace armarx::aron::codegenerator::cpp::generator /* constructors */ Float::Float(const type::Float& e) : detail::PrimitiveGenerator<type::Float, Float>( + "float", "float", simox::meta::get_type_name<data::dto::AronFloat>(), simox::meta::get_type_name<type::dto::AronFloat>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Int.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Int.cpp index ca09f8b02..9242126f5 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Int.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Int.cpp @@ -31,6 +31,7 @@ namespace armarx::aron::codegenerator::cpp::generator /* constructors */ Int::Int(const type::Int& e) : detail::PrimitiveGenerator<type::Int, Int>( + "int", "int", simox::meta::get_type_name<data::dto::AronInt>(), simox::meta::get_type_name<type::dto::AronInt>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Long.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Long.cpp index bc9a136ae..2231f3305 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Long.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Long.cpp @@ -31,6 +31,7 @@ namespace armarx::aron::codegenerator::cpp::generator /* constructors */ Long::Long(const type::Long& e) : detail::PrimitiveGenerator<type::Long, Long>( + "long", "long", simox::meta::get_type_name<data::dto::AronLong>(), simox::meta::get_type_name<type::dto::AronLong>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/String.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/String.cpp index 83a818f00..a335fafcb 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/String.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/String.cpp @@ -31,6 +31,7 @@ namespace armarx::aron::codegenerator::cpp::generator /* constructors */ String::String(const type::String& e) : detail::PrimitiveGenerator<type::String, String>( + "std::string", "std::string", simox::meta::get_type_name<data::dto::AronString>(), simox::meta::get_type_name<type::dto::AronString>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Time.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Time.cpp index d836e3cab..becc4f2d2 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Time.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/primitive/Time.cpp @@ -31,6 +31,7 @@ namespace armarx::aron::codegenerator::cpp::generator /* constructors */ Time::Time(const type::Time& e) : detail::PrimitiveGenerator<type::Time, Time>( + "IceUtil::Time", "IceUtil::Time", simox::meta::get_type_name<data::dto::AronLong>(), simox::meta::get_type_name<type::dto::AronTime>(), diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp index 16e93a66d..2b7b64550 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp @@ -32,6 +32,7 @@ namespace armarx::aron::codegenerator::cpp::generator // constructors IntEnumClass::IntEnumClass(const type::IntEnum& n) : detail::SpecializedGeneratorBase<type::IntEnum, IntEnumClass>( + n.getEnumName(), n.getEnumName(), simox::meta::get_type_name<data::dto::NDArray>(), simox::meta::get_type_name<type::dto::IntEnum>(), @@ -118,7 +119,7 @@ namespace armarx::aron::codegenerator::cpp::generator CppBlockPtr block_if_data = std::make_shared<CppBlock>(); block_if_data->addLine("return " + ARON_WRITER_ACCESSOR + ".writePrimitive(EnumToValueMap.at(value), " + - ARON_PATH_ACCESSOR + "); // of top level enum " + getCoreCppTypename()); + ARON_PATH_ACCESSOR + "); // of top level enum " + getInstantiatedCppTypename()); return block_if_data; } @@ -128,7 +129,7 @@ namespace armarx::aron::codegenerator::cpp::generator CppBlockPtr block_if_data = std::make_shared<CppBlock>(); block_if_data->addLine("int " + INT_ENUM_TMP_VALUE + ";"); - block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readPrimitive("+variantAccessor+", "+INT_ENUM_TMP_VALUE+"); // of top level enum " + getCoreCppTypename()); + block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readPrimitive("+variantAccessor+", "+INT_ENUM_TMP_VALUE+"); // of top level enum " + getInstantiatedCppTypename()); block_if_data->addLine("value = ValueToEnumMap.at("+INT_ENUM_TMP_VALUE+");"); return block_if_data; } @@ -144,7 +145,7 @@ namespace armarx::aron::codegenerator::cpp::generator CppCtorPtr IntEnumClass::toCopyCtor(const std::string& name) const { - CppCtorPtr c = std::make_shared<CppCtor>(name + "(const " + getFullCppTypename() + "& i)"); + CppCtorPtr c = std::make_shared<CppCtor>(name + "(const " + getFullInstantiatedCppTypename() + "& i)"); std::vector<std::pair<std::string, std::string>> initList = {{"value", "i.value"}}; c->addInitListEntries(initList); return c; @@ -187,7 +188,7 @@ namespace armarx::aron::codegenerator::cpp::generator doc << "@brief operator=() - Assignment operator for copy \n"; doc << "@return - nothing"; - CppMethodPtr m = CppMethodPtr(new CppMethod(getFullCppTypename() + "& operator=(const " + getFullCppTypename() + "& c)", doc.str())); + CppMethodPtr m = CppMethodPtr(new CppMethod(getFullInstantiatedCppTypename() + "& operator=(const " + getFullInstantiatedCppTypename() + "& c)", doc.str())); CppBlockPtr b = std::make_shared<CppBlock>(); b->addLine("value = c.value;"); b->addLine("return *this;"); @@ -201,7 +202,7 @@ namespace armarx::aron::codegenerator::cpp::generator doc << "@brief operator=() - Assignment operator for the internally defined enum \n"; doc << "@return - nothing"; - CppMethodPtr m = CppMethodPtr(new CppMethod(getFullCppTypename() + "& operator=(" + std::string(IMPL_ENUM) + " v)", doc.str())); + CppMethodPtr m = CppMethodPtr(new CppMethod(getFullInstantiatedCppTypename() + "& operator=(" + std::string(IMPL_ENUM) + " v)", doc.str())); CppBlockPtr b = std::make_shared<CppBlock>(); b->addLine("value = v;"); b->addLine("return *this;"); @@ -215,7 +216,7 @@ namespace armarx::aron::codegenerator::cpp::generator doc << "@brief operator=() - Assignment operator for the internally defined enum \n"; doc << "@return - nothing"; - CppMethodPtr m = CppMethodPtr(new CppMethod(getFullCppTypename() + "& operator=(int v)", doc.str())); + CppMethodPtr m = CppMethodPtr(new CppMethod(getFullInstantiatedCppTypename() + "& operator=(int v)", doc.str())); CppBlockPtr b = std::make_shared<CppBlock>(); b->addLine("if (auto it = ValueToEnumMap.find(v); it == ValueToEnumMap.end())"); CppBlockPtr b2 = std::make_shared<CppBlock>(); diff --git a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp index 7b8c63115..70df7d108 100644 --- a/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp +++ b/source/RobotAPI/libraries/aron/core/codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp @@ -31,7 +31,11 @@ namespace armarx::aron::codegenerator::cpp::generator { // constructors ObjectClass::ObjectClass(const type::Object& e) : - detail::SpecializedGeneratorBase<type::Object, ObjectClass>(e.getObjectName(), simox::meta::get_type_name<data::dto::Dict>(), simox::meta::get_type_name<type::dto::AronObject>(), e) + detail::SpecializedGeneratorBase<type::Object, ObjectClass>( + e.getObjectName(), + e.getObjectNameWithTemplates(), + simox::meta::get_type_name<data::dto::Dict>(), + simox::meta::get_type_name<type::dto::AronObject>(), e) { if (type.getMaybe() != type::Maybe::eNone) { @@ -57,7 +61,7 @@ namespace armarx::aron::codegenerator::cpp::generator if (type.getExtends() != nullptr) { const auto extends_s = FromAronType(*type.getExtends()); - block_if_data->addLine(extends_s->getFullCppTypename() + "::resetSoft();"); + block_if_data->addLine(extends_s->getFullInstantiatedCppTypename() + "::resetSoft();"); } for (const auto& [key, child] : type.getMemberTypes()) @@ -75,7 +79,7 @@ namespace armarx::aron::codegenerator::cpp::generator if (type.getExtends() != nullptr) { const auto extends_s = FromAronType(*type.getExtends()); - block_if_data->addLine(extends_s->getFullCppTypename() + "::resetHard();"); + block_if_data->addLine(extends_s->getFullInstantiatedCppTypename() + "::resetHard();"); } for (const auto& [key, child] : type.getMemberTypes()) @@ -98,30 +102,45 @@ namespace armarx::aron::codegenerator::cpp::generator if (type.getExtends() != nullptr) { const auto extends_s = FromAronType(*type.getExtends()); - b->addLine("// get base class of " + this->getFullCppTypename()); - b->addLine("auto " + OBJECT_EXTENDS_ACCESSOR + " = " + extends_s->getFullCppTypename() + "::writeType(" + ARON_WRITER_ACCESSOR + ", ::armarx::aron::type::Maybe::eNone);"); + b->addLine("// get base class of " + this->getFullInstantiatedCppTypename()); + b->addLine("auto " + OBJECT_EXTENDS_ACCESSOR + " = " + extends_s->getFullInstantiatedCppTypename() + "::writeType(" + + ARON_WRITER_ACCESSOR + ", " + + "{" + simox::alg::join(type.getExtends()->getTemplateInstantiations(), ", ") + "}, " + + "::armarx::aron::type::Maybe::eNone);"); } else { b->addLine("auto " + OBJECT_EXTENDS_ACCESSOR + " = std::nullopt;"); } - b->addLine("// members of " + this->getFullCppTypename()); + b->addLine("// members of " + this->getFullInstantiatedCppTypename()); for (const auto& [key, child] : type.getMemberTypes()) { const auto child_s = FromAronType(*child); std::string child_return_variant; Path nextPath = p.withElement(key, true); - CppBlockPtr child_b = child_s->getWriteTypeBlock(child_s->getFullCppTypename(), key, nextPath, child_return_variant); + CppBlockPtr child_b = child_s->getWriteTypeBlock(child_s->getFullInstantiatedCppTypename(), key, nextPath, child_return_variant); b->appendBlock(child_b); b->addLine(OBJECT_MEMBERS_ACCESSOR + ".emplace(\"" + key + "\", " + child_return_variant + ");"); } + std::vector<std::string> templatesQuoted; + std::vector<std::string> templateIntantiationsQuoted; + for (const auto& t : type.getTemplates()) + { + templatesQuoted.push_back("\"" + t + "\""); + } + for (const auto& t : type.getTemplateInstantiations()) + { + templateIntantiationsQuoted.push_back("\"" + t + "\""); + } b->addLine("return " + ARON_WRITER_ACCESSOR + ".writeObject(\"" + type.getObjectName() + "\", " + + "{" + simox::alg::join(templatesQuoted, ", ") + "}, " + + ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR + ", " + OBJECT_MEMBERS_ACCESSOR + ", " + OBJECT_EXTENDS_ACCESSOR + ", " + ARON_MAYBE_TYPE_ACCESSOR + ", " + - ARON_PATH_ACCESSOR + "); // of top level object " + getCoreCppTypename());; + ARON_PATH_ACCESSOR + "); // of top level object " + getInstantiatedCppTypename());; return b; } @@ -139,11 +158,11 @@ namespace armarx::aron::codegenerator::cpp::generator if (type.getExtends() != nullptr) { const auto extends_s = FromAronType(*type.getExtends()); - block_if_data->addLine("// write base class of " + this->getFullCppTypename()); - block_if_data->addLine(OBJECT_EXTENDS_ACCESSOR + " = " + extends_s->getFullCppTypename() + "::write(" + ARON_WRITER_ACCESSOR + ");"); + block_if_data->addLine("// write base class of " + this->getFullInstantiatedCppTypename()); + block_if_data->addLine(OBJECT_EXTENDS_ACCESSOR + " = " + extends_s->getFullInstantiatedCppTypename() + "::write(" + ARON_WRITER_ACCESSOR + ");"); } - block_if_data->addLine("// members of " + this->getFullCppTypename()); + block_if_data->addLine("// members of " + this->getFullInstantiatedCppTypename()); for (const auto& [key, child] : type.getMemberTypes()) { const auto child_s = FromAronType(*child); @@ -158,7 +177,7 @@ namespace armarx::aron::codegenerator::cpp::generator block_if_data->addLine("return " + ARON_WRITER_ACCESSOR + ".writeDict("+OBJECT_MEMBERS_ACCESSOR+", " + OBJECT_EXTENDS_ACCESSOR + ", "+ - ARON_PATH_ACCESSOR + "); // of top level object " + getCoreCppTypename()); + ARON_PATH_ACCESSOR + "); // of top level object " + getInstantiatedCppTypename()); return block_if_data; } @@ -173,10 +192,10 @@ namespace armarx::aron::codegenerator::cpp::generator if (type.getExtends() != nullptr) { const auto extends_s = FromAronType(*type.getExtends()); - block_if_data->addLine(extends_s->getFullCppTypename() + "::read(" + ARON_READER_ACCESSOR + ", "+variantAccessor+");"); + block_if_data->addLine(extends_s->getFullInstantiatedCppTypename() + "::read(" + ARON_READER_ACCESSOR + ", "+variantAccessor+");"); } - block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readDict("+variantAccessor+", "+OBJECT_MEMBERS_ACCESSOR+"); // of top level object " + getCoreCppTypename()); + block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readDict("+variantAccessor+", "+OBJECT_MEMBERS_ACCESSOR+"); // of top level object " + getInstantiatedCppTypename()); for (const auto& [key, child] : type.getMemberTypes()) { @@ -193,7 +212,7 @@ namespace armarx::aron::codegenerator::cpp::generator if (type.getExtends() != nullptr) { const auto extends_s = FromAronType(*type.getExtends()); - block_if_data->addLine("if (not (" + extends_s->getFullCppTypename() + "::operator== (" + otherInstanceAccessor + ")))"); + block_if_data->addLine("if (not (" + extends_s->getFullInstantiatedCppTypename() + "::operator== (" + otherInstanceAccessor + ")))"); block_if_data->addLineAsBlock("return false;"); } for (const auto& [key, child] : type.getMemberTypes()) diff --git a/source/RobotAPI/libraries/aron/core/data/converter/Converter.h b/source/RobotAPI/libraries/aron/core/data/converter/Converter.h index 3c74e4197..0d8688338 100644 --- a/source/RobotAPI/libraries/aron/core/data/converter/Converter.h +++ b/source/RobotAPI/libraries/aron/core/data/converter/Converter.h @@ -60,6 +60,11 @@ namespace armarx::aron::data virtual ~Converter() = default; + data::Descriptor getDescriptor(ReaderInputType& o) final + { + return r.getDescriptor(o); + } + void visitDict(ReaderInputType& o) final { std::map<std::string, ReaderInputTypeNonConst> elementsOfInput; @@ -146,6 +151,15 @@ namespace armarx::aron::data r.readString(o, i, p); last_returned = w.writeString(i, p); }; + + void visitUnknown(ReaderInputType& o) final + { + if (!r.readNull(o)) + { + throw error::AronException(__PRETTY_FUNCTION__, "A visitor got data but the enum is unknown."); + } + w.writeNull(); + }; }; /// the function to read from a variant and write to a writer T diff --git a/source/RobotAPI/libraries/aron/core/data/converter/nlohmannJSON/NlohmannJSONConverter.h b/source/RobotAPI/libraries/aron/core/data/converter/nlohmannJSON/NlohmannJSONConverter.h index 0fab92803..dea8f1751 100644 --- a/source/RobotAPI/libraries/aron/core/data/converter/nlohmannJSON/NlohmannJSONConverter.h +++ b/source/RobotAPI/libraries/aron/core/data/converter/nlohmannJSON/NlohmannJSONConverter.h @@ -26,31 +26,25 @@ #include "../Converter.h" #include "../../visitor/nlohmannJSON/NlohmannJSONVisitor.h" #include "../../rw/reader/nlohmannJSON/NlohmannJSONReader.h" +#include "../../rw/writer/nlohmannJSON/NlohmannJSONWriter.h" namespace armarx::aron::data { - /// Converter struct providing the needed call operators. - /// WriterImplementation is a writer class, TODO: add concept - template <class WriterImplementation, class DerivedT> - struct NlohmannJSONConverter : - virtual public Converter<aron::data::reader::NlohmannJSONReader, WriterImplementation, DerivedT> + // WriterImplementation is a writer class + template <class WriterImplementation> + requires isWriter<WriterImplementation> + struct FromNlohmannJSONConverter : + virtual public Converter<aron::data::reader::NlohmannJSONReader, WriterImplementation, FromNlohmannJSONConverter<WriterImplementation>> { - using Base = Converter<aron::data::reader::NlohmannJSONReader, WriterImplementation, DerivedT>; - - virtual ~NlohmannJSONConverter() = default; - - data::Descriptor getDescriptor(typename Base::ReaderInputType& n) final - { - return ConstNlohmannJSONVisitor::GetDescriptor(n); - } + virtual ~FromNlohmannJSONConverter() = default; + }; - void visitUnknown(typename Base::ReaderInputType& o) final - { - if (!this->r.readNull(o)) - { - throw error::AronException(__PRETTY_FUNCTION__, "A visitor got data but the enum is unknown."); - } - this->w.writeNull(); - } + // ReaderImplementation is a reader class + template <class ReaderImplementation> + requires isReader<ReaderImplementation> + struct ToNlohmannJSONConverter : + virtual public Converter<ReaderImplementation, aron::data::writer::NlohmannJSONWriter, ToNlohmannJSONConverter<ReaderImplementation>> + { + virtual ~ToNlohmannJSONConverter() = default; }; } diff --git a/source/RobotAPI/libraries/aron/core/data/converter/variant/VariantConverter.h b/source/RobotAPI/libraries/aron/core/data/converter/variant/VariantConverter.h index eb122beb0..47a0e9fa1 100644 --- a/source/RobotAPI/libraries/aron/core/data/converter/variant/VariantConverter.h +++ b/source/RobotAPI/libraries/aron/core/data/converter/variant/VariantConverter.h @@ -26,32 +26,26 @@ #include "../Converter.h" #include "../../visitor/variant/VariantVisitor.h" #include "../../rw/reader/variant/VariantReader.h" +#include "../../rw/writer/variant/VariantWriter.h" namespace armarx::aron::data { - /// Converter struct providing the needed call operators. - /// WriterImplementation is a writer class, TODO: add concept - template <class WriterImplementation, class DerivedT> - struct VariantConverter : - virtual public Converter<aron::data::reader::VariantReader, WriterImplementation, DerivedT> + // WriterImplementation is a writer class + template <class WriterImplementation> + requires isWriter<WriterImplementation> + struct FromVariantConverter : + virtual public Converter<aron::data::reader::VariantReader, WriterImplementation, FromVariantConverter<WriterImplementation>> { - using Base = Converter<aron::data::reader::VariantReader, WriterImplementation, DerivedT>; - - virtual ~VariantConverter() = default; - - data::Descriptor getDescriptor(typename Base::ReaderInputType& n) final - { - return ConstVariantVisitor::GetDescriptor(n); - } + virtual ~FromVariantConverter() = default; + }; - virtual void visitUnknown(typename Base::ReaderInputType& o) final - { - if (!this->r.readNull(o)) - { - throw error::AronException(__PRETTY_FUNCTION__, "A visitor got data but the enum is unknown."); - } - this->w.writeNull(); - } + // ReaderImplementation is a reader class + template <class ReaderImplementation> + requires isReader<ReaderImplementation> + struct ToVariantConverter : + virtual public Converter<ReaderImplementation, aron::data::writer::VariantWriter, ToVariantConverter<ReaderImplementation>> + { + virtual ~ToVariantConverter() = default; }; } diff --git a/source/RobotAPI/libraries/aron/core/data/rw/Reader.h b/source/RobotAPI/libraries/aron/core/data/rw/Reader.h index 3c14c097d..c486d26a5 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/Reader.h +++ b/source/RobotAPI/libraries/aron/core/data/rw/Reader.h @@ -41,6 +41,8 @@ namespace armarx::aron::data virtual ~ReaderInterface() = default; + virtual data::Descriptor getDescriptor(InputType& input) = 0; + virtual void readList(InputType& input, std::vector<InputTypeNonConst>& elements, Path&) = 0; virtual void readDict(InputType& input, std::map<std::string, InputTypeNonConst>& elements, Path&) = 0; diff --git a/source/RobotAPI/libraries/aron/core/data/rw/Writer.h b/source/RobotAPI/libraries/aron/core/data/rw/Writer.h index 657205c16..5972d3bfe 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/Writer.h +++ b/source/RobotAPI/libraries/aron/core/data/rw/Writer.h @@ -36,9 +36,12 @@ namespace armarx::aron::data { public: using ReturnType = R; + using ReturnTypeConst = typename std::add_const<ReturnType>::type; virtual ~WriterInterface() = default; + virtual data::Descriptor getDescriptor(ReturnTypeConst& input) = 0; + virtual ReturnType writeList(const std::vector<ReturnType>& elements, const Path& p) = 0; virtual ReturnType writeDict(const std::map<std::string, ReturnType>& elements, const std::optional<ReturnType>& extends, const Path& p) = 0; diff --git a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp index cece44b58..5e91dfaca 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp +++ b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp @@ -28,7 +28,7 @@ // ArmarX #include "../../json/Data.h" #include <RobotAPI/libraries/aron/core/Exception.h> - +#include <RobotAPI/libraries/aron/core/data/visitor/nlohmannJSON/NlohmannJSONVisitor.h> namespace armarx::aron::data::reader @@ -50,6 +50,11 @@ namespace armarx::aron::data::reader } } + data::Descriptor NlohmannJSONReader::getDescriptor(InputType& input) + { + return ConstNlohmannJSONVisitor::GetDescriptor(input); + } + void NlohmannJSONReader::readList(const nlohmann::json& input, std::vector<nlohmann::json>& elements, Path& p) { getAronMetaInformationForType(input, rw::json::constantes::LIST_TYPENAME_SLUG, p); diff --git a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.h b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.h index 575dee835..509aae5dd 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.h +++ b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.h @@ -35,10 +35,24 @@ namespace armarx::aron::data::reader class NlohmannJSONReader : public ReaderInterface<const nlohmann::json> { + using Base = ReaderInterface<const nlohmann::json>; + public: // constructors NlohmannJSONReader() = default; + data::Descriptor getDescriptor(InputType& input) final; + + using Base::readList; + using Base::readDict; + using Base::readNDArray; + using Base::readInt; + using Base::readLong; + using Base::readFloat; + using Base::readDouble; + using Base::readString; + using Base::readBool; + void readList(InputType& input, std::vector<InputTypeNonConst>& elements, Path& p) override; void readDict(InputType& input, std::map<std::string, InputTypeNonConst>& elements, Path& p) override; diff --git a/source/RobotAPI/libraries/aron/core/data/rw/reader/variant/VariantReader.cpp b/source/RobotAPI/libraries/aron/core/data/rw/reader/variant/VariantReader.cpp index 2eaea6536..cee7c8a1b 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/reader/variant/VariantReader.cpp +++ b/source/RobotAPI/libraries/aron/core/data/rw/reader/variant/VariantReader.cpp @@ -28,11 +28,16 @@ // ArmarX #include <RobotAPI/libraries/aron/core/Exception.h> #include <RobotAPI/libraries/aron/core/data/variant/All.h> - - +#include <RobotAPI/libraries/aron/core/data/visitor/variant/VariantVisitor.h> namespace armarx::aron::data::reader { + + data::Descriptor VariantReader::getDescriptor(InputType& input) + { + return ConstVariantVisitor::GetDescriptor(input); + } + void VariantReader::readList(const data::VariantPtr& input, std::vector<data::VariantPtr>& elements, Path& p) { ARMARX_CHECK_NOT_NULL(input); diff --git a/source/RobotAPI/libraries/aron/core/data/rw/reader/variant/VariantReader.h b/source/RobotAPI/libraries/aron/core/data/rw/reader/variant/VariantReader.h index 14d2b40b3..e92102233 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/reader/variant/VariantReader.h +++ b/source/RobotAPI/libraries/aron/core/data/rw/reader/variant/VariantReader.h @@ -36,20 +36,34 @@ namespace armarx::aron::data::reader class VariantReader : public ReaderInterface<const data::VariantPtr> { + using Base = ReaderInterface<const data::VariantPtr>; + public: // constructors VariantReader() = default; - void readList(InputType& input, std::vector<InputTypeNonConst>& elements, Path& p) override; - void readDict(InputType& input, std::map<std::string, InputTypeNonConst>& elements, Path& p) override; + using Base::readList; + using Base::readDict; + using Base::readNDArray; + using Base::readInt; + using Base::readLong; + using Base::readFloat; + using Base::readDouble; + using Base::readString; + using Base::readBool; + + data::Descriptor getDescriptor(InputType& input) final; + + void readList(InputType& input, std::vector<InputTypeNonConst>& elements, Path& p) final; + void readDict(InputType& input, std::map<std::string, InputTypeNonConst>& elements, Path& p) final; - void readNDArray(InputType& input, std::vector<int>& shape, std::string& typeAsString, std::vector<unsigned char>& data, Path& p) override; + void readNDArray(InputType& input, std::vector<int>& shape, std::string& typeAsString, std::vector<unsigned char>& data, Path& p) final; - void readInt(InputType& input, int& i, Path& p) override; - void readLong(InputType& input, long& i, Path& p) override; - void readFloat(InputType& input, float& i, Path& p) override; - void readDouble(InputType& input, double& i, Path& p) override; - void readString(InputType& input, std::string& s, Path& p) override; - void readBool(InputType& input, bool& i, Path& p) override; + void readInt(InputType& input, int& i, Path& p) final; + void readLong(InputType& input, long& i, Path& p) final; + void readFloat(InputType& input, float& i, Path& p) final; + void readDouble(InputType& input, double& i, Path& p) final; + void readString(InputType& input, std::string& s, Path& p) final; + void readBool(InputType& input, bool& i, Path& p) final; }; } diff --git a/source/RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.cpp b/source/RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.cpp index 55b7a5e32..c73819295 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.cpp +++ b/source/RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.cpp @@ -28,6 +28,7 @@ // ArmarX #include "../../json/Data.h" +#include <RobotAPI/libraries/aron/core/data/visitor/nlohmannJSON/NlohmannJSONVisitor.h> namespace armarx::aron::data::writer { @@ -41,6 +42,11 @@ namespace armarx::aron::data::writer } } + data::Descriptor NlohmannJSONWriter::getDescriptor(ReturnTypeConst& input) + { + return ConstNlohmannJSONVisitor::GetDescriptor(input); + } + nlohmann::json NlohmannJSONWriter::writeList(const std::vector<nlohmann::json>& elements, const Path& p) { nlohmann::json o; diff --git a/source/RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.h b/source/RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.h index fd281e5bc..05be3e9a6 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.h +++ b/source/RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.h @@ -39,6 +39,8 @@ namespace armarx::aron::data::writer public: NlohmannJSONWriter() = default; + data::Descriptor getDescriptor(ReturnTypeConst& input) final; + nlohmann::json writeList(const std::vector<nlohmann::json>& elements, const Path& p = Path()) override; nlohmann::json writeDict(const std::map<std::string, nlohmann::json>& elements, const std::optional<nlohmann::json>& extends = std::nullopt, const Path& p = Path()) override; diff --git a/source/RobotAPI/libraries/aron/core/data/rw/writer/variant/VariantWriter.cpp b/source/RobotAPI/libraries/aron/core/data/rw/writer/variant/VariantWriter.cpp index d7aaa4b0c..40098d2e5 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/writer/variant/VariantWriter.cpp +++ b/source/RobotAPI/libraries/aron/core/data/rw/writer/variant/VariantWriter.cpp @@ -27,11 +27,16 @@ // ArmarX #include <RobotAPI/libraries/aron/core/data/variant/All.h> - +#include <RobotAPI/libraries/aron/core/data/visitor/variant/VariantVisitor.h> namespace armarx::aron::data::writer { + data::Descriptor VariantWriter::getDescriptor(ReturnTypeConst& input) + { + return ConstVariantVisitor::GetDescriptor(input); + } + data::VariantPtr VariantWriter::writeList(const std::vector<data::VariantPtr>& elements, const Path& p) { auto o = std::make_shared<data::List>(p); diff --git a/source/RobotAPI/libraries/aron/core/data/rw/writer/variant/VariantWriter.h b/source/RobotAPI/libraries/aron/core/data/rw/writer/variant/VariantWriter.h index 9f46f49ac..4ff55335a 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/writer/variant/VariantWriter.h +++ b/source/RobotAPI/libraries/aron/core/data/rw/writer/variant/VariantWriter.h @@ -38,6 +38,8 @@ namespace armarx::aron::data::writer public: VariantWriter() = default; + data::Descriptor getDescriptor(ReturnTypeConst& input) final; + data::VariantPtr writeList(const std::vector<data::VariantPtr>& elements, const Path& p = Path()) override; data::VariantPtr writeDict(const std::map<std::string, data::VariantPtr>& elements, const std::optional<data::VariantPtr>& extends = std::nullopt, const Path& p = Path()) override; diff --git a/source/RobotAPI/libraries/aron/core/data/variant/container/Dict.cpp b/source/RobotAPI/libraries/aron/core/data/variant/container/Dict.cpp index 0e7de7dae..1745474f3 100644 --- a/source/RobotAPI/libraries/aron/core/data/variant/container/Dict.cpp +++ b/source/RobotAPI/libraries/aron/core/data/variant/container/Dict.cpp @@ -88,6 +88,12 @@ namespace armarx::aron::data { return getElement(s); } + Dict& Dict::operator=(const Dict& other) + { + this->aron = other.aron; + this->childrenNavigators = other.childrenNavigators; + return *this; + } // static methods DictPtr Dict::FromAronDictDTO(const data::dto::DictPtr& aron) @@ -144,12 +150,7 @@ namespace armarx::aron::data auto it = childrenNavigators.find(key); if (it == childrenNavigators.end()) { - std::string all_keys = ""; - for (const auto& child : this->getAllKeys()) - { - all_keys += child + ", "; - } - throw error::AronException(__PRETTY_FUNCTION__, "Could not find key '" + key + "'. But I found the following keys: [" + all_keys + "]", getPath()); + throw error::AronException(__PRETTY_FUNCTION__, "Could not find key '" + key + "'. But I found the following keys: [" + simox::alg::join(this->getAllKeys(), ", ") + "]", getPath()); } return it->second; } diff --git a/source/RobotAPI/libraries/aron/core/data/variant/container/Dict.h b/source/RobotAPI/libraries/aron/core/data/variant/container/Dict.h index 3c8f5a74d..f022d0264 100644 --- a/source/RobotAPI/libraries/aron/core/data/variant/container/Dict.h +++ b/source/RobotAPI/libraries/aron/core/data/variant/container/Dict.h @@ -52,6 +52,7 @@ namespace armarx::aron::data bool operator==(const Dict&) const override; bool operator==(const DictPtr&) const override; VariantPtr operator[](const std::string&) const; + Dict& operator=(const Dict&); static PointerType FromAronDictDTO(const data::dto::DictPtr& aron); static data::dto::DictPtr ToAronDictDTO(const PointerType& navigator); diff --git a/source/RobotAPI/libraries/aron/core/data/visitor/RecursiveVisitor.h b/source/RobotAPI/libraries/aron/core/data/visitor/RecursiveVisitor.h index df9896b90..3701db911 100644 --- a/source/RobotAPI/libraries/aron/core/data/visitor/RecursiveVisitor.h +++ b/source/RobotAPI/libraries/aron/core/data/visitor/RecursiveVisitor.h @@ -111,6 +111,7 @@ namespace armarx::aron::data virtual void visitBool(DataInput& elementData, TypeInput& elementType) {}; virtual void visitString(DataInput& elementData, TypeInput& elementType) {}; virtual void visitTime(DataInput& elementData, TypeInput& elementType) {}; + virtual void visitAnyObject(DataInput& elementData, TypeInput& elementType) {}; virtual void visitUnknown(DataInput& elementData, TypeInput& elementType) { throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor."); } @@ -270,6 +271,8 @@ namespace armarx::aron::data return v.visitBool(o, t); case type::Descriptor::eTime: return v.visitTime(o, t); + case type::Descriptor::eAnyObject: + return v.visitAnyObject(o, t); case type::Descriptor::eIntEnum: return v.visitIntEnum(o, t); case type::Descriptor::eUnknown: diff --git a/source/RobotAPI/libraries/aron/core/data/visitor/Visitor.h b/source/RobotAPI/libraries/aron/core/data/visitor/Visitor.h index dc9ae66d4..3df87f620 100644 --- a/source/RobotAPI/libraries/aron/core/data/visitor/Visitor.h +++ b/source/RobotAPI/libraries/aron/core/data/visitor/Visitor.h @@ -108,6 +108,7 @@ namespace armarx::aron::data virtual void visitBool(DataInput&, TypeInput&) {}; virtual void visitString(DataInput&, TypeInput&) {}; virtual void visitTime(DataInput&, TypeInput&) {}; + virtual void visitAnyObject(DataInput&, TypeInput&) {}; virtual void visitUnknown(DataInput&, TypeInput&) { throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor."); } virtual ~TypedVisitor() = default; }; @@ -204,6 +205,8 @@ namespace armarx::aron::data return v.visitBool(o, t); case type::Descriptor::eTime: return v.visitTime(o, t); + case type::Descriptor::eAnyObject: + return v.visitAnyObject(o, t); case type::Descriptor::eIntEnum: return v.visitIntEnum(o, t); case type::Descriptor::eUnknown: diff --git a/source/RobotAPI/libraries/aron/core/test/CMakeLists.txt b/source/RobotAPI/libraries/aron/core/test/CMakeLists.txt index 86d22c8ae..50aee382c 100644 --- a/source/RobotAPI/libraries/aron/core/test/CMakeLists.txt +++ b/source/RobotAPI/libraries/aron/core/test/CMakeLists.txt @@ -42,8 +42,8 @@ armarx_add_test( ArmarXCore RobotAPI::aron ARON_FILES - # xmls/BaseClass.xml - # xmls/DerivedClassTest.xml + aron/AnyTest.xml + aron/TemplateTest.xml aron/DictTest.xml aron/MatrixTest.xml aron/QuaternionTest.xml diff --git a/source/RobotAPI/libraries/aron/core/test/Randomizer.h b/source/RobotAPI/libraries/aron/core/test/Randomizer.h index bfc6328c2..20c30f94b 100644 --- a/source/RobotAPI/libraries/aron/core/test/Randomizer.h +++ b/source/RobotAPI/libraries/aron/core/test/Randomizer.h @@ -205,6 +205,7 @@ namespace armarx::aron switch (nextType) { + case type::Descriptor::eAnyObject: [[fallthrough]]; case type::Descriptor::eObject: { std::string objectName = util::generateRandomWord(); @@ -221,7 +222,7 @@ namespace armarx::aron elements.insert({key, m}); } - auto t = std::make_shared<type::Object>(objectName, elements); + auto t = std::make_shared<type::Object>(objectName, std::vector<std::string>(), std::vector<std::string>(), elements); return t; } case type::Descriptor::eIntEnum: @@ -561,6 +562,7 @@ namespace armarx::aron type::Descriptor desc = type->getDescriptor(); switch (desc) { + case type::Descriptor::eAnyObject: [[fallthrough]]; case type::Descriptor::eObject: { auto t = type::Object::DynamicCastAndCheck(type); diff --git a/source/RobotAPI/libraries/aron/core/test/aron/AnyTest.xml b/source/RobotAPI/libraries/aron/core/test/aron/AnyTest.xml new file mode 100644 index 000000000..b5ea28a22 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/test/aron/AnyTest.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<AronTypeDefinition> + <GenerateTypes> + <Object name='armarx::AnyTest'> + + <ObjectChild key='any_object'> + <AnyObject shared_ptr='true'/> + </ObjectChild> + + </Object> + </GenerateTypes> +</AronTypeDefinition> diff --git a/source/RobotAPI/libraries/aron/core/test/aron/BaseClassTest.xml b/source/RobotAPI/libraries/aron/core/test/aron/BaseClassTest.xml index af8a3451c..d8ca3c690 100644 --- a/source/RobotAPI/libraries/aron/core/test/aron/BaseClassTest.xml +++ b/source/RobotAPI/libraries/aron/core/test/aron/BaseClassTest.xml @@ -2,7 +2,7 @@ <?xml version="1.0" encoding="UTF-8" ?> <AronTypeDefinition> <GenerateTypes> - <Object name="InnerClass"> + <Object name="OtherClass"> <ObjectChild key="inner_class_member"> <Bool /> </ObjectChild> @@ -20,7 +20,7 @@ </ObjectChild> <ObjectChild key='base_class_member3'> - <InnerClass /> + <OtherClass /> </ObjectChild> </Object> diff --git a/source/RobotAPI/libraries/aron/core/test/aron/TemplateTest.xml b/source/RobotAPI/libraries/aron/core/test/aron/TemplateTest.xml new file mode 100644 index 000000000..304ae4193 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/test/aron/TemplateTest.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<AronTypeDefinition> + <GenerateTypes> + <Object name='armarx::WithoutTemplateTest'> + + <ObjectChild key='da_string'> + <String /> + </ObjectChild> + + </Object> + + <Object name='armarx::TemplateTest1' template="Blarg"> + + <ObjectChild key='da_blarg'> + <Blarg /> + </ObjectChild> + + </Object> + + <Object name='armarx::TemplateTest2' template="Foo, Bar"> + + <ObjectChild key='da_foo'> + <Foo /> + </ObjectChild> + + <ObjectChild key='da_bar'> + <Bar /> + </ObjectChild> + + <ObjectChild key='da_tmpl'> + <armarx::TemplateTest1 template="armarx::WithoutTemplateTest" /> + </ObjectChild> + + <ObjectChild key='da_int'> + <Int /> + </ObjectChild> + + </Object> + </GenerateTypes> +</AronTypeDefinition> diff --git a/source/RobotAPI/libraries/aron/core/test/aronCodeGenerationTest.cpp b/source/RobotAPI/libraries/aron/core/test/aronCodeGenerationTest.cpp index 592c50a0e..e62f3d3d7 100644 --- a/source/RobotAPI/libraries/aron/core/test/aronCodeGenerationTest.cpp +++ b/source/RobotAPI/libraries/aron/core/test/aronCodeGenerationTest.cpp @@ -42,6 +42,8 @@ #include <RobotAPI/libraries/aron/core/test/Randomizer.h> // Generated File +#include <RobotAPI/libraries/aron/core/test/aron/TemplateTest.aron.generated.h> +#include <RobotAPI/libraries/aron/core/test/aron/AnyTest.aron.generated.h> #include <RobotAPI/libraries/aron/core/test/aron/ListTest.aron.generated.h> #include <RobotAPI/libraries/aron/core/test/aron/DictTest.aron.generated.h> #include <RobotAPI/libraries/aron/core/test/aron/PrimitiveTest.aron.generated.h> @@ -59,6 +61,81 @@ using namespace armarx; using namespace aron; +BOOST_AUTO_TEST_CASE(AronCodeGenerationAnyTest) +{ + std::cout << "Running Code Gen Any test" << std::endl; + AnyTest p; + p.any_object = std::make_shared<aron::data::Dict>(aron::Path({"any_object"})); + p.any_object->addElement("hello_world", std::make_shared<aron::data::Int>(5)); + BOOST_CHECK_EQUAL(p.any_object->childrenSize(), 1); + + auto int_var = aron::data::Int::DynamicCastAndCheck(p.any_object->getElement("hello_world")); + BOOST_CHECK_EQUAL(int_var->getValue(), 5); + + auto aron = p.toAron(); + auto any_object_member = aron->getElement("any_object"); + auto any_object_member_casted = aron::data::Dict::DynamicCastAndCheck(any_object_member); + auto any_object_int_var = aron::data::Int::DynamicCastAndCheck(any_object_member_casted->getElement("hello_world")); + BOOST_CHECK_EQUAL(any_object_int_var->getValue(), 5); + + AnyTest p2; + p2.fromAron(aron); + auto int_var2 = aron::data::Int::DynamicCastAndCheck(p2.any_object->getElement("hello_world")); + BOOST_CHECK_EQUAL(int_var2->getValue(), 5); +} + +BOOST_AUTO_TEST_CASE(AronCodeGenerationTemplateTest) +{ + std::cout << "Running Code Gen Tmpl test" << std::endl; + TemplateTest2<WithoutTemplateTest, WithoutTemplateTest> t; + t.da_bar.da_string = "da_bar"; + t.da_foo.da_string = "da_foo"; + t.da_tmpl.da_blarg.da_string = "da_tmpl.da_blarg"; + t.da_int = 1234; + + auto aron = t.toAron(); + auto t_dabar = aron::data::Dict::DynamicCastAndCheck(aron->getElement("da_bar")); + auto t_dafoo = aron::data::Dict::DynamicCastAndCheck(aron->getElement("da_foo")); + auto t_daimpl = aron::data::Dict::DynamicCastAndCheck(aron->getElement("da_tmpl")); + auto t_daint = aron::data::Int::DynamicCastAndCheck(aron->getElement("da_int")); + BOOST_CHECK_EQUAL(t_daint->getValue(), 1234); + + auto t_dabar_dastring = aron::data::String::DynamicCastAndCheck(t_dabar->getElement("da_string")); + BOOST_CHECK_EQUAL(t_dabar_dastring->getValue(), "da_bar"); + auto t_dafoo_dastring = aron::data::String::DynamicCastAndCheck(t_dafoo->getElement("da_string")); + BOOST_CHECK_EQUAL(t_dafoo_dastring->getValue(), "da_foo"); + auto t_daimpl_dablarg = aron::data::Dict::DynamicCastAndCheck(t_daimpl->getElement("da_blarg")); + + auto t_daimpl_dablarg_dastring = aron::data::String::DynamicCastAndCheck(t_daimpl_dablarg->getElement("da_string")); + BOOST_CHECK_EQUAL(t_daimpl_dablarg_dastring->getValue(), "da_tmpl.da_blarg"); + + auto aronT = TemplateTest2<WithoutTemplateTest, WithoutTemplateTest>::ToAronType(); + BOOST_CHECK_EQUAL(aronT->getObjectName(), "armarx::TemplateTest2"); + BOOST_CHECK_EQUAL(aronT->getTemplates(), std::vector<std::string>({"Foo", "Bar"})); + BOOST_CHECK_EQUAL(aronT->getTemplateInstantiations(), std::vector<std::string>{}); + + + auto tt_dabar = aron::type::Object::DynamicCastAndCheck(aronT->getMemberType("da_bar")); + auto tt_dafoo = aron::type::Object::DynamicCastAndCheck(aronT->getMemberType("da_foo")); + auto tt_datmpl = aron::type::Object::DynamicCastAndCheck(aronT->getMemberType("da_tmpl")); + BOOST_CHECK_EQUAL(tt_dabar->getObjectName(), "armarx::WithoutTemplateTest"); + BOOST_CHECK_EQUAL(tt_dafoo->getObjectName(), "armarx::WithoutTemplateTest"); + BOOST_CHECK_EQUAL(tt_datmpl->getObjectName(), "armarx::TemplateTest1"); + + BOOST_CHECK_EQUAL(tt_dabar->getTemplates(), std::vector<std::string>{}); + BOOST_CHECK_EQUAL(tt_dafoo->getTemplates(), std::vector<std::string>{}); + BOOST_CHECK_EQUAL(tt_datmpl->getTemplates(), std::vector<std::string>({"Blarg"})); + + BOOST_CHECK_EQUAL(tt_dabar->getTemplateInstantiations(), std::vector<std::string>{}); + BOOST_CHECK_EQUAL(tt_dafoo->getTemplateInstantiations(), std::vector<std::string>{}); + BOOST_CHECK_EQUAL(tt_datmpl->getTemplateInstantiations(), std::vector<std::string>{"armarx::WithoutTemplateTest"}); + + auto tt_daimpl_dablarg = aron::type::Object::DynamicCastAndCheck(tt_datmpl->getMemberType("da_blarg")); + BOOST_CHECK_EQUAL(tt_daimpl_dablarg->getObjectName(), "armarx::WithoutTemplateTest"); + BOOST_CHECK_EQUAL(tt_daimpl_dablarg->getTemplates(), std::vector<std::string>{}); + BOOST_CHECK_EQUAL(tt_daimpl_dablarg->getTemplateInstantiations(), std::vector<std::string>{}); +} + BOOST_AUTO_TEST_CASE(AronCodeGenerationListTest) { std::cout << "Running Code Gen List test" << std::endl; @@ -191,7 +268,7 @@ BOOST_AUTO_TEST_CASE(AronCodeGenerationOptionalTest) BOOST_CHECK_EQUAL(typeid(p.some_string) == typeid(std::optional<std::string>), true); - auto aronType = p.toAronType(); + auto aronType = p.ToAronType(); BOOST_CHECK_EQUAL(aronType->getMemberType("some_float")->getMaybe() == aron::type::Maybe::eOptional, true); BOOST_CHECK_EQUAL(p.some_float.has_value() == false, true); diff --git a/source/RobotAPI/libraries/aron/core/test/aronExtendsTest.cpp b/source/RobotAPI/libraries/aron/core/test/aronExtendsTest.cpp index d6dd46a4d..81a11bda2 100644 --- a/source/RobotAPI/libraries/aron/core/test/aronExtendsTest.cpp +++ b/source/RobotAPI/libraries/aron/core/test/aronExtendsTest.cpp @@ -52,10 +52,10 @@ BOOST_AUTO_TEST_CASE(AronExtendsTest) { std::cout << "Aron extends test" << std::endl; BaseClassTest base; - auto baseType = base.toAronType(); + auto baseType = base.ToAronType(); DerivedClassTest derived; - auto derivedType = derived.toAronType(); + auto derivedType = derived.ToAronType(); for (const auto& [key, value] : baseType->getMemberTypes()) { diff --git a/source/RobotAPI/libraries/aron/core/test/aronRandomizedTest.cpp b/source/RobotAPI/libraries/aron/core/test/aronRandomizedTest.cpp index 33bb9c320..cf7821c55 100644 --- a/source/RobotAPI/libraries/aron/core/test/aronRandomizedTest.cpp +++ b/source/RobotAPI/libraries/aron/core/test/aronRandomizedTest.cpp @@ -98,14 +98,14 @@ using namespace aron; template <typename T> -void test_toAronType(T& in, T& out) +void test_ToAronType(T& in, T& out) { nlohmann::json in_type_json; nlohmann::json out_type_json; BOOST_TEST_CONTEXT("getting in type") { - type::ObjectPtr in_type_nav = in.toAronType(); + type::ObjectPtr in_type_nav = in.ToAronType(); type::dto::AronObjectPtr in_type = in_type_nav->toAronObjectDTO(); BOOST_CHECK(in_type); @@ -118,7 +118,7 @@ void test_toAronType(T& in, T& out) BOOST_TEST_CONTEXT("getting out type") { - type::ObjectPtr out_type_nav = out.toAronType(); + type::ObjectPtr out_type_nav = out.ToAronType(); type::dto::AronObjectPtr out_type = out_type_nav->toAronObjectDTO(); BOOST_CHECK(out_type); @@ -142,7 +142,7 @@ void test_toAron(T& in, T& out) data::DictPtr in_aron_nav = in.toAron(); BOOST_TEST_CONTEXT("initialize in aron randomly") { - type::ObjectPtr in_type_nav = in.toAronType(); + type::ObjectPtr in_type_nav = in.ToAronType(); r.initializeRandomly(in_aron_nav, *in_type_nav); } @@ -187,7 +187,7 @@ void test_toJson(T& in, T& out) data::DictPtr in_aron_nav = in.toAron(); { - type::ObjectPtr in_type_nav = in.toAronType(); + type::ObjectPtr in_type_nav = in.ToAronType(); r.initializeRandomly(in_aron_nav, *in_type_nav); } @@ -240,7 +240,7 @@ template <typename T> void runTestWithInstances(T& in, T& out) { // assumes not nullptrs as in and out. If you have a maybe type then make sure that it is set properly - test_toAronType(in, out); + test_ToAronType(in, out); test_toAron(in, out); #if 0 test_toJson(in, out); diff --git a/source/RobotAPI/libraries/aron/core/type/converter/Converter.h b/source/RobotAPI/libraries/aron/core/type/converter/Converter.h index cc7ab4a23..a0962322d 100644 --- a/source/RobotAPI/libraries/aron/core/type/converter/Converter.h +++ b/source/RobotAPI/libraries/aron/core/type/converter/Converter.h @@ -60,21 +60,28 @@ namespace armarx::aron::type virtual ~Converter() = default; + type::Descriptor getDescriptor(ReaderInputType& o) final + { + return r.getDescriptor(o); + } + void visitObject(ReaderInputType& o) final { std::map<std::string, ReaderInputTypeNonConst> elementsOfInput; std::string name; + std::vector<std::string> templates; + std::vector<std::string> templateInstantiations; type::Maybe maybe; std::map<std::string, WriterReturnType> elementsReturn; Path p; - r.readObject(o, name, elementsOfInput, maybe, p); + r.readObject(o, name, templates, templateInstantiations, elementsOfInput, maybe, p); for (const auto& [key, value] : elementsOfInput) { auto converted = readAndWrite<DerivedT>(value); elementsReturn.insert({key, converted}); } - last_returned = w.writeObject(name, elementsReturn, std::nullopt, maybe, p); + last_returned = w.writeObject(name, templates, templateInstantiations, elementsReturn, std::nullopt, maybe, p); } void visitDict(ReaderInputType& o) final @@ -269,6 +276,15 @@ namespace armarx::aron::type r.readTime(o, maybe, p); last_returned = w.writeTime(maybe, p); }; + + void visitUnknown(ReaderInputType& o) final + { + if (!r.readNull(o)) + { + throw error::AronException(__PRETTY_FUNCTION__, "A visitor got type but the enum is unknown."); + } + w.writeNull(); + } }; /// the function to read from a variant and write to a writer T diff --git a/source/RobotAPI/libraries/aron/core/type/converter/nlohmannJSON/NlohmannJSONConverter.h b/source/RobotAPI/libraries/aron/core/type/converter/nlohmannJSON/NlohmannJSONConverter.h index dd59bbd3f..ba64ac905 100644 --- a/source/RobotAPI/libraries/aron/core/type/converter/nlohmannJSON/NlohmannJSONConverter.h +++ b/source/RobotAPI/libraries/aron/core/type/converter/nlohmannJSON/NlohmannJSONConverter.h @@ -26,31 +26,25 @@ #include "../Converter.h" #include "../../visitor/nlohmannJSON/NlohmannJSONVisitor.h" #include "../../rw/reader/nlohmannJSON/NlohmannJSONReader.h" +#include "../../rw/writer/nlohmannJSON/NlohmannJSONWriter.h" namespace armarx::aron::type { - /// Converter struct providing the needed call operators. - /// WriterImplementation is a writer class, TODO: add concept - template <class WriterImplementation, class DerivedT> - struct NlohmannJSONConverter : - virtual public Converter<aron::type::reader::NlohmannJSONReader, WriterImplementation, DerivedT> + // WriterImplementation is a writer class + template <class WriterImplementation> + requires isWriter<WriterImplementation> + struct FromNlohmannJSONConverter : + virtual public Converter<aron::type::reader::NlohmannJSONReader, WriterImplementation, FromNlohmannJSONConverter<WriterImplementation>> { - using Base = Converter<aron::type::reader::NlohmannJSONReader, WriterImplementation, DerivedT>; - - virtual ~NlohmannJSONConverter() = default; - - type::Descriptor getDescriptor(typename Base::ReaderInputType& n) final - { - return ConstNlohmannJSONVisitor::GetDescriptor(n); - } + virtual ~FromNlohmannJSONConverter() = default; + }; - void visitUnknown(typename Base::ReaderInputType& o) final - { - if (!this->r.readNull(o)) - { - throw error::AronException(__PRETTY_FUNCTION__, "A visitor got data but the enum is unknown."); - } - this->w.writeNull(); - } + // WriterImplementation is a reader class + template <class ReaderImplementation> + requires isReader<ReaderImplementation> + struct ToNlohmannJSONConverter : + virtual public Converter<ReaderImplementation, aron::type::writer::NlohmannJSONWriter, ToNlohmannJSONConverter<ReaderImplementation>> + { + virtual ~ToNlohmannJSONConverter() = default; }; } diff --git a/source/RobotAPI/libraries/aron/core/type/converter/variant/VariantConverter.h b/source/RobotAPI/libraries/aron/core/type/converter/variant/VariantConverter.h index 69a563dc1..bd81fa777 100644 --- a/source/RobotAPI/libraries/aron/core/type/converter/variant/VariantConverter.h +++ b/source/RobotAPI/libraries/aron/core/type/converter/variant/VariantConverter.h @@ -26,32 +26,27 @@ #include "../Converter.h" #include "../../visitor/variant/VariantVisitor.h" #include "../../rw/reader/variant/VariantReader.h" +#include "../../rw/writer/variant/VariantWriter.h" namespace armarx::aron::type { - /// Converter struct providing the needed call operators. - /// WriterImplementation is a writer class, TODO: add concept - template <class WriterImplementation, class DerivedT> - struct VariantConverter : - virtual public Converter<aron::type::reader::VariantReader, WriterImplementation, DerivedT> - { - using Base = Converter<aron::type::reader::VariantReader, WriterImplementation, DerivedT>; - - virtual ~VariantConverter() = default; - type::Descriptor getDescriptor(typename Base::ReaderInputType& n) final - { - return ConstVariantVisitor::GetDescriptor(n); - } + // WriterImplementation is a writer class + template <class WriterImplementation> + requires isWriter<WriterImplementation> + struct FromVariantConverter : + virtual public Converter<aron::type::reader::VariantReader, WriterImplementation, FromVariantConverter<WriterImplementation>> + { + virtual ~FromVariantConverter() = default; + }; - virtual void visitUnknown(typename Base::ReaderInputType& o) final - { - if (!this->r.readNull(o)) - { - throw error::AronException(__PRETTY_FUNCTION__, "A visitor got type but the enum is unknown."); - } - this->w.writeNull(); - } + // WriterImplementation is a reader class + template <class ReaderImplementation> + requires isReader<ReaderImplementation> + struct ToVariantConverter : + virtual public Converter<ReaderImplementation, aron::type::writer::VariantWriter, ToVariantConverter<ReaderImplementation>> + { + virtual ~ToVariantConverter() = default; }; } diff --git a/source/RobotAPI/libraries/aron/core/type/rw/Reader.h b/source/RobotAPI/libraries/aron/core/type/rw/Reader.h index 53c0ae381..b59a20157 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/Reader.h +++ b/source/RobotAPI/libraries/aron/core/type/rw/Reader.h @@ -43,8 +43,10 @@ namespace armarx::aron::type virtual ~ReaderInterface() = default; + virtual type::Descriptor getDescriptor(InputType& input) = 0; + /// Extract information from an Object type - virtual void readObject(const InputType& input, std::string& name, std::map<std::string, InputTypeNonConst>& memberTypes, type::Maybe& maybe, Path& p) = 0; + virtual void readObject(const InputType& input, std::string& name, std::vector<std::string>& templates, std::vector<std::string>& templateInstantiations, std::map<std::string, InputTypeNonConst>& memberTypes, type::Maybe& maybe, Path& p) = 0; /// Extract information from a list type virtual void readList(const InputType& input, InputTypeNonConst& acceptedType, type::Maybe& maybe, Path& p) = 0; @@ -106,6 +108,9 @@ namespace armarx::aron::type /// Extract information from an time type virtual void readTime(const InputType& input, type::Maybe& maybe, Path& p) = 0; + /// Extract information from an time type + virtual void readAnyObject(const InputType& input, type::Maybe& maybe, Path& p) = 0; + /// Check if input is null virtual bool readNull(InputType& input) // defaulted implementation { diff --git a/source/RobotAPI/libraries/aron/core/type/rw/Writer.h b/source/RobotAPI/libraries/aron/core/type/rw/Writer.h index 528ed3206..c0eb1d8bf 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/Writer.h +++ b/source/RobotAPI/libraries/aron/core/type/rw/Writer.h @@ -40,11 +40,14 @@ namespace armarx::aron::type { public: using ReturnType = R; + using ReturnTypeConst = typename std::add_const<ReturnType>::type; virtual ~WriterInterface() = default; + virtual type::Descriptor getDescriptor(ReturnTypeConst& input) = 0; + /// Construct an object from the params - virtual ReturnType writeObject(const std::string& name, const std::map<std::string, ReturnType>& memberTypes, const std::optional<ReturnType>& extends, const type::Maybe maybe, const Path& p) = 0; + virtual ReturnType writeObject(const std::string& name, const std::vector<std::string>& templates, const std::vector<std::string>& templateInstantiations, const std::map<std::string, ReturnType>& memberTypes, const std::optional<ReturnType>& extends, const type::Maybe maybe, const Path& p) = 0; /// Construct a list from the params virtual ReturnType writeList(const ReturnType& acceptedType, const type::Maybe maybe, const Path& p) = 0; @@ -106,6 +109,9 @@ namespace armarx::aron::type /// Construct a time from the params virtual ReturnType writeTime(const type::Maybe maybe, const Path& p) = 0; + /// Construct a time from the params + virtual ReturnType writeAnyObject(const type::Maybe maybe, const Path& p) = 0; + /// write a null virtual ReturnType writeNull(const Path& p = Path()) // defaulted implementation { diff --git a/source/RobotAPI/libraries/aron/core/type/rw/json/Data.h b/source/RobotAPI/libraries/aron/core/type/rw/json/Data.h index fbaff7c10..2bbec89c4 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/json/Data.h +++ b/source/RobotAPI/libraries/aron/core/type/rw/json/Data.h @@ -50,6 +50,8 @@ namespace armarx::aron::type::rw::json const std::string DIMENSIONS_SLUG = "_ARON_DIMESIONS"; const std::string DATA_SLUG = "_ARON_DATA"; const std::string USED_TYPE_SLUG = "_ARON_USED_TYPE"; + const std::string TEMPLATES_SLUG = "_ARON_TEMPLATES"; + const std::string TEMPLATE_INSTANTIATIONS_SLUG = "_ARON_TEMPLATE_INSTANTIATION"; const std::string LIST_TYPENAME_SLUG = "_ARON_LIST"; const std::string DICT_TYPENAME_SLUG = "_ARON_DICT"; @@ -72,6 +74,7 @@ namespace armarx::aron::type::rw::json const std::string STRING_TYPENAME_SLUG = "_ARON_STRING"; const std::string BOOL_TYPENAME_SLUG = "_ARON_BOOL"; const std::string TIME_TYPENAME_SLUG = "_ARON_TIME"; + const std::string ANY_OBJECT_TYPENAME_SLUG = "_ARON_ANY_OBJECT"; } namespace conversion @@ -97,7 +100,8 @@ namespace armarx::aron::type::rw::json {type::Descriptor::eDouble, rw::json::constantes::DOUBLE_TYPENAME_SLUG}, {type::Descriptor::eBool, rw::json::constantes::BOOL_TYPENAME_SLUG}, {type::Descriptor::eString, rw::json::constantes::STRING_TYPENAME_SLUG}, - {type::Descriptor::eTime, rw::json::constantes::TIME_TYPENAME_SLUG} + {type::Descriptor::eTime, rw::json::constantes::TIME_TYPENAME_SLUG}, + {type::Descriptor::eAnyObject, rw::json::constantes::ANY_OBJECT_TYPENAME_SLUG} }; const auto String2Descriptor = aron::conversion::util::InvertMap(Descriptor2String); diff --git a/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp b/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp index 2ae071ed8..246a3698a 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp +++ b/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp @@ -28,6 +28,7 @@ // ArmarX #include <RobotAPI/libraries/aron/core/Exception.h> #include "../../json/Data.h" +#include <RobotAPI/libraries/aron/core/type/visitor/nlohmannJSON/NlohmannJSONVisitor.h> namespace armarx::aron::type::reader { @@ -47,11 +48,25 @@ namespace armarx::aron::type::reader } } - void NlohmannJSONReader::readObject(const nlohmann::json& input, std::string& name, std::map<std::string, nlohmann::json>& memberTypes, type::Maybe& maybe, Path& p) + type::Descriptor NlohmannJSONReader::getDescriptor(InputType& input) + { + return ConstNlohmannJSONVisitor::GetDescriptor(input); + } + + void NlohmannJSONReader::readObject(const nlohmann::json& input, std::string& name, std::vector<std::string>& templates, std::vector<std::string>& templateInstantiations, std::map<std::string, nlohmann::json>& memberTypes, type::Maybe& maybe, Path& p) { getAronMetaInformationForType(input, rw::json::constantes::OBJECT_TYPENAME_SLUG, p); name = input[rw::json::constantes::NAME_SLUG]; + if (input.count(rw::json::constantes::TEMPLATES_SLUG)) + { + templates = input[rw::json::constantes::TEMPLATES_SLUG].get<std::vector<std::string>>(); + } + if (input.count(rw::json::constantes::TEMPLATE_INSTANTIATIONS_SLUG)) + { + templateInstantiations = input[rw::json::constantes::TEMPLATE_INSTANTIATIONS_SLUG].get<std::vector<std::string>>(); + } + maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]); memberTypes = input[rw::json::constantes::MEMBERS_SLUG].get<std::map<std::string, nlohmann::json>>(); @@ -213,4 +228,11 @@ namespace armarx::aron::type::reader maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]); } + + void NlohmannJSONReader::readAnyObject(const nlohmann::json& input, type::Maybe& maybe, Path& p) + { + getAronMetaInformationForType(input, rw::json::constantes::ANY_OBJECT_TYPENAME_SLUG, p); + + maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]); + } } diff --git a/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.h b/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.h index 4666244c2..6b8ea0ef0 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.h +++ b/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.h @@ -39,7 +39,9 @@ namespace armarx::aron::type::reader // constructors NlohmannJSONReader() = default; - void readObject(InputType& input, std::string& name, std::map<std::string, InputTypeNonConst>& memberTypes, type::Maybe& maybe, Path& p) override; + type::Descriptor getDescriptor(InputType& input) final; + + void readObject(InputType& input, std::string& name, std::vector<std::string>& templates, std::vector<std::string>& templateInstantiations, std::map<std::string, InputTypeNonConst>& memberTypes, type::Maybe& maybe, Path& p) override; void readList(InputType& input, InputTypeNonConst& acceptedType, type::Maybe& maybe, Path& p) override; void readDict(InputType& input, InputTypeNonConst& acceptedType, type::Maybe& maybe, Path& p) override; void readTuple(InputType& input, std::vector<InputTypeNonConst>& acceptedTypes, type::Maybe& maybe, Path& p) override; @@ -63,5 +65,7 @@ namespace armarx::aron::type::reader void readString(InputType& input, type::Maybe& maybe, Path& p) override; void readBool(InputType& input, type::Maybe& maybe, Path& p) override; void readTime(InputType& input, type::Maybe& maybe, Path& p) override; + + void readAnyObject(InputType& input, type::Maybe& maybe, Path& p) override; }; } diff --git a/source/RobotAPI/libraries/aron/core/type/rw/reader/variant/VariantReader.cpp b/source/RobotAPI/libraries/aron/core/type/rw/reader/variant/VariantReader.cpp index 26c6180ab..dae6ba15d 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/reader/variant/VariantReader.cpp +++ b/source/RobotAPI/libraries/aron/core/type/rw/reader/variant/VariantReader.cpp @@ -28,16 +28,24 @@ // ArmarX #include <RobotAPI/libraries/aron/core/Exception.h> #include <RobotAPI/libraries/aron/core/type/variant/All.h> - +#include <RobotAPI/libraries/aron/core/type/visitor/variant/VariantVisitor.h> namespace armarx::aron::type::reader { - void VariantReader::readObject(const aron::type::VariantPtr& input, std::string& name, std::map<std::string, aron::type::VariantPtr>& memberTypes, type::Maybe& maybe, Path& p) + + type::Descriptor VariantReader::getDescriptor(InputType& input) + { + return ConstVariantVisitor::GetDescriptor(input); + } + + void VariantReader::readObject(const aron::type::VariantPtr& input, std::string& name, std::vector<std::string>& templates, std::vector<std::string>& templateInstantiations, std::map<std::string, aron::type::VariantPtr>& memberTypes, type::Maybe& maybe, Path& p) { ARMARX_CHECK_NOT_NULL(input); auto o = type::Object::DynamicCastAndCheck(input); name = o->getObjectName(); + templates = o->getTemplates(); + templateInstantiations = o->getTemplateInstantiations(); maybe = o->getMaybe(); memberTypes = o->getMemberTypes(); p = o->getPath(); @@ -236,4 +244,13 @@ namespace armarx::aron::type::reader maybe = o->getMaybe(); p = o->getPath(); } + + void VariantReader::readAnyObject(const aron::type::VariantPtr& input, type::Maybe& maybe, Path& p) + { + ARMARX_CHECK_NOT_NULL(input); + auto o = type::AnyObject::DynamicCastAndCheck(input); + + maybe = o->getMaybe(); + p = o->getPath(); + } } diff --git a/source/RobotAPI/libraries/aron/core/type/rw/reader/variant/VariantReader.h b/source/RobotAPI/libraries/aron/core/type/rw/reader/variant/VariantReader.h index ec2230698..104a4643b 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/reader/variant/VariantReader.h +++ b/source/RobotAPI/libraries/aron/core/type/rw/reader/variant/VariantReader.h @@ -40,7 +40,9 @@ namespace armarx::aron::type::reader // constructors VariantReader() = default; - void readObject(InputType& input, std::string& name, std::map<std::string, InputTypeNonConst>& memberTypes, type::Maybe& maybe, Path& p) override; + type::Descriptor getDescriptor(InputType& input) final; + + void readObject(InputType& input, std::string& name, std::vector<std::string>& templates, std::vector<std::string>& templateInstantiations, std::map<std::string, InputTypeNonConst>& memberTypes, type::Maybe& maybe, Path& p) override; void readList(InputType& input, InputTypeNonConst& acceptedType, type::Maybe& maybe, Path& p) override; void readDict(InputType& input, InputTypeNonConst& acceptedType, type::Maybe& maybe, Path& p) override; void readTuple(InputType& input, std::vector<InputTypeNonConst>& acceptedTypes, type::Maybe& maybe, Path& p) override; @@ -64,5 +66,7 @@ namespace armarx::aron::type::reader void readString(InputType& input, type::Maybe& maybe, Path& p) override; void readBool(InputType& input, type::Maybe& maybe, Path& p) override; void readTime(InputType& input, type::Maybe& maybe, Path& p) override; + + void readAnyObject(InputType& input, type::Maybe& maybe, Path& p) override; }; } diff --git a/source/RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.cpp b/source/RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.cpp index 303ee55bc..9a67e3b25 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.cpp +++ b/source/RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.cpp @@ -26,6 +26,8 @@ // Constantes #include "../../json/Data.h" +#include <RobotAPI/libraries/aron/core/type/visitor/nlohmannJSON/NlohmannJSONVisitor.h> + namespace armarx::aron::type::writer { @@ -40,7 +42,12 @@ namespace armarx::aron::type::writer } } - nlohmann::json NlohmannJSONWriter::writeObject(const std::string& name, const std::map<std::string, nlohmann::json>& memberTypes, const std::optional<nlohmann::json>& extends, const type::Maybe maybe, const Path& p) + type::Descriptor NlohmannJSONWriter::getDescriptor(ReturnTypeConst& input) + { + return ConstNlohmannJSONVisitor::GetDescriptor(input); + } + + nlohmann::json NlohmannJSONWriter::writeObject(const std::string& name, const std::vector<std::string>& templates, const std::vector<std::string>& templateInstantiations, const std::map<std::string, nlohmann::json>& memberTypes, const std::optional<nlohmann::json>& extends, const type::Maybe maybe, const Path& p) { nlohmann::json o; setupAronMetaInformationForType(o, rw::json::constantes::OBJECT_TYPENAME_SLUG, maybe, p); @@ -49,6 +56,14 @@ namespace armarx::aron::type::writer { o[rw::json::constantes::EXTENDS_SLUG] = *extends; } + if (!templates.empty()) + { + o[rw::json::constantes::TEMPLATES_SLUG] = templates; + } + if (!templateInstantiations.empty()) + { + o[rw::json::constantes::TEMPLATE_INSTANTIATIONS_SLUG] = templateInstantiations; + } o[rw::json::constantes::MEMBERS_SLUG] = nlohmann::json(nlohmann::json::value_t::object); for (const auto& [key, value] : memberTypes) @@ -215,4 +230,11 @@ namespace armarx::aron::type::writer setupAronMetaInformationForType(o, rw::json::constantes::TIME_TYPENAME_SLUG, maybe, p); return o; } + + nlohmann::json NlohmannJSONWriter::writeAnyObject(const type::Maybe maybe, const Path& p) + { + nlohmann::json o; + setupAronMetaInformationForType(o, rw::json::constantes::ANY_OBJECT_TYPENAME_SLUG, maybe, p); + return o; + } } diff --git a/source/RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.h b/source/RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.h index ce15b2932..54570c30b 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.h +++ b/source/RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.h @@ -39,7 +39,9 @@ namespace armarx::aron::type::writer public: NlohmannJSONWriter() = default; - ReturnType writeObject(const std::string& name, const std::map<std::string, ReturnType>& memberTypes, const std::optional<ReturnType>& extends, const type::Maybe maybe, const Path& p = Path()) override; + type::Descriptor getDescriptor(ReturnTypeConst& input) final; + + ReturnType writeObject(const std::string& name, const std::vector<std::string>& templates, const std::vector<std::string>& templateInstantiations, const std::map<std::string, ReturnType>& memberTypes, const std::optional<ReturnType>& extends, const type::Maybe maybe, const Path& p = Path()) override; ReturnType writeList(const ReturnType& acceptedType, const type::Maybe maybe, const Path& p = Path()) override; ReturnType writeDict(const ReturnType& acceptedType, const type::Maybe maybe, const Path& p = Path()) override; ReturnType writePair(const ReturnType& acceptedType1, const ReturnType& acceptedType2, const type::Maybe maybe, const Path& p = Path()) override; @@ -63,5 +65,7 @@ namespace armarx::aron::type::writer ReturnType writeString(const type::Maybe maybe, const Path& p = Path()) override; ReturnType writeBool(const type::Maybe maybe, const Path& p = Path()) override; ReturnType writeTime(const type::Maybe maybe, const Path& p = Path()) override; + + ReturnType writeAnyObject(const type::Maybe maybe, const Path& p = Path()) override; }; } diff --git a/source/RobotAPI/libraries/aron/core/type/rw/writer/variant/VariantWriter.cpp b/source/RobotAPI/libraries/aron/core/type/rw/writer/variant/VariantWriter.cpp index 8c16a0c86..b96493423 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/writer/variant/VariantWriter.cpp +++ b/source/RobotAPI/libraries/aron/core/type/rw/writer/variant/VariantWriter.cpp @@ -24,14 +24,19 @@ #include <memory> #include <numeric> - +#include <RobotAPI/libraries/aron/core/type/visitor/variant/VariantVisitor.h> namespace armarx::aron::type::writer { - // interface - aron::type::VariantPtr VariantWriter::writeObject(const std::string& name, const std::map<std::string, aron::type::VariantPtr>& memberTypes, const std::optional<aron::type::VariantPtr>& extends, const type::Maybe maybe, const Path& p) + + type::Descriptor VariantWriter::getDescriptor(ReturnTypeConst& input) + { + return ConstVariantVisitor::GetDescriptor(input); + } + + aron::type::VariantPtr VariantWriter::writeObject(const std::string& name, const std::vector<std::string>& templates, const std::vector<std::string>& templateInstantiations, const std::map<std::string, aron::type::VariantPtr>& memberTypes, const std::optional<aron::type::VariantPtr>& extends, const type::Maybe maybe, const Path& p) { - auto o = std::make_shared<type::Object>(name, memberTypes, p); + auto o = std::make_shared<type::Object>(name, templates, templateInstantiations, memberTypes, p); o->setMaybe(maybe); if (extends.has_value()) { @@ -185,4 +190,11 @@ namespace armarx::aron::type::writer o->setMaybe(maybe); return o; } + + aron::type::VariantPtr VariantWriter::writeAnyObject(const type::Maybe maybe, const Path& p) + { + auto o = std::make_shared<type::AnyObject>(p); + o->setMaybe(maybe); + return o; + } } diff --git a/source/RobotAPI/libraries/aron/core/type/rw/writer/variant/VariantWriter.h b/source/RobotAPI/libraries/aron/core/type/rw/writer/variant/VariantWriter.h index daf3b9944..9f2e15cdd 100644 --- a/source/RobotAPI/libraries/aron/core/type/rw/writer/variant/VariantWriter.h +++ b/source/RobotAPI/libraries/aron/core/type/rw/writer/variant/VariantWriter.h @@ -35,7 +35,9 @@ namespace armarx::aron::type::writer public: VariantWriter() = default; - ReturnType writeObject(const std::string& name, const std::map<std::string, ReturnType>& memberTypes, const std::optional<ReturnType>& extends, const type::Maybe maybe, const Path& p = Path()) override; + type::Descriptor getDescriptor(ReturnTypeConst& input) final; + + ReturnType writeObject(const std::string& name, const std::vector<std::string>& templates, const std::vector<std::string>& templateInstantiations, const std::map<std::string, ReturnType>& memberTypes, const std::optional<ReturnType>& extends, const type::Maybe maybe, const Path& p = Path()) override; ReturnType writeList(const ReturnType& acceptedType, const type::Maybe maybe, const Path& p = Path()) override; ReturnType writeDict(const ReturnType& acceptedType, const type::Maybe maybe, const Path& p = Path()) override; ReturnType writePair(const ReturnType& acceptedType1, const ReturnType& acceptedType2, const type::Maybe maybe, const Path& p = Path()) override; @@ -59,5 +61,7 @@ namespace armarx::aron::type::writer ReturnType writeString(const type::Maybe maybe, const Path& p = Path()) override; ReturnType writeBool(const type::Maybe maybe, const Path& p = Path()) override; ReturnType writeTime(const type::Maybe maybe, const Path& p = Path()) override; + + ReturnType writeAnyObject(const type::Maybe maybe, const Path& p = Path()) override; }; } diff --git a/source/RobotAPI/libraries/aron/core/type/variant/All.h b/source/RobotAPI/libraries/aron/core/type/variant/All.h index c4a66915a..46934fb07 100644 --- a/source/RobotAPI/libraries/aron/core/type/variant/All.h +++ b/source/RobotAPI/libraries/aron/core/type/variant/All.h @@ -4,6 +4,7 @@ #include "ndarray/All.h" #include "enum/All.h" #include "primitive/All.h" +#include "any/All.h" /** * A convenience header to include all aron files (full include, not forward declared) diff --git a/source/RobotAPI/libraries/aron/core/type/variant/any/All.h b/source/RobotAPI/libraries/aron/core/type/variant/any/All.h new file mode 100644 index 000000000..41c8b9989 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/type/variant/any/All.h @@ -0,0 +1,11 @@ +#pragma once + +#include "AnyObject.h" + +/** + * A convenience header to include all primitive aron files (full include, not forward declared) + */ +namespace armarx::aron::type +{ + +} diff --git a/source/RobotAPI/libraries/aron/core/type/variant/any/AnyObject.cpp b/source/RobotAPI/libraries/aron/core/type/variant/any/AnyObject.cpp new file mode 100644 index 000000000..bd53947d9 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/type/variant/any/AnyObject.cpp @@ -0,0 +1,61 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + + +// STD/STL +#include <string> +#include <map> + +// Header +#include "AnyObject.h" + +namespace armarx::aron::type +{ + /* constructors */ + AnyObject::AnyObject(const Path& path) : + detail::AnyVariant<type::dto::AnyObject, AnyObject>(type::Descriptor::eAnyObject, path) + { + } + + AnyObject::AnyObject(const type::dto::AnyObject&o, const Path& path) : + detail::AnyVariant<type::dto::AnyObject, AnyObject>(o, type::Descriptor::eAnyObject, path) + { + } + + /* public member functions */ + type::dto::AnyObjectPtr AnyObject::toAnyObjectDTO() const + { + return aron; + } + + /* virtual implementations */ + std::string AnyObject::getShortName() const + { + return "AnyObject"; + } + + std::string AnyObject::getFullName() const + { + return "armarx::aron::type::AnyObject"; + } +} diff --git a/source/RobotAPI/libraries/aron/core/type/variant/any/AnyObject.h b/source/RobotAPI/libraries/aron/core/type/variant/any/AnyObject.h new file mode 100644 index 000000000..db153869b --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/type/variant/any/AnyObject.h @@ -0,0 +1,51 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +#pragma once + +// STD/STL +#include <string> + +// Base class +#include "../detail/AnyVariant.h" + +namespace armarx::aron::type +{ + /** + * @brief The AnyObject class. It represents the any object type + */ + class AnyObject : + public detail::AnyVariant<type::dto::AnyObject, AnyObject> + { + public: + /* constructors */ + AnyObject(const Path& = Path()); + AnyObject(const type::dto::AnyObject&, const Path& = Path()); + + type::dto::AnyObjectPtr toAnyObjectDTO() const; + + /* virtual implementations */ + virtual std::string getShortName() const override; + virtual std::string getFullName() const override; + }; +} diff --git a/source/RobotAPI/libraries/aron/core/type/variant/container/Object.cpp b/source/RobotAPI/libraries/aron/core/type/variant/container/Object.cpp index b3bf45605..56dd6f22a 100644 --- a/source/RobotAPI/libraries/aron/core/type/variant/container/Object.cpp +++ b/source/RobotAPI/libraries/aron/core/type/variant/container/Object.cpp @@ -33,10 +33,12 @@ namespace armarx::aron::type { // constructors - Object::Object(const std::string& name, const std::map<std::string, VariantPtr>& m, const Path& path) : + Object::Object(const std::string& name, const std::vector<std::string>& templates, const std::vector<std::string>& templateInstantiations, const std::map<std::string, VariantPtr>& m, const Path& path) : detail::ContainerVariant<type::dto::AronObject, Object>(type::Descriptor::eObject, path), memberTypes(m) { + aron->templates = templates; + aron->templateInstantiations = templateInstantiations; setObjectName(name); for (const auto& [key, value] : memberTypes) { @@ -78,6 +80,7 @@ namespace armarx::aron::type { throw error::AronException(__PRETTY_FUNCTION__, "The object name is empty.", getPath()); } + return true; } @@ -128,6 +131,7 @@ namespace armarx::aron::type void Object::setObjectName(const std::string& n) { + checkObjectName(n); //path.setRootIdentifier(n); this->aron->objectName = n; } @@ -145,6 +149,58 @@ namespace armarx::aron::type return memberTypes.count(k) > 0 or (extends && extends->hasMemberType(k)); } + std::string Object::getObjectNameWithoutNamespace() const + { + std::vector<std::string> split = simox::alg::split(aron->objectName, "::"); + return split[split.size() -1]; + } + + std::string Object::getObjectNameWithTemplateInstantiations() const + { + if (aron->templateInstantiations.empty()) + { + return getObjectName(); + } + return getObjectName() + "<" + simox::alg::join(aron->templateInstantiations, ", ") + ">"; + } + + std::string Object::getObjectNameWithTemplates() const + { + if (aron->templates.empty()) + { + return getObjectName(); + } + return getObjectName() + "<" + simox::alg::join(aron->templates, ", ") + ">"; + } + + void Object::addTemplate(const std::string& s) const + { + if (std::find(aron->templates.begin(), aron->templates.end(), s) != aron->templates.end()) + { + throw error::ValueNotValidException(__PRETTY_FUNCTION__, "The template already exists!", s); + } + aron->templates.push_back(s); + } + + void Object::addTemplateInstantiation(const std::string& s) const + { + if (std::find(aron->templateInstantiations.begin(), aron->templateInstantiations.end(), s) != aron->templateInstantiations.end()) + { + throw error::ValueNotValidException(__PRETTY_FUNCTION__, "The template arg already exists!", s); + } + aron->templateInstantiations.push_back(s); + } + + std::vector<std::string> Object::getTemplates() const + { + return aron->templates; + } + + std::vector<std::string> Object::getTemplateInstantiations() const + { + return aron->templateInstantiations; + } + std::string Object::getObjectName() const { return this->aron->objectName; @@ -174,11 +230,6 @@ namespace armarx::aron::type type::dto::AronObjectPtr Object::toAronObjectDTO() const { - // TODO: Shall we allow empty objects? For now yes! - //if(acceptedTypeNavigators.empty()) - //{ - // throw exception::AronExceptionWithPathInfo("ObjectNavigator", "getResult", "No accepted types set", getPath()); - //} return this->aron; } diff --git a/source/RobotAPI/libraries/aron/core/type/variant/container/Object.h b/source/RobotAPI/libraries/aron/core/type/variant/container/Object.h index 094d1f532..9924c2cbd 100644 --- a/source/RobotAPI/libraries/aron/core/type/variant/container/Object.h +++ b/source/RobotAPI/libraries/aron/core/type/variant/container/Object.h @@ -45,7 +45,7 @@ namespace armarx::aron::type { public: // constructors - Object(const std::string&, const std::map<std::string, VariantPtr>& = {}, const Path& = Path()); + Object(const std::string&, const std::vector<std::string>& templates = {}, const std::vector<std::string>& templateInstantiations = {}, const std::map<std::string, VariantPtr>& = {}, const Path& = Path()); Object(const type::dto::AronObject&, const Path& = Path()); static ObjectPtr FromAronObjectDTO(const type::dto::AronObjectPtr&); @@ -59,13 +59,21 @@ namespace armarx::aron::type VariantPtr getMemberType(const std::string&) const; std::string getObjectName() const; std::shared_ptr<Object> getExtends() const; + std::vector<std::string> getTemplates() const; + std::vector<std::string> getTemplateInstantiations() const; void setObjectName(const std::string&); void setExtends(const std::shared_ptr<Object>&); void addMemberType(const std::string&, const VariantPtr&); + void addTemplate(const std::string&) const; + void addTemplateInstantiation(const std::string&) const; bool hasMemberType(const std::string&) const; + std::string getObjectNameWithoutNamespace() const; + std::string getObjectNameWithTemplates() const; + std::string getObjectNameWithTemplateInstantiations() const; + std::vector<std::string> getAllKeys() const; type::dto::AronObjectPtr toAronObjectDTO() const; diff --git a/source/RobotAPI/libraries/aron/core/type/variant/detail/AnyVariant.cpp b/source/RobotAPI/libraries/aron/core/type/variant/detail/AnyVariant.cpp new file mode 100644 index 000000000..b42fd2019 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/type/variant/detail/AnyVariant.cpp @@ -0,0 +1,25 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +// Header +#include "AnyVariant.h" diff --git a/source/RobotAPI/libraries/aron/core/type/variant/detail/AnyVariant.h b/source/RobotAPI/libraries/aron/core/type/variant/detail/AnyVariant.h new file mode 100644 index 000000000..70c9c0896 --- /dev/null +++ b/source/RobotAPI/libraries/aron/core/type/variant/detail/AnyVariant.h @@ -0,0 +1,63 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +#pragma once + +// STD/STL +#include <memory> +#include <string> +#include <unordered_map> + +// Base class +#include "SpecializedVariant.h" + +// ArmarX + +namespace armarx::aron::type::detail +{ + template<typename AronTypeT, typename DerivedT> + class AnyVariant : + public SpecializedVariantBase<AronTypeT, DerivedT> + { + public: + using SpecializedVariantBase<AronTypeT, DerivedT>::SpecializedVariantBase; + + virtual ~AnyVariant() = default; + + /* virtual implementations */ + VariantPtr navigateAbsolute(const Path &path) const override + { + throw error::AronException(__PRETTY_FUNCTION__, "Could not navigate through an any navigator. The input path was: " + path.toString(), Variant::getPath()); + } + + std::vector<VariantPtr> getChildren() const override + { + return {}; + } + + size_t childrenSize() const override + { + return 0; + } + }; +} diff --git a/source/RobotAPI/libraries/aron/core/type/visitor/RecursiveVisitor.h b/source/RobotAPI/libraries/aron/core/type/visitor/RecursiveVisitor.h index b11894e7b..3ee591092 100644 --- a/source/RobotAPI/libraries/aron/core/type/visitor/RecursiveVisitor.h +++ b/source/RobotAPI/libraries/aron/core/type/visitor/RecursiveVisitor.h @@ -116,6 +116,8 @@ namespace armarx::aron::type return v.visitBool(t); case type::Descriptor::eTime: return v.visitTime(t); + case type::Descriptor::eAnyObject: + return v.visitAnyObject(t); case type::Descriptor::eIntEnum: return v.visitIntEnum(t); case type::Descriptor::eUnknown: @@ -170,6 +172,7 @@ namespace armarx::aron::type virtual void visitBool(Input&) {}; virtual void visitString(Input&) {}; virtual void visitTime(Input&) {}; + virtual void visitAnyObject(Input&) {}; virtual void visitUnknown(Input&) { throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor."); } diff --git a/source/RobotAPI/libraries/aron/core/type/visitor/Visitor.h b/source/RobotAPI/libraries/aron/core/type/visitor/Visitor.h index 3978c029a..f4472c700 100644 --- a/source/RobotAPI/libraries/aron/core/type/visitor/Visitor.h +++ b/source/RobotAPI/libraries/aron/core/type/visitor/Visitor.h @@ -84,6 +84,8 @@ namespace armarx::aron::type return v.visitBool(t); case type::Descriptor::eTime: return v.visitTime(t); + case type::Descriptor::eAnyObject: + return v.visitAnyObject(t); case type::Descriptor::eIntEnum: return v.visitIntEnum(t); case type::Descriptor::eUnknown: @@ -131,6 +133,7 @@ namespace armarx::aron::type virtual void visitBool(Input&) {}; virtual void visitString(Input&) {}; virtual void visitTime(Input&) {}; + virtual void visitAnyObject(Input&) {}; virtual void visitUnknown(Input&) { throw error::AronException(__PRETTY_FUNCTION__, "Unknown type in visitor."); } virtual ~Visitor() = default; }; diff --git a/source/RobotAPI/libraries/aron/core/typereader/helper/GenerateInfo.h b/source/RobotAPI/libraries/aron/core/typereader/helper/GenerateInfo.h index ad22e564e..1c9e82555 100644 --- a/source/RobotAPI/libraries/aron/core/typereader/helper/GenerateInfo.h +++ b/source/RobotAPI/libraries/aron/core/typereader/helper/GenerateInfo.h @@ -26,6 +26,10 @@ // STD/STL #include <memory> #include <string> +#include <vector> + +#include <SimoxUtility/algorithm/string.h> +#include <ArmarXCore/core/exceptions/local/ExpressionException.h> namespace armarx::aron::typereader { @@ -36,5 +40,34 @@ namespace armarx::aron::typereader std::string definedIn; std::string doc_brief; std::string doc_author; + + + std::string getNameWithoutNamespace() const + { + std::vector<std::string> split = simox::alg::split(typeName, "::"); + return split[split.size() -1]; + } + + std::vector<std::string> getTemplates() const + { + auto first = typeName.find("<"); + if (first == std::string::npos) + { + return {}; + } + + auto last = typeName.find(">"); + ARMARX_CHECK_NOT_EQUAL(last, std::string::npos); + + return simox::alg::split(typeName.substr(first,last-first), ","); + } + + std::vector<std::string> getNamespaces() const + { + std::vector<std::string> split = simox::alg::split(typeName, "::"); + std::vector<std::string> namespaces(split); + namespaces.pop_back(); + return namespaces; + } }; } diff --git a/source/RobotAPI/libraries/aron/core/typereader/xml/Data.h b/source/RobotAPI/libraries/aron/core/typereader/xml/Data.h index 34fc92ef7..26fb8f36c 100644 --- a/source/RobotAPI/libraries/aron/core/typereader/xml/Data.h +++ b/source/RobotAPI/libraries/aron/core/typereader/xml/Data.h @@ -59,6 +59,7 @@ namespace armarx::aron::typereader::xml const std::string EXTENDS_ATTRIBUTE_NAME = "extends"; const std::string NAME_ATTRIBUTE_NAME = "name"; const std::string VALUE_ATTRIBUTE_NAME = "value"; + const std::string TEMPLATE_ATTRIBUTE_NAME = "template"; const std::string KEY_ATTRIBUTE_NAME = "key"; const std::string TYPE_ATTRIBUTE_NAME = "type"; const std::string WIDTH_ATTRIBUTE_NAME = "width"; @@ -102,6 +103,7 @@ namespace armarx::aron::typereader::xml const std::string STRING_TAG = "string"; const std::string BOOL_TAG = "bool"; const std::string TIME_TAG = "time"; + const std::string ANY_OBJECT_TAG = "anyobject"; } diff --git a/source/RobotAPI/libraries/aron/core/typereader/xml/Factory.cpp b/source/RobotAPI/libraries/aron/core/typereader/xml/Factory.cpp index da30a7e70..1e25870a0 100644 --- a/source/RobotAPI/libraries/aron/core/typereader/xml/Factory.cpp +++ b/source/RobotAPI/libraries/aron/core/typereader/xml/Factory.cpp @@ -35,8 +35,8 @@ namespace armarx::aron::typereader::xml { - std::map<std::string, typereader::GenerateObjectInfo> ReaderFactory::AllGeneratedPublicObjects; - std::map<std::string, typereader::GenerateIntEnumInfo> ReaderFactory::AllGeneratedPublicIntEnums; + //std::map<std::string, typereader::GenerateObjectInfo> ReaderFactory::allGeneratedPublicObjects; + //std::map<std::string, typereader::GenerateIntEnumInfo> ReaderFactory::allGeneratedPublicIntEnums; type::VariantPtr ReaderFactory::create(const RapidXmlReaderNode& node, const Path& path) { @@ -62,7 +62,8 @@ namespace armarx::aron::typereader::xml {constantes::DOUBLE_TAG, type::Descriptor::eDouble}, {constantes::STRING_TAG, type::Descriptor::eString}, {constantes::BOOL_TAG, type::Descriptor::eBool}, - {constantes::TIME_TAG, type::Descriptor::eTime} + {constantes::TIME_TAG, type::Descriptor::eTime}, + {constantes::ANY_OBJECT_TAG, type::Descriptor::eAnyObject} }; const std::string tag = simox::alg::to_lower(node.name()); @@ -92,28 +93,46 @@ namespace armarx::aron::typereader::xml case type::Descriptor::eString: return createString(node, path); case type::Descriptor::eBool: return createBool(node, path); case type::Descriptor::eTime: return createTime(node, path); + case type::Descriptor::eAnyObject: return createAnyObject(node, path); default: return findExistingObject(node.name()); } } type::VariantPtr ReaderFactory::findExistingObject(const std::string& name) const { - const auto public_intenum_it = AllGeneratedPublicIntEnums.find(name); - if (public_intenum_it != AllGeneratedPublicIntEnums.end()) + const auto public_intenum_it = allGeneratedPublicIntEnums.find(name); + if (public_intenum_it != allGeneratedPublicIntEnums.end()) { // copy the navigator auto v = type::Variant::FromAronDTO(*public_intenum_it->second.correspondingType->toAronDTO()); return v; } - const auto public_obj_it = AllGeneratedPublicObjects.find(name); - if (public_obj_it != AllGeneratedPublicObjects.end()) + const auto public_obj_it = allGeneratedPublicObjects.find(name); + if (public_obj_it != allGeneratedPublicObjects.end()) { - // copy the navigator + // copy the navigator and set instantiation template args auto v = type::Variant::FromAronDTO(*public_obj_it->second.correspondingType->toAronDTO()); return v; } + const auto public_unk_it = std::find(allPreviouslyKnownPublicTypes.begin(), allPreviouslyKnownPublicTypes.end(), name); + if(public_unk_it != allPreviouslyKnownPublicTypes.end()) + { + // create an empty navigator + auto v = std::make_shared<aron::type::Object>(*public_unk_it); + return v; + } + + // only works during generation process (not for top-level) + const auto public_priv_it = std::find(allPreviouslyKnownPrivateTypes.begin(), allPreviouslyKnownPrivateTypes.end(), name); + if(public_priv_it != allPreviouslyKnownPrivateTypes.end()) + { + // create an empty navigator + auto v = std::make_shared<aron::type::Object>(*public_priv_it); + return v; + } + throw error::ValueNotValidException(__PRETTY_FUNCTION__, "Cannot find a valid object.", name); } @@ -157,15 +176,21 @@ namespace armarx::aron::typereader::xml { throw error::AronException(__PRETTY_FUNCTION__, "Having an inner class is not supported anymore since Aron Version 'beta 0.2.3'. Please move the inner class definition to the <" + constantes::GENERATE_TYPES_TAG + ">-tag."); } + // ensured we are toplevel! util::EnforceAttribute(node, constantes::NAME_ATTRIBUTE_NAME); const std::string extends = util::GetAttributeWithDefault(node, constantes::EXTENDS_ATTRIBUTE_NAME, ""); const std::string name = util::GetAttribute(node, constantes::NAME_ATTRIBUTE_NAME); - auto newObject = typereader::GenerateObjectInfo(); - newObject.typeName = name; - newObject.doc_brief = util::GetAttributeWithDefault(node, constantes::DOC_BRIEF_NAME, ""); - newObject.doc_author = util::GetAttributeWithDefault(node, constantes::DOC_AUTHOR_NAME, ""); + auto templates = simox::alg::split(util::GetAttributeWithDefault(node, constantes::TEMPLATE_ATTRIBUTE_NAME, ""), ","); + + auto newObjectInfo = typereader::GenerateObjectInfo(); + newObjectInfo.typeName = name; + newObjectInfo.doc_brief = util::GetAttributeWithDefault(node, constantes::DOC_BRIEF_NAME, ""); + newObjectInfo.doc_author = util::GetAttributeWithDefault(node, constantes::DOC_AUTHOR_NAME, ""); + + // reset private known types + allPreviouslyKnownPrivateTypes = templates; std::map<std::string, type::VariantPtr> members; for (const RapidXmlReaderNode& objectChild : node.nodes()) @@ -180,7 +205,7 @@ namespace armarx::aron::typereader::xml if (util::HasAttribute(objectChild, constantes::DOC_BRIEF_NAME)) { - newObject.doc_members.insert({key, util::GetAttribute(objectChild, constantes::DOC_BRIEF_NAME)}); + newObjectInfo.doc_members.insert({key, util::GetAttribute(objectChild, constantes::DOC_BRIEF_NAME)}); } std::vector<RapidXmlReaderNode> children = objectChild.nodes(); @@ -188,12 +213,23 @@ namespace armarx::aron::typereader::xml auto maybe = getMaybe(children[0]); type::VariantPtr childNavigator = create(children[0], path.withElement(key)); + if (childNavigator->getDescriptor() == aron::type::Descriptor::eObject) + { + // check if template args are present + std::vector<std::string> templates = simox::alg::split(util::GetAttributeWithDefault(children[0], constantes::TEMPLATE_ATTRIBUTE_NAME, ""), ","); + auto obj = aron::type::Object::DynamicCastAndCheck(childNavigator); + for (const auto& t : templates) + { + obj->addTemplateInstantiation(t); + } + } + childNavigator->setMaybe(maybe); members.insert({key, childNavigator}); } // set the new object - auto aronObjectType = std::make_shared<type::Object>(name, members, path); + auto aronObjectType = std::make_shared<type::Object>(name, templates, std::vector<std::string>(), members, path); if (extends != "") { @@ -201,8 +237,10 @@ namespace armarx::aron::typereader::xml aronObjectType->setExtends(parentObj); } - newObject.correspondingType = aronObjectType; - AllGeneratedPublicObjects.emplace(newObject.typeName, newObject); + newObjectInfo.correspondingType = aronObjectType; + allGeneratedPublicObjects.emplace(newObjectInfo.typeName, newObjectInfo); + + allPreviouslyKnownPrivateTypes.clear(); return aronObjectType; } @@ -433,12 +471,15 @@ namespace armarx::aron::typereader::xml { throw error::AronException(__PRETTY_FUNCTION__, "Having an inner int-enum is not supported anymore since Aron Version 'beta 0.2.3'. Please move the inner int-enum definition to the <" + constantes::GENERATE_TYPES_TAG + ">-tag."); } + // ensured we are top-level! const std::string name = util::GetAttribute(node, constantes::NAME_ATTRIBUTE_NAME); auto newEnumInfo = typereader::GenerateIntEnumInfo(); newEnumInfo.typeName = name; + allPreviouslyKnownPrivateTypes.clear(); + std::map<std::string, int> acceptedValues; for (const RapidXmlReaderNode& valueChild : node.nodes()) { @@ -459,9 +500,9 @@ namespace armarx::aron::typereader::xml auto o = std::make_shared<type::IntEnum>(name, acceptedValues, path); newEnumInfo.correspondingType = o; - AllGeneratedPublicIntEnums.emplace(newEnumInfo.typeName, newEnumInfo); - + allGeneratedPublicIntEnums.emplace(newEnumInfo.typeName, newEnumInfo); + allPreviouslyKnownPrivateTypes.clear(); return o; } @@ -499,4 +540,9 @@ namespace armarx::aron::typereader::xml { return std::make_shared<type::Time>(path); } + + type::VariantPtr ReaderFactory::createAnyObject(const RapidXmlReaderNode& node, const Path& path) const + { + return std::make_shared<type::AnyObject>(path); + } } diff --git a/source/RobotAPI/libraries/aron/core/typereader/xml/Factory.h b/source/RobotAPI/libraries/aron/core/typereader/xml/Factory.h index 33861a4ba..a15c84116 100644 --- a/source/RobotAPI/libraries/aron/core/typereader/xml/Factory.h +++ b/source/RobotAPI/libraries/aron/core/typereader/xml/Factory.h @@ -50,6 +50,7 @@ namespace armarx::aron::typereader::xml type::VariantPtr create(const RapidXmlReaderNode&, const Path&); private: + /// check, whether a given name corresponds to an already created object name. type::VariantPtr findExistingObject(const std::string& n) const; @@ -84,11 +85,19 @@ namespace armarx::aron::typereader::xml type::VariantPtr createBool(const RapidXmlReaderNode& node, const Path& path) const; type::VariantPtr createTime(const RapidXmlReaderNode& node, const Path& path) const; + type::VariantPtr createAnyObject(const RapidXmlReaderNode& node, const Path& path) const; + public: /// static map of all generated objects. Since this factory may be called recursively, it must be static - static std::map<std::string, typereader::GenerateObjectInfo> AllGeneratedPublicObjects; + std::map<std::string, typereader::GenerateObjectInfo> allGeneratedPublicObjects; /// same for int enums - static std::map<std::string, typereader::GenerateIntEnumInfo> AllGeneratedPublicIntEnums; + std::map<std::string, typereader::GenerateIntEnumInfo> allGeneratedPublicIntEnums; + + /// previously known types + std::vector<std::string> allPreviouslyKnownPublicTypes; + + private: + std::vector<std::string> allPreviouslyKnownPrivateTypes; }; } diff --git a/source/RobotAPI/libraries/aron/core/typereader/xml/Reader.cpp b/source/RobotAPI/libraries/aron/core/typereader/xml/Reader.cpp index a8f2f0cc4..c12e495c0 100644 --- a/source/RobotAPI/libraries/aron/core/typereader/xml/Reader.cpp +++ b/source/RobotAPI/libraries/aron/core/typereader/xml/Reader.cpp @@ -125,14 +125,14 @@ namespace armarx::aron::typereader::xml if (util::HasTagName(generateType, constantes::OBJECT_TAG)) { const auto nav = readGenerateObject(generateType); - generateObjects.push_back(factory.AllGeneratedPublicObjects.at(nav->getObjectName())); + generateObjects.push_back(factory.allGeneratedPublicObjects.at(nav->getObjectName())); continue; } if (util::HasTagName(generateType, constantes::INT_ENUM_TAG)) { const auto nav = readGenerateIntEnum(generateType); - generateIntEnums.push_back(factory.AllGeneratedPublicIntEnums.at(nav->getEnumName())); + generateIntEnums.push_back(factory.allGeneratedPublicIntEnums.at(nav->getEnumName())); continue; } throw error::ValueNotValidException("XMLReader", "parse", "Could not find a valid tag inside generatetypes", generateType.name()); @@ -190,6 +190,18 @@ namespace armarx::aron::typereader::xml // parse parent xml file and add objects to alreday known Reader anotherReader; anotherReader.parseFile(resolved_absolute_path); + for (const auto& previouslyKnown : anotherReader.factory.allPreviouslyKnownPublicTypes) + { + factory.allPreviouslyKnownPublicTypes.push_back(previouslyKnown); + } + for (const auto& knownObjectInfo : anotherReader.factory.allGeneratedPublicObjects) + { + factory.allPreviouslyKnownPublicTypes.push_back(knownObjectInfo.first); + } + for (const auto& knownIntEnumInfo : anotherReader.factory.allGeneratedPublicIntEnums) + { + factory.allPreviouslyKnownPublicTypes.push_back(knownIntEnumInfo.first); + } if (util::HasAttribute(node, constantes::AUTO_CODE_INCLUDE)) { -- GitLab