aboutsummaryrefslogtreecommitdiff
path: root/src/DI
diff options
context:
space:
mode:
Diffstat (limited to 'src/DI')
-rw-r--r--src/DI/container.tpp14
-rw-r--r--src/DI/object_type.cpp14
-rw-r--r--src/DI/object_type.hpp9
3 files changed, 30 insertions, 7 deletions
diff --git a/src/DI/container.tpp b/src/DI/container.tpp
index 21bf81a..e5bf7f7 100644
--- a/src/DI/container.tpp
+++ b/src/DI/container.tpp
@@ -5,6 +5,8 @@
#include "function_wrapper.hpp"
#include "object_wrapper.hpp"
+#include <iostream>
+
template <class Interface>
BindingBuilder<Interface>::BindingBuilder(Container *container) : _container(container)
{
@@ -37,8 +39,18 @@ BindingBuilder<Interface> Container::bind()
template <class Interface, class>
std::shared_ptr<Interface> Container::get() const
{
+ ObjectType<Interface> interface_type;
+
+ if (bindings.count(interface_type) == 0)
+ {
+ std::cerr
+ << "Error: Tried to get a item from the container using unbound interface '"
+ << interface_type.name() << "'" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
auto wrapper = std::dynamic_pointer_cast<IWrapper<std::shared_ptr<Interface>>>(
- bindings.at(ObjectType<Interface>()));
+ bindings.at(interface_type));
return wrapper->get();
}
diff --git a/src/DI/object_type.cpp b/src/DI/object_type.cpp
index 60c7c78..f6004cb 100644
--- a/src/DI/object_type.cpp
+++ b/src/DI/object_type.cpp
@@ -1,17 +1,25 @@
#include "object_type.hpp"
-BaseObjectType::BaseObjectType(const std::type_info &type_info) : _type_info(type_info) {}
+BaseObjectType::BaseObjectType(const std::type_info &type_info) noexcept
+ : _type_info(type_info)
+{
+}
-bool BaseObjectType::operator==(const BaseObjectType &object_type) const
+bool BaseObjectType::operator==(const BaseObjectType &object_type) const noexcept
{
return hash() == object_type.hash();
}
-std::size_t BaseObjectType::hash() const
+std::size_t BaseObjectType::hash() const noexcept
{
return _type_info.hash_code();
}
+std::string_view BaseObjectType::name() const noexcept
+{
+ return std::string_view(_type_info.name());
+}
+
std::size_t ObjectTypeHasher::operator()(const BaseObjectType &object_type) const
{
return object_type.hash();
diff --git a/src/DI/object_type.hpp b/src/DI/object_type.hpp
index f690a64..3910c03 100644
--- a/src/DI/object_type.hpp
+++ b/src/DI/object_type.hpp
@@ -1,15 +1,18 @@
#pragma once
+#include <string_view>
#include <typeinfo>
class BaseObjectType
{
public:
- explicit BaseObjectType(const std::type_info &type_info);
+ explicit BaseObjectType(const std::type_info &type_info) noexcept;
- bool operator==(const BaseObjectType &object_type) const;
+ bool operator==(const BaseObjectType &object_type) const noexcept;
- [[nodiscard]] std::size_t hash() const;
+ [[nodiscard]] std::size_t hash() const noexcept;
+
+ [[nodiscard]] std::string_view name() const noexcept;
private:
const std::type_info &_type_info;