aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-09 17:46:24 +0200
committerHampusM <hampus@hampusmat.com>2022-06-09 17:46:24 +0200
commit59d8202868364b6efeb92dbd694d961723b74cc0 (patch)
tree573aea529c556bb2b893f93ac6ffd85cfb02f4c9 /include
parentcb169dfac720de614821480a2633bb93b6be5245 (diff)
refactor: replace BaseObjectType with static ObjectType method
Diffstat (limited to 'include')
-rw-r--r--include/yacppdic/container.hpp16
-rw-r--r--include/yacppdic/detail/container-impl.hpp39
-rw-r--r--include/yacppdic/object_type.hpp36
3 files changed, 41 insertions, 50 deletions
diff --git a/include/yacppdic/container.hpp b/include/yacppdic/container.hpp
index 6308142..0f3a9fc 100644
--- a/include/yacppdic/container.hpp
+++ b/include/yacppdic/container.hpp
@@ -21,16 +21,13 @@ template <typename Interface>
class BindingWhen
{
public:
- explicit BindingWhen(
- Container *container,
- BaseObjectType object_type
- ) noexcept;
+ explicit BindingWhen(Container *container, ObjectType object_type) noexcept;
void when_tagged(std::string_view tag) noexcept;
private:
Container *_container;
- BaseObjectType _object_type;
+ ObjectType _object_type;
};
template <typename Interface>
@@ -78,15 +75,14 @@ public:
requires IsFactory<AFactory>
auto get_tagged(const char *tag) const noexcept -> AFactory;
- void
- add(const BaseObjectType &type, const WrapperPtr<IGenericWrapper> &wrapper) noexcept;
+ void add(const ObjectType &type, const WrapperPtr<IGenericWrapper> &wrapper) noexcept;
- void remove(const BaseObjectType &type) noexcept;
+ void remove(const ObjectType &type) noexcept;
- auto at(const BaseObjectType &type) const noexcept -> WrapperPtr<IGenericWrapper>;
+ auto at(const ObjectType &type) const noexcept -> WrapperPtr<IGenericWrapper>;
private:
- std::unordered_map<BaseObjectType, WrapperPtr<IGenericWrapper>, ObjectTypeHasher>
+ std::unordered_map<ObjectType, WrapperPtr<IGenericWrapper>, ObjectTypeHasher>
_bindings;
};
diff --git a/include/yacppdic/detail/container-impl.hpp b/include/yacppdic/detail/container-impl.hpp
index 869641c..720a88b 100644
--- a/include/yacppdic/detail/container-impl.hpp
+++ b/include/yacppdic/detail/container-impl.hpp
@@ -11,10 +11,7 @@ namespace yacppdic
{
template <typename Interface>
-BindingWhen<Interface>::BindingWhen(
- Container *container,
- BaseObjectType object_type
-) noexcept
+BindingWhen<Interface>::BindingWhen(Container *container, ObjectType object_type) noexcept
: _container(container), _object_type(std::move(object_type))
{
}
@@ -26,9 +23,9 @@ void BindingWhen<Interface>::when_tagged(std::string_view tag) noexcept
_container->remove(_object_type);
- auto object_type = ObjectType<Interface>(tag);
+ auto type = ObjectType::create<Interface>(tag);
- _container->add(object_type, wrapped);
+ _container->add(type, wrapped);
}
template <typename Interface>
@@ -46,11 +43,11 @@ auto BindingBuilder<Interface>::to() noexcept -> BindingWhen<Interface>
auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(*_container));
- auto object_type = ObjectType<Interface>();
+ auto type = ObjectType::create<Interface>();
- _container->add(object_type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
+ _container->add(type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
- return BindingWhen<Interface>(_container, object_type);
+ return BindingWhen<Interface>(_container, type);
}
template <typename Interface>
@@ -63,11 +60,11 @@ auto BindingBuilder<Interface>::to_factory(FactoryFunc factory) noexcept
auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(factory));
- auto object_type = ObjectType<Interface>();
+ auto type = ObjectType::create<Interface>();
- _container->add(object_type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
+ _container->add(type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
- return BindingWhen<Interface>(_container, object_type);
+ return BindingWhen<Interface>(_container, type);
}
template <typename Interface>
@@ -80,19 +77,19 @@ template <typename Interface>
requires Abstract<Interface>
auto Container::get() const noexcept -> std::unique_ptr<Interface>
{
- ObjectType<Interface> interface_type;
+ auto type = ObjectType::create<Interface>();
- if (_bindings.count(interface_type) == 0)
+ if (_bindings.count(type) == 0)
{
std::cerr
<< "Error: Tried to get a item from the container using unbound interface '"
- << interface_type.name() << "'" << std::endl;
+ << type.name() << "'" << std::endl;
exit(EXIT_FAILURE);
}
- auto wrapper = std::dynamic_pointer_cast<IWrapper<std::unique_ptr<Interface>>>(
- _bindings.at(interface_type)
- );
+ auto wrapper =
+ std::dynamic_pointer_cast<IWrapper<std::unique_ptr<Interface>>>(_bindings.at(type)
+ );
return wrapper->get();
}
@@ -101,7 +98,7 @@ template <typename AFactory>
requires IsFactory<AFactory>
auto Container::get() const noexcept -> AFactory
{
- BaseObjectType type = ObjectType<AFactory>();
+ auto type = ObjectType::create<AFactory>();
if (!_bindings.contains(type))
{
@@ -120,7 +117,7 @@ template <class Interface>
requires Abstract<Interface>
auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptr<Interface>
{
- BaseObjectType type = ObjectType<Interface>(tag);
+ auto type = ObjectType::create<Interface>(tag);
if (!_bindings.contains(type))
{
@@ -141,7 +138,7 @@ template <typename AFactory>
requires IsFactory<AFactory>
auto Container::get_tagged(const char *tag) const noexcept -> AFactory
{
- BaseObjectType type = ObjectType<AFactory>(tag);
+ auto type = ObjectType::create<AFactory>(tag);
if (!_bindings.contains(type))
{
diff --git a/include/yacppdic/object_type.hpp b/include/yacppdic/object_type.hpp
index 1765e65..3a7a639 100644
--- a/include/yacppdic/object_type.hpp
+++ b/include/yacppdic/object_type.hpp
@@ -6,17 +6,14 @@
namespace yacppdic
{
-class BaseObjectType
+class ObjectType
{
public:
- explicit BaseObjectType(const std::type_info &type_info) noexcept;
+ explicit ObjectType(const std::type_info &type_info) noexcept;
- explicit BaseObjectType(
- const std::type_info &type_info,
- std::string_view tag
- ) noexcept;
+ explicit ObjectType(const std::type_info &type_info, std::string_view tag) noexcept;
- auto operator==(const BaseObjectType &object_type) const noexcept -> bool;
+ auto operator==(const ObjectType &object_type) const noexcept -> bool;
[[nodiscard]] auto hash() const noexcept -> std::size_t;
@@ -24,26 +21,27 @@ public:
[[nodiscard]] auto tag() const noexcept -> std::string_view;
-private:
- const std::type_info &_type_info;
- const std::string_view _tag;
-};
-
-template <class Object>
-class ObjectType : public BaseObjectType
-{
-public:
- ObjectType() noexcept : BaseObjectType(typeid(Object)) {}
+ template <typename Type>
+ static auto create() noexcept -> ObjectType
+ {
+ return static_cast<ObjectType>(typeid(Type));
+ }
- explicit ObjectType(const std::string_view &tag) noexcept : BaseObjectType(typeid(Object), tag)
+ 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 BaseObjectType &object_type) const noexcept -> std::size_t;
+ auto operator()(const ObjectType &object_type) const noexcept -> std::size_t;
};
} // namespace yacppdic