Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
_uniqueptr.h
Go to the documentation of this file.
1// -*- C++ -*-
2
33#ifndef _RWSTD_RW_UNIQUEPTR_H_INCLUDED
34#define _RWSTD_RW_UNIQUEPTR_H_INCLUDED
35
36#ifndef _RWSTD_NULLPTR_T
37#define _RWSTD_NULLPTR_T decltype(nullptr)
38#endif
39
40#ifndef _RWSTD_RW_DEFS_H_INCLUDED
41# include <rw/_defs.h>
42#endif
43#ifndef _RWSTD_RW_FORWARD_INCLUDED
44# include <rw/_forward.h>
45#endif
46#ifndef _RWSTD_RW_META_ARR_H_INCLUDED
47# include <rw/_meta_arr.h>
48#endif
50
51 template <class _TypeT>
52 struct __rw_unique_if;
53
54 template <class _TypeT>
55 struct __rw_unique_if<_TypeT[]>;
56
57 template <class _TypeT, _RWSTD_SIZE_T _N>
58 struct __rw_unique_if<_TypeT[_N]>;
59
60
61} // namespace __rw
62
64
65// 20.7.1 - class template default_delete
66
67template <class _TypeT>
68struct default_delete
69{
70 default_delete () _THROWS (()) { }
71
72 template <class _TypeU>
73 default_delete (const default_delete<_TypeU>&) _THROWS (()) { }
74
75 void operator() (_TypeT* __p) const _THROWS (()) {
76 delete __p;
77 }
78};
79
80template <class _TypeT>
81struct default_delete<_TypeT[]>
82{
83 void operator() (_TypeT* __p) const _THROWS (()) {
84 delete[] __p;
85 }
86};
87
88
89// 20.7.1.2 - class template unique_ptr for single objects
90
91template <class _TypeT, class _Deleter = default_delete<_TypeT> >
92class unique_ptr
93{
94public:
95 typedef _TypeT element_type;
96 typedef _Deleter deleter_type;
97 typedef _TypeT* pointer;
98
99 // 20.7.1.2.1, constructors
100
101 unique_ptr () _THROWS (())
102 : _C_ptr (0), _C_del () { }
103
104#ifndef _RWSTD_NO_NULLPTR
105 unique_ptr (_RWSTD_NULLPTR_T) _THROWS (())
106 : _C_ptr (0), _C_del () { }
107#endif
108
109 explicit unique_ptr (pointer __p) _THROWS (())
110 : _C_ptr (__p), _C_del () { }
111
112 unique_ptr (pointer __p, const deleter_type& __d) _THROWS (())
113 : _C_ptr (__p), _C_del (__d) { }
114
115#ifndef _RWSTD_NO_RVALUE_REFERENCES
116
117 unique_ptr (unique_ptr&& __rhs) _THROWS (())
118 : _C_ptr (__rhs.release ()), _C_del (__rhs._C_del) { }
119
120 template <class _TypeU, class _DeleterU>
121 unique_ptr (unique_ptr<_TypeU, _DeleterU>&& __rhs) _THROWS (())
122 : _C_ptr (__rhs.release ()), _C_del (__rhs.get_deleter ()) { }
123
124#endif // !_RWSTD_NO_RVALUE_REFERENCES
125
126 // 20.7.1.2.2, destructor
127 ~unique_ptr () _THROWS (()) {
128 if (_C_ptr)
129 _C_del (_C_ptr);
130 }
131
132 // 20.7.1.2.3, assignment
133
134#ifndef _RWSTD_NO_RVALUE_REFERENCES
135
136 unique_ptr& operator= (unique_ptr&& __rhs) _THROWS (()) {
137 reset (__rhs.release ());
138 _C_del = _RWSTD_MOVE (__rhs._C_del);
139 return *this;
140 }
141
142 template <class _TypeU, class _DeleterU>
143 unique_ptr& operator= (unique_ptr<_TypeU, _DeleterU>&& __rhs) _THROWS (()) {
144 reset (__rhs.release ());
145 _C_del = _RWSTD_MOVE (__rhs.get_deleter ());
146 return *this;
147 }
148
149#endif // !_RWSTD_NO_RVALUE_REFERENCES
150
151 // 20.7.1.2.4, observers
152
153 element_type& operator* () const _THROWS (()) {
154 _RWSTD_ASSERT (0 != _C_ptr);
155 return *_C_ptr;
156 }
157
158 pointer operator-> () const _THROWS (()) {
159 _RWSTD_ASSERT (0 != _C_ptr);
160 return _C_ptr;
161 }
162
163 pointer get () const _THROWS (()) {
164 return _C_ptr;
165 }
166
167 deleter_type& get_deleter () _THROWS (()) {
168 return _C_del;
169 }
170
171 const deleter_type& get_deleter () const _THROWS (()) {
172 return _C_del;
173 }
174
175 operator bool () const _THROWS (()) {
176 return 0 != _C_ptr;
177 }
178
179 // 20.7.1.2.5, modifiers
180
181 pointer release () _THROWS (()) {
182 pointer __tmp = _C_ptr;
183 _C_ptr = 0;
184 return __tmp;
185 }
186
187 void reset (pointer __p = 0) _THROWS (()) {
188 if (_C_ptr != __p) {
189 if (_C_ptr)
190 _C_del (_C_ptr);
191 _C_ptr = __p;
192 }
193 }
194
195 void swap (unique_ptr& __other) _THROWS (()) {
196 pointer __tmp_ptr = _C_ptr;
197 deleter_type __tmp_del = _C_del;
198 _C_ptr = __other._C_ptr;
199 _C_del = __other._C_del;
200 __other._C_ptr = __tmp_ptr;
201 __other._C_del = __tmp_del;
202 }
203
204private:
205 unique_ptr (const unique_ptr&);
206 unique_ptr& operator= (const unique_ptr&);
207
208 pointer _C_ptr;
209 deleter_type _C_del;
210};
211
212
213// 20.7.1.3 - class template unique_ptr for arrays
214
215template <class _TypeT, class _Deleter>
216class unique_ptr<_TypeT[], _Deleter>
217{
218public:
219 typedef _TypeT element_type;
220 typedef _Deleter deleter_type;
221 typedef _TypeT* pointer;
222
223 // 20.7.1.3.1, constructors
224
225 unique_ptr () _THROWS (())
226 : _C_ptr (0), _C_del () { }
227
228#ifndef _RWSTD_NO_NULLPTR
229 unique_ptr (_RWSTD_NULLPTR_T) _THROWS (())
230 : _C_ptr (0), _C_del () { }
231#endif
232
233 explicit unique_ptr (pointer __p) _THROWS (())
234 : _C_ptr (__p), _C_del () { }
235
236 unique_ptr (pointer __p, const deleter_type& __d) _THROWS (())
237 : _C_ptr (__p), _C_del (__d) { }
238
239#ifndef _RWSTD_NO_RVALUE_REFERENCES
240
241 unique_ptr (unique_ptr&& __rhs) _THROWS (())
242 : _C_ptr (__rhs.release ()), _C_del (__rhs._C_del) { }
243
244#endif // !_RWSTD_NO_RVALUE_REFERENCES
245
246 // 20.7.1.3.2, destructor
247 ~unique_ptr () _THROWS (()) {
248 if (_C_ptr)
249 _C_del (_C_ptr);
250 }
251
252 // 20.7.1.3.3, assignment
253
254#ifndef _RWSTD_NO_RVALUE_REFERENCES
255
256 unique_ptr& operator= (unique_ptr&& __rhs) _THROWS (()) {
257 reset (__rhs.release ());
258 _C_del = _RWSTD_MOVE (__rhs._C_del);
259 return *this;
260 }
261
262#endif // !_RWSTD_NO_RVALUE_REFERENCES
263
264 // 20.7.1.3.4, observers
265
266 element_type& operator[] (_RWSTD_SIZE_T __i) const _THROWS (()) {
267 return _C_ptr[__i];
268 }
269
270 pointer get () const _THROWS (()) { return _C_ptr; }
271
272 deleter_type& get_deleter () _THROWS (()) { return _C_del; }
273
274 const deleter_type& get_deleter () const _THROWS (()) { return _C_del; }
275
276 operator bool () const _THROWS (()) { return 0 != _C_ptr; }
277
278 // 20.7.1.3.5, modifiers
279
280 pointer release () _THROWS (()) {
281 pointer __tmp = _C_ptr;
282 _C_ptr = 0;
283 return __tmp;
284 }
285
286 void reset (pointer __p = 0) _THROWS (()) {
287 if (_C_ptr != __p) {
288 if (_C_ptr)
289 _C_del (_C_ptr);
290 _C_ptr = __p;
291 }
292 }
293
294 void swap (unique_ptr& __other) _THROWS (()) {
295 pointer __tmp_ptr = _C_ptr;
296 deleter_type __tmp_del = _C_del;
297 _C_ptr = __other._C_ptr;
298 _C_del = __other._C_del;
299 __other._C_ptr = __tmp_ptr;
300 __other._C_del = __tmp_del;
301 }
302
303private:
304 unique_ptr (const unique_ptr&);
305 unique_ptr& operator= (const unique_ptr&);
306
307 pointer _C_ptr;
308 deleter_type _C_del;
309};
310
311
312// 20.7.1.4 - unique_ptr specialized algorithms
313
314template <class _TypeT, class _Deleter>
315inline void
316swap (unique_ptr<_TypeT, _Deleter>& __lhs,
317 unique_ptr<_TypeT, _Deleter>& __rhs) _THROWS (())
318{
319 __lhs.swap (__rhs);
320}
321
322
323// 20.7.1.5 - unique_ptr comparisons
324
325template <class _TypeT, class _D1, class _TypeU, class _D2>
326inline bool
327operator== (const unique_ptr<_TypeT, _D1>& __lhs,
328 const unique_ptr<_TypeU, _D2>& __rhs)
329{
330 return __lhs.get () == __rhs.get ();
331}
332
333template <class _TypeT, class _D1, class _TypeU, class _D2>
334inline bool
335operator!= (const unique_ptr<_TypeT, _D1>& __lhs,
336 const unique_ptr<_TypeU, _D2>& __rhs)
337{
338 return __lhs.get () != __rhs.get ();
339}
340
341template <class _TypeT, class _D1, class _TypeU, class _D2>
342inline bool
343operator< (const unique_ptr<_TypeT, _D1>& __lhs,
344 const unique_ptr<_TypeU, _D2>& __rhs)
345{
346 return __lhs.get () < __rhs.get ();
347}
348
349template <class _TypeT, class _D1, class _TypeU, class _D2>
350inline bool
351operator<= (const unique_ptr<_TypeT, _D1>& __lhs,
352 const unique_ptr<_TypeU, _D2>& __rhs)
353{
354 return !(__rhs < __lhs);
355}
356
357template <class _TypeT, class _D1, class _TypeU, class _D2>
358inline bool
359operator> (const unique_ptr<_TypeT, _D1>& __lhs,
360 const unique_ptr<_TypeU, _D2>& __rhs)
361{
362 return __rhs < __lhs;
363}
364
365template <class _TypeT, class _D1, class _TypeU, class _D2>
366inline bool
367operator>= (const unique_ptr<_TypeT, _D1>& __lhs,
368 const unique_ptr<_TypeU, _D2>& __rhs)
369{
370 return !(__lhs < __rhs);
371}
372
373#ifndef _RWSTD_NO_NULLPTR
374
375template <class _TypeT, class _Deleter>
376inline bool
377operator== (const unique_ptr<_TypeT, _Deleter>& __p, _RWSTD_NULLPTR_T)
378{
379 return !__p;
380}
381
382template <class _TypeT, class _Deleter>
383inline bool
384operator== (_RWSTD_NULLPTR_T, const unique_ptr<_TypeT, _Deleter>& __p)
385{
386 return !__p;
387}
388
389template <class _TypeT, class _Deleter>
390inline bool
391operator!= (const unique_ptr<_TypeT, _Deleter>& __p, _RWSTD_NULLPTR_T)
392{
393 return static_cast<bool> (__p);
394}
395
396template <class _TypeT, class _Deleter>
397inline bool
398operator!= (_RWSTD_NULLPTR_T, const unique_ptr<_TypeT, _Deleter>& __p)
399{
400 return static_cast<bool> (__p);
401}
402
403template <class _TypeT, class _Deleter>
404inline bool
405operator< (const unique_ptr<_TypeT, _Deleter>& __p, _RWSTD_NULLPTR_T)
406{
407 typedef typename unique_ptr<_TypeT, _Deleter>::pointer _Ptr;
408 return __p.get () < static_cast<_Ptr> (0);
409}
410
411template <class _TypeT, class _Deleter>
412inline bool
413operator< (_RWSTD_NULLPTR_T, const unique_ptr<_TypeT, _Deleter>& __p)
414{
415 typedef typename unique_ptr<_TypeT, _Deleter>::pointer _Ptr;
416 return static_cast<_Ptr> (0) < __p.get ();
417}
418
419template <class _TypeT, class _Deleter>
420inline bool
421operator<= (const unique_ptr<_TypeT, _Deleter>& __p, _RWSTD_NULLPTR_T)
422{
423 return !(nullptr < __p);
424}
425
426template <class _TypeT, class _Deleter>
427inline bool
428operator<= (_RWSTD_NULLPTR_T, const unique_ptr<_TypeT, _Deleter>& __p)
429{
430 return !(__p < nullptr);
431}
432
433template <class _TypeT, class _Deleter>
434inline bool
435operator> (const unique_ptr<_TypeT, _Deleter>& __p, _RWSTD_NULLPTR_T)
436{
437 return nullptr < __p;
438}
439
440template <class _TypeT, class _Deleter>
441inline bool
442operator> (_RWSTD_NULLPTR_T, const unique_ptr<_TypeT, _Deleter>& __p)
443{
444 return __p < nullptr;
445}
446
447template <class _TypeT, class _Deleter>
448inline bool
449operator>= (const unique_ptr<_TypeT, _Deleter>& __p, _RWSTD_NULLPTR_T)
450{
451 return !(__p < nullptr);
452}
453
454template <class _TypeT, class _Deleter>
455inline bool
456operator>= (_RWSTD_NULLPTR_T, const unique_ptr<_TypeT, _Deleter>& __p)
457{
458 return !(nullptr < __p);
459}
460
461#endif // !_RWSTD_NO_NULLPTR
462
463
464// 20.7.1.6 - make_unique
465
466#if !defined (_RWSTD_NO_RVALUE_REFERENCES) && \
467 !defined (_RWSTD_NO_VARIADIC_TEMPLATES)
468
469template <class _TypeT, class... _Args>
470inline typename _RW::__rw_unique_if<_TypeT>::__single_object
471make_unique (_Args&&... __args)
472{
473 return unique_ptr<_TypeT>(new _TypeT (_RWSTD_FORWARD (_Args, __args)...));
474}
475
476template <class _TypeT>
477inline typename _RW::__rw_unique_if<_TypeT>::__unknown_bound
478make_unique (_RWSTD_SIZE_T __n)
479{
480 typedef typename _RW::__rw_remove_extent<_TypeT>::type _TypeU;
481 return unique_ptr<_TypeT>(new _TypeU[__n]);
482}
483
484#endif // rvalue refs + variadic templates
485
486
487} // namespace std
488
490
491 template <class _TypeT>
492 struct __rw_unique_if
493 {
494 typedef _STD::unique_ptr<_TypeT> __single_object;
495 };
496
497 template <class _TypeT>
498 struct __rw_unique_if<_TypeT[]>
499 {
500 typedef _STD::unique_ptr<_TypeT[]> __unknown_bound;
501 };
502
503 template <class _TypeT, _RWSTD_SIZE_T _N>
504 struct __rw_unique_if<_TypeT[_N]>
505 {
506 };
507
508
509} // namespace __rw
510
511#endif // _RWSTD_RW_UNIQUEPTR_H_INCLUDED
const plus_asgn< T > & operator*(const plus_asgn< T > &lhs, const plus_asgn< T > &rhs)
#define _THROWS()
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
#define _RWSTD_ASSERT(ignore)
Definition _defs.h:1108
#define _RWSTD_MOVE(__x)
#define _RWSTD_FORWARD(_TypeT, __x)
#define _RWSTD_NULLPTR_T
Definition _uniqueptr.h:37
bool operator>(const DistancePair &lhs, const DistancePair &rhs)
Definition graph.cpp:47
bool operator==(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.h:14737
bool operator!=(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.h:14762
bool operator<(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.h:14787
Definition json.h:5363
#define bool
Definition stdbool.h:24