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 +++++++++++++-------------- src/container.cpp | 8 +++--- src/object_type.cpp | 15 ++++++------ 5 files changed, 52 insertions(+), 62 deletions(-) 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 diff --git a/src/container.cpp b/src/container.cpp index 00efc27..044d487 100644 --- a/src/container.cpp +++ b/src/container.cpp @@ -7,20 +7,20 @@ namespace yacppdic { void Container::add( - const BaseObjectType &type, + const ObjectType &type, const WrapperPtr &wrapper ) noexcept { _bindings.insert({ type, wrapper }); } -void Container::remove(const BaseObjectType &type) noexcept +void Container::remove(const ObjectType &type) noexcept { _bindings.erase(type); } -Container::WrapperPtr Container::at(const BaseObjectType &type -) const noexcept +auto Container::at(const ObjectType &type) const noexcept + -> Container::WrapperPtr { return _bindings.at(type); } diff --git a/src/object_type.cpp b/src/object_type.cpp index bd61e09..15b798c 100644 --- a/src/object_type.cpp +++ b/src/object_type.cpp @@ -7,12 +7,11 @@ namespace yacppdic { -BaseObjectType::BaseObjectType(const std::type_info &type_info) noexcept - : _type_info(type_info) +ObjectType::ObjectType(const std::type_info &type_info) noexcept : _type_info(type_info) { } -BaseObjectType::BaseObjectType( +ObjectType::ObjectType( const std::type_info &type_info, const std::string_view tag ) noexcept @@ -20,12 +19,12 @@ BaseObjectType::BaseObjectType( { } -auto BaseObjectType::operator==(const BaseObjectType &object_type) const noexcept -> bool +auto ObjectType::operator==(const ObjectType &object_type) const noexcept -> bool { return hash() == object_type.hash(); } -auto BaseObjectType::hash() const noexcept -> std::size_t +auto ObjectType::hash() const noexcept -> std::size_t { const auto type_hash = _type_info.hash_code(); @@ -33,17 +32,17 @@ auto BaseObjectType::hash() const noexcept -> std::size_t : combine_hashes(type_hash, std::hash()(_tag)); } -auto BaseObjectType::name() const noexcept -> std::string_view +auto ObjectType::name() const noexcept -> std::string_view { return { _type_info.name() }; } -auto BaseObjectType::tag() const noexcept -> std::string_view +auto ObjectType::tag() const noexcept -> std::string_view { return _tag; } -auto ObjectTypeHasher::operator()(const BaseObjectType &object_type) const noexcept +auto ObjectTypeHasher::operator()(const ObjectType &object_type) const noexcept -> std::size_t { return object_type.hash(); -- cgit v1.2.3-18-g5258