From 203db8ffe8d4ee82ca126926232f4d4de21b7aec Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 9 Jun 2022 18:46:00 +0200 Subject: refactor: rename ObjectType to ObjectIdentifier --- include/yacppdic/container.hpp | 20 ++++---- include/yacppdic/detail/container-impl.hpp | 77 +++++++++++++++++------------- include/yacppdic/object_identifier.hpp | 56 ++++++++++++++++++++++ include/yacppdic/object_type.hpp | 47 ------------------ 4 files changed, 113 insertions(+), 87 deletions(-) create mode 100644 include/yacppdic/object_identifier.hpp delete mode 100644 include/yacppdic/object_type.hpp (limited to 'include') diff --git a/include/yacppdic/container.hpp b/include/yacppdic/container.hpp index 0f3a9fc..6073150 100644 --- a/include/yacppdic/container.hpp +++ b/include/yacppdic/container.hpp @@ -4,7 +4,7 @@ #include "yacppdic/concepts.hpp" #include "yacppdic/factory.hpp" -#include "yacppdic/object_type.hpp" +#include "yacppdic/object_identifier.hpp" #include #include @@ -21,13 +21,16 @@ template class BindingWhen { public: - explicit BindingWhen(Container *container, ObjectType object_type) noexcept; + explicit BindingWhen( + Container *container, + ObjectIdentifier object_identifier + ) noexcept; void when_tagged(std::string_view tag) noexcept; private: Container *_container; - ObjectType _object_type; + ObjectIdentifier _object_identifier; }; template @@ -75,15 +78,16 @@ public: requires IsFactory auto get_tagged(const char *tag) const noexcept -> AFactory; - void add(const ObjectType &type, const WrapperPtr &wrapper) noexcept; + void + add(const ObjectIdentifier &type, + const WrapperPtr &wrapper) noexcept; - void remove(const ObjectType &type) noexcept; + void remove(const ObjectIdentifier &type) noexcept; - auto at(const ObjectType &type) const noexcept -> WrapperPtr; + auto at(const ObjectIdentifier &type) const noexcept -> WrapperPtr; private: - std::unordered_map, ObjectTypeHasher> - _bindings; + std::unordered_map> _bindings; }; } // namespace yacppdic diff --git a/include/yacppdic/detail/container-impl.hpp b/include/yacppdic/detail/container-impl.hpp index 720a88b..a8f6f7b 100644 --- a/include/yacppdic/detail/container-impl.hpp +++ b/include/yacppdic/detail/container-impl.hpp @@ -11,21 +11,24 @@ namespace yacppdic { template -BindingWhen::BindingWhen(Container *container, ObjectType object_type) noexcept - : _container(container), _object_type(std::move(object_type)) +BindingWhen::BindingWhen( + Container *container, + ObjectIdentifier object_identifier +) noexcept + : _container(container), _object_identifier(std::move(object_identifier)) { } template void BindingWhen::when_tagged(std::string_view tag) noexcept { - auto wrapped = _container->at(_object_type); + auto wrapped = _container->at(_object_identifier); - _container->remove(_object_type); + _container->remove(_object_identifier); - auto type = ObjectType::create(tag); + auto tagged_object_identifier = ObjectIdentifier::create(tag); - _container->add(type, wrapped); + _container->add(tagged_object_identifier, wrapped); } template @@ -43,11 +46,14 @@ auto BindingBuilder::to() noexcept -> BindingWhen auto wrapper = Container::WrapperPtr(new Wrapper(*_container)); - auto type = ObjectType::create(); + auto object_identifier = ObjectIdentifier::create(); - _container->add(type, std::dynamic_pointer_cast(wrapper)); + _container->add( + object_identifier, + std::dynamic_pointer_cast(wrapper) + ); - return BindingWhen(_container, type); + return BindingWhen(_container, object_identifier); } template @@ -60,11 +66,14 @@ auto BindingBuilder::to_factory(FactoryFunc factory) noexcept auto wrapper = Container::WrapperPtr(new Wrapper(factory)); - auto type = ObjectType::create(); + auto object_identifier = ObjectIdentifier::create(); - _container->add(type, std::dynamic_pointer_cast(wrapper)); + _container->add( + object_identifier, + std::dynamic_pointer_cast(wrapper) + ); - return BindingWhen(_container, type); + return BindingWhen(_container, object_identifier); } template @@ -77,19 +86,19 @@ template requires Abstract auto Container::get() const noexcept -> std::unique_ptr { - auto type = ObjectType::create(); + auto object_identifier = ObjectIdentifier::create(); - if (_bindings.count(type) == 0) + if (_bindings.count(object_identifier) == 0) { std::cerr << "Error: Tried to get a item from the container using unbound interface '" - << type.name() << "'" << std::endl; + << object_identifier.name() << "'" << std::endl; exit(EXIT_FAILURE); } - auto wrapper = - std::dynamic_pointer_cast>>(_bindings.at(type) - ); + auto wrapper = std::dynamic_pointer_cast>>( + _bindings.at(object_identifier) + ); return wrapper->get(); } @@ -98,17 +107,18 @@ template requires IsFactory auto Container::get() const noexcept -> AFactory { - auto type = ObjectType::create(); + auto object_identifier = ObjectIdentifier::create(); - if (!_bindings.contains(type)) + if (!_bindings.contains(object_identifier)) { std::cerr << "Error: Tried to get a item from the container using unbound interface '" - << type.name() << "'" << std::endl; + << object_identifier.name() << "'" << std::endl; exit(EXIT_FAILURE); } - auto wrapper = std::dynamic_pointer_cast>(_bindings.at(type)); + auto wrapper = + std::dynamic_pointer_cast>(_bindings.at(object_identifier)); return wrapper->get(); } @@ -117,19 +127,20 @@ template requires Abstract auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptr { - auto type = ObjectType::create(tag); + auto object_identifier = ObjectIdentifier::create(tag); - if (!_bindings.contains(type)) + if (!_bindings.contains(object_identifier)) { std::cerr << "Error: Tried to get a item from the container using unbound interface '" - << type.name() << "' with tag '" << type.tag() << "'" << std::endl; + << object_identifier.name() << "' with tag '" << object_identifier.tag() + << "'" << std::endl; exit(EXIT_FAILURE); } - auto wrapper = - std::dynamic_pointer_cast>>(_bindings.at(type) - ); + auto wrapper = std::dynamic_pointer_cast>>( + _bindings.at(object_identifier) + ); return wrapper->get(); } @@ -138,17 +149,19 @@ template requires IsFactory auto Container::get_tagged(const char *tag) const noexcept -> AFactory { - auto type = ObjectType::create(tag); + auto object_identifier = ObjectIdentifier::create(tag); - if (!_bindings.contains(type)) + if (!_bindings.contains(object_identifier)) { std::cerr << "Error: Tried to get a item from the container using unbound interface '" - << type.name() << "' With tag '" << type.tag() << "'" << std::endl; + << object_identifier.name() << "' With tag '" << object_identifier.tag() + << "'" << std::endl; exit(EXIT_FAILURE); } - auto wrapper = std::dynamic_pointer_cast>(_bindings.at(type)); + auto wrapper = + std::dynamic_pointer_cast>(_bindings.at(object_identifier)); return wrapper->get(); } diff --git a/include/yacppdic/object_identifier.hpp b/include/yacppdic/object_identifier.hpp new file mode 100644 index 0000000..812e5c9 --- /dev/null +++ b/include/yacppdic/object_identifier.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include + +namespace yacppdic +{ + +class ObjectIdentifier +{ +public: + explicit ObjectIdentifier(const std::type_info &type_info) noexcept; + + explicit ObjectIdentifier( + const std::type_info &type_info, + std::string_view tag + ) noexcept; + + auto operator==(const ObjectIdentifier &object_type) const noexcept -> bool; + + [[nodiscard]] auto hash() const noexcept -> std::size_t; + + [[nodiscard]] auto name() const noexcept -> std::string_view; + + [[nodiscard]] auto tag() const noexcept -> std::string_view; + + template + static auto create() noexcept -> ObjectIdentifier + { + return static_cast(typeid(Type)); + } + + template + static auto create(std::string_view tag) noexcept -> ObjectIdentifier + { + return ObjectIdentifier(typeid(Type), tag); + } + +private: + const std::type_info &_type_info; + const std::string_view _tag; +}; + +} // namespace yacppdic + +template <> +class std::hash +{ +public: + auto operator()(const yacppdic::ObjectIdentifier &object_identifier) const noexcept + -> std::size_t + { + return object_identifier.hash(); + } +}; + diff --git a/include/yacppdic/object_type.hpp b/include/yacppdic/object_type.hpp deleted file mode 100644 index 3a7a639..0000000 --- a/include/yacppdic/object_type.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include -#include - -namespace yacppdic -{ - -class ObjectType -{ -public: - explicit ObjectType(const std::type_info &type_info) noexcept; - - explicit ObjectType(const std::type_info &type_info, std::string_view tag) noexcept; - - auto operator==(const ObjectType &object_type) const noexcept -> bool; - - [[nodiscard]] auto hash() const noexcept -> std::size_t; - - [[nodiscard]] auto name() const noexcept -> std::string_view; - - [[nodiscard]] auto tag() const noexcept -> std::string_view; - - template - static auto create() noexcept -> ObjectType - { - return static_cast(typeid(Type)); - } - - 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 ObjectType &object_type) const noexcept -> std::size_t; -}; - -} // namespace yacppdic -- cgit v1.2.3-18-g5258