diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-27 12:54:10 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:53 +0200 |
commit | 5864e5abc43b201c3801fa39a2fcaf9e3a9e8914 (patch) | |
tree | 98e5e324066ef4d1cbd3cc4c792a258fbd86c12d /src/DI/container.hpp | |
parent | e233dc28491c33e8a7dc0a11576d3b8ce91cce2c (diff) |
refactor: use dependency injection
Diffstat (limited to 'src/DI/container.hpp')
-rw-r--r-- | src/DI/container.hpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/DI/container.hpp b/src/DI/container.hpp new file mode 100644 index 0000000..2402894 --- /dev/null +++ b/src/DI/container.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include "DI/object_type.hpp" +#include "interfaces/wrapper.hpp" + +#include <functional> +#include <memory> +#include <type_traits> +#include <unordered_map> + +class Container; + +template <typename Interface> +class BindingBuilder +{ +public: + explicit BindingBuilder(Container *container); + + template <class ObjectImpl, + class = std::enable_if_t<std::is_base_of_v<Interface, ObjectImpl>>> + void to(); + + void to_factory(Interface func); + +private: + Container *_container; +}; + +template <typename Satan> +struct is_func : public std::false_type // NOLINT(readability-identifier-naming) +{ +}; + +template <typename Satan, typename... Args> +struct is_func<std::function<Satan(Args...)>> : public std::true_type +{ +}; + +class Container +{ +public: + Container() = default; + + template <class Interface> + BindingBuilder<Interface> bind(); + + template <class Interface, class = std::enable_if_t<std::is_abstract_v<Interface>>> + std::shared_ptr<Interface> get() const; + + /* + template <typename Interface, + typename = std::enable_if_t< + !std::is_abstract_v<Interface> && + std::is_invocable_v<Interface, typename Interface::argument_type>>> + Interface get() const; + */ + + template <typename Interface, typename = std::enable_if_t<is_func<Interface>::value>> + Interface get() const; + + std::unordered_map<BaseObjectType, std::shared_ptr<IGenericWrapper>, ObjectTypeHasher> + bindings; +}; + +#include "container.tpp" |