114 #ifndef LIB_DEPEND_INJECT_H 115 #define LIB_DEPEND_INJECT_H 126 #include <type_traits> 174 __assert_compatible<SUB>();
175 installFactory<SUB>();
190 using Sub =
typename SubclassFactoryType<FUN>::Subclass;
191 using Fun =
typename SubclassFactoryType<FUN>::Functor;
193 __assert_compatible<Sub>();
194 installFactory<Sub,Fun> (forward<FUN> (ctor));
210 template<
class IMP =SRV>
214 std::unique_ptr<IMP> instance_;
217 template<
typename...ARGS>
219 : instance_{
new IMP(forward<ARGS> (ctorArgs)...)}
221 __assert_compatible<IMP>();
222 activateServiceAccess (*instance_);
225 enum StartMode { NOT_YET_STARTED };
231 __assert_compatible<IMP>();
239 template<
typename...ARGS>
241 createInstance(ARGS&& ...ctorArgs)
243 instance_.reset (
new IMP(forward<ARGS> (ctorArgs)...));
244 activateServiceAccess (*instance_);
253 deactivateServiceAccess();
260 operator bool()
const 262 return bool(instance_);
276 return instance_.get();
289 template<
class MOC =SRV>
293 std::unique_ptr<MOC> mock_;
296 Factory origFactory_;
300 :
Local([]{
return new MOC{}; })
305 Local (FUN&& buildInstance)
307 __assert_compatible<MOC>();
308 __assert_compatible<typename SubclassFactoryType<FUN>::Subclass>();
310 temporarilyInstallAlternateFactory (origInstance_, origFactory_
313 mock_.reset (buildInstance());
319 restoreOriginalFactory (origInstance_, move(origFactory_));
323 operator bool()
const 363 __assert_compatible()
366 ,
"Installed implementation class must be compatible to the interface.");
373 throw error::Logic(
"Attempt to reconfigure dependency injection after the fact. " 374 "The previously installed factory (typically Singleton) was already used." 375 , error::LUMIERA_ERROR_LIFECYCLE);
378 template<
typename FUN>
382 "Need a Lambda or Function object to create a heap allocated instance");
384 using Functor =
typename meta::_Fun<FUN>::Functor;
386 using Subclass =
typename meta::Strip<ResultVal>::TypePlain;
388 static_assert (std::is_pointer<ResultVal>::value,
389 "Function must yield a pointer to a heap allocated instance");
393 template<
class SUB,
typename FUN>
395 installFactory (FUN&& ctor)
398 if (std::is_same<SRV,SUB>())
415 if (not std::is_same<SRV,SUB>())
425 template<
typename FUN>
427 temporarilyInstallAlternateFactory (SRV*& stashInstance, Factory& stashFac, FUN&& newFac)
437 restoreOriginalFactory (SRV*& stashInstance, Factory&& stashFac)
445 activateServiceAccess (SRV& newInstance)
449 throw error::Logic(
"Attempt to activate an external service implementation, " 450 "but another instance has already been dependency-injected." 451 , error::LUMIERA_ERROR_LIFECYCLE);
457 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.