105 #ifndef LIB_DEPEND_INJECT_H 106 #define LIB_DEPEND_INJECT_H 117 #include <type_traits> 165 __assert_compatible<SUB>();
166 installFactory<SUB>();
181 using Sub =
typename SubclassFactoryType<FUN>::Subclass;
182 using Fun =
typename SubclassFactoryType<FUN>::Functor;
184 __assert_compatible<Sub>();
185 installFactory<Sub,Fun> (forward<FUN> (ctor));
201 template<
class IMP =SRV>
205 std::unique_ptr<IMP> instance_;
208 template<
typename...ARGS>
210 : instance_{
new IMP(forward<ARGS> (ctorArgs)...)}
212 __assert_compatible<IMP>();
213 activateServiceAccess (*instance_);
216 enum StartMode { NOT_YET_STARTED };
222 __assert_compatible<IMP>();
230 template<
typename...ARGS>
232 createInstance(ARGS&& ...ctorArgs)
234 instance_.reset (
new IMP(forward<ARGS> (ctorArgs)...));
235 activateServiceAccess (*instance_);
244 deactivateServiceAccess();
251 operator bool()
const 253 return bool(instance_);
267 return instance_.get();
280 template<
class MOC =SRV>
284 std::unique_ptr<MOC> mock_;
287 Factory origFactory_;
291 :
Local([]{
return new MOC{}; })
296 Local (FUN&& buildInstance)
298 __assert_compatible<MOC>();
299 __assert_compatible<typename SubclassFactoryType<FUN>::Subclass>();
301 temporarilyInstallAlternateFactory (origInstance_, origFactory_
304 mock_.reset (buildInstance());
310 restoreOriginalFactory (origInstance_, move(origFactory_));
314 operator bool()
const 354 __assert_compatible()
357 ,
"Installed implementation class must be compatible to the interface.");
364 throw error::Logic(
"Attempt to reconfigure dependency injection after the fact. " 365 "The previously installed factory (typically Singleton) was already used." 366 , error::LUMIERA_ERROR_LIFECYCLE);
369 template<
typename FUN>
373 "Need a Lambda or Function object to create a heap allocated instance");
375 using Functor =
typename meta::_Fun<FUN>::Functor;
377 using Subclass =
typename meta::Strip<ResultVal>::TypePlain;
379 static_assert (std::is_pointer<ResultVal>::value,
380 "Function must yield a pointer to a heap allocated instance");
384 template<
class SUB,
typename FUN>
386 installFactory (FUN&& ctor)
389 if (std::is_same<SRV,SUB>())
406 if (not std::is_same<SRV,SUB>())
416 template<
typename FUN>
418 temporarilyInstallAlternateFactory (SRV*& stashInstance, Factory& stashFac, FUN&& newFac)
428 restoreOriginalFactory (SRV*& stashInstance, Factory&& stashFac)
436 activateServiceAccess (SRV& newInstance)
440 throw error::Logic(
"Attempt to activate an external service implementation, " 441 "but another instance has already been dependency-injected." 442 , error::LUMIERA_ERROR_LIFECYCLE);
448 deactivateServiceAccess()
Configuration handle to expose a service implementation through the Depend<SRV> front-end.
Not meant to be instantiated in any way.
Types marked with this mix-in may be moved but not copied.
static void useSingleton()
configure dependency-injection for type SRV to build a subclass singleton.
Access point to singletons and other kinds of dependencies designated by type.
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera's exception hierarchy.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
This framework allows to (re)configure the lib::Depend front-end for dependency-injection.
Metaprogramming tools for transforming functor types.
A special implementation of lib::Sync, where the storage of the object monitor is associated directly...
MOC & triggerCreate()
trigger lazy service object instantiation
Singleton services and Dependency Injection.
Helpers for type detection, type rewriting and metaprogramming.
static void useSingleton(FUN &&ctor)
configure dependency-injection for type SRV to manage a subclass singleton, which is created lazily o...
Lumiera error handling (C++ interface).
A synchronisation protection guard employing a lock scoped to the parameter type as a whole...
Helper to abstract creation and lifecycle of a dependency.
ServiceInstance(StartMode)
create in deactivated state.
Configuration handle for temporarily shadowing a dependency by a test mock instance.