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 /include | |
parent | 203db8ffe8d4ee82ca126926232f4d4de21b7aec (diff) |
refactor: clean up container API
Diffstat (limited to 'include')
-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 |
3 files changed, 60 insertions, 56 deletions
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(); } |