7#if !defined(__cpp_concepts)
8#error "This library requires concepts"
18#include "min-cpp-version/cpp20.h"
45 struct DefaultImplementationStruct {};
75 template <
typename INTERFACE,
typename IMPLEMENTATION,
typename... PROPS>
79 template <
typename INTERFACE>
88 template <
typename INTERFACE>
89 std::shared_ptr<INTERFACE>
Resolve();
93 template <
typename INTERFACE>
94 std::shared_ptr<INTERFACE>
ResolveByName(std::string_view name);
97 template <
typename INTERFACE>
98 std::vector<std::shared_ptr<INTERFACE>>
ResolveAll();
120 template <
typename TYPE>
121 std::shared_ptr<TYPE>
Create();
124 template <
typename TYPE>
132 mParentContainer =
nullptr;
138 auto const& Containers()
const {
return mContainers; }
141 auto const& DebugStore()
const {
return mDebugStore; }
146 struct BaseInterfaceContainer
148 virtual ~BaseInterfaceContainer() noexcept = default;
151 virtual bool HasAnyImplementations()
const = 0;
154 template <
typename INTERFACE>
155 struct InterfaceContainer;
157 template <
typename INTERFACE>
158 struct ImplementationContainer;
160 template <
typename INTERFACE>
161 InterfaceContainer<INTERFACE>& GetInterfaceContainer();
163 template <
typename INTERFACE,
typename IMPLEMENTATION>
164 ImplementationContainer<INTERFACE>* GetImplementationContainer();
166 std::map<std::type_index, std::unique_ptr<BaseInterfaceContainer>> mContainers;
167 std::vector<std::type_index> mResolutionStack;
168 std::vector<std::pair<std::shared_ptr<void>, std::function<void(Container&, std::shared_ptr<void>)>>> mCreationsToReport;
169 Container* mParentContainer =
nullptr;
171 std::map<void const*, std::pair<std::type_index, std::weak_ptr<void>>> mDebugStore;
173 template <
typename INSTANCE>
174 void ReportCreation(std::shared_ptr<INSTANCE>
const& obj, std::function<
void(Container&, std::shared_ptr<void>)> func);
176 void ReportAwaitingCreations()
178 auto creations = std::exchange(mCreationsToReport, {});
179 for (
auto&& [ptr, callback] : creations)
180 callback(*
this, std::move(ptr));
183 template <
typename INTERFACE,
typename T>
184 std::shared_ptr<INTERFACE> Instantiate(T& factory);
188 template <
typename T>
If the interface or implementation has a DefaultLifetime static member field, that specified lifetime...
constexpr DefaultImplementationStruct DefaultImplementation
Used to specify an implementation is the default one for the interface being registered.
Lifetime
Specifies the lifetime of an implementation.
@ WeakSingleton
Same as InstanceSingleton, but will not hold on to an instance, so when your last shared_ptr to it wi...
@ InstanceSingleton
If a singleton instance of the implementation has not yet been created, it will be created and return...
@ ThreadSingleton
Same as InstanceSingleton, but each thread gets its own instance.
@ Default
Does not specify which lifetime of the implementation you want; you will be given one that's availabl...
@ Transient
Will always give you a freshly instantiated implementation object when requesting the interface.
The container for implementations; used to request interfaces.
std::shared_ptr< TYPE > Create()
Creates a new (shared pointer to an) object of TYPE, resolving any interface pointer parameters its c...
std::shared_ptr< INTERFACE > Resolve()
Returns a pointer to an implementation of INTERFACE.
std::unique_ptr< TYPE > CreateRaw()
Same as Create, but returns a unique_ptr to the new object.
std::vector< std::shared_ptr< INTERFACE > > ResolveAll()
Will return a vector of all available implementations of INTERFACE; useful for interfaces like image_...
void DestroyAll()
Unregisters all the registered implementations and removes any pointers to their instances.
std::shared_ptr< INTERFACE > ResolveByName(std::string_view name)
Returns a pointer to an implementation of INTERFACE with the name name If an implementation with that...
bool HasAnyImplementationsOf() const
Checks if there are any implementation registered for INTERFACE.
Lifetime DefaultLifetime
The default lifetime of implementations in this container, unless specified otherwise by the interfac...
void RegisterType(PROPS &&... properties)
Registers the type IMPLEMENTATION as an implementation of INTERFACE.