]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/libs/generic/static.h
Rename the compiled fteqcc to fteqcc-win32 (as that's what it is)
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / libs / generic / static.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #if !defined(INCLUDED_GENERIC_STATIC_H)
23 #define INCLUDED_GENERIC_STATIC_H
24
25 /// \file
26 /// \brief Template techniques for instantiating singletons.
27
28 #include <cstddef>
29
30 class Null
31 {
32 };
33
34 /// \brief A singleton which is statically initialised.
35 ///
36 /// \param Type The singleton object type.
37 /// \param Type The type distinguishing this instance from others of the same type.
38 ///
39 /// \dontinclude generic/static.cpp
40 /// \skipline Static example
41 /// \until end example
42 template<typename Type, typename Context = Null>
43 class Static
44 {
45   static Type m_instance;
46 public:
47   static Type& instance()
48   {
49     return m_instance;
50   }
51 };
52
53 template<typename Type, typename Context>
54 Type Static<Type, Context>::m_instance;
55
56
57 /// \brief A singleton which is lazily initialised.
58 /// The instance is constructed the first time it is referenced, and is never destroyed.
59 ///
60 /// \param Type The singleton object type.
61 /// \param Type The type distinguishing this instance from others of the same type.
62 ///
63 /// \dontinclude generic/static.cpp
64 /// \skipline LazyStatic example
65 /// \until end example
66 template<typename Type, typename Context = Null>
67 class LazyStatic
68 {
69   static Type* m_instance; // this will be initialised to 0 by the CRT, according to the c++ standard
70 public:
71   static Type& instance()
72   {
73     if(m_instance == 0)
74     {
75       m_instance = new Type; // allocate using 'new' to get the correct alignment
76     }
77     return *m_instance;
78   }
79 };
80
81 template<typename Type, typename Context>
82 Type* LazyStatic<Type, Context>::m_instance;
83
84
85 /// \brief A singleton which keeps a count of the number of times it is referenced.
86 ///
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.
89 ///
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>
93 class CountedStatic
94 {
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;
97 public:
98   static Type& instance()
99   {
100     return *m_instance;
101   }
102   static void capture()
103   {
104     if(++m_refcount == 1)
105     {
106       m_instance = new Type; // allocate using 'new' to get the correct alignment
107     }
108   }
109   static void release()
110   {
111     if(--m_refcount == 0)
112     {
113       delete m_instance;
114     }
115   }
116 };
117
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;
122
123 /// \brief A reference to a CountedStatic.
124 /// Guarantees that CountedStatic<Type> will be constructed for the lifetime of this object.
125 ///
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.
128 ///
129 /// \dontinclude generic/static.cpp
130 /// \skipline SmartStatic example
131 /// \until end example
132 template<typename Type, typename Context = Null>
133 class SmartStatic
134 {
135 public:
136   SmartStatic()
137   {
138     CountedStatic<Type, Context>::capture();
139   }
140   ~SmartStatic()
141   {
142     CountedStatic<Type, Context>::release();
143   }
144   Type& instance()
145   {
146     return CountedStatic<Type, Context>::instance();
147   }
148 };
149
150
151 #endif