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 --- CMakeLists.txt | 2 +- include/yacppdic/container.hpp | 20 ++++---- include/yacppdic/detail/container-impl.hpp | 77 +++++++++++++++++------------- include/yacppdic/object_identifier.hpp | 56 ++++++++++++++++++++++ include/yacppdic/object_type.hpp | 47 ------------------ src/container.cpp | 6 +-- src/object_identifier.cpp | 48 +++++++++++++++++++ src/object_type.cpp | 52 -------------------- 8 files changed, 165 insertions(+), 143 deletions(-) create mode 100644 include/yacppdic/object_identifier.hpp delete mode 100644 include/yacppdic/object_type.hpp create mode 100644 src/object_identifier.cpp delete mode 100644 src/object_type.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index af8fc8a..066e0cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ target_sources( ${PROJECT_NAME} PRIVATE src/container.cpp - src/object_type.cpp + src/object_identifier.cpp ) target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20) 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 diff --git a/src/container.cpp b/src/container.cpp index 044d487..f8c4931 100644 --- a/src/container.cpp +++ b/src/container.cpp @@ -7,19 +7,19 @@ namespace yacppdic { void Container::add( - const ObjectType &type, + const ObjectIdentifier &type, const WrapperPtr &wrapper ) noexcept { _bindings.insert({ type, wrapper }); } -void Container::remove(const ObjectType &type) noexcept +void Container::remove(const ObjectIdentifier &type) noexcept { _bindings.erase(type); } -auto Container::at(const ObjectType &type) const noexcept +auto Container::at(const ObjectIdentifier &type) const noexcept -> Container::WrapperPtr { return _bindings.at(type); diff --git a/src/object_identifier.cpp b/src/object_identifier.cpp new file mode 100644 index 0000000..a743271 --- /dev/null +++ b/src/object_identifier.cpp @@ -0,0 +1,48 @@ +#include "yacppdic/object_identifier.hpp" + +#include "yacppdic/detail/internal/hash.hpp" + +#include + +namespace yacppdic +{ + +ObjectIdentifier::ObjectIdentifier(const std::type_info &type_info) noexcept + : _type_info(type_info) +{ +} + +ObjectIdentifier::ObjectIdentifier( + const std::type_info &type_info, + const std::string_view tag +) noexcept + : _type_info(type_info), _tag(tag) +{ +} + +auto ObjectIdentifier::operator==(const ObjectIdentifier &object_type) const noexcept + -> bool +{ + return hash() == object_type.hash(); +} + +auto ObjectIdentifier::hash() const noexcept -> std::size_t +{ + const auto type_hash = _type_info.hash_code(); + + return _tag.empty() ? type_hash + : combine_hashes(type_hash, std::hash()(_tag)); +} + +auto ObjectIdentifier::name() const noexcept -> std::string_view +{ + return { _type_info.name() }; +} + +auto ObjectIdentifier::tag() const noexcept -> std::string_view +{ + return _tag; +} + +} // namespace yacppdic + diff --git a/src/object_type.cpp b/src/object_type.cpp deleted file mode 100644 index 15b798c..0000000 --- a/src/object_type.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "yacppdic/object_type.hpp" - -#include "yacppdic/detail/internal/hash.hpp" - -#include - -namespace yacppdic -{ - -ObjectType::ObjectType(const std::type_info &type_info) noexcept : _type_info(type_info) -{ -} - -ObjectType::ObjectType( - const std::type_info &type_info, - const std::string_view tag -) noexcept - : _type_info(type_info), _tag(tag) -{ -} - -auto ObjectType::operator==(const ObjectType &object_type) const noexcept -> bool -{ - return hash() == object_type.hash(); -} - -auto ObjectType::hash() const noexcept -> std::size_t -{ - const auto type_hash = _type_info.hash_code(); - - return _tag == "" ? type_hash - : combine_hashes(type_hash, std::hash()(_tag)); -} - -auto ObjectType::name() const noexcept -> std::string_view -{ - return { _type_info.name() }; -} - -auto ObjectType::tag() const noexcept -> std::string_view -{ - return _tag; -} - -auto ObjectTypeHasher::operator()(const ObjectType &object_type) const noexcept - -> std::size_t -{ - return object_type.hash(); -} - -} // namespace yacppdic - -- cgit v1.2.3-18-g5258