Crazy Eddie's GUI System  0.8.7
RefCounted.h
1 /************************************************************************
2  created: Wed Mar 1 2006
3  author: Paul D Turner <paul@cegui.org.uk>
4 *************************************************************************/
5 /***************************************************************************
6  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #ifndef _CEGUIRefCounted_h_
28 #define _CEGUIRefCounted_h_
29 
30 // Start of CEGUI namespace section
31 namespace CEGUI
32 {
41 template<typename T>
43 {
44 public:
50  d_object(0),
51  d_count(0)
52  {
53  }
54 
59  RefCounted(T* ob) :
60  d_object(ob),
61  // use system heap for this! no CEGUI_NEW_PT!
62  d_count((ob != 0) ? new unsigned int(1) : 0)
63  {
64  }
65 
70  RefCounted(const RefCounted<T>& other) :
71  d_object(other.d_object),
72  d_count(other.d_count)
73  {
74  if (d_count)
75  addRef();
76  }
77 
84  {
85  if (d_object)
86  release();
87  }
88 
96  {
97  if (*this != other)
98  {
99  if (d_object)
100  release();
101 
102  d_object = other.d_object;
103  d_count = d_object ? other.d_count : 0;
104 
105  if (d_count)
106  addRef();
107  }
108 
109  return *this;
110  }
111 
117  bool operator==(const RefCounted<T>& other) const
118  {
119  return d_object == other.d_object;
120  }
121 
127  bool operator!=(const RefCounted<T>& other) const
128  {
129  return d_object != other.d_object;
130  }
131 
137  const T& operator*() const
138  {
139  return *d_object;
140  }
141 
142  T& operator*()
143  {
144  return *d_object;
145  }
146 
151  const T* operator->() const
152  {
153  return d_object;
154  }
155 
156  T* operator->()
157  {
158  return d_object;
159  }
160 
165  bool isValid() const
166  {
167  return d_object != 0;
168  }
169 
170 private:
175  void addRef()
176  {
177  ++*d_count;
178  }
179 
185  void release()
186  {
187  if (!--*d_count)
188  {
189  // use CEGUI allocators for the object
190  CEGUI_DELETE_AO d_object;
191 
192  // use system heap for this! no CEGUI_DELETE_PT!
193  delete d_count;
194  d_object = 0;
195  d_count = 0;
196  }
197  }
198 
199  T* d_object;
200  unsigned int* d_count;
201 };
202 
203 } // End of CEGUI namespace section
204 
205 #endif // end of guard _CEGUIRefCounted_h_
~RefCounted()
Destructor. Only deletes the associated object if no further references exist.
Definition: RefCounted.h:83
RefCounted()
Default constructor.
Definition: RefCounted.h:49
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
bool operator==(const RefCounted< T > &other) const
Return whether the two RefCounted ptrs are equal (point to the same object)
Definition: RefCounted.h:117
bool operator!=(const RefCounted< T > &other) const
Return whether the two RefCounted ptrs are not equal (point to different objects) ...
Definition: RefCounted.h:127
RefCounted(T *ob)
Contruct a RefCounted object that wraps the pointer ob.
Definition: RefCounted.h:59
const T & operator*() const
Return the object referred to by the wrapped pointer. (beware of null pointers when using this!) ...
Definition: RefCounted.h:137
RefCounted< T > & operator=(const RefCounted< T > &other)
Assignment operator. Previously held object gets its reference count reduced, and is deleted if no fu...
Definition: RefCounted.h:95
Simple, generic, reference counted pointer class. This is primarily here for use by the Events system...
Definition: RefCounted.h:42
RefCounted(const RefCounted< T > &other)
Copy constructor.
Definition: RefCounted.h:70
const T * operator->() const
Return the wrapped pointer.
Definition: RefCounted.h:151
bool isValid() const
Return whether the wrapped pointer is valid. i.e. that it is not null.
Definition: RefCounted.h:165