source: OGRE/trunk/ogre_dependencies/Dependencies/include/CEGUI/CEGUIRefPtr.h @ 692

Revision 692, 4.0 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/************************************************************************
2        filename:       CEGUIRefPtr.h
3        created:        15/10/2004
4        author:         Gerald Lindsly
5*************************************************************************/
6/*************************************************************************
7    Crazy Eddie's GUI System (http://www.cegui.org.uk)
8    Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2.1 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23*************************************************************************/
24#ifndef _CEGUIRefPtr_h_
25#define _CEGUIRefPtr_h_
26
27#include "CEGUIBase.h"
28
29namespace CEGUI {
30
31class CEGUIEXPORT Referenced
32{
33public:
34  Referenced() : d_refCount(0) {}
35  Referenced(const Referenced&) : d_refCount(0) {}
36
37  Referenced& operator=(Referenced&) { return *this; }
38
39  /* Increment the reference count by one, indicating that
40      a pointer to this object is referencing it. */
41  void addRef() const { ++d_refCount; }
42 
43  /* Decrement the reference count by one, indicating that
44     a pointer to this object is no longer referencing it.
45     if the count drops to zero, this is deleted. */
46  void release() const { if (!--d_refCount) delete this; }
47 
48  /* Decrement the reference count by one, indicating that
49      a pointer to this object is no longer referencing it;
50      however, do not delete it, even if ref count goes to 0. */
51  void releaseButKeep() const { --d_refCount; }
52 
53  /** return the number pointers currently referencing this object. */
54  int refCount() const { return d_refCount; }
55 
56protected:
57  virtual ~Referenced();
58  mutable int d_refCount;
59};
60
61
62template<class T> class RefPtr
63{
64  T* d_p;
65
66public:
67//  RefPtr()     : d_p(0) {}
68 
69  RefPtr()     : d_p(new T()) { d_p->addRef(); }
70
71  RefPtr(T* t) : d_p(t) { if (t)   d_p->addRef(); }
72  RefPtr(const RefPtr& r) : d_p(r.d_p)
73                        { if (r.d_p) d_p->addRef(); }
74  ~RefPtr()
75  { if (d_p) {
76      d_p->release();
77      d_p = 0;
78    }
79  }
80
81  RefPtr& operator=(T* q)
82  {
83    if (d_p != q) {
84      T* t = d_p;
85      d_p = q;
86      if (q) q->addRef();
87      if (t) t->release();
88    }
89    return *this;
90  }
91
92  RefPtr& operator=(const RefPtr& r) { return *this = r.d_p; }
93
94  bool operator==(const RefPtr& r) const { return d_p == r.d_p; }
95  bool operator!=(const RefPtr& r) const { return d_p != r.d_p; }
96  bool operator< (const RefPtr& r) const { return d_p <  r.d_p; }
97  bool operator> (const RefPtr& r) const { return d_p >  r.d_p; }
98
99  bool operator ==(const T* q) const { return d_p == q; }
100  bool operator !=(const T* q) const { return d_p != q; }
101  bool operator < (const T* q) const { return d_p <  q; }
102  bool operator > (const T* q) const { return d_p >  q; }
103
104
105        T& operator*()        { return *d_p; }
106  const T& operator*() const  { return *d_p; }
107
108        T* operator->()       { return d_p; }
109  const T* operator->() const { return d_p; }
110
111        T* get()              { return d_p; }
112  const T* get() const        { return d_p; }
113
114  bool operator!() const            { return d_p == 0; }
115  bool valid() const            { return d_p != 0; }
116
117  T* release()
118  {
119    T* t = d_p;
120    if (d_p) {
121      d_p->releaseButKeep();
122      d_p = 0;
123    }
124    return t;
125  }
126};
127
128} // end namespace CEGUI
129
130#endif
Note: See TracBrowser for help on using the repository browser.