2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_GENERIC_STATIC_H)
23 #define INCLUDED_GENERIC_STATIC_H
26 /// \brief Template techniques for instantiating singletons.
34 /// \brief A singleton which is statically initialised.
36 /// \param Type The singleton object type.
37 /// \param Type The type distinguishing this instance from others of the same type.
39 /// \dontinclude generic/static.cpp
40 /// \skipline Static example
41 /// \until end example
42 template<typename Type, typename Context = Null>
45 static Type m_instance;
47 static Type& instance()
53 template<typename Type, typename Context>
54 Type Static<Type, Context>::m_instance;
57 /// \brief A singleton which is lazily initialised.
58 /// The instance is constructed the first time it is referenced, and is never destroyed.
60 /// \param Type The singleton object type.
61 /// \param Type The type distinguishing this instance from others of the same type.
63 /// \dontinclude generic/static.cpp
64 /// \skipline LazyStatic example
65 /// \until end example
66 template<typename Type, typename Context = Null>
69 static Type* m_instance; // this will be initialised to 0 by the CRT, according to the c++ standard
71 static Type& instance()
75 m_instance = new Type; // allocate using 'new' to get the correct alignment
81 template<typename Type, typename Context>
82 Type* LazyStatic<Type, Context>::m_instance;
85 /// \brief A singleton which keeps a count of the number of times it is referenced.
87 /// The instance is constructed when its reference count changes from 0 to 1 and destroyed when its reference count changes from 1 to 0.
88 /// Use with SmartStatic.
90 /// \param Type The singleton object type.
91 /// \param Type The type distinguishing this instance from others of the same type.
92 template<typename Type, typename Context = Null>
95 static std::size_t m_refcount; // this will be initialised to 0 by the CRT, according to the c++ standard
96 static Type* m_instance;
98 static Type& instance()
102 static void capture()
104 if(++m_refcount == 1)
106 m_instance = new Type; // allocate using 'new' to get the correct alignment
109 static void release()
111 if(--m_refcount == 0)
118 template<typename Type, typename Context>
119 std::size_t CountedStatic<Type, Context>::m_refcount; // this will be initialised to 0 by the CRT, according to the c++ standard
120 template<typename Type, typename Context>
121 Type* CountedStatic<Type, Context>::m_instance;
123 /// \brief A reference to a CountedStatic.
124 /// Guarantees that CountedStatic<Type> will be constructed for the lifetime of this object.
126 /// \param Type The type parameter of the CountedStatic to reference.
127 /// \param Type The type distinguishing this instance from others of the same type.
129 /// \dontinclude generic/static.cpp
130 /// \skipline SmartStatic example
131 /// \until end example
132 template<typename Type, typename Context = Null>
138 CountedStatic<Type, Context>::capture();
142 CountedStatic<Type, Context>::release();
146 return CountedStatic<Type, Context>::instance();