]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGSharedPtr.hxx
canvas::Layout: support for contents margins.
[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 template<typename T>
28 class SGWeakPtr;
29
30 /// This class is a pointer proxy doing reference counting on the object
31 /// it is pointing to.
32 /// The SGSharedPtr class handles reference counting and possible
33 /// destruction if no nore references are in use automatically.
34 /// Classes derived from @SGReferenced can be handled with SGSharedPtr.
35 /// Once you have a SGSharedPtr available you can use it just like
36 /// a usual pointer with the exception that you don't need to delete it.
37 /// Such a reference is initialized by zero if not initialized with a
38 /// class pointer.
39 /// One thing you need to avoid are cyclic loops with such pointers.
40 /// As long as such a cyclic loop exists the reference count never drops
41 /// to zero and consequently the objects will never be destroyed.
42 /// Always try to use directed graphs where the references away from the
43 /// top node are made with SGSharedPtr's and the back references are done with
44 /// ordinary pointers or SGWeakPtr's.
45 /// There is a very good description of OpenSceneGraphs ref_ptr which is
46 /// pretty much the same than this one at
47 /// http://dburns.dhs.org/OSG/Articles/RefPointers/RefPointers.html
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   template<typename U>
64   explicit SGSharedPtr(const SGWeakPtr<U>& p): _ptr(0)
65   { reset(p.lock().get()); }
66   ~SGSharedPtr(void)
67   { reset(); }
68   
69   SGSharedPtr& operator=(const SGSharedPtr& p)
70   { reset(p.get()); return *this; }
71   template<typename U>
72   SGSharedPtr& operator=(const SGSharedPtr<U>& p)
73   { reset(p.get()); return *this; }
74   template<typename U>
75   SGSharedPtr& operator=(U* p)
76   { reset(p); return *this; }
77
78   T* operator->(void) const
79   { return _ptr; }
80   T& operator*(void) const
81   { return *_ptr; }
82   operator T*(void) const
83   { return _ptr; }
84   T* ptr(void) const
85   { return _ptr; }
86   T* get(void) const
87   { return _ptr; }
88   T* release()
89   { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
90   void reset()
91   { if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
92   void reset(T* p)
93   { SGSharedPtr(p).swap(*this); }
94
95   bool isShared(void) const
96   { return T::shared(_ptr); }
97   unsigned getNumRefs(void) const
98   { return T::count(_ptr); }
99
100   bool valid(void) const
101   { return _ptr != (T*)0; }
102
103   void clear()
104   { reset(); }
105   void swap(SGSharedPtr& other)
106   { std::swap(_ptr, other._ptr); }
107
108 private:
109   void assignNonRef(T* p)
110   { reset(); _ptr = p; }
111
112   void get(const T* p) const
113   { T::get(p); }
114   
115   // The reference itself.
116   T* _ptr;
117
118   template<typename U>
119   friend class SGWeakPtr;
120 };
121
122 /**
123  * Support for boost::mem_fn
124  */
125 template<typename T>
126 T* get_pointer(SGSharedPtr<T> const & p)
127 {
128   return p.ptr();
129 }
130
131 /**
132  * static_cast for SGSharedPtr
133  */
134 template<class T, class U>
135 SGSharedPtr<T> static_pointer_cast(SGSharedPtr<U> const & r)
136 {
137   return SGSharedPtr<T>( static_cast<T*>(r.get()) );
138 }
139
140 /**
141  * Compare two SGSharedPtr<T> objects for equality.
142  *
143  * @note Only pointer values are compared, not the actual objects they are
144  *       pointing at.
145  */
146 template<class T, class U>
147 bool operator==(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
148 {
149   return lhs.get() == rhs.get();
150 }
151
152 /**
153  * Compare two SGSharedPtr<T> objects for equality.
154  *
155  * @note Only pointer values are compared, not the actual objects they are
156  *       pointing at.
157  */
158 template<class T, class U>
159 bool operator!=(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
160 {
161   return lhs.get() != rhs.get();
162 }
163
164 /**
165  * Compare two SGSharedPtr<T> objects for weak ordering.
166  *
167  * @note Only pointer values are compared, not the actual objects they are
168  *       pointing at.
169  * @note This allows using SGSharedPtr as key in associative containers like for
170  *       example std::map and std::set.
171  */
172 template<class T, class U>
173 bool operator<(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
174 {
175   return lhs.get() < rhs.get();
176 }
177 #endif