From b4b064a1520b3d9c6dd36b299ade93af3226225c Mon Sep 17 00:00:00 2001 From: Levi Armstrong Date: Fri, 29 Nov 2024 23:53:07 -0600 Subject: [PATCH] Simplify type erasure --- .../include/tesseract_common/any_poly.h | 13 ++++---- .../include/tesseract_common/type_erasure.h | 33 ++++--------------- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/tesseract_common/include/tesseract_common/any_poly.h b/tesseract_common/include/tesseract_common/any_poly.h index 0650cb46d93..1224b25434d 100644 --- a/tesseract_common/include/tesseract_common/any_poly.h +++ b/tesseract_common/include/tesseract_common/any_poly.h @@ -46,20 +46,16 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP { \ using K##AnyInstanceBase = tesseract_common::TypeErasureInstance; \ using K##AnyInstance = tesseract_common::detail_any::AnyInstance; \ - using K##AnyInstanceWrapper = tesseract_common::TypeErasureInstanceWrapper; \ } \ BOOST_CLASS_EXPORT_KEY(tesseract_serialization::any_poly::K##AnyInstanceBase) \ BOOST_CLASS_EXPORT_KEY(tesseract_serialization::any_poly::K##AnyInstance) \ - BOOST_CLASS_EXPORT_KEY(tesseract_serialization::any_poly::K##AnyInstanceWrapper) \ BOOST_CLASS_TRACKING(tesseract_serialization::any_poly::K##AnyInstanceBase, boost::serialization::track_never) \ - BOOST_CLASS_TRACKING(tesseract_serialization::any_poly::K##AnyInstance, boost::serialization::track_never) \ - BOOST_CLASS_TRACKING(tesseract_serialization::any_poly::K##AnyInstanceWrapper, boost::serialization::track_never) + BOOST_CLASS_TRACKING(tesseract_serialization::any_poly::K##AnyInstance, boost::serialization::track_never) /** @brief If shared library, this must go in the cpp after the implicit instantiation of the serialize function */ #define TESSERACT_ANY_EXPORT_IMPLEMENT(K) \ BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_serialization::any_poly::K##AnyInstanceBase) \ - BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_serialization::any_poly::K##AnyInstance) \ - BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_serialization::any_poly::K##AnyInstanceWrapper) + BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_serialization::any_poly::K##AnyInstance) /** * @brief This should not be used within shared libraries use the two above. @@ -102,6 +98,11 @@ struct AnyInstance : tesseract_common::TypeErasureInstance)); + std::unique_ptr clone() const final + { + return std::make_unique>(this->get()); + } + private: friend class boost::serialization::access; friend struct tesseract_common::Serialization; diff --git a/tesseract_common/include/tesseract_common/type_erasure.h b/tesseract_common/include/tesseract_common/type_erasure.h index cb5da046d10..166cb0ccc51 100644 --- a/tesseract_common/include/tesseract_common/type_erasure.h +++ b/tesseract_common/include/tesseract_common/type_erasure.h @@ -95,34 +95,12 @@ struct TypeErasureInstance : ConceptInterface return this->getType() == other.getType() && this->get() == *static_cast(other.recover()); } - ConcreteType value_; - -private: - friend class boost::serialization::access; - friend struct tesseract_common::Serialization; - template - void serialize(Archive& ar, const unsigned int /*version*/) // NOLINT + std::unique_ptr clone() const override { - // If this line is removed a exception is thrown for unregistered cast need to too look into this. - ar& boost::serialization::make_nvp("base", boost::serialization::base_object(*this)); - ar& boost::serialization::make_nvp("impl", value_); + throw std::runtime_error("This should never be called!"); } -}; - -template -struct TypeErasureInstanceWrapper : F // NOLINT -{ - using ConceptValueType = typename F::ConceptValueType; - using ConceptInterfaceType = typename F::ConceptInterfaceType; - TypeErasureInstanceWrapper() = default; - TypeErasureInstanceWrapper(const ConceptValueType& x) : F(x) {} - TypeErasureInstanceWrapper(TypeErasureInstanceWrapper&& x) noexcept : F(std::move(x)) {} - - std::unique_ptr clone() const final - { - return std::make_unique>(this->get()); - } + ConcreteType value_; private: friend class boost::serialization::access; @@ -131,7 +109,8 @@ struct TypeErasureInstanceWrapper : F // NOLINT void serialize(Archive& ar, const unsigned int /*version*/) // NOLINT { // If this line is removed a exception is thrown for unregistered cast need to too look into this. - ar& boost::serialization::make_nvp("base", boost::serialization::base_object(*this)); + ar& boost::serialization::make_nvp("base", boost::serialization::base_object(*this)); + ar& boost::serialization::make_nvp("impl", value_); } }; @@ -152,7 +131,7 @@ struct TypeErasureBase template = 0> TypeErasureBase(T&& value) // NOLINT - : value_(std::make_unique>>>(value)) + : value_(std::make_unique>>(value)) { }