diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-11 22:03:46 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-11 22:03:46 +0200 |
commit | 77d97c914a157037b6729d0f3b0fb265aacf4741 (patch) | |
tree | 60b4e9463f1d36450129bf518b79e1d9ae7fa970 | |
parent | 203db8ffe8d4ee82ca126926232f4d4de21b7aec (diff) |
refactor: clean up container API
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/yacppdic/container.hpp | 53 | ||||
-rw-r--r-- | include/yacppdic/detail/container-impl.hpp | 53 | ||||
-rw-r--r-- | include/yacppdic/detail/internal/object_identifier.hpp (renamed from include/yacppdic/object_identifier.hpp) | 10 | ||||
-rw-r--r-- | src/container.cpp | 28 | ||||
-rw-r--r-- | src/object_identifier.cpp | 6 |
6 files changed, 63 insertions, 88 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 066e0cb..59ed07c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,6 @@ add_library(${PROJECT_NAME} STATIC) target_sources( ${PROJECT_NAME} PRIVATE - src/container.cpp src/object_identifier.cpp ) 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 <concepts> #include <functional> @@ -15,30 +15,43 @@ namespace yacppdic { +namespace internal +{ + +template <typename Type> +using WrapperPtr = std::shared_ptr<Type>; + +using Bindings = std::unordered_map<ObjectIdentifier, WrapperPtr<IGenericWrapper>>; + +} // namespace internal + class Container; template <typename Interface> +class BindingBuilder; + +template <typename Interface> 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<Interface>; }; template <typename Interface> class BindingBuilder { public: - explicit BindingBuilder(Container *container) noexcept; - template <typename Impl> requires Abstract<Interface> && std::derived_from<Impl, Interface> auto to() noexcept -> BindingWhen<Interface>; @@ -48,7 +61,12 @@ public: auto to_factory(FactoryFunc factory) noexcept -> BindingWhen<Interface>; 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 <typename Type> - using WrapperPtr = std::shared_ptr<Type>; - template <class Interface> auto bind() noexcept -> BindingBuilder<Interface>; @@ -78,16 +93,8 @@ public: requires IsFactory<AFactory> auto get_tagged(const char *tag) const noexcept -> AFactory; - void - add(const ObjectIdentifier &type, - const WrapperPtr<IGenericWrapper> &wrapper) noexcept; - - void remove(const ObjectIdentifier &type) noexcept; - - auto at(const ObjectIdentifier &type) const noexcept -> WrapperPtr<IGenericWrapper>; - private: - std::unordered_map<ObjectIdentifier, WrapperPtr<IGenericWrapper>> _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 <typename Interface> BindingWhen<Interface>::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 <typename Interface> void BindingWhen<Interface>::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<Interface>(tag); + auto tagged_object_identifier = internal::ObjectIdentifier::create<Interface>(tag); - _container->add(tagged_object_identifier, wrapped); + _bindings[tagged_object_identifier] = wrapped; } template <typename Interface> -BindingBuilder<Interface>::BindingBuilder(Container *container) noexcept - : _container(container) +BindingBuilder<Interface>::BindingBuilder( + Container &container, + internal::Bindings &bindings +) noexcept + : _container(container), _bindings(bindings) { } @@ -44,16 +47,13 @@ auto BindingBuilder<Interface>::to() noexcept -> BindingWhen<Interface> { using Wrapper = internal::ObjectWrapper<Interface, Impl>; - auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(*_container)); + auto wrapper = internal::WrapperPtr<Wrapper>(new Wrapper(_container)); - auto object_identifier = ObjectIdentifier::create<Interface>(); + auto object_identifier = internal::ObjectIdentifier::create<Interface>(); - _container->add( - object_identifier, - std::dynamic_pointer_cast<IGenericWrapper>(wrapper) - ); + _bindings[object_identifier] = std::dynamic_pointer_cast<IGenericWrapper>(wrapper); - return BindingWhen<Interface>(_container, object_identifier); + return BindingWhen<Interface>(_bindings, object_identifier); } template <typename Interface> @@ -64,29 +64,26 @@ auto BindingBuilder<Interface>::to_factory(FactoryFunc factory) noexcept { using Wrapper = internal::FunctionWrapper<Interface>; - auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(factory)); + auto wrapper = internal::WrapperPtr<Wrapper>(new Wrapper(factory)); - auto object_identifier = ObjectIdentifier::create<Interface>(); + auto object_identifier = internal::ObjectIdentifier::create<Interface>(); - _container->add( - object_identifier, - std::dynamic_pointer_cast<IGenericWrapper>(wrapper) - ); + _bindings[object_identifier] = std::dynamic_pointer_cast<IGenericWrapper>(wrapper); - return BindingWhen<Interface>(_container, object_identifier); + return BindingWhen<Interface>(_bindings, object_identifier); } template <typename Interface> auto Container::bind() noexcept -> BindingBuilder<Interface> { - return BindingBuilder<Interface>(this); + return BindingBuilder<Interface>(*this, _bindings); } template <typename Interface> requires Abstract<Interface> auto Container::get() const noexcept -> std::unique_ptr<Interface> { - auto object_identifier = ObjectIdentifier::create<Interface>(); + auto object_identifier = internal::ObjectIdentifier::create<Interface>(); if (_bindings.count(object_identifier) == 0) { @@ -107,7 +104,7 @@ template <typename AFactory> requires IsFactory<AFactory> auto Container::get() const noexcept -> AFactory { - auto object_identifier = ObjectIdentifier::create<AFactory>(); + auto object_identifier = internal::ObjectIdentifier::create<AFactory>(); if (!_bindings.contains(object_identifier)) { @@ -127,7 +124,7 @@ template <class Interface> requires Abstract<Interface> auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptr<Interface> { - auto object_identifier = ObjectIdentifier::create<Interface>(tag); + auto object_identifier = internal::ObjectIdentifier::create<Interface>(tag); if (!_bindings.contains(object_identifier)) { @@ -149,7 +146,7 @@ template <typename AFactory> requires IsFactory<AFactory> auto Container::get_tagged(const char *tag) const noexcept -> AFactory { - auto object_identifier = ObjectIdentifier::create<AFactory>(tag); + auto object_identifier = internal::ObjectIdentifier::create<AFactory>(tag); if (!_bindings.contains(object_identifier)) { diff --git a/include/yacppdic/object_identifier.hpp b/include/yacppdic/detail/internal/object_identifier.hpp index 812e5c9..c82b3e8 100644 --- a/include/yacppdic/object_identifier.hpp +++ b/include/yacppdic/detail/internal/object_identifier.hpp @@ -3,7 +3,7 @@ #include <string_view> #include <typeinfo> -namespace yacppdic +namespace yacppdic::internal { class ObjectIdentifier @@ -41,14 +41,14 @@ private: const std::string_view _tag; }; -} // namespace yacppdic +} // namespace yacppdic::internal template <> -class std::hash<yacppdic::ObjectIdentifier> +class std::hash<yacppdic::internal::ObjectIdentifier> { public: - auto operator()(const yacppdic::ObjectIdentifier &object_identifier) const noexcept - -> std::size_t + auto operator()(const yacppdic::internal::ObjectIdentifier &object_identifier + ) const noexcept -> std::size_t { return object_identifier.hash(); } diff --git a/src/container.cpp b/src/container.cpp deleted file mode 100644 index f8c4931..0000000 --- a/src/container.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "yacppdic/container.hpp" - -#include "yacppdic/detail/internal/wrapper/function_wrapper.hpp" -#include "yacppdic/detail/internal/wrapper/object_wrapper.hpp" - -namespace yacppdic -{ - -void Container::add( - const ObjectIdentifier &type, - const WrapperPtr<IGenericWrapper> &wrapper -) noexcept -{ - _bindings.insert({ type, wrapper }); -} - -void Container::remove(const ObjectIdentifier &type) noexcept -{ - _bindings.erase(type); -} - -auto Container::at(const ObjectIdentifier &type) const noexcept - -> Container::WrapperPtr<IGenericWrapper> -{ - return _bindings.at(type); -} - -} // namespace yacppdic diff --git a/src/object_identifier.cpp b/src/object_identifier.cpp index a743271..44ca1b3 100644 --- a/src/object_identifier.cpp +++ b/src/object_identifier.cpp @@ -1,10 +1,10 @@ -#include "yacppdic/object_identifier.hpp" +#include "yacppdic/detail/internal/object_identifier.hpp" #include "yacppdic/detail/internal/hash.hpp" #include <functional> -namespace yacppdic +namespace yacppdic::internal { ObjectIdentifier::ObjectIdentifier(const std::type_info &type_info) noexcept @@ -44,5 +44,5 @@ auto ObjectIdentifier::tag() const noexcept -> std::string_view return _tag; } -} // namespace yacppdic +} // namespace yacppdic::internal |