2007-09-06 22:13:26 +02:00
|
|
|
/*
|
2010-12-18 00:58:19 +01:00
|
|
|
SINGLETON-FACTORY.hpp - template for implementing the singleton pattern
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-03-10 04:25:03 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-09-06 22:13:26 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2007-09-06 22:13:26 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
====================================================================
|
|
|
|
|
This code is heavily inspired by
|
|
|
|
|
The Loki Library (loki-lib/trunk/include/loki/Singleton.h)
|
|
|
|
|
Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
|
This Loki code accompanies the book:
|
|
|
|
|
Alexandrescu, Andrei. "Modern C++ Design: Generic Programming
|
|
|
|
|
and Design Patterns Applied".
|
|
|
|
|
Copyright (c) 2001. Addison-Wesley. ISBN 0201704315
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-18 00:58:19 +01:00
|
|
|
#ifndef LIB_SINGLETON_FACTORY_H
|
|
|
|
|
#define LIB_SINGLETON_FACTORY_H
|
2007-09-06 22:13:26 +02:00
|
|
|
|
|
|
|
|
|
2010-12-18 00:58:19 +01:00
|
|
|
#include "lib/singleton-policies.hpp" // several Policies usable together with SingletonFactory
|
2007-09-06 22:13:26 +02:00
|
|
|
|
2009-01-26 01:09:49 +01:00
|
|
|
#include "lib/nobug-init.hpp"
|
2009-01-25 00:24:42 +01:00
|
|
|
#include "include/logging.h"
|
2008-12-29 05:18:22 +01:00
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
#include "lib/sync-classlock.hpp"
|
2007-09-10 06:45:36 +02:00
|
|
|
|
2009-09-30 00:27:14 +02:00
|
|
|
namespace lib {
|
2007-09-06 22:13:26 +02:00
|
|
|
|
|
|
|
|
/**
|
2010-12-18 00:58:19 +01:00
|
|
|
* A configurable Template for implementing Singletons.
|
2008-12-29 05:18:22 +01:00
|
|
|
* Actually this is a Factory object, which could be placed into a static field
|
2010-12-18 00:58:19 +01:00
|
|
|
* of the Singleton (target) class or used directly.
|
2008-12-29 05:18:22 +01:00
|
|
|
* @note internally uses static fields, so all factory instances share pInstance_
|
|
|
|
|
* @note there is an ongoing discussion regarding Double Checked Locking pattern,
|
|
|
|
|
* which in this case boils down to the question: does \c pthread_mutex_lock/unlock
|
|
|
|
|
* constitute a memory barrier, such as to force any memory writes done within
|
|
|
|
|
* the singleton ctor to be flushed and visible to other threads when releasing
|
|
|
|
|
* the lock? To my understanding, the answer is yes. See
|
|
|
|
|
* http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap04.html#tag_04_10
|
|
|
|
|
* @param SI the class of the Singleton instance
|
|
|
|
|
* @param Create policy defining how to create/destroy the instance
|
2009-01-14 12:15:13 +01:00
|
|
|
* @param Life policy defining how to manage Singleton Lifecycle
|
2007-09-06 22:13:26 +02:00
|
|
|
*/
|
|
|
|
|
template
|
2008-12-29 05:18:22 +01:00
|
|
|
< class SI
|
|
|
|
|
, template <class> class Create = singleton::StaticCreate
|
|
|
|
|
, template <class> class Life = singleton::AutoDestroy
|
2007-09-06 22:13:26 +02:00
|
|
|
>
|
2007-09-23 13:31:33 +02:00
|
|
|
class SingletonFactory
|
2007-09-06 22:13:26 +02:00
|
|
|
{
|
2008-12-29 05:18:22 +01:00
|
|
|
typedef SI* volatile PType;
|
|
|
|
|
typedef lib::ClassLock<SI> ThreadLock;
|
|
|
|
|
|
|
|
|
|
static PType pInstance_;
|
2007-09-06 22:13:26 +02:00
|
|
|
static bool isDead_;
|
2010-12-18 00:58:19 +01:00
|
|
|
|
2007-09-06 22:13:26 +02:00
|
|
|
|
2007-09-10 06:45:36 +02:00
|
|
|
public:
|
2007-09-23 13:31:33 +02:00
|
|
|
/** Interface to be used by SingletonFactory's clients.
|
2007-09-10 06:45:36 +02:00
|
|
|
* Manages internally the instance creation, lifecycle
|
|
|
|
|
* and access handling in a multithreaded context.
|
|
|
|
|
* @return "the" single instance of class S
|
|
|
|
|
*/
|
|
|
|
|
SI& operator() ()
|
|
|
|
|
{
|
|
|
|
|
if (!pInstance_)
|
|
|
|
|
{
|
2007-09-14 19:18:11 +02:00
|
|
|
ThreadLock guard SIDEEFFECT;
|
2010-12-18 00:58:19 +01:00
|
|
|
|
2007-09-10 06:45:36 +02:00
|
|
|
if (!pInstance_)
|
|
|
|
|
{
|
|
|
|
|
if (isDead_)
|
|
|
|
|
{
|
|
|
|
|
Life<SI>::onDeadReference();
|
|
|
|
|
isDead_ = false;
|
|
|
|
|
}
|
|
|
|
|
pInstance_ = Create<SI>::create();
|
|
|
|
|
Life<SI>::scheduleDelete (&destroy);
|
|
|
|
|
} }
|
|
|
|
|
ENSURE (pInstance_);
|
|
|
|
|
ENSURE (!isDead_);
|
|
|
|
|
return *pInstance_;
|
|
|
|
|
}
|
2007-09-06 22:13:26 +02:00
|
|
|
|
|
|
|
|
private:
|
2007-09-10 06:45:36 +02:00
|
|
|
/** @internal helper used to delegate destroying the single instance
|
|
|
|
|
* to the Create policy, at the same time allowing the Life policy
|
|
|
|
|
* to control the point in the Application lifecycle when the
|
2008-12-06 07:35:37 +01:00
|
|
|
* destruction of this instance occurs.
|
2007-09-10 06:45:36 +02:00
|
|
|
*/
|
|
|
|
|
static void destroy()
|
|
|
|
|
{
|
|
|
|
|
REQUIRE (!isDead_);
|
|
|
|
|
Create<SI>::destroy (pInstance_);
|
|
|
|
|
pInstance_ = 0;
|
|
|
|
|
isDead_ = true;
|
|
|
|
|
}
|
2007-09-06 22:13:26 +02:00
|
|
|
};
|
2007-09-10 06:45:36 +02:00
|
|
|
|
2010-12-18 00:58:19 +01:00
|
|
|
|
|
|
|
|
// Storage for SingletonFactory's static fields...
|
2007-09-10 06:45:36 +02:00
|
|
|
template
|
|
|
|
|
< class SI,
|
|
|
|
|
template <class> class C,
|
2008-12-29 05:18:22 +01:00
|
|
|
template <class> class L
|
2007-09-10 06:45:36 +02:00
|
|
|
>
|
2010-12-18 00:58:19 +01:00
|
|
|
typename SingletonFactory<SI,C,L>::PType
|
2008-12-29 05:18:22 +01:00
|
|
|
SingletonFactory<SI,C,L>::pInstance_;
|
2007-09-06 22:13:26 +02:00
|
|
|
|
2007-09-10 06:45:36 +02:00
|
|
|
template
|
|
|
|
|
< class SI,
|
|
|
|
|
template <class> class C,
|
2008-12-29 05:18:22 +01:00
|
|
|
template <class> class L
|
2007-09-10 06:45:36 +02:00
|
|
|
>
|
2008-12-29 05:18:22 +01:00
|
|
|
bool SingletonFactory<SI,C,L>::isDead_;
|
|
|
|
|
|
|
|
|
|
|
2007-09-10 06:45:36 +02:00
|
|
|
|
2009-01-14 12:15:13 +01:00
|
|
|
///// Question: can we get rid of the static fields?
|
|
|
|
|
///// this is tricky because of invoking the destructors. If we rely on instance vars,
|
2007-09-10 06:45:36 +02:00
|
|
|
///// the object may already have been released when the runtime system calls the
|
|
|
|
|
///// destructors of static objects at shutdown.
|
2007-09-23 16:50:05 +02:00
|
|
|
///// It seems this would either cost us much of the flexibility or get complicated
|
2008-12-06 07:35:37 +01:00
|
|
|
///// to a point where we could as well implement our own Dependency Injection Manager.
|
2009-01-14 12:15:13 +01:00
|
|
|
///// But the whole point of my pervasive use of this singleton template is to remain
|
|
|
|
|
///// below this borderline of integration ("IoC yes, but avoid DI").
|
2008-12-29 05:18:22 +01:00
|
|
|
|
2009-09-30 00:27:14 +02:00
|
|
|
} // namespace lib
|
2007-09-06 22:13:26 +02:00
|
|
|
#endif
|