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"
26 /// This class is a pointer proxy doing reference counting on the object
27 /// it is pointing to.
28 /// The SGSharedPtr class handles reference counting and possible
29 /// destruction if no nore references are in use automatically.
30 /// Classes derived from @SGReferenced can be handled with SGSharedPtr.
31 /// Once you have a SGSharedPtr available you can use it just like
32 /// a usual pointer with the exception that you don't need to delete it.
33 /// Such a reference is initialized by zero if not initialized with a
35 /// One thing you need to avoid are cyclic loops with such pointers.
36 /// As long as such a cyclic loop exists the reference count never drops
37 /// to zero and consequently the objects will never be destroyed.
38 /// Always try to use directed graphs where the references away from the
39 /// top node are made with SGSharedPtr's and the back references are done with
40 /// ordinary pointers or SGWeakPtr's.
41 /// There is a very good description of OpenSceneGraphs ref_ptr which is
42 /// pretty much the same than this one at
43 /// 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())
65 SGSharedPtr& operator=(const SGSharedPtr& p)
66 { assign(p.get()); return *this; }
68 SGSharedPtr& operator=(const SGSharedPtr<U>& p)
69 { assign(p.get()); return *this; }
71 SGSharedPtr& operator=(U* p)
72 { assign(p); return *this; }
74 T* operator->(void) const
76 T& operator*(void) const
78 operator T*(void) const
85 { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
87 bool isShared(void) const
88 { return T::shared(_ptr); }
89 unsigned getNumRefs(void) const
90 { return T::count(_ptr); }
92 bool valid(void) const
93 { return _ptr != (T*)0; }
97 void swap(SGSharedPtr& sharedPtr)
98 { T* tmp = _ptr; _ptr = sharedPtr._ptr; sharedPtr._ptr = tmp; }
102 { get(p); put(); _ptr = p; }
103 void assignNonRef(T* p)
106 void get(const T* p) const
109 { if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
111 // The reference itself.
115 friend class SGWeakPtr;
119 * Support for boost::mem_fn
122 T* get_pointer(SGSharedPtr<T> const & p)
128 * static_cast for SGSharedPtr
130 template<class T, class U>
131 SGSharedPtr<T> static_pointer_cast(SGSharedPtr<U> const & r)
133 return SGSharedPtr<T>( static_cast<T*>(r.get()) );