2018-03-11 03:20:21 +01:00
|
|
|
/*
|
|
|
|
|
DEPEND.hpp - access point to singletons and dependencies
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2013, Hermann Vosseler <Ichthyostega@web.de>
|
2018-03-19 05:27:37 +01:00
|
|
|
2018, Hermann Vosseler <Ichthyostega@web.de>
|
2018-03-11 03:20:21 +01:00
|
|
|
|
|
|
|
|
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
|
2018-03-25 09:32:35 +02:00
|
|
|
** 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
|
2018-03-11 03:20:21 +01:00
|
|
|
** 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
|
2018-03-25 09:32:35 +02:00
|
|
|
** 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_.
|
2018-03-11 03:20:21 +01:00
|
|
|
**
|
2018-03-25 09:32:35 +02:00
|
|
|
** 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.
|
2018-03-11 03:20:21 +01:00
|
|
|
**
|
2018-03-25 09:32:35 +02:00
|
|
|
** ## 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
|
2018-03-19 05:27:37 +01:00
|
|
|
** @see Singleton_test
|
|
|
|
|
** @see DependencyConfiguration_test
|
2018-03-11 03:20:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WIP_LIB_DEPEND_H
|
|
|
|
|
#define WIP_LIB_DEPEND_H
|
|
|
|
|
|
|
|
|
|
|
2018-03-19 03:31:59 +01:00
|
|
|
#include "lib/error.hpp"
|
2018-03-24 05:35:13 +01:00
|
|
|
#include "lib/nocopy.hpp"
|
2018-03-19 03:31:59 +01:00
|
|
|
#include "lib/nobug-init.hpp"
|
2018-03-11 03:20:21 +01:00
|
|
|
#include "lib/sync-classlock.hpp"
|
2018-03-19 03:31:59 +01:00
|
|
|
#include "lib/meta/util.hpp"
|
|
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <functional>
|
2018-03-24 10:13:08 +01:00
|
|
|
#include <atomic>
|
2018-03-19 03:31:59 +01:00
|
|
|
#include <memory>
|
2018-03-11 03:20:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2018-03-19 03:31:59 +01:00
|
|
|
namespace error = lumiera::error;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-25 09:32:35 +02:00
|
|
|
|
|
|
|
|
/**
|
2018-03-19 03:31:59 +01:00
|
|
|
* @internal access point to reconfigure dependency injection on a per type base
|
|
|
|
|
* @see depend-inject.hpp
|
|
|
|
|
*/
|
|
|
|
|
template<class SRV>
|
|
|
|
|
class DependInject;
|
|
|
|
|
|
2018-03-11 03:20:21 +01:00
|
|
|
|
|
|
|
|
/**
|
2018-03-25 09:32:35 +02:00
|
|
|
* 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.
|
2018-03-19 03:31:59 +01:00
|
|
|
* @tparam SRV the class of the Service or Singleton instance
|
2018-03-11 03:20:21 +01:00
|
|
|
* @note uses static fields internally, so all factory configuration is shared per type
|
2018-03-25 09:32:35 +02:00
|
|
|
* @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.
|
2018-03-11 03:20:21 +01:00
|
|
|
*/
|
2018-03-19 03:31:59 +01:00
|
|
|
template<class SRV>
|
|
|
|
|
class Depend
|
2018-03-11 03:20:21 +01:00
|
|
|
{
|
2018-03-19 03:31:59 +01:00
|
|
|
using Factory = std::function<SRV*()>;
|
2018-03-24 09:03:14 +01:00
|
|
|
using Lock = ClassLock<SRV, NonrecursiveLock_NoWait>;
|
2018-03-19 03:31:59 +01:00
|
|
|
|
2018-03-24 10:13:08 +01:00
|
|
|
static std::atomic<SRV*> instance;
|
|
|
|
|
static Factory factory;
|
2018-03-11 03:20:21 +01:00
|
|
|
|
2018-03-19 03:31:59 +01:00
|
|
|
friend class DependInject<SRV>;
|
2018-03-11 03:20:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/** Interface to be used by clients for retrieving the service instance.
|
|
|
|
|
* Manages the instance creation, lifecycle and access in multithreaded context.
|
2018-03-19 03:31:59 +01:00
|
|
|
* @return instance of class `SRV`. When used in default configuration,
|
2018-03-11 03:20:21 +01:00
|
|
|
* this service instance is a singleton
|
|
|
|
|
*/
|
2018-03-19 03:31:59 +01:00
|
|
|
SRV&
|
2018-03-11 03:20:21 +01:00
|
|
|
operator() ()
|
|
|
|
|
{
|
2018-03-24 10:13:08 +01:00
|
|
|
SRV* object = instance.load (std::memory_order_acquire);
|
|
|
|
|
if (!object)
|
2018-03-24 09:31:46 +01:00
|
|
|
{
|
2018-03-24 10:13:08 +01:00
|
|
|
Lock guard;
|
|
|
|
|
|
|
|
|
|
object = instance.load (std::memory_order_relaxed);
|
|
|
|
|
if (!object)
|
|
|
|
|
{
|
|
|
|
|
if (!factory)
|
2018-03-29 16:57:55 +02:00
|
|
|
{
|
|
|
|
|
object = buildInstance<SRV>();
|
|
|
|
|
deleter = []{
|
|
|
|
|
destroy (instance);
|
|
|
|
|
instance = nullptr;
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-03-24 10:13:08 +01:00
|
|
|
else
|
|
|
|
|
object = factory();
|
|
|
|
|
factory = disabledFactory;
|
|
|
|
|
}
|
|
|
|
|
instance.store (object, std::memory_order_release);
|
2018-03-24 09:31:46 +01:00
|
|
|
}
|
2018-03-25 09:32:35 +02:00
|
|
|
ENSURE (object);
|
2018-03-24 10:13:08 +01:00
|
|
|
return *object;
|
2018-03-11 03:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-24 10:13:08 +01:00
|
|
|
|
2018-03-25 09:32:35 +02:00
|
|
|
private:
|
|
|
|
|
/** @internal preconfigured factory to block any (further) on-demand instance creation */
|
2018-03-19 03:31:59 +01:00
|
|
|
static SRV*
|
|
|
|
|
disabledFactory()
|
2018-03-11 03:20:21 +01:00
|
|
|
{
|
2018-03-19 03:31:59 +01:00
|
|
|
throw error::Fatal("Service not available at this point of the Application Lifecycle"
|
|
|
|
|
,error::LUMIERA_ERROR_LIFECYCLE);
|
2018-03-11 03:20:21 +01:00
|
|
|
}
|
2018-03-29 16:57:55 +02:00
|
|
|
|
|
|
|
|
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;
|
2018-03-11 03:20:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-19 03:31:59 +01:00
|
|
|
/* === allocate Storage for static per type instance management === */
|
|
|
|
|
template<class SRV>
|
2018-03-24 10:13:08 +01:00
|
|
|
std::atomic<SRV*> Depend<SRV>::instance;
|
2018-03-19 03:31:59 +01:00
|
|
|
|
|
|
|
|
template<class SRV>
|
|
|
|
|
typename Depend<SRV>::Factory Depend<SRV>::factory;
|
2018-03-11 03:20:21 +01:00
|
|
|
|
2018-03-19 03:31:59 +01:00
|
|
|
template<class SRV>
|
2018-03-29 16:57:55 +02:00
|
|
|
typename Depend<SRV>::Deleter Depend<SRV>::deleter;
|
2018-03-11 03:20:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lib
|
|
|
|
|
#endif
|