cartobase 6.0.6
weakptr.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
35// The Loki Library
36// Copyright (c) 2001 by Andrei Alexandrescu
37// This code accompanies the book:
38// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
39// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
40// Permission to use, copy, modify, distribute and sell this software for any
41// purpose is hereby granted without fee, provided that the above copyright
42// notice appear in all copies and that both that copyright notice and this
43// permission notice appear in supporting documentation.
44// The author or Addison-Welsey Longman make no representations about the
45// suitability of this software for any purpose. It is provided "as is"
46// without express or implied warranty.
48
49#ifndef CARTOBASE_SMART_WEAKPTR_H
50#define CARTOBASE_SMART_WEAKPTR_H
51
52
55#include <memory>
56
57
58namespace carto {
59
60
61template <typename T> class weak_ptr;
62template <typename T, typename U> weak_ptr<U> weak_cast( const weak_ptr<T>& );
63template <typename T> class weak_shared_ptr;
64template <typename T> class shared_ptr;
65
66
67template <typename T>
69{
70
71 friend class WeakObject;
72
73#if !defined( CARTO_NO_MEMBER_TEMPLATES ) && \
74 !defined( CARTO_NO_MEMBER_TEMPLATE_FRIENDS )
75 template <typename Y> friend class weak_ptr;
76#endif
77
78#if defined( CARTO_BROKEN_FRIEND_TEMPLATE_FUNCTION )
79 template <typename U, typename V>
80 friend weak_cast( const weak_ptr< U >& );
81#else
82 template < typename U, typename V >
84#endif
85
86#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
87 class InsipidProxyPointer
88 {
89 protected:
90 void operator delete( void* );
91 void operator delete[]( void* );
92 };
93#endif
94
95 public:
96
97 explicit weak_ptr( T* p = 0 ) : pointee( p )
98 {
99 if ( pointee )
100 pointee->attachWeakPtr( *this );
101 }
102
103 void reset( T* r = 0 )
104 {
105 if ( pointee != r )
106 {
107 if ( pointee )
108 {
109 pointee->detachWeakPtr( *this );
110 pointee = 0;
111 }
112 if ( r )
113 {
114 r->attachWeakPtr( *this );
115 }
116 pointee = r;
117 }
118 }
119
120#if !defined( CARTO_NO_MEMBER_TEMPLATES )
121
122 template <typename Y>
123 weak_ptr( const weak_ptr<Y>& r ) : pointee( r.pointee )
124 {
125 if ( pointee )
126 pointee->attachWeakPtr( *this );
127 }
128
129 template <typename Y>
130 explicit weak_ptr( const rc_ptr<Y>& r ) : pointee( r.get() )
131 {
132 if ( pointee )
133 pointee->attachWeakPtr( *this );
134 }
135
136 template <typename Y>
137 explicit weak_ptr( const std::unique_ptr<Y>& r ) : pointee( r.get() )
138 {
139 if ( pointee )
140 pointee->attachWeakPtr( *this );
141 }
142
143 template <typename Y>
145 {
146 reset( r.get() );
147 return *this;
148 }
149
150 template <typename Y>
152 {
153 reset( r.get() );
154 return *this;
155 }
156
157 template <typename Y>
158 weak_ptr& operator=( const std::unique_ptr<Y>& r )
159 {
160 reset( r.get() );
161 return *this;
162 }
163
164#else
165
166 explicit weak_ptr( const rc_ptr<T>& r ) : pointee( r.get() )
167 {
168 if ( pointee )
169 pointee->attachWeakPtr( *this );
170 }
171
172 explicit weak_ptr( const std::unique_ptr<T>& r ) : pointee( r.get() )
173 {
174 if ( pointee )
175 pointee->attachWeakPtr( *this );
176 }
177
178 weak_ptr& operator=( const rc_ptr<T>& r )
179 {
180 reset( r.get() );
181 return *this;
182 }
183
184 weak_ptr& operator=( const std::unique_ptr<T>& r )
185 {
186 reset( r.get() );
187 return *this;
188 }
189
190#endif
191
192 weak_ptr( const weak_ptr& r ) : pointee( r.pointee )
193 {
194 if ( pointee )
195 pointee->attachWeakPtr( *this );
196 }
197
199 {
200 reset( w.get() );
201 return *this;
202 }
203
205 {
206 if ( pointee )
207 pointee->detachWeakPtr( *this );
208 }
209
210#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
211 operator InsipidProxyPointer*() const
212 {
213 return reinterpret_cast<InsipidProxyPointer*>( pointee );
214 }
215#endif
216
217 T& operator*() const
218 {
219 return *pointee;
220 }
221
222 T* operator->() const
223 {
224 return pointee;
225 }
226
227 T* get() const
228 {
229 return pointee;
230 }
231
233 {
234 if ( pointee )
235 pointee->detachWeakPtr( *this );
236 T* tmp = pointee;
237 pointee = 0;
238 return tmp;
239 }
240
241 bool operator == ( const weak_ptr<T> & x ) const
242 { return pointee == x.get(); }
243 bool operator == ( const weak_shared_ptr<T> & x ) const
244 { return pointee == x.get(); }
245 bool operator == ( const rc_ptr<T> & x ) const
246 { return pointee == x.get(); }
247 bool operator == ( const shared_ptr<T> & x ) const
248 { return pointee == x.get(); }
249 bool operator != ( const weak_ptr<T> & x ) const
250 { return pointee != x.get(); }
251 bool operator != ( const weak_shared_ptr<T> & x ) const
252 { return pointee != x.get(); }
253 bool operator != ( const rc_ptr<T> & x ) const
254 { return pointee != x.get(); }
255 bool operator != ( const shared_ptr<T> & x ) const
256 { return pointee != x.get(); }
257 bool operator < ( const weak_ptr<T> & x ) const
258 { return pointee < x.get(); }
259 bool operator < ( const weak_shared_ptr<T> & x ) const
260 { return pointee < x.get(); }
261 bool operator < ( const shared_ptr<T> & x ) const
262 { return pointee < x.get(); }
263 bool operator < ( const rc_ptr<T> & x ) const
264 { return pointee < x.get(); }
265
266 private:
267
268 void update()
269 {
270 pointee = 0;
271 }
272
273#if defined( CARTO_NO_MEMBER_TEMPLATES) || \
274 !defined( CARTO_NO_MEMBER_TEMPLATE_FRIENDS )
275 private:
276#else
277 public:
278#endif
279
280 T* pointee;
281
282};
283
284
285} // namespace carto
286
287
288template <typename T, typename U>
289inline
291{
292 return weak_ptr<U>( dynamic_cast<U*>( r.pointee ) );
293}
294
295
296#endif
void detachWeakPtr(weak_ptr< T > &) const
Definition weakobject.h:91
Reference-counting pointer.
Definition rcptr.h:640
T * get() const
Definition rcptr.h:666
A multi-purpose general smart pointer, which can act as either a rc_ptr, a weak_ptr or a weak_shared_...
Definition sharedptr.h:153
Observer pointer, observing a shfj::WeakObject.
Definition weakptr.h:69
WeakObject * get() const
Definition weakptr.h:227
weak_ptr(const weak_ptr &r)
Definition weakptr.h:192
friend class weak_ptr
Definition weakptr.h:75
weak_ptr(const rc_ptr< Y > &r)
Definition weakptr.h:130
bool operator!=(const weak_ptr< T > &x) const
Definition weakptr.h:249
weak_ptr & operator=(const weak_ptr &w)
Definition weakptr.h:198
friend weak_ptr< V > weak_cast(const weak_ptr< U > &)
weak_ptr & operator=(const weak_ptr< Y > &r)
Definition weakptr.h:144
weak_ptr(const weak_ptr< Y > &r)
Definition weakptr.h:123
weak_ptr(const std::unique_ptr< Y > &r)
Definition weakptr.h:137
T & operator*() const
Definition weakptr.h:217
weak_ptr & operator=(const std::unique_ptr< Y > &r)
Definition weakptr.h:158
void reset(T *r=0)
Definition weakptr.h:103
weak_ptr & operator=(const rc_ptr< Y > &r)
Definition weakptr.h:151
weak_ptr(T *p=0)
Definition weakptr.h:97
bool operator<(const weak_ptr< T > &x) const
Definition weakptr.h:257
bool operator==(const weak_ptr< T > &x) const
Definition weakptr.h:241
T * operator->() const
Definition weakptr.h:222
weak_shared_ptr: increments a reference count, is told and becomes null whenever the shared object is...
Definition sharedptr.h:95
weak_ptr< U > weak_cast(const weak_ptr< T > &)