Crazy Eddies GUI System  0.7.8
CEGUIRefCounted.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 {
39 template<typename T>
41 {
42 public:
48  d_object(0),
49  d_count(0)
50  {
51  }
52 
57  RefCounted(T* ob) :
58  d_object(ob),
59  d_count((ob != 0) ? new unsigned int(1) : 0)
60  {
61  }
62 
67  RefCounted(const RefCounted<T>& other) :
68  d_object(other.d_object),
69  d_count(other.d_count)
70  {
71  if (d_count)
72  addRef();
73  }
74 
81  {
82  if (d_object)
83  release();
84  }
85 
93  {
94  if (*this != other)
95  {
96  if (d_object)
97  release();
98 
99  d_object = other.d_object;
100  d_count = d_object ? other.d_count : 0;
101 
102  if (d_count)
103  addRef();
104  }
105 
106  return *this;
107  }
108 
114  bool operator==(const RefCounted<T>& other) const
115  {
116  return d_object == other.d_object;
117  }
118 
124  bool operator!=(const RefCounted<T>& other) const
125  {
126  return d_object != other.d_object;
127  }
128 
134  const T& operator*() const
135  {
136  return *d_object;
137  }
138 
139  T& operator*()
140  {
141  return *d_object;
142  }
143 
148  const T* operator->() const
149  {
150  return d_object;
151  }
152 
153  T* operator->()
154  {
155  return d_object;
156  }
157 
162  bool isValid() const
163  {
164  return d_object != 0;
165  }
166 
167 private:
172  void addRef()
173  {
174  ++*d_count;
175  }
176 
182  void release()
183  {
184  if (!--*d_count)
185  {
186  delete d_object;
187  delete d_count;
188  d_object = 0;
189  d_count = 0;
190  }
191  }
192 
193  T* d_object;
194  unsigned int* d_count;
195 };
196 
197 } // End of CEGUI namespace section
198 
199 #endif // end of guard _CEGUIRefCounted_h_