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.
30 /// \brief A singleton which is statically initialised.
32 /// \param Type The singleton object type.
34 /// \dontinclude generic/static.cpp
35 /// \skipline Static example
36 /// \until end example
37 template<typename Type>
40 static Type m_instance;
42 static Type& instance()
48 template<typename Type>
49 Type Static<Type>::m_instance;
52 /// \brief A singleton which is lazily initialised.
53 /// The instance is constructed the first time it is referenced, and is never destroyed.
55 /// \param Type The singleton object type.
57 /// \dontinclude generic/static.cpp
58 /// \skipline LazyStatic example
59 /// \until end example
60 template<typename Type>
63 static Type* m_instance; // this will be initialised to 0 by the CRT, according to the c++ standard
65 static Type& instance()
69 m_instance = new Type; // allocate using 'new' to get the correct alignment
75 template<typename Type>
76 Type* LazyStatic<Type>::m_instance;
79 /// \brief A singleton which keeps a count of the number of times it is referenced.
81 /// The instance is constructed when its reference count changes from 0 to 1 and destroyed when its reference count changes from 1 to 0.
82 /// Use with SmartStatic.
84 /// \param Type The singleton object type.
85 template<typename Type>
88 static std::size_t m_refcount; // this will be initialised to 0 by the CRT, according to the c++ standard
89 static Type* m_instance;
91 static Type& instance()
99 m_instance = new Type; // allocate using 'new' to get the correct alignment
102 static void release()
104 if(--m_refcount == 0)
111 template<typename Type>
112 std::size_t CountedStatic<Type>::m_refcount; // this will be initialised to 0 by the CRT, according to the c++ standard
113 template<typename Type>
114 Type* CountedStatic<Type>::m_instance;
116 /// \brief A reference to a CountedStatic.
117 /// Guarantees that CountedStatic<Type> will be constructed for the lifetime of this object.
119 /// \param Type The type parameter of the CountedStatic to reference.
121 /// \dontinclude generic/static.cpp
122 /// \skipline SmartStatic example
123 /// \until end example
124 template<typename Type>
130 CountedStatic<Type>::capture();
134 CountedStatic<Type>::release();
138 return CountedStatic<Type>::instance();