Crazy Eddie's GUI System  0.8.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
RefCounted.h
1 /************************************************************************
2  filename: CEGUIRefCounted.h
3  created: Wed Mar 1 2006
4  author: Paul D Turner <paul@cegui.org.uk>
5 *************************************************************************/
6 /***************************************************************************
7  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  ***************************************************************************/
28 #ifndef _CEGUIRefCounted_h_
29 #define _CEGUIRefCounted_h_
30 
31 // Start of CEGUI namespace section
32 namespace CEGUI
33 {
42 template<typename T>
44 {
45 public:
51  d_object(0),
52  d_count(0)
53  {
54  }
55 
60  RefCounted(T* ob) :
61  d_object(ob),
62  // use system heap for this! no CEGUI_NEW_PT!
63  d_count((ob != 0) ? new unsigned int(1) : 0)
64  {
65  }
66 
71  RefCounted(const RefCounted<T>& other) :
72  d_object(other.d_object),
73  d_count(other.d_count)
74  {
75  if (d_count)
76  addRef();
77  }
78 
85  {
86  if (d_object)
87  release();
88  }
89 
97  {
98  if (*this != other)
99  {
100  if (d_object)
101  release();
102 
103  d_object = other.d_object;
104  d_count = d_object ? other.d_count : 0;
105 
106  if (d_count)
107  addRef();
108  }
109 
110  return *this;
111  }
112 
118  bool operator==(const RefCounted<T>& other) const
119  {
120  return d_object == other.d_object;
121  }
122 
128  bool operator!=(const RefCounted<T>& other) const
129  {
130  return d_object != other.d_object;
131  }
132 
138  const T& operator*() const
139  {
140  return *d_object;
141  }
142 
143  T& operator*()
144  {
145  return *d_object;
146  }
147 
152  const T* operator->() const
153  {
154  return d_object;
155  }
156 
157  T* operator->()
158  {
159  return d_object;
160  }
161 
166  bool isValid() const
167  {
168  return d_object != 0;
169  }
170 
171 private:
176  void addRef()
177  {
178  ++*d_count;
179  }
180 
186  void release()
187  {
188  if (!--*d_count)
189  {
190  // use CEGUI allocators for the object
191  CEGUI_DELETE_AO d_object;
192 
193  // use system heap for this! no CEGUI_DELETE_PT!
194  delete d_count;
195  d_object = 0;
196  d_count = 0;
197  }
198  }
199 
200  T* d_object;
201  unsigned int* d_count;
202 };
203 
204 } // End of CEGUI namespace section
205 
206 #endif // end of guard _CEGUIRefCounted_h_