]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGSharedPtr.hxx
Remove SVN sync code.
[simgear.git] / simgear / structure / SGSharedPtr.hxx
1 ///@file
2 /// Pointer proxy doing reference counting.
3 //
4 // Copyright (C) 2005-2012 Mathias Froehlich
5 //
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.
10 //
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.
15 //
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.
19
20 #ifndef SGSharedPtr_HXX
21 #define SGSharedPtr_HXX
22
23 #include "SGReferenced.hxx"
24 #include <algorithm>
25
26 template<typename T>
27 class SGWeakPtr;
28
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
37 /// class pointer.
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
47
48 template<typename T>
49 class SGSharedPtr {
50 public:
51   typedef T element_type;
52
53   SGSharedPtr(void) : _ptr(0)
54   {}
55   SGSharedPtr(T* ptr) : _ptr(ptr)
56   { get(_ptr); }
57   SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get())
58   { get(_ptr); }
59   template<typename U>
60   SGSharedPtr(const SGSharedPtr<U>& p) : _ptr(p.get())
61   { get(_ptr); }
62   template<typename U>
63   explicit SGSharedPtr(const SGWeakPtr<U>& p): _ptr(0)
64   { reset(p.lock().get()); }
65   ~SGSharedPtr(void)
66   { reset(); }
67   
68   SGSharedPtr& operator=(const SGSharedPtr& p)
69   { reset(p.get()); return *this; }
70   template<typename U>
71   SGSharedPtr& operator=(const SGSharedPtr<U>& p)
72   { reset(p.get()); return *this; }
73   template<typename U>
74   SGSharedPtr& operator=(U* p)
75   { reset(p); return *this; }
76
77   T* operator->(void) const
78   { return _ptr; }
79   T& operator*(void) const
80   { return *_ptr; }
81   operator T*(void) const
82   { return _ptr; }
83   T* ptr(void) const
84   { return _ptr; }
85   T* get(void) const
86   { return _ptr; }
87   T* release()
88   { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
89   void reset()
90   { if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
91   void reset(T* p)
92   { SGSharedPtr(p).swap(*this); }
93
94   bool isShared(void) const
95   { return T::shared(_ptr); }
96   unsigned getNumRefs(void) const
97   { return T::count(_ptr); }
98
99   bool valid(void) const
100   { return _ptr != (T*)0; }
101
102   void clear()
103   { reset(); }
104   void swap(SGSharedPtr& other)
105   { std::swap(_ptr, other._ptr); }
106
107 private:
108   void assignNonRef(T* p)
109   { reset(); _ptr = p; }
110
111   void get(const T* p) const
112   { T::get(p); }
113   
114   // The reference itself.
115   T* _ptr;
116
117   template<typename U>
118   friend class SGWeakPtr;
119 };
120
121 /**
122  * Support for boost::mem_fn
123  */
124 template<typename T>
125 T* get_pointer(SGSharedPtr<T> const & p)
126 {
127   return p.ptr();
128 }
129
130 /**
131  * static_cast for SGSharedPtr
132  */
133 template<class T, class U>
134 SGSharedPtr<T> static_pointer_cast(SGSharedPtr<U> const & r)
135 {
136   return SGSharedPtr<T>( static_cast<T*>(r.get()) );
137 }
138
139 /**
140  * Compare two SGSharedPtr<T> objects for equality.
141  *
142  * @note Only pointer values are compared, not the actual objects they are
143  *       pointing at.
144  */
145 template<class T, class U>
146 bool operator==(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
147 {
148   return lhs.get() == rhs.get();
149 }
150
151 /**
152  * Compare two SGSharedPtr<T> objects for equality.
153  *
154  * @note Only pointer values are compared, not the actual objects they are
155  *       pointing at.
156  */
157 template<class T, class U>
158 bool operator!=(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
159 {
160   return lhs.get() != rhs.get();
161 }
162
163 /**
164  * Compare two SGSharedPtr<T> objects for weak ordering.
165  *
166  * @note Only pointer values are compared, not the actual objects they are
167  *       pointing at.
168  * @note This allows using SGSharedPtr as key in associative containers like for
169  *       example std::map and std::set.
170  */
171 template<class T, class U>
172 bool operator<(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
173 {
174   return lhs.get() < rhs.get();
175 }
176 #endif