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