Debian Codegraph @

headers/memory

raw
1 /* Copyright (C) 2015 Povilas Kanapickas <[email protected]>
2
3 This file is part of cppreference-doc
4
5 This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
6 Unported License. To view a copy of this license, visit
7 http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative
8 Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
9
10 Permission is granted to copy, distribute and/or modify this document
11 under the terms of the GNU Free Documentation License, Version 1.3 or
12 any later version published by the Free Software Foundation; with no
13 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
14 */
15
16 #ifndef CPPREFERENCE_MEMORY_H
17 #define CPPREFERENCE_MEMORY_H
18
19 namespace std {
20
21 #if CPPREFERENCE_STDVER>= 2011
22 template<class T>
23 struct default_delete {
24 constexpr default_delete() = default;
25 template <class U>
26 default_delete(const default_delete<U>& d);
27 template<class U>
28 default_delete(const default_delete<U[]>& d);
29
30 void operator()(T* ptr) const;
31
32 template <class U>
33 void operator()(U* ptr) const;
34 };
35
36 template <
37 class T,
38 class Deleter = std::default_delete<T>
39 > class unique_ptr {
40 public:
41 // SIMPLIFIED: the array specialization is omitted
42
43 #if CPPREFERENCE_SIMPLIFY_TYPEDEFS
44 typedef T* pointer;
45 #else
46 typedef typename std::remove_reference<Deleter>::type::pointer pointer;
47 #endif
48 typedef T element_type;
49 typedef Deleter deleter_type;
50
51 constexpr unique_ptr();
52 constexpr unique_ptr(nullptr_t);
53 explicit unique_ptr(pointer p);
54 unique_ptr(pointer p, const Deleter& d1); // SIMPLIFIED
55 unique_ptr(pointer p, Deleter&& d2); // SIMPLIFIED
56 unique_ptr(unique_ptr&& u);
57
58 template<class U, class E>
59 unique_ptr(unique_ptr<U, E>&& u);
60 // SIMPLIFIED: the array versions are omitted
61
62 ~unique_ptr();
63
64 unique_ptr& operator=(unique_ptr&& r);
65
66 template<class U, class E>
67 unique_ptr& operator=(unique_ptr<U, E>&& r);
68
69 unique_ptr& operator=(nullptr_t);
70
71 pointer release();
72
73 void reset(pointer ptr = pointer());
74
75 void swap(unique_ptr& other);
76 pointer get() const;
77
78 Deleter& get_deleter();
79 const Deleter& get_deleter() const;
80
81 explicit operator bool() const;
82
83 T& operator*() const; // SIMPLIFIED
84 pointer operator->();
85
86 // only in array versions
87 T& operator[](size_t i) const;
88 };
89
90 // simplified: the comparison operators are omitted
91
92 template<class T, class Deleter>
93 void swap(unique_ptr<T, Deleter>& lhs, unique_ptr<T, Deleter>& rhs);
94
95 #if CPPREFERENCE_STDVER>= 2014
96
97 template<class T, class... Args>
98 unique_ptr<T> make_unique(Args&& ... args);
99 // SIMPLIFIED: the array overload are omitted
100
101 #endif
102
103
104 template<class T> class shared_ptr {
105 public:
106 typedef T element_type;
107
108 constexpr shared_ptr();
109
110 template<class Y>
111 explicit shared_ptr(Y* ptr);
112
113 template<class Y, class Deleter>
114 shared_ptr(Y* ptr, Deleter d);
115
116 template<class Y, class Deleter, class Alloc>
117 shared_ptr(Y* ptr, Deleter d, Alloc alloc);
118
119 constexpr shared_ptr(std::nullptr_t);
120
121 template<class Deleter>
122 shared_ptr(std::nullptr_t, Deleter d);
123
124 template<class Deleter, class Alloc>
125 shared_ptr(std::nullptr_t, Deleter d, Alloc alloc);
126
127 template<class Y>
128 shared_ptr(const shared_ptr<Y>& r, T* ptr);
129
130 shared_ptr(const shared_ptr& r);
131
132 template<class Y>
133 shared_ptr(const shared_ptr<Y>& r);
134
135 shared_ptr(shared_ptr&& r);
136
137 template<class Y>
138 shared_ptr(shared_ptr<Y>&& r);
139
140 template<class Y>
141 explicit shared_ptr(const std::weak_ptr<Y>& r);
142
143 template<class Y>
144 shared_ptr(std::auto_ptr<Y>&& r);
145
146 template<class Y, class Deleter>
147 shared_ptr(std::unique_ptr<Y, Deleter>&& r);
148
149 ~shared_ptr();
150
151 shared_ptr& operator=(const shared_ptr& r);
152
153 template<class Y>
154 shared_ptr& operator=(const shared_ptr<Y>& r);
155
156 shared_ptr& operator=(shared_ptr&& r);
157
158 template<class Y>
159 shared_ptr& operator=(shared_ptr<Y>&& r);
160
161 template<class Y>
162 shared_ptr& operator=(std::auto_ptr<Y>&& r);
163
164 template<class Y, class Deleter>
165 shared_ptr& operator=(std::unique_ptr<Y, Deleter>&& r);
166
167 void reset();
168
169 template<class Y>
170 void reset(Y* ptr);
171
172 template<class Y, class Deleter>
173 void reset(Y* ptr, Deleter d);
174
175 template<class Y, class Deleter, class Alloc>
176 void reset(Y* ptr, Deleter d, Alloc alloc);
177
178 void swap(shared_ptr& r);
179
180 T* get() const;
181 T& operator*() const;
182 T* operator->() const;
183
184 long use_count() const;
185 bool unique() const;
186
187 explicit operator bool() const;
188
189 template<class Y>
190 bool owner_before(const shared_ptr<Y>& other) const;
191
192 template<class Y>
193 bool owner_before(const std::weak_ptr<Y>& other) const;
194
195 };
196
197 template <class T, class U>
198 bool operator==(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs);
199
200 template<class T, class U>
201 bool operator!=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs);
202
203 template<class T, class U>
204 bool operator<(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs);
205
206 template<class T, class U>
207 bool operator>(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs);
208
209 template<class T, class U>
210 bool operator<=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs);
211
212 template<class T, class U>
213 bool operator>=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs);
214
215 template<class T>
216 bool operator==(const shared_ptr<T>& lhs, std::nullptr_t rhs);
217
218 template<class T>
219 bool operator==(std::nullptr_t lhs, const shared_ptr<T>& rhs);
220
221 template<class T>
222 bool operator!=(const shared_ptr<T>& lhs, std::nullptr_t rhs);
223
224 template<class T>
225 bool operator!=(std::nullptr_t lhs, const shared_ptr<T>& rhs);
226
227 template<class T>
228 bool operator<(const shared_ptr<T>& lhs, std::nullptr_t rhs);
229
230 template<class T>
231 bool operator<(std::nullptr_t lhs, const shared_ptr<T>& rhs);
232
233 template<class T>
234 bool operator>(const shared_ptr<T>& lhs, std::nullptr_t rhs);
235
236 template<class T>
237 bool operator>(std::nullptr_t lhs, const shared_ptr<T>& rhs);
238
239 template<class T>
240 bool operator<=(const shared_ptr<T>& lhs, std::nullptr_t rhs);
241
242 template<class T>
243 bool operator<=(std::nullptr_t lhs, const shared_ptr<T>& rhs);
244
245 template<class T>
246 bool operator>=(const shared_ptr<T>& lhs, std::nullptr_t rhs);
247
248 template<class T>
249 bool operator>=(std::nullptr_t lhs, const shared_ptr<T>& rhs);
250
251 template <class T, class U, class V>
252 basic_ostream<U, V>& operator<<(basic_ostream<U, V>& os, const shared_ptr<T>& ptr);
253
254 template<class T>
255 void swap(shared_ptr<T>& lhs, shared_ptr<T>& rhs);
256
257 template<class T, class... Args>
258 shared_ptr<T> make_shared(Args&& ... args);
259 template<class T, class Alloc, class... Args>
260 shared_ptr<T> allocate_shared(const Alloc& alloc, Args... args);
261
262 template<class T, class U>
263 std::shared_ptr<T> static_pointer_cast(const std::shared_ptr<U>& r);
264 template<class T, class U>
265 std::shared_ptr<T> dynamic_pointer_cast(const std::shared_ptr<U>& r);
266 template<class T, class U>
267 std::shared_ptr<T> const_pointer_cast(const std::shared_ptr<U>& r);
268
269 template<class Deleter, class T>
270 Deleter* get_deleter(const std::shared_ptr<T>& p);
271
272 template<class T>
273 class weak_ptr {
274 public:
275 typedef T element_type;
276
277 constexpr weak_ptr();
278 weak_ptr(const weak_ptr& r);
279
280 template<class Y>
281 weak_ptr(const weak_ptr<Y>& r);
282
283 template<class Y>
284 weak_ptr(const std::shared_ptr<Y>& r);
285
286 #if CPPREFERENCE_STDVER>= 2014
287 weak_ptr(weak_ptr&& r);
288
289 template<class Y>
290 weak_ptr(weak_ptr<Y>&& r);
291 #endif
292
293 ~weak_ptr();
294
295 weak_ptr& operator=(const weak_ptr& r);
296
297 template<class Y>
298 weak_ptr& operator=(const weak_ptr<Y>& r);
299
300 template<class Y>
301 weak_ptr& operator=(const shared_ptr<Y>& r);
302
303 #if CPPREFERENCE_STDVER>= 2014
304 weak_ptr& operator=(weak_ptr&& r);
305
306 template<class Y>
307 weak_ptr& operator=(weak_ptr<Y>&& r);
308 #endif
309
310 void reset();
311 void swap(weak_ptr& r);
312 long use_count() const;
313 bool expired() const;
314 std::shared_ptr<T> lock() const;
315
316 template<class Y>
317 bool owner_before(const weak_ptr<Y>& other) const;
318
319 template<class Y>
320 bool owner_before(const std::shared_ptr<Y>& other) const;
321 };
322
323 template<class T>
324 void swap(weak_ptr<T>& lhs, weak_ptr<T>& rhs);
325
326 #endif // CPPREFERENCE_STDVER>= 2011
327
328 template<class T>
329 class auto_ptr {
330 public:
331 typedef T element_type;
332
333 explicit auto_ptr(X* p = 0);
334
335 auto_ptr(auto_ptr& r);
336
337 template<class Y>
338 auto_ptr(auto_ptr<Y>& r);
339
340 template<class Y>
341 auto_ptr(auto_ptr_ref<Y> m);
342
343 ~auto_ptr();
344
345 auto_ptr& operator=(auto_ptr& r);
346
347 template<class Y>
348 auto_ptr& operator=(auto_ptr<Y>& r);
349
350 auto_ptr& operator=(auto_ptr_ref m);
351
352 template<class Y>
353 operator auto_ptr_ref<Y>();
354
355 template<class Y>
356 operator auto_ptr<Y>();
357
358 T* get() const;
359
360 T& operator*() const;
361 T* operator->() const;
362
363 void reset(T* p = 0);
364 T* release();
365 };
366
367 #if CPPREFERENCE_STDVER>= 2011
368 template<class T>
369 struct owner_less {
370 public:
371 // SIMPLIFIED: actually only specializations for shared_ptr<T> and
372 // weak_ptr<T> are defined
373 typedef bool result_type;
374 typedef void first_argument_type; // actually ether shared_ptr<T> or weak_ptr<T>
375 typedef void second_argument_type; // actually ether shared_ptr<T> or weak_ptr<T>
376
377 bool operator()(const shared_ptr<T>& lhs,
378 const shared_ptr<T>& rhs) const;
379 bool operator()(const shared_ptr<T>& lhs,
380 const weak_ptr<T>& rhs) const;
381 bool operator()(const weak_ptr<T>& lhs,
382 const shared_ptr<T>& rhs) const;
383 bool operator()(const weak_ptr<T>& lhs,
384 const weak_ptr<T>& rhs) const;
385 };
386
387 template<class T>
388 class enable_shared_from_this {
389 public:
390 constexpr enable_shared_from_this();
391 enable_shared_from_this(const enable_shared_from_this<T>& obj);
392 ~enable_shared_from_this();
393
394 enable_shared_from_this<T>& operator=(const enable_shared_from_this<T>& obj);
395
396 shared_ptr<T> shared_from_this();
397 shared_ptr<T const> shared_from_this() const;
398 };
399
400 class bad_weak_ptr : public exception {
401 public:
402 bad_weak_ptr();
403 };
404
405 #endif // CPPREFERENCE_STDVER>= 2011
406
407 template<class T>
408 struct allocator {
409 // SIMPLIFIED: allocator<void> specialization is not provided
410 typedef T value_type;
411 typedef T* pointer;
412 typedef const T* const_pointer;
413 typedef T& reference;
414 typedef const T& const_reference;
415 typedef size_t size_type;
416 typedef ptrdiff_t difference_type;
417 #if CPPREFERENCE_STDVER>= 2014
418 typedef true_type propagate_on_container_move_assignment;
419 #endif
420 template<class U> struct rebind {
421 typedef allocator<U> other;
422 };
423
424 allocator();
425 allocator(const allocator& other);
426
427 template<class U>
428 allocator(const allocator<U>& other);
429
430 ~allocator();
431
432 pointer address(reference x) const;
433 const_pointer address(const_reference x) const;
434
435 pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0);
436 void deallocate(pointer p, size_type n);
437 size_type max_size() const;
438
439 void construct(pointer p, const_reference val);
440
441 template<class U, class... Args>
442 void construct(U* p, Args&& ... args);
443
444 void destroy(pointer p);
445
446 template<class U>
447 void destroy(U* p);
448 };
449
450 template<class T1, class T2>
451 bool operator==(const allocator<T1>& lhs, const allocator<T2>& rhs);
452
453 template<class T1, class T2>
454 bool operator!=(const allocator<T1>& lhs, const allocator<T2>& rhs);
455
456
457 #if CPPREFERENCE_STDVER>= 2011
458 template<class Alloc>
459 struct allocator_traits {
460 // SIMPLIFIED: actual typedefs select between Alloc types and if they
461 // are not provided, fallback to pointer traits
462 typedef Alloc allocator_type;
463 typedef typename Alloc::value_type value_type;
464 typedef value_type* pointer;
465 typedef const value_type* const_pointer;
466 typedef nullptr_t void_pointer;
467 typedef const nullptr_t const_void_pointer;
468 typedef ptrdiff_t difference_type;
469 typedef size_t size_type;
470 typedef false_type propagate_on_container_copy_assignment;
471 typedef false_type propagate_on_container_move_assignment;
472 typedef false_type propagate_on_container_swap;
473
474 // SIMPLIFIED: actually is alias template and uses Alloc::rebind
475 template<class T>
476 class rebind_alloc<T> : public Alloc {};
477 // SIMPLIFIED: actually is alias template
478 template<class T>
479 class rebind_traits<T> : public allocator_traits<rebind_alloc<T>> {};
480
481 static pointer allocate(Alloc& a, size_type n);
482 static pointer allocate(Alloc& a, size_type n, const_void_pointer hint);
483 static void deallocate(Alloc& a, pointer p, size_type n);
484
485 template<class T, class... Args>
486 static void construct(Alloc& a, T* p, Args&& ... args);
487 template<class T>
488 static void destroy(Alloc& a, T* p);
489
490 static size_type max_size(const Alloc& a);
491
492 static Alloc select_on_container_copy_construction(const Alloc& a);
493 };
494
495 struct allocator_arg_t {};
496 constexpr std::allocator_arg_t allocator_arg;
497
498 template<class T, class Alloc>
499 struct uses_allocator : public integral_constant<bool, false> {};
500
501 enum class pointer_safety {
502 relaxed,
503 preferred,
504 strict
505 };
506
507 // SIMPLIFIED: provides pointer specialization
508 template<class Ptr>
509 struct pointer_traits {
510 typedef Ptr pointer;
511 typedef void element_type; // SIMPLIFIED
512 typedef ptrdiff_t difference_type;
513
514 template<class U>
515 using rebind = U* ;
516
517 static pointer pointer_to(element_type& r);
518 };
519
520 #endif // CPPREFERENCE_STDVER>= 2011
521
522 template<class InputIt, class ForwardIt>
523 ForwardIt uninitialized_copy(InputIt first, InputIt last, ForwardIt d_first);
524
525 template<class ForwardIt, class T>
526 void uninitialized_fill(ForwardIt first, ForwardIt last, const T& value);
527
528
529 #if CPPREFERENCE_STDVER <2011
530 template<class ForwardIt, class Size, class T>
531 void uninitialized_fill_n(ForwardIt first, Size count, const T& value);
532
533 #else
534
535 template<class ForwardIt, class Size, class T>
536 ForwardIt uninitialized_fill_n(ForwardIt first, Size count, const T& value);
537
538 template<class InputIt, class Size, class ForwardIt>
539 ForwardIt uninitialized_copy_n(InputIt first, Size count, ForwardIt d_first);
540
541 #endif // CPPREFERENCE_STDVER <2011
542
543 template<class OutputIt, class T>
544 class raw_storage_iterator {
545 public:
546 typedef void value_type;
547 typedef void difference_type;
548 typedef void pointer;
549 typedef void reference;
550 typedef output_iterator_tag iterator_category;
551
552 explicit raw_storage_iterator(OutputIt it);
553 raw_storage_iterator& operator=(const T& el);
554 raw_storage_iterator& operator*();
555 raw_storage_iterator& operator++();
556 raw_storage_iterator operator++(int);
557 #if CPPREFERENCE_STDVER>= 2017
558 OutputIt base() const;
559 #endif
560 };
561
562 template<class T>
563 std::pair<T*, std::ptrdiff_t> get_temporary_buffer(std::ptrdiff_t count);
564 template<class T>
565 void return_temporary_buffer(T* p);
566
567 #if CPPREFERENCE_STDVER>= 2011
568 void declare_reachable(void* p);
569 template<class T>
570 T* undeclare_reachable(T* p);
571 void declare_no_pointers(char* p, std::size_t n);
572 void undeclare_no_pointers(char* p, std::size_t n);
573 std::pointer_safety get_pointer_safety();
574
575 template<class T>
576 T* addressof(T& arg);
577
578 void* align(std::size_t alignment,
579 std::size_t size,
580 void*& ptr,
581 std::size_t& space);
582
583 #endif // CPPREFERENCE_STDVER>= 2011
584
585 } // namespace std
586
587 #endif // CPPREFERENCE_MEMORY_H