aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-09 18:46:00 +0200
committerHampusM <hampus@hampusmat.com>2022-06-09 18:46:00 +0200
commit203db8ffe8d4ee82ca126926232f4d4de21b7aec (patch)
tree5ff26298a748f3ddfb9a86c2fcf5c1bd6873e452
parent59d8202868364b6efeb92dbd694d961723b74cc0 (diff)
refactor: rename ObjectType to ObjectIdentifier
-rw-r--r--CMakeLists.txt2
-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
-rw-r--r--src/container.cpp6
-rw-r--r--src/object_identifier.cpp48
-rw-r--r--src/object_type.cpp52
8 files changed, 165 insertions, 143 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index af8fc8a..066e0cb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,7 +14,7 @@ target_sources(
${PROJECT_NAME}
PRIVATE
src/container.cpp
- src/object_type.cpp
+ src/object_identifier.cpp
)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
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
diff --git a/src/container.cpp b/src/container.cpp
index 044d487..f8c4931 100644
--- a/src/container.cpp
+++ b/src/container.cpp
@@ -7,19 +7,19 @@ namespace yacppdic
{
void Container::add(
- const ObjectType &type,
+ const ObjectIdentifier &type,
const WrapperPtr<IGenericWrapper> &wrapper
) noexcept
{
_bindings.insert({ type, wrapper });
}
-void Container::remove(const ObjectType &type) noexcept
+void Container::remove(const ObjectIdentifier &type) noexcept
{
_bindings.erase(type);
}
-auto Container::at(const ObjectType &type) const noexcept
+auto Container::at(const ObjectIdentifier &type) const noexcept
-> Container::WrapperPtr<IGenericWrapper>
{
return _bindings.at(type);
diff --git a/src/object_identifier.cpp b/src/object_identifier.cpp
new file mode 100644
index 0000000..a743271
--- /dev/null
+++ b/src/object_identifier.cpp
@@ -0,0 +1,48 @@
+#include "yacppdic/object_identifier.hpp"
+
+#include "yacppdic/detail/internal/hash.hpp"
+
+#include <functional>
+
+namespace yacppdic
+{
+
+ObjectIdentifier::ObjectIdentifier(const std::type_info &type_info) noexcept
+ : _type_info(type_info)
+{
+}
+
+ObjectIdentifier::ObjectIdentifier(
+ const std::type_info &type_info,
+ const std::string_view tag
+) noexcept
+ : _type_info(type_info), _tag(tag)
+{
+}
+
+auto ObjectIdentifier::operator==(const ObjectIdentifier &object_type) const noexcept
+ -> bool
+{
+ return hash() == object_type.hash();
+}
+
+auto ObjectIdentifier::hash() const noexcept -> std::size_t
+{
+ const auto type_hash = _type_info.hash_code();
+
+ return _tag.empty() ? type_hash
+ : combine_hashes(type_hash, std::hash<std::string_view>()(_tag));
+}
+
+auto ObjectIdentifier::name() const noexcept -> std::string_view
+{
+ return { _type_info.name() };
+}
+
+auto ObjectIdentifier::tag() const noexcept -> std::string_view
+{
+ return _tag;
+}
+
+} // namespace yacppdic
+
diff --git a/src/object_type.cpp b/src/object_type.cpp
deleted file mode 100644
index 15b798c..0000000
--- a/src/object_type.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "yacppdic/object_type.hpp"
-
-#include "yacppdic/detail/internal/hash.hpp"
-
-#include <functional>
-
-namespace yacppdic
-{
-
-ObjectType::ObjectType(const std::type_info &type_info) noexcept : _type_info(type_info)
-{
-}
-
-ObjectType::ObjectType(
- const std::type_info &type_info,
- const std::string_view tag
-) noexcept
- : _type_info(type_info), _tag(tag)
-{
-}
-
-auto ObjectType::operator==(const ObjectType &object_type) const noexcept -> bool
-{
- return hash() == object_type.hash();
-}
-
-auto ObjectType::hash() const noexcept -> std::size_t
-{
- const auto type_hash = _type_info.hash_code();
-
- return _tag == "" ? type_hash
- : combine_hashes(type_hash, std::hash<std::string_view>()(_tag));
-}
-
-auto ObjectType::name() const noexcept -> std::string_view
-{
- return { _type_info.name() };
-}
-
-auto ObjectType::tag() const noexcept -> std::string_view
-{
- return _tag;
-}
-
-auto ObjectTypeHasher::operator()(const ObjectType &object_type) const noexcept
- -> std::size_t
-{
- return object_type.hash();
-}
-
-} // namespace yacppdic
-