3 * Copyright (C) 2005-2009 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 SGSharedPtr(void) : _ptr(0)
53 SGSharedPtr(T* ptr) : _ptr(ptr)
55 SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get())
58 SGSharedPtr(const SGSharedPtr<U>& p) : _ptr(p.get())
63 SGSharedPtr& operator=(const SGSharedPtr& p)
64 { assign(p.get()); return *this; }
66 SGSharedPtr& operator=(const SGSharedPtr<U>& p)
67 { assign(p.get()); return *this; }
69 SGSharedPtr& operator=(U* p)
70 { assign(p); return *this; }
72 T* operator->(void) const
74 T& operator*(void) const
76 operator T*(void) const
83 { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
85 bool isShared(void) const
86 { return T::shared(_ptr); }
87 unsigned getNumRefs(void) const
88 { return T::count(_ptr); }
90 bool valid(void) const
95 void swap(SGSharedPtr& sharedPtr)
96 { T* tmp = _ptr; _ptr = sharedPtr._ptr; sharedPtr._ptr = tmp; }
100 { get(p); put(); _ptr = p; }
101 void assignNonRef(T* p)
104 void get(const T* p) const
107 { if (!T::put(_ptr)) { delete _ptr; _ptr = 0; } }
109 // The reference itself.
113 friend class SGWeakPtr;
117 * Support for boost::mem_fn
120 T* get_pointer(SGSharedPtr<T> const & p)