lumiera_/src/lib/depend2.hpp

331 lines
11 KiB
C++
Raw Normal View History

/*
DEPEND.hpp - access point to singletons and dependencies
Copyright (C) Lumiera.org
2013, Hermann Vosseler <Ichthyostega@web.de>
2018, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/** @file depend.hpp
** Singleton services and Dependency Injection.
** The <b>Singleton Pattern</b> provides a single access point to a class or
** service and exploits this ubiquitous access point to limit the number of objects
** of this type to a single shared instance. Within Lumiera, we mostly employ a
** factory template for this purpose; the intention is to use on-demand initialisation
** and a standardised lifecycle. In the default configuration, this `Depend<TY>` factory
** maintains a singleton instance of type `TY`. The possibility to install other factory
** functions allows for subclass creation and various other kinds of service management.
**
**
** # Why Singletons? Inversion-of-Control and Dependency Injection
**
** Singletons are frequently over-used, and often they serve as disguised
** global variables to support a procedural programming style. As a remedy, typically
** the use of a »Dependency Injection Container« is promoted. And -- again typically --
** these DI containers tend to evolve into heavyweight universal tools and substitute
** the original problem by metadata hell.
**
** Thus, for Lumiera, the choice to use Singletons was deliberate: we understand the
** Inversion-of-Control principle, yet we want to stay just below the level of building
** a central application manager core. At the usage site, we access a factory for some
** service *by name*, where the »name« is actually the type name of an interface or
** facade. Singleton is used as an _implementation_ of this factory, when the service
** is self-contained and can be brought up lazily.
**
** ## Conventions, Lifecycle and Unit Testing
**
** Usually we place an instance of the singleton factory (or some other kind of factory)
** as a static variable within the interface class describing the service or facade.
** As a rule, everything accessible as Singleton is sufficiently self-contained to come
** up any time -- even prior to `main()`. But at shutdown, any deregistration must be done
** explicitly using a lifecycle hook. In Lumiera, destructors aren't allowed to do
** _any significant work_ beyond releasing references, and we acknowledge that
** singletons can be released in _arbitrary order_.
**
** Lifecycle and management of dependencies is beyond the scope of this access mechanism
** exposed here. However, the actual product to be created or exposed lazily can be
** configured behind the scenes, as long as this configuration is performed _prior_
** to the first access. This configuration is achieved with the help of the "sibling"
** template lib::DependInject, which is declared friend within `Depend<T>` for type `T`
** - a service with distinct lifecycle can be exposed through the `Depend<T>` front-end
** - it is possible to create a mock instance, which temporarily shadows what
** Depend<T> delivers on access.
**
** ## Implementation and performance
**
** Due to this option for flexible configuration, the implementation can not be built
** as Meyer's Singleton. Rather, Double Checked Locking of a Mutex is combined with an
** std::atomic to work around the known (rather theoretical) problems of this pattern.
** Microbenchmarks indicate that this implementation technique ranges close to the
** speed of a direct access to an already existing object; in the fully optimised
** variant it was found to be roughly at 1ns and thus about 3 to 4 times slower
** than the comparable unprotected direct access without lazy initialisation.
** This is orders of magnitude better than any flavour of conventional locking.
**
** @see depend-inject.hpp
** @see lib::DependInject
** @see Singleton_test
** @see DependencyConfiguration_test
*/
#ifndef WIP_LIB_DEPEND_H
#define WIP_LIB_DEPEND_H
#include "lib/error.hpp"
#include "lib/nocopy.hpp"
#include "lib/nobug-init.hpp"
#include "lib/sync-classlock.hpp"
#include "lib/meta/util.hpp"
#include <type_traits>
#include <functional>
#include <atomic>
#include <memory>
namespace lib {
namespace error = lumiera::error;
/**
* Helper to abstract creation and lifecycle of a dependency
*/
template<class OBJ>
class DependencyFactory
: util::MoveOnly
{
using Creator = std::function<OBJ*()>;
using Deleter = std::function<void()>;
Creator creator_;
Deleter deleter_;
public:
DependencyFactory() = default;
~DependencyFactory()
{
if (deleter_)
deleter_();
}
explicit operator bool() const
{
return bool(creator_);
}
OBJ*
operator() ()
{
return creator_? creator_()
: buildAndManage();
}
OBJ*
buildAndManage()
{
OBJ* obj = buildInstance<OBJ>();
atDestruction ([obj]{ delete obj; });
}
template<typename FUN>
DependencyFactory&
atDestruction (FUN&& additionalAction)
{
if (deleter_)
{
Deleter oldDeleter{std::move (deleter_)};
deleter_ = [oldDeleter, additionalAction]
{
additionalAction();
oldDeleter();
};
}
else
deleter_ = std::forward<FUN> (additionalAction);
return *this;
}
private:
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 (OBJ* obj)
{
if (obj)
delete obj;
}
};
/**
* @internal access point to reconfigure dependency injection on a per type base
* @see depend-inject.hpp
*/
template<class SRV>
class DependInject;
/**
* Access point to singletons and other kinds of dependencies designated *by type*.
* Actually this is a Factory object, which is typically placed into a static field
* of the Singleton (target) class or some otherwise suitable interface.
* @tparam SRV the class of the Service or Singleton instance
* @note uses static fields internally, so all factory configuration is shared per type
* @remarks
* - threadsafe lazy instantiation implemented by Double Checked Locking with std::atomic.
* - by default, without any explicit configuration, this template creates a singleton.
* - a per-type factory function can be configured with the help of lib::DependInject<SRV>
* - singletons will be destroyed when the embedded static InstanceHolder is destroyed.
*/
template<class SRV>
class Depend
{
using Factory = std::function<SRV*()>;
using Lock = ClassLock<SRV, NonrecursiveLock_NoWait>;
static std::atomic<SRV*> instance;
static Factory factory;
friend class DependInject<SRV>;
public:
/** Interface to be used by clients for retrieving the service instance.
* Manages the instance creation, lifecycle and access in multithreaded context.
* @return instance of class `SRV`. When used in default configuration,
* this service instance is a singleton
*/
SRV&
operator() ()
{
SRV* object = instance.load (std::memory_order_acquire);
if (!object)
{
Lock guard;
object = instance.load (std::memory_order_relaxed);
if (!object)
{
if (!factory)
{
object = buildInstance<SRV>();
deleter = []{
destroy (instance);
instance = nullptr;
};
}
else
object = factory();
factory = disabledFactory;
}
instance.store (object, std::memory_order_release);
}
ENSURE (object);
return *object;
}
private:
/** @internal preconfigured factory to block any (further) on-demand instance creation */
static SRV*
disabledFactory()
{
throw error::Fatal("Service not available at this point of the Application 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;
};
/* === allocate Storage for static per type instance management === */
template<class SRV>
std::atomic<SRV*> Depend<SRV>::instance;
template<class SRV>
typename Depend<SRV>::Factory Depend<SRV>::factory;
template<class SRV>
typename Depend<SRV>::Deleter Depend<SRV>::deleter;
} // namespace lib
#endif