]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/netradiant-src/libs/generic/static.cpp
Move all other sources in a separate subfolder
[voretournament/voretournament.git] / misc / mediasource / netradiant-src / libs / generic / static.cpp
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 #include "static.h"
23
24 #if defined(_DEBUG) || defined(DOXYGEN)
25
26 namespace ExampleStatic
27 {
28   // Static example
29   // ---- myclass.h
30   class MyClass
31   {
32   public:
33     int value;
34     MyClass() : value(3)
35     {
36     }
37   };
38
39   typedef Static<MyClass> StaticMyClass;
40
41   // ---- main.cpp
42   class DynamicInitialisation
43   {
44   public:
45     DynamicInitialisation()
46     {
47       // StaticMyClass::instance() may be invalid here because construction order is undefined
48     }
49   };
50
51   DynamicInitialisation g_dynamicInitialisation;
52
53   void duringMain()
54   {
55     int bar = StaticMyClass::instance().value;
56   }
57   // end example
58 }
59
60 namespace ExampleLazyStatic
61 {
62   // LazyStatic example
63   // ---- myclass.h
64   class MyClass
65   {
66   public:
67     int value;
68     MyClass() : value(3)
69     {
70     }
71     // destructor will never be called
72   };
73
74   typedef LazyStatic<MyClass> StaticMyClass;
75
76   // ---- main.cpp
77   class DynamicInitialisation
78   {
79   public:
80     DynamicInitialisation()
81     {
82       int bar = StaticMyClass::instance().value;
83     }
84   };
85
86   DynamicInitialisation g_dynamicInitialisation;
87
88   void duringMain()
89   {
90     int bar = StaticMyClass::instance().value;
91   }
92   // end example
93 }
94
95 namespace ExampleSmartStatic
96 {
97   // SmartStatic example
98   // ---- myclass.h
99   class MyClass
100   {
101   public:
102     int value;
103     MyClass() : value(3)
104     {
105     }
106   };
107
108   typedef CountedStatic<MyClass> StaticMyClass;
109
110   // ---- main.cpp
111   class DynamicInitialisation
112   {
113   public:
114     DynamicInitialisation()
115     {
116       // StaticMyClass::instance() is invalid before the ref is constructed
117       SmartStatic<MyClass> ref;
118       int bar = ref.instance().value;
119
120       SmartStatic<MyClass> ref2; // any number of instances are allowed.
121     }
122   };
123
124   DynamicInitialisation g_dynamicInitialisation;
125
126   void duringMain()
127   {
128     int bar = SmartStatic<MyClass>().instance().value; // an instance can be a temporary
129   }
130   // end example
131 }
132
133 #endif