aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/yacppdic/container.hpp20
-rw-r--r--include/yacppdic/detail/container-impl.hpp77
-rw-r--r--include/yacppdic/object_identifier.hpp56
-rw-r--r--include/yacppdic/object_type.hpp47
4 files changed, 113 insertions, 87 deletions
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 <concepts>
#include <functional>
@@ -21,13 +21,16 @@ template <typename Interface>
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 <typename Interface>
@@ -75,15 +78,16 @@ public:
requires IsFactory<AFactory>
auto get_tagged(const char *tag) const noexcept -> AFactory;
- void add(const ObjectType &type, const WrapperPtr<IGenericWrapper> &wrapper) noexcept;
+ void
+ add(const ObjectIdentifier &type,
+ const WrapperPtr<IGenericWrapper> &wrapper) noexcept;
- void remove(const ObjectType &type) noexcept;
+ void remove(const ObjectIdentifier &type) noexcept;
- auto at(const ObjectType &type) const noexcept -> WrapperPtr<IGenericWrapper>;
+ auto at(const ObjectIdentifier &type) const noexcept -> WrapperPtr<IGenericWrapper>;
private:
- std::unordered_map<ObjectType, WrapperPtr<IGenericWrapper>, ObjectTypeHasher>
- _bindings;
+ std::unordered_map<ObjectIdentifier, WrapperPtr<IGenericWrapper>> _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 <typename Interface>
-BindingWhen<Interface>::BindingWhen(Container *container, ObjectType object_type) noexcept
- : _container(container), _object_type(std::move(object_type))
+BindingWhen<Interface>::BindingWhen(
+ Container *container,
+ ObjectIdentifier object_identifier
+) noexcept
+ : _container(container), _object_identifier(std::move(object_identifier))
{
}
template <typename Interface>
void BindingWhen<Interface>::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<Interface>(tag);
+ auto tagged_object_identifier = ObjectIdentifier::create<Interface>(tag);
- _container->add(type, wrapped);
+ _container->add(tagged_object_identifier, wrapped);
}
template <typename Interface>
@@ -43,11 +46,14 @@ auto BindingBuilder<Interface>::to() noexcept -> BindingWhen<Interface>
auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(*_container));
- auto type = ObjectType::create<Interface>();
+ auto object_identifier = ObjectIdentifier::create<Interface>();
- _container->add(type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
+ _container->add(
+ object_identifier,
+ std::dynamic_pointer_cast<IGenericWrapper>(wrapper)
+ );
- return BindingWhen<Interface>(_container, type);
+ return BindingWhen<Interface>(_container, object_identifier);
}
template <typename Interface>
@@ -60,11 +66,14 @@ auto BindingBuilder<Interface>::to_factory(FactoryFunc factory) noexcept
auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(factory));
- auto type = ObjectType::create<Interface>();
+ auto object_identifier = ObjectIdentifier::create<Interface>();
- _container->add(type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
+ _container->add(
+ object_identifier,
+ std::dynamic_pointer_cast<IGenericWrapper>(wrapper)
+ );
- return BindingWhen<Interface>(_container, type);
+ return BindingWhen<Interface>(_container, object_identifier);
}
template <typename Interface>
@@ -77,19 +86,19 @@ template <typename Interface>
requires Abstract<Interface>
auto Container::get() const noexcept -> std::unique_ptr<Interface>
{
- auto type = ObjectType::create<Interface>();
+ auto object_identifier = ObjectIdentifier::create<Interface>();
- 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<IWrapper<std::unique_ptr<Interface>>>(_bindings.at(type)
- );
+ auto wrapper = std::dynamic_pointer_cast<IWrapper<std::unique_ptr<Interface>>>(
+ _bindings.at(object_identifier)
+ );
return wrapper->get();
}
@@ -98,17 +107,18 @@ template <typename AFactory>
requires IsFactory<AFactory>
auto Container::get() const noexcept -> AFactory
{
- auto type = ObjectType::create<AFactory>();
+ auto object_identifier = ObjectIdentifier::create<AFactory>();
- 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<IWrapper<AFactory>>(_bindings.at(type));
+ auto wrapper =
+ std::dynamic_pointer_cast<IWrapper<AFactory>>(_bindings.at(object_identifier));
return wrapper->get();
}
@@ -117,19 +127,20 @@ template <class Interface>
requires Abstract<Interface>
auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptr<Interface>
{
- auto type = ObjectType::create<Interface>(tag);
+ auto object_identifier = ObjectIdentifier::create<Interface>(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<IWrapper<std::unique_ptr<Interface>>>(_bindings.at(type)
- );
+ auto wrapper = std::dynamic_pointer_cast<IWrapper<std::unique_ptr<Interface>>>(
+ _bindings.at(object_identifier)
+ );
return wrapper->get();
}
@@ -138,17 +149,19 @@ template <typename AFactory>
requires IsFactory<AFactory>
auto Container::get_tagged(const char *tag) const noexcept -> AFactory
{
- auto type = ObjectType::create<AFactory>(tag);
+ auto object_identifier = ObjectIdentifier::create<AFactory>(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<IWrapper<AFactory>>(_bindings.at(type));
+ auto wrapper =
+ std::dynamic_pointer_cast<IWrapper<AFactory>>(_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 <string_view>
+#include <typeinfo>
+
+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 <typename Type>
+ static auto create() noexcept -> ObjectIdentifier
+ {
+ return static_cast<ObjectIdentifier>(typeid(Type));
+ }
+
+ template <typename Type>
+ 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<yacppdic::ObjectIdentifier>
+{
+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 <string_view>
-#include <typeinfo>
-
-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 <typename Type>
- static auto create() noexcept -> ObjectType
- {
- return static_cast<ObjectType>(typeid(Type));
- }
-
- template <typename Type>
- 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