From 59d8202868364b6efeb92dbd694d961723b74cc0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 9 Jun 2022 17:46:24 +0200 Subject: refactor: replace BaseObjectType with static ObjectType method --- include/yacppdic/container.hpp | 16 +++++------- include/yacppdic/detail/container-impl.hpp | 39 ++++++++++++++---------------- include/yacppdic/object_type.hpp | 36 +++++++++++++-------------- 3 files changed, 41 insertions(+), 50 deletions(-) (limited to 'include') diff --git a/include/yacppdic/container.hpp b/include/yacppdic/container.hpp index 6308142..0f3a9fc 100644 --- a/include/yacppdic/container.hpp +++ b/include/yacppdic/container.hpp @@ -21,16 +21,13 @@ template class BindingWhen { public: - explicit BindingWhen( - Container *container, - BaseObjectType object_type - ) noexcept; + explicit BindingWhen(Container *container, ObjectType object_type) noexcept; void when_tagged(std::string_view tag) noexcept; private: Container *_container; - BaseObjectType _object_type; + ObjectType _object_type; }; template @@ -78,15 +75,14 @@ public: requires IsFactory auto get_tagged(const char *tag) const noexcept -> AFactory; - void - add(const BaseObjectType &type, const WrapperPtr &wrapper) noexcept; + void add(const ObjectType &type, const WrapperPtr &wrapper) noexcept; - void remove(const BaseObjectType &type) noexcept; + void remove(const ObjectType &type) noexcept; - auto at(const BaseObjectType &type) const noexcept -> WrapperPtr; + auto at(const ObjectType &type) const noexcept -> WrapperPtr; private: - std::unordered_map, ObjectTypeHasher> + std::unordered_map, ObjectTypeHasher> _bindings; }; diff --git a/include/yacppdic/detail/container-impl.hpp b/include/yacppdic/detail/container-impl.hpp index 869641c..720a88b 100644 --- a/include/yacppdic/detail/container-impl.hpp +++ b/include/yacppdic/detail/container-impl.hpp @@ -11,10 +11,7 @@ namespace yacppdic { template -BindingWhen::BindingWhen( - Container *container, - BaseObjectType object_type -) noexcept +BindingWhen::BindingWhen(Container *container, ObjectType object_type) noexcept : _container(container), _object_type(std::move(object_type)) { } @@ -26,9 +23,9 @@ void BindingWhen::when_tagged(std::string_view tag) noexcept _container->remove(_object_type); - auto object_type = ObjectType(tag); + auto type = ObjectType::create(tag); - _container->add(object_type, wrapped); + _container->add(type, wrapped); } template @@ -46,11 +43,11 @@ auto BindingBuilder::to() noexcept -> BindingWhen auto wrapper = Container::WrapperPtr(new Wrapper(*_container)); - auto object_type = ObjectType(); + auto type = ObjectType::create(); - _container->add(object_type, std::dynamic_pointer_cast(wrapper)); + _container->add(type, std::dynamic_pointer_cast(wrapper)); - return BindingWhen(_container, object_type); + return BindingWhen(_container, type); } template @@ -63,11 +60,11 @@ auto BindingBuilder::to_factory(FactoryFunc factory) noexcept auto wrapper = Container::WrapperPtr(new Wrapper(factory)); - auto object_type = ObjectType(); + auto type = ObjectType::create(); - _container->add(object_type, std::dynamic_pointer_cast(wrapper)); + _container->add(type, std::dynamic_pointer_cast(wrapper)); - return BindingWhen(_container, object_type); + return BindingWhen(_container, type); } template @@ -80,19 +77,19 @@ template requires Abstract auto Container::get() const noexcept -> std::unique_ptr { - ObjectType interface_type; + auto type = ObjectType::create(); - if (_bindings.count(interface_type) == 0) + if (_bindings.count(type) == 0) { std::cerr << "Error: Tried to get a item from the container using unbound interface '" - << interface_type.name() << "'" << std::endl; + << type.name() << "'" << std::endl; exit(EXIT_FAILURE); } - auto wrapper = std::dynamic_pointer_cast>>( - _bindings.at(interface_type) - ); + auto wrapper = + std::dynamic_pointer_cast>>(_bindings.at(type) + ); return wrapper->get(); } @@ -101,7 +98,7 @@ template requires IsFactory auto Container::get() const noexcept -> AFactory { - BaseObjectType type = ObjectType(); + auto type = ObjectType::create(); if (!_bindings.contains(type)) { @@ -120,7 +117,7 @@ template requires Abstract auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptr { - BaseObjectType type = ObjectType(tag); + auto type = ObjectType::create(tag); if (!_bindings.contains(type)) { @@ -141,7 +138,7 @@ template requires IsFactory auto Container::get_tagged(const char *tag) const noexcept -> AFactory { - BaseObjectType type = ObjectType(tag); + auto type = ObjectType::create(tag); if (!_bindings.contains(type)) { diff --git a/include/yacppdic/object_type.hpp b/include/yacppdic/object_type.hpp index 1765e65..3a7a639 100644 --- a/include/yacppdic/object_type.hpp +++ b/include/yacppdic/object_type.hpp @@ -6,17 +6,14 @@ namespace yacppdic { -class BaseObjectType +class ObjectType { public: - explicit BaseObjectType(const std::type_info &type_info) noexcept; + explicit ObjectType(const std::type_info &type_info) noexcept; - explicit BaseObjectType( - const std::type_info &type_info, - std::string_view tag - ) noexcept; + explicit ObjectType(const std::type_info &type_info, std::string_view tag) noexcept; - auto operator==(const BaseObjectType &object_type) const noexcept -> bool; + auto operator==(const ObjectType &object_type) const noexcept -> bool; [[nodiscard]] auto hash() const noexcept -> std::size_t; @@ -24,26 +21,27 @@ public: [[nodiscard]] auto tag() const noexcept -> std::string_view; -private: - const std::type_info &_type_info; - const std::string_view _tag; -}; - -template -class ObjectType : public BaseObjectType -{ -public: - ObjectType() noexcept : BaseObjectType(typeid(Object)) {} + template + static auto create() noexcept -> ObjectType + { + return static_cast(typeid(Type)); + } - explicit ObjectType(const std::string_view &tag) noexcept : BaseObjectType(typeid(Object), tag) + template + static auto create(std::string_view tag) noexcept -> ObjectType { + return ObjectType(typeid(Type), tag); } + +private: + const std::type_info &_type_info; + const std::string_view _tag; }; class ObjectTypeHasher { public: - auto operator()(const BaseObjectType &object_type) const noexcept -> std::size_t; + auto operator()(const ObjectType &object_type) const noexcept -> std::size_t; }; } // namespace yacppdic -- cgit v1.2.3-18-g5258