3 * Copyright (C) 2005-2012 Mathias Froehlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef SGSharedPtr_HXX
22 #define SGSharedPtr_HXX
24 #include "SGReferenced.hxx"
27 /// This class is a pointer proxy doing reference counting on the object
28 /// it is pointing to.
29 /// The SGSharedPtr class handles reference counting and possible
30 /// destruction if no nore references are in use automatically.
31 /// Classes derived from @SGReferenced can be handled with SGSharedPtr.
32 /// Once you have a SGSharedPtr available you can use it just like
33 /// a usual pointer with the exception that you don't need to delete it.
34 /// Such a reference is initialized by zero if not initialized with a
36 /// One thing you need to avoid are cyclic loops with such pointers.
37 /// As long as such a cyclic loop exists the reference count never drops
38 /// to zero and consequently the objects will never be destroyed.
39 /// Always try to use directed graphs where the references away from the
40 /// top node are made with SGSharedPtr's and the back references are done with
41 /// ordinary pointers or SGWeakPtr's.
42 /// There is a very good description of OpenSceneGraphs ref_ptr which is
43 /// pretty much the same than this one at
44 /// http://dburns.dhs.org/OSG/Articles/RefPointers/RefPointers.html
52 typedef T element_type;
54 SGSharedPtr(void) : _ptr(0)
56 SGSharedPtr(T* ptr) : _ptr(ptr)
58 SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get())
61 SGSharedPtr(const SGSharedPtr<U>& p) : _ptr(p.get())
66 SGSharedPtr& operator=(const SGSharedPtr& p)
67 { reset(p.get()); return *this; }
69 SGSharedPtr& operator=(const SGSharedPtr<U>& p)
70 { reset(p.get()); return *this; }
72 SGSharedPtr& operator=(U* p)
73 { reset(p); return *this; }
75 T* operator->(void) const
77 T& operator*(void) const
79 operator T*(void) const
86 { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
88 { if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
90 { SGSharedPtr(p).swap(*this); }
92 bool isShared(void) const
93 { return T::shared(_ptr); }
94 unsigned getNumRefs(void) const
95 { return T::count(_ptr); }
97 bool valid(void) const
98 { return _ptr != (T*)0; }
102 void swap(SGSharedPtr& other)
103 { std::swap(_ptr, other._ptr); }
106 void assignNonRef(T* p)
107 { reset(); _ptr = p; }
109 void get(const T* p) const
112 // The reference itself.
116 friend class SGWeakPtr;
120 * Support for boost::mem_fn
123 T* get_pointer(SGSharedPtr<T> const & p)
129 * static_cast for SGSharedPtr
131 template<class T, class U>
132 SGSharedPtr<T> static_pointer_cast(SGSharedPtr<U> const & r)
134 return SGSharedPtr<T>( static_cast<T*>(r.get()) );
138 * Compare two SGSharedPtr<T> objects for equality.
140 * @note Only pointer values are compared, not the actual objects they are
143 template<class T, class U>
144 bool operator==(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
146 return lhs.get() == rhs.get();
150 * Compare two SGSharedPtr<T> objects for equality.
152 * @note Only pointer values are compared, not the actual objects they are
155 template<class T, class U>
156 bool operator!=(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
158 return lhs.get() != rhs.get();
162 * Compare two SGSharedPtr<T> objects for weak ordering.
164 * @note Only pointer values are compared, not the actual objects they are
166 * @note This allows using SGSharedPtr as key in associative containers like for
167 * example std::map and std::set.
169 template<class T, class U>
170 bool operator<(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
172 return lhs.get() < rhs.get();