libcommonc++  0.7
SharedPtr.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_SharedPtr_hxx
24 #define __ccxx_SharedPtr_hxx
25 
26 #include <commonc++/Common.h++>
29 
30 namespace ccxx {
31 
39 template<class T> class SharedPtr
40 {
41  public:
42 
44  SharedPtr(T* object = NULL)
45  : _object(object), _refs(new AtomicCounter(1))
46  { }
47 
49  SharedPtr(const SharedPtr& other)
50  : _object(other._object),
51  _refs(other._refs)
52  {
53  ++(*_refs);
54  }
55 
58  {
59  _release();
60  }
61 
66  template<class U> SharedPtr<U> staticCast()
67  { return SharedPtr<U>(*this, false); }
68 
73  template<class U> SharedPtr<U> dynamicCast()
74  { return SharedPtr<U>(*this); }
75 
77  SharedPtr& operator=(const SharedPtr& other)
78  {
79  if(other._object == _object)
80  return(*this);
81 
82  _release();
83  _object = other._object;
84  _refs = other._refs;
85  ++(*_refs);
86  return(*this);
87  }
88 
90  SharedPtr& operator=(T* object)
91  {
92  if(_object != object)
93  {
94  _release();
95  _object = object;
96  _refs = new AtomicCounter(1);
97  }
98 
99  return(*this);
100  }
101 
104  { return(_object); }
105 
107  const T* operator->() const
108  { return(_object); }
109 
116  {
117  if(! _object)
118  throw NullPointerException();
119 
120  return(*_object);
121  }
122 
127  const T& operator*() const
128  {
129  if(! _object)
130  throw NullPointerException();
131  return(*_object);
132  }
133 
135  T* get() const
136  { return(_object); }
137 
139  bool operator!() const
140  { return(isNull()); }
141 
143  operator const void *() const
144  { return(_object ? this : NULL); }
145 
147  bool isNull() const
148  { return(_object == NULL); }
149 
151  bool operator==(const SharedPtr &other) const
152  { return(_object == other._object); }
153 
155  bool operator!=(const SharedPtr &other) const
156  { return(! operator==(other)); }
157 
159  int getRefCount() const
160  { return(_object ? _refs->get() : 0); }
161 
162  private:
163 
164  template<class U> friend class SharedPtr;
165 
166  template<typename U>
167  explicit SharedPtr(const SharedPtr<U> &other)
168  : _object(dynamic_cast<T *>(other._object))
169  {
170  if(_object)
171  {
172  _refs = other._refs;
173  ++(*_refs);
174  }
175  else
176  _refs = new AtomicCounter(1);
177  }
178 
179  template<typename U>
180  SharedPtr(const SharedPtr<U> &other, bool tag)
181  : _object(static_cast<T *>(other._object)),
182  _refs(other._refs)
183  {
184  ++(*_refs);
185  }
186 
187  void _release()
188  {
189  if(--(*_refs) == 0)
190  {
191  delete _object;
192  delete _refs;
193  }
194  }
195 
196  T* _object;
197  AtomicCounter* _refs;
198 };
199 
200 } // namespace ccxx
201 
202 #endif // __ccxx_SharedPtr_hxx
~SharedPtr()
Destructor.
Definition: SharedPtr.h++:57
int getRefCount() const
Get the reference count for this pointer.
Definition: SharedPtr.h++:159
T & operator*()
Dereference operator.
Definition: SharedPtr.h++:115
A threadsafe, reference-counting smart pointer.
Definition: SharedPtr.h++:39
const T * operator->() const
Pointer-to-member operator.
Definition: SharedPtr.h++:107
bool operator!() const
Test if the pointer is NULL.
Definition: SharedPtr.h++:139
An exception indicating an attempt to dereference a null pointer.
Definition: NullPointerException.h++:37
int32_t get() const
Get the current value of the counter.
Definition: AtomicCounter.c++:139
friend class SharedPtr
Definition: SharedPtr.h++:164
SharedPtr(const SharedPtr &other)
Copy constructor.
Definition: SharedPtr.h++:49
SharedPtr & operator=(const SharedPtr &other)
Assignment operator.
Definition: SharedPtr.h++:77
bool isNull() const
Test if the pointer is NULL.
Definition: SharedPtr.h++:147
T * operator->()
Pointer-to-member operator.
Definition: SharedPtr.h++:103
An integer counter whose value is modified in an atomic fashion.
Definition: AtomicCounter.h++:36
bool operator==(const SharedPtr &other) const
Equality operator.
Definition: SharedPtr.h++:151
SharedPtr< U > staticCast()
Static cast.
Definition: SharedPtr.h++:66
const T & operator*() const
Dereference operator.
Definition: SharedPtr.h++:127
Definition: AllocationMap.c++:25
SharedPtr< U > dynamicCast()
Dynamic cast.
Definition: SharedPtr.h++:73
bool operator!=(const SharedPtr &other) const
Inequality operator.
Definition: SharedPtr.h++:155
SharedPtr(T *object=NULL)
Construct a new SharedPtr for the given object pointer.
Definition: SharedPtr.h++:44
SharedPtr & operator=(T *object)
Assignment operator.
Definition: SharedPtr.h++:90