Library of Assembled Shared Sources
singleton.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43/** @class lass::util::Singleton
44 * @brief non-intrusive singleton holder.
45 * @author Bram de Greve
46 * @date 2003
47 *
48 * With this class, it's possible to create a singleton of a class Foo. It's a non-instrusive
49 * singleton because it's not required to derive Foo from this Singleton class. Instead,
50 * this class will be a holder to a singleton object of Foo. To use class Foo as a singleton
51 * is very straight forward, but can be done in a couple of ways:
52 *
53 * 1. The @e typedef way: With a typedef, you create a new type that will act as singleton holder
54 * for class @c Foo. To access the singleton, you must now use the @c instance() method on
55 * that new type:
56 *
57 * @code
58 * typedef lass::util::Singleton<Foo> TFooSingleton;
59 *
60 * TFooSingleton()::instance()->bar(fun);
61 * @endcode
62 *
63 * 2. The @e function way (@b prefered/b>): you write a nice little function that returns a pointer
64 * to the singleton. To access the singleton, you simple call that function:
65 *
66 * @code
67 * Foo* foo()
68 * {
69 * return lass::util::Singleton<Foo>::instance();
70 * }
71 *
72 * foo()->bar(fun);
73 * @endcode
74 *
75 * Both ways only differ in how to access the instance. All other properties are common:
76 *
77 * - The instance of the singleton is constructed on the heap on first access on the heap. i.e.
78 * the first time in the program @c TFooSingleton::instance() or @c foo() is called, a new object
79 * of @c Foo is allocated on the heap, and a pointer to it is returned. All the following calls
80 * will return that same pointer. This is the way how to control the order of construction:
81 * on first access.
82 *
83 * - The order of destruction can be set by the @c DestructionPriority. Singletons with a
84 * smaller destruction priority will be destructed later than those with a higher destruction
85 * priority. It's not specified in which order singletons with identical destruction priorities
86 * will be destructed (i.e. its not guaranteed for such singletons that the last one constructed
87 * will be the first to be destroyed). You can set the destruction priority by the second
88 * template argument of @c Singleton: @c lass::util::Singleton<Foo,2000> will specify the
89 * singleton to have a destruction priority of 2000. The default destruction priority is set to
90 * be 1000. Priorities below 500 are reserved for the Lass implementation (and for you who
91 * know what you're doing ;)
92 *
93 * @b warning: lass::io::proxyMan() is a proxy man that is needed for the implementation of
94 * all logging activities etc. Its destruction priority is set to 0 and there should be
95 * no other singleton have a destruction priority lower or equal than this.
96 *
97 * - Requirements for the class @c Foo: Since it has to be constructed on the heap without any
98 * parameters in the neighbourhoud, it has to have a default constructor.
99 *
100 * @b Reference:
101 * -# ALEXANDRESCU A. (2001), <i>Modern C++ Design: Generic Programming and Design Patterns
102 * applied</i>, C++ in depth series, Addison-Wesley, pages 129-156
103 * -# GAMMA E., HELM R., JOHNSON R. & VLISSIDES J. (1995), <i>Design Patters: Elements of
104 * Reusable Object-Oriented Software</i>, Addison-Wesley.
105 */
106
107#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_SINGLETON_H
108#define LASS_GUARDIAN_OF_INCLUSION_UTIL_SINGLETON_H
109
110#include "util_common.h"
111#include "impl/singleton_impl.h"
112
113namespace lass
114{
115namespace util
116{
117
118/** Destruction priority constants.
119 * @relates Singleton
120 * Priorities levels below singletonDestructionPriorityBeginUserRange (=500) are reserved for the
121 * implementation of Lass, and should not be used by user code (unless you know what you're doing
122 * of course ;)
123 */
125{
126 destructionPriorityDefault = 1000, /**< default priority level =) */
127 destructionPriorityBeginUser = 500, /**< lowest legal level for user code */
128 destructionPriorityNever = -1, /**< never destruct the singleton, let it live forever. */
129
130 // the following levels are internal and should not be used by user code
131 //
132 destructionPriorityInternalProxyMan = 0, /**< @internal */
133 destructionPriorityInternalPythonMutex = 50, /**< @internal */
134 destructionPriorityInternalTlsDestructors = 100, /**< @internal */
135 destructionPriorityInternalAllocators = 200, /**< @internal */
136 destructionPriorityInternalTlsLocalsMain = 400 /**< @internal */
137};
138
139template<class T, int destructPriority = destructionPriorityDefault>
140class Singleton: public impl::SingletonBase
141{
142public:
143
144 typedef Singleton<T, destructPriority> TSelf;
145
146 typedef T TInstance;
147 enum { destructionPriority = destructPriority };
148
149 Singleton();
150 virtual ~Singleton();
151
152 static TInstance* instance();
153
154private:
155
156 static bool deadReference(bool setReferenceToDead = false);
157
158 std::unique_ptr<TInstance> instance_;
159};
160
161}
162}
163
164#include "singleton.inl"
165
166#endif
167
168// EOF
169
static TInstance * instance()
Return pointer to singleton instance.
Definition singleton.inl:82
DestructionPriorities
Destruction priority constants.
Definition singleton.h:125
base class of all singletons.
general utility, debug facilities, ...
@ destructionPriorityDefault
default priority level =)
Definition singleton.h:126
@ destructionPriorityNever
never destruct the singleton, let it live forever.
Definition singleton.h:128
@ destructionPriorityBeginUser
lowest legal level for user code
Definition singleton.h:127
Library for Assembled Shared Sources.
Definition config.h:53