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(){
52 template<typename Type, typename Context>
53 Type Static<Type, Context>::m_instance;
56 /// \brief A singleton which is lazily initialised.
57 /// The instance is constructed the first time it is referenced, and is never destroyed.
59 /// \param Type The singleton object type.
60 /// \param Type The type distinguishing this instance from others of the same type.
62 /// \dontinclude generic/static.cpp
63 /// \skipline LazyStatic example
64 /// \until end example
65 template<typename Type, typename Context = Null>
68 static Type* m_instance; // this will be initialised to 0 by the CRT, according to the c++ standard
70 static Type& instance(){
71 if ( m_instance == 0 ) {
72 m_instance = new Type; // allocate using 'new' to get the correct alignment
78 template<typename Type, typename Context>
79 Type * LazyStatic<Type, Context>::m_instance;
82 /// \brief A singleton which keeps a count of the number of times it is referenced.
84 /// The instance is constructed when its reference count changes from 0 to 1 and destroyed when its reference count changes from 1 to 0.
85 /// Use with SmartStatic.
87 /// \param Type The singleton object type.
88 /// \param Type The type distinguishing this instance from others of the same type.
89 template<typename Type, typename Context = Null>
92 static std::size_t m_refcount; // this will be initialised to 0 by the CRT, according to the c++ standard
93 static Type* m_instance;
95 static Type& instance(){
98 static void capture(){
99 if ( ++m_refcount == 1 ) {
100 m_instance = new Type; // allocate using 'new' to get the correct alignment
103 static void release(){
104 if ( --m_refcount == 0 ) {
110 template<typename Type, typename Context>
111 std::size_t CountedStatic<Type, Context>::m_refcount; // this will be initialised to 0 by the CRT, according to the c++ standard
112 template<typename Type, typename Context>
113 Type * CountedStatic<Type, Context>::m_instance;
115 /// \brief A reference to a CountedStatic.
116 /// Guarantees that CountedStatic<Type> will be constructed for the lifetime of this object.
118 /// \param Type The type parameter of the CountedStatic to reference.
119 /// \param Type The type distinguishing this instance from others of the same type.
121 /// \dontinclude generic/static.cpp
122 /// \skipline SmartStatic example
123 /// \until end example
124 template<typename Type, typename Context = Null>
129 CountedStatic<Type, Context>::capture();
132 CountedStatic<Type, Context>::release();
135 return CountedStatic<Type, Context>::instance();