2 /// Pointer proxy doing reference counting.
4 // Copyright (C) 2005-2012 Mathias Froehlich
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 #ifndef SGSharedPtr_HXX
21 #define SGSharedPtr_HXX
23 #include "SGReferenced.hxx"
29 /// This class is a pointer proxy doing reference counting on the object
30 /// it is pointing to.
31 /// The SGSharedPtr class handles reference counting and possible
32 /// destruction if no nore references are in use automatically.
33 /// Classes derived from SGReferenced can be handled with SGSharedPtr.
34 /// Once you have a SGSharedPtr available you can use it just like
35 /// a usual pointer with the exception that you don't need to delete it.
36 /// Such a reference is initialized by zero if not initialized with a
38 /// One thing you need to avoid are cyclic loops with such pointers.
39 /// As long as such a cyclic loop exists the reference count never drops
40 /// to zero and consequently the objects will never be destroyed.
41 /// Always try to use directed graphs where the references away from the
42 /// top node are made with SGSharedPtr's and the back references are done with
43 /// ordinary pointers or SGWeakPtr's.
44 /// There is a very good description of OpenSceneGraphs ref_ptr which is
45 /// pretty much the same than this one at
46 /// http://dburns.dhs.org/OSG/Articles/RefPointers/RefPointers.html
51 typedef T element_type;
53 SGSharedPtr(void) : _ptr(0)
55 SGSharedPtr(T* ptr) : _ptr(ptr)
57 SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get())
60 SGSharedPtr(const SGSharedPtr<U>& p) : _ptr(p.get())
63 explicit SGSharedPtr(const SGWeakPtr<U>& p): _ptr(0)
64 { reset(p.lock().get()); }
68 SGSharedPtr& operator=(const SGSharedPtr& p)
69 { reset(p.get()); return *this; }
71 SGSharedPtr& operator=(const SGSharedPtr<U>& p)
72 { reset(p.get()); return *this; }
74 SGSharedPtr& operator=(U* p)
75 { reset(p); return *this; }
77 T* operator->(void) const
79 T& operator*(void) const
81 operator T*(void) const
88 { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
90 { if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
92 { SGSharedPtr(p).swap(*this); }
94 bool isShared(void) const
95 { return T::shared(_ptr); }
96 unsigned getNumRefs(void) const
97 { return T::count(_ptr); }
99 bool valid(void) const
100 { return _ptr != (T*)0; }
104 void swap(SGSharedPtr& other)
105 { std::swap(_ptr, other._ptr); }
108 void assignNonRef(T* p)
109 { reset(); _ptr = p; }
111 void get(const T* p) const
114 // The reference itself.
118 friend class SGWeakPtr;
122 * Support for boost::mem_fn
125 T* get_pointer(SGSharedPtr<T> const & p)
131 * static_cast for SGSharedPtr
133 template<class T, class U>
134 SGSharedPtr<T> static_pointer_cast(SGSharedPtr<U> const & r)
136 return SGSharedPtr<T>( static_cast<T*>(r.get()) );
140 * Compare two SGSharedPtr<T> objects for equality.
142 * @note Only pointer values are compared, not the actual objects they are
145 template<class T, class U>
146 bool operator==(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
148 return lhs.get() == rhs.get();
152 * Compare two SGSharedPtr<T> objects for equality.
154 * @note Only pointer values are compared, not the actual objects they are
157 template<class T, class U>
158 bool operator!=(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
160 return lhs.get() != rhs.get();
164 * Compare two SGSharedPtr<T> objects for weak ordering.
166 * @note Only pointer values are compared, not the actual objects they are
168 * @note This allows using SGSharedPtr as key in associative containers like for
169 * example std::map and std::set.
171 template<class T, class U>
172 bool operator<(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
174 return lhs.get() < rhs.get();