header_utils
Loading...
Searching...
No Matches
di.h
1
4
5#pragma once
6
7#if !defined(__cpp_concepts)
8#error "This library requires concepts"
9#endif
10
11#include <typeindex>
12#include <vector>
13#include <memory>
14#include <map>
15#include <functional>
16#include <string>
17#include <thread>
18
31{
32 template <typename T, typename... OTHERS>
33 constexpr inline bool is_same_as_any_v = std::disjunction_v<std::is_same<std::decay_t<OTHERS>, std::decay_t<T>>...>;
34
35 enum class Lifetime
36 {
37 Default,
38 Transient,
39 InstanceSingleton,
40 WeakSingleton,
41 ThreadSingleton,
42 };
43
44 struct DefaultImplementationStruct {};
45 constexpr inline DefaultImplementationStruct DefaultImplementation;
46
49
50 struct Container
51 {
52 Lifetime DefaultLifetime = Lifetime::Transient;
53
55
56 template <typename INTERFACE, typename IMPLEMENTATION, typename... ARGS>
57 void RegisterType(ARGS&&... args);
58
59 template <typename INTERFACE>
60 bool HasAnyImplementationsOf() const;
61
63
64 template <typename INTERFACE>
65 std::shared_ptr<INTERFACE> Resolve();
66
67 template <typename INTERFACE>
68 std::shared_ptr<INTERFACE> ResolveByName(std::string_view name);
69
70 template <typename INTERFACE>
71 std::vector<std::shared_ptr<INTERFACE>> ResolveAll();
72
74
75 template <typename TYPE>
76 std::shared_ptr<TYPE> Create();
77
78 template <typename TYPE>
79 std::unique_ptr<TYPE> CreateRaw();
80
81 void DestroyAll()
82 {
83 mContainers.clear();
84 mParentContainer = nullptr;
85 }
86
87 auto const& Containers() const { return mContainers; }
88 auto const& DebugStore() const { return mDebugStore; }
89
90 private:
91
92 struct BaseInterfaceContainer
93 {
94 virtual ~BaseInterfaceContainer() noexcept = default;
95 explicit BaseInterfaceContainer(Lifetime default_lifetime) : DefaultLifetime(default_lifetime) {}
96 Lifetime DefaultLifetime = Lifetime::Default;
97 };
98
99 template <typename INTERFACE>
100 struct InterfaceContainer;
101
102 template <typename INTERFACE>
103 struct ImplementationContainer;
104
105 template <typename INTERFACE>
106 InterfaceContainer<INTERFACE>& GetInterfaceContainer();
107
108 template <typename INTERFACE, typename IMPLEMENTATION>
109 ImplementationContainer<INTERFACE>* GetImplementationContainer();
110
111 std::map<std::type_index, std::unique_ptr<BaseInterfaceContainer>> mContainers;
112 std::vector<std::type_index> mResolutionStack;
113 std::vector<std::pair<std::shared_ptr<void>, std::function<void(Container&, std::shared_ptr<void>)>>> mCreationsToReport;
114 Container* mParentContainer = nullptr;
115
116 std::map<void const*, std::pair<std::type_index, std::weak_ptr<void>>> mDebugStore;
117
118 template <typename INSTANCE>
119 void ReportCreation(std::shared_ptr<INSTANCE> const& obj, std::function<void(Container&, std::shared_ptr<void>)> func);
120
121 void ReportAwaitingCreations()
122 {
123 auto creations = std::exchange(mCreationsToReport, {});
124 for (auto&& [ptr, callback] : creations)
125 callback(*this, std::move(ptr));
126 }
127
128 template <typename INTERFACE, typename T>
129 std::shared_ptr<INTERFACE> Instantiate(T& factory);
130 };
131}
132
133#include "di_impl.h"
constexpr auto bit_count
Equal to the number of bits in the type.
Definition bits.h:33
TODO: Split into ContainerBuilder and Container (or [Dependency]Registry and [Dependency]Container) O...
Definition di.h:51
std::shared_ptr< TYPE > Create()
Other.
Definition di_impl.h:410
std::shared_ptr< INTERFACE > Resolve()
Resolves.
Definition di_impl.h:386
void RegisterType(ARGS &&... args)
Registers.
Definition di_impl.h:366