Crazy Eddie's GUI System
0.8.4
|
00001 /************************************************************************ 00002 created: Wed Mar 1 2006 00003 author: Paul D Turner <paul@cegui.org.uk> 00004 *************************************************************************/ 00005 /*************************************************************************** 00006 * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining 00009 * a copy of this software and associated documentation files (the 00010 * "Software"), to deal in the Software without restriction, including 00011 * without limitation the rights to use, copy, modify, merge, publish, 00012 * distribute, sublicense, and/or sell copies of the Software, and to 00013 * permit persons to whom the Software is furnished to do so, subject to 00014 * the following conditions: 00015 * 00016 * The above copyright notice and this permission notice shall be 00017 * included in all copies or substantial portions of the Software. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00020 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00021 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00022 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 00023 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00024 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00025 * OTHER DEALINGS IN THE SOFTWARE. 00026 ***************************************************************************/ 00027 #ifndef _CEGUIRefCounted_h_ 00028 #define _CEGUIRefCounted_h_ 00029 00030 // Start of CEGUI namespace section 00031 namespace CEGUI 00032 { 00041 template<typename T> 00042 class RefCounted 00043 { 00044 public: 00049 RefCounted() : 00050 d_object(0), 00051 d_count(0) 00052 { 00053 } 00054 00059 RefCounted(T* ob) : 00060 d_object(ob), 00061 // use system heap for this! no CEGUI_NEW_PT! 00062 d_count((ob != 0) ? new unsigned int(1) : 0) 00063 { 00064 } 00065 00070 RefCounted(const RefCounted<T>& other) : 00071 d_object(other.d_object), 00072 d_count(other.d_count) 00073 { 00074 if (d_count) 00075 addRef(); 00076 } 00077 00083 ~RefCounted() 00084 { 00085 if (d_object) 00086 release(); 00087 } 00088 00095 RefCounted<T>& operator=(const RefCounted<T>& other) 00096 { 00097 if (*this != other) 00098 { 00099 if (d_object) 00100 release(); 00101 00102 d_object = other.d_object; 00103 d_count = d_object ? other.d_count : 0; 00104 00105 if (d_count) 00106 addRef(); 00107 } 00108 00109 return *this; 00110 } 00111 00117 bool operator==(const RefCounted<T>& other) const 00118 { 00119 return d_object == other.d_object; 00120 } 00121 00127 bool operator!=(const RefCounted<T>& other) const 00128 { 00129 return d_object != other.d_object; 00130 } 00131 00137 const T& operator*() const 00138 { 00139 return *d_object; 00140 } 00141 00142 T& operator*() 00143 { 00144 return *d_object; 00145 } 00146 00151 const T* operator->() const 00152 { 00153 return d_object; 00154 } 00155 00156 T* operator->() 00157 { 00158 return d_object; 00159 } 00160 00165 bool isValid() const 00166 { 00167 return d_object != 0; 00168 } 00169 00170 private: 00175 void addRef() 00176 { 00177 ++*d_count; 00178 } 00179 00185 void release() 00186 { 00187 if (!--*d_count) 00188 { 00189 // use CEGUI allocators for the object 00190 CEGUI_DELETE_AO d_object; 00191 00192 // use system heap for this! no CEGUI_DELETE_PT! 00193 delete d_count; 00194 d_object = 0; 00195 d_count = 0; 00196 } 00197 } 00198 00199 T* d_object; 00200 unsigned int* d_count; 00201 }; 00202 00203 } // End of CEGUI namespace section 00204 00205 #endif // end of guard _CEGUIRefCounted_h_