DI: inline into lib::Depend to obsolete InstanceHolder
but now we've got two factory functors. So there is yet more potential for simplification & refactoring
This commit is contained in:
parent
c3e149028f
commit
5fc85df385
5 changed files with 69 additions and 51 deletions
|
|
@ -105,49 +105,6 @@ namespace lib {
|
||||||
namespace error = lumiera::error;
|
namespace error = lumiera::error;
|
||||||
|
|
||||||
|
|
||||||
namespace { // Implementation helper...
|
|
||||||
|
|
||||||
using lib::meta::enable_if;
|
|
||||||
|
|
||||||
template<typename TAR, typename SEL =void>
|
|
||||||
class InstanceHolder
|
|
||||||
: util::NonCopyable
|
|
||||||
{
|
|
||||||
std::unique_ptr<TAR> instance_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
TAR*
|
|
||||||
buildInstance()
|
|
||||||
{
|
|
||||||
return buildInstance ([]{ return new TAR{}; });
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class FUN>
|
|
||||||
TAR*
|
|
||||||
buildInstance(FUN&& ctor)
|
|
||||||
{
|
|
||||||
if (instance_)
|
|
||||||
throw error::Fatal("Attempt to double-create a singleton service. "
|
|
||||||
"Either the application logic, or the compiler "
|
|
||||||
"or runtime system is seriously broken"
|
|
||||||
,error::LUMIERA_ERROR_LIFECYCLE);
|
|
||||||
instance_.reset (ctor());
|
|
||||||
return instance_.get();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename ABS>
|
|
||||||
class InstanceHolder<ABS, enable_if<std::is_abstract<ABS>>>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ABS*
|
|
||||||
buildInstance(...)
|
|
||||||
{
|
|
||||||
throw error::Fatal("Attempt to create a singleton instance of an abstract class. "
|
|
||||||
"Application architecture or lifecycle is seriously broken.");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}//(End)Implementation helper
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -181,8 +138,6 @@ namespace lib {
|
||||||
static std::atomic<SRV*> instance;
|
static std::atomic<SRV*> instance;
|
||||||
static Factory factory;
|
static Factory factory;
|
||||||
|
|
||||||
static InstanceHolder<SRV> singleton;
|
|
||||||
|
|
||||||
friend class DependInject<SRV>;
|
friend class DependInject<SRV>;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -204,7 +159,13 @@ namespace lib {
|
||||||
if (!object)
|
if (!object)
|
||||||
{
|
{
|
||||||
if (!factory)
|
if (!factory)
|
||||||
object = singleton.buildInstance();
|
{
|
||||||
|
object = buildInstance<SRV>();
|
||||||
|
deleter = []{
|
||||||
|
destroy (instance);
|
||||||
|
instance = nullptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
else
|
else
|
||||||
object = factory();
|
object = factory();
|
||||||
factory = disabledFactory;
|
factory = disabledFactory;
|
||||||
|
|
@ -224,6 +185,48 @@ namespace lib {
|
||||||
throw error::Fatal("Service not available at this point of the Application Lifecycle"
|
throw error::Fatal("Service not available at this point of the Application Lifecycle"
|
||||||
,error::LUMIERA_ERROR_LIFECYCLE);
|
,error::LUMIERA_ERROR_LIFECYCLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class ABS>
|
||||||
|
static meta::enable_if<std::is_abstract<ABS>,
|
||||||
|
ABS* >
|
||||||
|
buildInstance()
|
||||||
|
{
|
||||||
|
throw error::Fatal("Attempt to create a singleton instance of an abstract class. "
|
||||||
|
"Application architecture or lifecycle is seriously broken.");
|
||||||
|
}
|
||||||
|
template<class TAR>
|
||||||
|
static meta::disable_if<std::is_abstract<TAR>,
|
||||||
|
TAR* >
|
||||||
|
buildInstance()
|
||||||
|
{
|
||||||
|
return new TAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
destroy (SRV* obj)
|
||||||
|
{
|
||||||
|
if (obj)
|
||||||
|
delete obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Deleter
|
||||||
|
{
|
||||||
|
std::function<void(void)> cleanUp_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
~Deleter()
|
||||||
|
{
|
||||||
|
if (cleanUp_)
|
||||||
|
cleanUp_();
|
||||||
|
}
|
||||||
|
template<typename DEL>
|
||||||
|
void operator= (DEL&& fun)
|
||||||
|
{
|
||||||
|
cleanUp_ = std::forward<DEL> (fun);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static Deleter deleter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -237,7 +240,7 @@ namespace lib {
|
||||||
typename Depend<SRV>::Factory Depend<SRV>::factory;
|
typename Depend<SRV>::Factory Depend<SRV>::factory;
|
||||||
|
|
||||||
template<class SRV>
|
template<class SRV>
|
||||||
InstanceHolder<SRV> Depend<SRV>::singleton;
|
typename Depend<SRV>::Deleter Depend<SRV>::deleter;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -306,7 +306,7 @@ apologies for that."
|
||||||
static regex identifierChars {"[A-Za-z]\\w*", regex::ECMAScript | regex::optimize};
|
static regex identifierChars {"[A-Za-z]\\w*", regex::ECMAScript | regex::optimize};
|
||||||
|
|
||||||
return regex_replace (text, identifierChars, "$&", std::regex_constants::format_no_copy);
|
return regex_replace (text, identifierChars, "$&", std::regex_constants::format_no_copy);
|
||||||
}
|
} // don't copy what does not match
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,8 +66,7 @@ namespace test{
|
||||||
Interface () : TestTargetObj(cnt) {}
|
Interface () : TestTargetObj(cnt) {}
|
||||||
virtual ~Interface() {}
|
virtual ~Interface() {}
|
||||||
|
|
||||||
friend class lib::InstanceHolder<Interface>;
|
friend class lib::Depend<Interface>;
|
||||||
friend class std::default_delete<Interface>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int Interface::cnt = 0;
|
int Interface::cnt = 0;
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ namespace test{
|
||||||
protected:
|
protected:
|
||||||
TargetObj () : TestTargetObj(cnt) {}
|
TargetObj () : TestTargetObj(cnt) {}
|
||||||
|
|
||||||
friend class lib::InstanceHolder<TargetObj>;
|
friend class lib::Depend<TargetObj>;
|
||||||
};
|
};
|
||||||
|
|
||||||
int TargetObj::cnt = 0;
|
int TargetObj::cnt = 0;
|
||||||
|
|
|
||||||
|
|
@ -27802,6 +27802,22 @@
|
||||||
</node>
|
</node>
|
||||||
</node>
|
</node>
|
||||||
</node>
|
</node>
|
||||||
|
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1522334796724" ID="ID_1080992734" MODIFIED="1522334873818" TEXT="Funktoren zusammenführen">
|
||||||
|
<icon BUILTIN="flag-yellow"/>
|
||||||
|
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1522334843062" ID="ID_1526236017" MODIFIED="1522334870859" TEXT="DependencyFactory für">
|
||||||
|
<icon BUILTIN="flag-yellow"/>
|
||||||
|
<node CREATED="1522334805635" ID="ID_315782435" MODIFIED="1522334817813" TEXT="Erzeugungs-Funktor"/>
|
||||||
|
<node CREATED="1522334818473" ID="ID_750129060" MODIFIED="1522334821036" TEXT="Deleter"/>
|
||||||
|
<node CREATED="1522334822001" ID="ID_644059909" MODIFIED="1522334935421" TEXT="automatisches Instanz-Management"/>
|
||||||
|
</node>
|
||||||
|
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1522334859291" ID="ID_1496044958" MODIFIED="1522334866755" TEXT="einfach konfigurierbar">
|
||||||
|
<icon BUILTIN="flag-yellow"/>
|
||||||
|
<node CREATED="1522334896942" ID="ID_1215786079" MODIFIED="1522334902129" TEXT="default-Fall"/>
|
||||||
|
<node CREATED="1522334903741" ID="ID_1875541619" MODIFIED="1522334911776" TEXT="abstrakte Basisklasse"/>
|
||||||
|
<node CREATED="1522334912412" ID="ID_1391948557" MODIFIED="1522334916463" TEXT="custom-Funktor"/>
|
||||||
|
<node CREATED="1522334917035" ID="ID_259163474" MODIFIED="1522334922006" TEXT="custom-Deleter"/>
|
||||||
|
</node>
|
||||||
|
</node>
|
||||||
</node>
|
</node>
|
||||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521160700669" HGAP="4" ID="ID_978221585" MODIFIED="1521791636399" TEXT="Dokumentation" VSHIFT="25">
|
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521160700669" HGAP="4" ID="ID_978221585" MODIFIED="1521791636399" TEXT="Dokumentation" VSHIFT="25">
|
||||||
<icon BUILTIN="flag-yellow"/>
|
<icon BUILTIN="flag-yellow"/>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue