]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/generic/static.h
netradiant: strip 16-bit png to 8-bit, fix #153
[xonotic/netradiant.git] / 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         return m_instance;
49 }
50 };
51
52 template<typename Type, typename Context>
53 Type Static<Type, Context>::m_instance;
54
55
56 /// \brief A singleton which is lazily initialised.
57 /// The instance is constructed the first time it is referenced, and is never destroyed.
58 ///
59 /// \param Type The singleton object type.
60 /// \param Type The type distinguishing this instance from others of the same type.
61 ///
62 /// \dontinclude generic/static.cpp
63 /// \skipline LazyStatic example
64 /// \until end example
65 template<typename Type, typename Context = Null>
66 class LazyStatic
67 {
68 static Type* m_instance;   // this will be initialised to 0 by the CRT, according to the c++ standard
69 public:
70 static Type& instance(){
71         if ( m_instance == 0 ) {
72                 m_instance = new Type; // allocate using 'new' to get the correct alignment
73         }
74         return *m_instance;
75 }
76 };
77
78 template<typename Type, typename Context>
79 Type * LazyStatic<Type, Context>::m_instance;
80
81
82 /// \brief A singleton which keeps a count of the number of times it is referenced.
83 ///
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.
86 ///
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>
90 class CountedStatic
91 {
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;
94 public:
95 static Type& instance(){
96         return *m_instance;
97 }
98 static void capture(){
99         if ( ++m_refcount == 1 ) {
100                 m_instance = new Type; // allocate using 'new' to get the correct alignment
101         }
102 }
103 static void release(){
104         if ( --m_refcount == 0 ) {
105                 delete m_instance;
106         }
107 }
108 };
109
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;
114
115 /// \brief A reference to a CountedStatic.
116 /// Guarantees that CountedStatic<Type> will be constructed for the lifetime of this object.
117 ///
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.
120 ///
121 /// \dontinclude generic/static.cpp
122 /// \skipline SmartStatic example
123 /// \until end example
124 template<typename Type, typename Context = Null>
125 class SmartStatic
126 {
127 public:
128 SmartStatic(){
129         CountedStatic<Type, Context>::capture();
130 }
131 ~SmartStatic(){
132         CountedStatic<Type, Context>::release();
133 }
134 Type& instance(){
135         return CountedStatic<Type, Context>::instance();
136 }
137 };
138
139
140 #endif