From 77d97c914a157037b6729d0f3b0fb265aacf4741 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 11 Jun 2022 22:03:46 +0200 Subject: refactor: clean up container API --- include/yacppdic/container.hpp | 53 +++++++++++--------- include/yacppdic/detail/container-impl.hpp | 53 ++++++++++---------- .../yacppdic/detail/internal/object_identifier.hpp | 56 ++++++++++++++++++++++ include/yacppdic/object_identifier.hpp | 56 ---------------------- 4 files changed, 111 insertions(+), 107 deletions(-) create mode 100644 include/yacppdic/detail/internal/object_identifier.hpp delete mode 100644 include/yacppdic/object_identifier.hpp (limited to 'include') diff --git a/include/yacppdic/container.hpp b/include/yacppdic/container.hpp index 6073150..0003520 100644 --- a/include/yacppdic/container.hpp +++ b/include/yacppdic/container.hpp @@ -3,8 +3,8 @@ #include "yacppdic/interfaces/wrapper.hpp" #include "yacppdic/concepts.hpp" +#include "yacppdic/detail/internal/object_identifier.hpp" #include "yacppdic/factory.hpp" -#include "yacppdic/object_identifier.hpp" #include #include @@ -15,30 +15,43 @@ namespace yacppdic { +namespace internal +{ + +template +using WrapperPtr = std::shared_ptr; + +using Bindings = std::unordered_map>; + +} // namespace internal + class Container; +template +class BindingBuilder; + template class BindingWhen { public: - explicit BindingWhen( - Container *container, - ObjectIdentifier object_identifier - ) noexcept; - void when_tagged(std::string_view tag) noexcept; private: - Container *_container; - ObjectIdentifier _object_identifier; + internal::Bindings &_bindings; + internal::ObjectIdentifier _object_identifier; + + explicit BindingWhen( + internal::Bindings &bindings, + internal::ObjectIdentifier object_identifier + ) noexcept; + + friend BindingBuilder; }; template class BindingBuilder { public: - explicit BindingBuilder(Container *container) noexcept; - template requires Abstract && std::derived_from auto to() noexcept -> BindingWhen; @@ -48,7 +61,12 @@ public: auto to_factory(FactoryFunc factory) noexcept -> BindingWhen; private: - Container *_container; + Container &_container; + internal::Bindings &_bindings; + + explicit BindingBuilder(Container &container, internal::Bindings &bindings) noexcept; + + friend Container; }; class Container @@ -56,9 +74,6 @@ class Container public: Container() noexcept = default; - template - using WrapperPtr = std::shared_ptr; - template auto bind() noexcept -> BindingBuilder; @@ -78,16 +93,8 @@ public: requires IsFactory auto get_tagged(const char *tag) const noexcept -> AFactory; - void - add(const ObjectIdentifier &type, - const WrapperPtr &wrapper) noexcept; - - void remove(const ObjectIdentifier &type) noexcept; - - auto at(const ObjectIdentifier &type) const noexcept -> WrapperPtr; - private: - std::unordered_map> _bindings; + internal::Bindings _bindings; }; } // namespace yacppdic diff --git a/include/yacppdic/detail/container-impl.hpp b/include/yacppdic/detail/container-impl.hpp index a8f6f7b..7480dee 100644 --- a/include/yacppdic/detail/container-impl.hpp +++ b/include/yacppdic/detail/container-impl.hpp @@ -12,28 +12,31 @@ namespace yacppdic template BindingWhen::BindingWhen( - Container *container, - ObjectIdentifier object_identifier + internal::Bindings &bindings, + internal::ObjectIdentifier object_identifier ) noexcept - : _container(container), _object_identifier(std::move(object_identifier)) + : _bindings(bindings), _object_identifier(std::move(object_identifier)) { } template void BindingWhen::when_tagged(std::string_view tag) noexcept { - auto wrapped = _container->at(_object_identifier); + auto wrapped = _bindings.at(_object_identifier); - _container->remove(_object_identifier); + _bindings.erase(_object_identifier); - auto tagged_object_identifier = ObjectIdentifier::create(tag); + auto tagged_object_identifier = internal::ObjectIdentifier::create(tag); - _container->add(tagged_object_identifier, wrapped); + _bindings[tagged_object_identifier] = wrapped; } template -BindingBuilder::BindingBuilder(Container *container) noexcept - : _container(container) +BindingBuilder::BindingBuilder( + Container &container, + internal::Bindings &bindings +) noexcept + : _container(container), _bindings(bindings) { } @@ -44,16 +47,13 @@ auto BindingBuilder::to() noexcept -> BindingWhen { using Wrapper = internal::ObjectWrapper; - auto wrapper = Container::WrapperPtr(new Wrapper(*_container)); + auto wrapper = internal::WrapperPtr(new Wrapper(_container)); - auto object_identifier = ObjectIdentifier::create(); + auto object_identifier = internal::ObjectIdentifier::create(); - _container->add( - object_identifier, - std::dynamic_pointer_cast(wrapper) - ); + _bindings[object_identifier] = std::dynamic_pointer_cast(wrapper); - return BindingWhen(_container, object_identifier); + return BindingWhen(_bindings, object_identifier); } template @@ -64,29 +64,26 @@ auto BindingBuilder::to_factory(FactoryFunc factory) noexcept { using Wrapper = internal::FunctionWrapper; - auto wrapper = Container::WrapperPtr(new Wrapper(factory)); + auto wrapper = internal::WrapperPtr(new Wrapper(factory)); - auto object_identifier = ObjectIdentifier::create(); + auto object_identifier = internal::ObjectIdentifier::create(); - _container->add( - object_identifier, - std::dynamic_pointer_cast(wrapper) - ); + _bindings[object_identifier] = std::dynamic_pointer_cast(wrapper); - return BindingWhen(_container, object_identifier); + return BindingWhen(_bindings, object_identifier); } template auto Container::bind() noexcept -> BindingBuilder { - return BindingBuilder(this); + return BindingBuilder(*this, _bindings); } template requires Abstract auto Container::get() const noexcept -> std::unique_ptr { - auto object_identifier = ObjectIdentifier::create(); + auto object_identifier = internal::ObjectIdentifier::create(); if (_bindings.count(object_identifier) == 0) { @@ -107,7 +104,7 @@ template requires IsFactory auto Container::get() const noexcept -> AFactory { - auto object_identifier = ObjectIdentifier::create(); + auto object_identifier = internal::ObjectIdentifier::create(); if (!_bindings.contains(object_identifier)) { @@ -127,7 +124,7 @@ template requires Abstract auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptr { - auto object_identifier = ObjectIdentifier::create(tag); + auto object_identifier = internal::ObjectIdentifier::create(tag); if (!_bindings.contains(object_identifier)) { @@ -149,7 +146,7 @@ template requires IsFactory auto Container::get_tagged(const char *tag) const noexcept -> AFactory { - auto object_identifier = ObjectIdentifier::create(tag); + auto object_identifier = internal::ObjectIdentifier::create(tag); if (!_bindings.contains(object_identifier)) { diff --git a/include/yacppdic/detail/internal/object_identifier.hpp b/include/yacppdic/detail/internal/object_identifier.hpp new file mode 100644 index 0000000..c82b3e8 --- /dev/null +++ b/include/yacppdic/detail/internal/object_identifier.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include + +namespace yacppdic::internal +{ + +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::internal + +template <> +class std::hash +{ +public: + auto operator()(const yacppdic::internal::ObjectIdentifier &object_identifier + ) const noexcept -> std::size_t + { + return object_identifier.hash(); + } +}; + diff --git a/include/yacppdic/object_identifier.hpp b/include/yacppdic/object_identifier.hpp deleted file mode 100644 index 812e5c9..0000000 --- a/include/yacppdic/object_identifier.hpp +++ /dev/null @@ -1,56 +0,0 @@ -#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(); - } -}; - -- cgit v1.2.3-18-g5258