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