1 // Custom pointer adapter and sample storage policies
3 // Copyright (C) 2008-2021 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This file is a GNU extension to the Standard C++ Library.
31 * Provides reusable _Pointer_adapter for assisting in the development of
32 * custom pointer types that can be used with the standard containers via
33 * the allocator::pointer and allocator::const_pointer typedefs.
39 #pragma GCC system_header
42 #include <bits/stl_iterator_base_types.h>
44 #include <ext/type_traits.h>
45 #if __cplusplus >= 201103L
46 # include <bits/move.h>
47 # include <bits/ptr_traits.h>
49 #if __cplusplus > 201703L
50 # include <iterator> // for indirectly_readable_traits
53 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 * @brief A storage policy for use with _Pointer_adapter<> which yields a
61 * A _Storage_policy is required to provide 4 things:
62 * 1) A get() API for returning the stored pointer value.
63 * 2) An set() API for storing a pointer value.
64 * 3) An element_type typedef to define the type this points to.
65 * 4) An operator<() to support pointer comparison.
66 * 5) An operator==() to support pointer comparison.
68 template<typename _Tp
>
69 class _Std_pointer_impl
72 // the type this pointer points to.
73 typedef _Tp element_type
;
75 // A method to fetch the pointer value as a standard T* value;
80 // A method to set the pointer value, from a standard T* value;
82 set(element_type
* __arg
)
85 // Comparison of pointers
87 operator<(const _Std_pointer_impl
& __rarg
) const
88 { return (_M_value
< __rarg
._M_value
); }
91 operator==(const _Std_pointer_impl
& __rarg
) const
92 { return (_M_value
== __rarg
._M_value
); }
95 element_type
* _M_value
;
99 * @brief A storage policy for use with _Pointer_adapter<> which stores
100 * the pointer's address as an offset value which is relative to
103 * This is intended for pointers within shared memory regions which
104 * might be mapped at different addresses by different processes.
105 * For null pointers, a value of 1 is used. (0 is legitimate
106 * sometimes for nodes in circularly linked lists) This value was
107 * chosen as the least likely to generate an incorrect null, As
108 * there is no reason why any normal pointer would point 1 byte into
109 * its own pointer address.
111 template<typename _Tp
>
112 class _Relative_pointer_impl
115 typedef _Tp element_type
;
123 return reinterpret_cast<_Tp
*>(reinterpret_cast<_UIntPtrType
>(this)
133 _M_diff
= reinterpret_cast<_UIntPtrType
>(__arg
)
134 - reinterpret_cast<_UIntPtrType
>(this);
137 // Comparison of pointers
139 operator<(const _Relative_pointer_impl
& __rarg
) const
140 { return (reinterpret_cast<_UIntPtrType
>(this->get())
141 < reinterpret_cast<_UIntPtrType
>(__rarg
.get())); }
144 operator==(const _Relative_pointer_impl
& __rarg
) const
145 { return (reinterpret_cast<_UIntPtrType
>(this->get())
146 == reinterpret_cast<_UIntPtrType
>(__rarg
.get())); }
149 #ifdef _GLIBCXX_USE_LONG_LONG
150 typedef __gnu_cxx::__conditional_type
<
151 (sizeof(unsigned long) >= sizeof(void*)),
152 unsigned long, unsigned long long>::__type _UIntPtrType
;
154 typedef unsigned long _UIntPtrType
;
156 _UIntPtrType _M_diff
;
160 * Relative_pointer_impl needs a specialization for const T because of
161 * the casting done during pointer arithmetic.
163 template<typename _Tp
>
164 class _Relative_pointer_impl
<const _Tp
>
167 typedef const _Tp element_type
;
175 return reinterpret_cast<const _Tp
*>
176 (reinterpret_cast<_UIntPtrType
>(this) + _M_diff
);
180 set(const _Tp
* __arg
)
185 _M_diff
= reinterpret_cast<_UIntPtrType
>(__arg
)
186 - reinterpret_cast<_UIntPtrType
>(this);
189 // Comparison of pointers
191 operator<(const _Relative_pointer_impl
& __rarg
) const
192 { return (reinterpret_cast<_UIntPtrType
>(this->get())
193 < reinterpret_cast<_UIntPtrType
>(__rarg
.get())); }
196 operator==(const _Relative_pointer_impl
& __rarg
) const
197 { return (reinterpret_cast<_UIntPtrType
>(this->get())
198 == reinterpret_cast<_UIntPtrType
>(__rarg
.get())); }
201 #ifdef _GLIBCXX_USE_LONG_LONG
202 typedef __gnu_cxx::__conditional_type
<
203 (sizeof(unsigned long) >= sizeof(void*)),
204 unsigned long, unsigned long long>::__type _UIntPtrType
;
206 typedef unsigned long _UIntPtrType
;
208 _UIntPtrType _M_diff
;
212 * The specialization on this type helps resolve the problem of
213 * reference to void, and eliminates the need to specialize
214 * _Pointer_adapter for cases of void*, const void*, and so on.
216 struct _Invalid_type
{ };
218 template<typename _Tp
>
219 struct _Reference_type
220 { typedef _Tp
& reference
; };
223 struct _Reference_type
<void>
224 { typedef _Invalid_type
& reference
; };
227 struct _Reference_type
<const void>
228 { typedef const _Invalid_type
& reference
; };
231 struct _Reference_type
<volatile void>
232 { typedef volatile _Invalid_type
& reference
; };
235 struct _Reference_type
<volatile const void>
236 { typedef const volatile _Invalid_type
& reference
; };
239 * This structure accommodates the way in which
240 * std::iterator_traits<> is normally specialized for const T*, so
241 * that value_type is still T.
243 template<typename _Tp
>
244 struct _Unqualified_type
245 { typedef _Tp type
; };
247 template<typename _Tp
>
248 struct _Unqualified_type
<const _Tp
>
249 { typedef _Tp type
; };
252 * The following provides an 'alternative pointer' that works with
253 * the containers when specified as the pointer typedef of the
256 * The pointer type used with the containers doesn't have to be this
257 * class, but it must support the implicit conversions, pointer
258 * arithmetic, comparison operators, etc. that are supported by this
259 * class, and avoid raising compile-time ambiguities. Because
260 * creating a working pointer can be challenging, this pointer
261 * template was designed to wrapper an easier storage policy type,
262 * so that it becomes reusable for creating other pointer types.
264 * A key point of this class is also that it allows container
265 * writers to 'assume' Allocator::pointer is a typedef for a normal
266 * pointer. This class supports most of the conventions of a true
267 * pointer, and can, for instance handle implicit conversion to
268 * const and base class pointer types. The only impositions on
269 * container writers to support extended pointers are: 1) use the
270 * Allocator::pointer typedef appropriately for pointer types. 2)
271 * if you need pointer casting, use the __pointer_cast<> functions
272 * from ext/cast.h. This allows pointer cast operations to be
273 * overloaded as necessary by custom pointers.
275 * Note: The const qualifier works with this pointer adapter as
278 * _Tp* == _Pointer_adapter<_Std_pointer_impl<_Tp> >;
279 * const _Tp* == _Pointer_adapter<_Std_pointer_impl<const _Tp> >;
280 * _Tp* const == const _Pointer_adapter<_Std_pointer_impl<_Tp> >;
281 * const _Tp* const == const _Pointer_adapter<_Std_pointer_impl<const _Tp> >;
283 template<typename _Storage_policy
>
284 class _Pointer_adapter
: public _Storage_policy
287 typedef typename
_Storage_policy::element_type element_type
;
289 // These are needed for iterator_traits
290 typedef std::random_access_iterator_tag iterator_category
;
291 typedef typename _Unqualified_type
<element_type
>::type value_type
;
292 typedef std::ptrdiff_t difference_type
;
293 typedef _Pointer_adapter pointer
;
294 typedef typename _Reference_type
<element_type
>::reference reference
;
296 // Reminder: 'const' methods mean that the method is valid when the
297 // pointer is immutable, and has nothing to do with whether the
298 // 'pointee' is const.
300 // Default Constructor (Convert from element_type*)
301 _Pointer_adapter(element_type
* __arg
= 0)
302 { _Storage_policy::set(__arg
); }
304 // Copy constructor from _Pointer_adapter of same type.
305 _Pointer_adapter(const _Pointer_adapter
& __arg
)
306 { _Storage_policy::set(__arg
.get()); }
308 // Convert from _Up* if conversion to element_type* is valid.
309 template<typename _Up
>
310 _Pointer_adapter(_Up
* __arg
)
311 { _Storage_policy::set(__arg
); }
313 // Conversion from another _Pointer_adapter if _Up if static cast is
315 template<typename _Up
>
316 _Pointer_adapter(const _Pointer_adapter
<_Up
>& __arg
)
317 { _Storage_policy::set(__arg
.get()); }
320 ~_Pointer_adapter() { }
322 // Assignment operator
324 operator=(const _Pointer_adapter
& __arg
)
326 _Storage_policy::set(__arg
.get());
330 template<typename _Up
>
332 operator=(const _Pointer_adapter
<_Up
>& __arg
)
334 _Storage_policy::set(__arg
.get());
338 template<typename _Up
>
340 operator=(_Up
* __arg
)
342 _Storage_policy::set(__arg
);
346 // Operator*, returns element_type&
349 { return *(_Storage_policy::get()); }
351 // Operator->, returns element_type*
354 { return _Storage_policy::get(); }
356 // Operator[], returns a element_type& to the item at that loc.
358 operator[](std::ptrdiff_t __index
) const
359 { return _Storage_policy::get()[__index
]; }
361 // To allow implicit conversion to "bool", for "if (ptr)..."
362 #if __cplusplus >= 201103L
363 explicit operator bool() const { return _Storage_policy::get() != 0; }
366 typedef element_type
*(_Pointer_adapter::*__unspecified_bool_type
)() const;
369 operator __unspecified_bool_type() const
371 return _Storage_policy::get() == 0 ? 0 :
372 &_Pointer_adapter::operator->;
375 // ! operator (for: if (!ptr)...)
378 { return (_Storage_policy::get() == 0); }
381 // Pointer differences
382 inline friend std::ptrdiff_t
383 operator-(const _Pointer_adapter
& __lhs
, element_type
* __rhs
)
384 { return (__lhs
.get() - __rhs
); }
386 inline friend std::ptrdiff_t
387 operator-(element_type
* __lhs
, const _Pointer_adapter
& __rhs
)
388 { return (__lhs
- __rhs
.get()); }
390 template<typename _Up
>
391 inline friend std::ptrdiff_t
392 operator-(const _Pointer_adapter
& __lhs
, _Up
* __rhs
)
393 { return (__lhs
.get() - __rhs
); }
395 template<typename _Up
>
396 inline friend std::ptrdiff_t
397 operator-(_Up
* __lhs
, const _Pointer_adapter
& __rhs
)
398 { return (__lhs
- __rhs
.get()); }
400 template<typename _Up
>
401 inline std::ptrdiff_t
402 operator-(const _Pointer_adapter
<_Up
>& __rhs
) const
403 { return (_Storage_policy::get() - __rhs
.get()); }
406 // Note: There is a reason for all this overloading based on different
407 // integer types. In some libstdc++-v3 test cases, a templated
408 // operator+ is declared which can match any types. This operator
409 // tends to "steal" the recognition of _Pointer_adapter's own operator+
410 // unless the integer type matches perfectly.
412 #define _CXX_POINTER_ARITH_OPERATOR_SET(INT_TYPE) \
413 inline friend _Pointer_adapter \
414 operator+(const _Pointer_adapter& __lhs, INT_TYPE __offset) \
415 { return _Pointer_adapter(__lhs.get() + __offset); } \
417 inline friend _Pointer_adapter \
418 operator+(INT_TYPE __offset, const _Pointer_adapter& __rhs) \
419 { return _Pointer_adapter(__rhs.get() + __offset); } \
421 inline friend _Pointer_adapter \
422 operator-(const _Pointer_adapter& __lhs, INT_TYPE __offset) \
423 { return _Pointer_adapter(__lhs.get() - __offset); } \
425 inline _Pointer_adapter& \
426 operator+=(INT_TYPE __offset) \
428 _Storage_policy::set(_Storage_policy::get() + __offset); \
432 inline _Pointer_adapter& \
433 operator-=(INT_TYPE __offset) \
435 _Storage_policy::set(_Storage_policy::get() - __offset); \
438 // END of _CXX_POINTER_ARITH_OPERATOR_SET macro
440 // Expand into the various pointer arithmetic operators needed.
441 _CXX_POINTER_ARITH_OPERATOR_SET(short);
442 _CXX_POINTER_ARITH_OPERATOR_SET(unsigned short);
443 _CXX_POINTER_ARITH_OPERATOR_SET(int);
444 _CXX_POINTER_ARITH_OPERATOR_SET(unsigned int);
445 _CXX_POINTER_ARITH_OPERATOR_SET(long);
446 _CXX_POINTER_ARITH_OPERATOR_SET(unsigned long);
447 #ifdef _GLIBCXX_USE_LONG_LONG
448 _CXX_POINTER_ARITH_OPERATOR_SET(long long);
449 _CXX_POINTER_ARITH_OPERATOR_SET(unsigned long long);
452 // Mathematical Manipulators
453 inline _Pointer_adapter
&
456 _Storage_policy::set(_Storage_policy::get() + 1);
460 inline _Pointer_adapter
463 _Pointer_adapter
__tmp(*this);
464 _Storage_policy::set(_Storage_policy::get() + 1);
468 inline _Pointer_adapter
&
471 _Storage_policy::set(_Storage_policy::get() - 1);
475 inline _Pointer_adapter
478 _Pointer_adapter
__tmp(*this);
479 _Storage_policy::set(_Storage_policy::get() - 1);
483 #if __cpp_lib_three_way_comparison
484 friend std::strong_ordering
485 operator<=>(const _Pointer_adapter
& __lhs
, const _Pointer_adapter
& __rhs
)
487 { return __lhs
.get() <=> __rhs
.get(); }
489 }; // class _Pointer_adapter
492 #define _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(OPERATOR) \
493 template<typename _Tp1, typename _Tp2> \
495 operator OPERATOR(const _Pointer_adapter<_Tp1>& __lhs, _Tp2 __rhs) \
496 { return __lhs.get() OPERATOR __rhs; } \
498 template<typename _Tp1, typename _Tp2> \
500 operator OPERATOR(_Tp1 __lhs, const _Pointer_adapter<_Tp2>& __rhs) \
501 { return __lhs OPERATOR __rhs.get(); } \
503 template<typename _Tp1, typename _Tp2> \
505 operator OPERATOR(const _Pointer_adapter<_Tp1>& __lhs, \
506 const _Pointer_adapter<_Tp2>& __rhs) \
507 { return __lhs.get() OPERATOR __rhs.get(); } \
509 // End GCC_CXX_POINTER_COMPARISON_OPERATION_SET Macro
511 // Expand into the various comparison operators needed.
512 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(==)
513 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(!=)
514 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(<)
515 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(<=)
516 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(>)
517 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(>=)
519 // These are here for expressions like "ptr == 0", "ptr != 0"
520 template<typename _Tp
>
522 operator==(const _Pointer_adapter
<_Tp
>& __lhs
, int __rhs
)
523 { return __lhs
.get() == reinterpret_cast<void*>(__rhs
); }
525 template<typename _Tp
>
527 operator==(int __lhs
, const _Pointer_adapter
<_Tp
>& __rhs
)
528 { return __rhs
.get() == reinterpret_cast<void*>(__lhs
); }
530 template<typename _Tp
>
532 operator!=(const _Pointer_adapter
<_Tp
>& __lhs
, int __rhs
)
533 { return __lhs
.get() != reinterpret_cast<void*>(__rhs
); }
535 template<typename _Tp
>
537 operator!=(int __lhs
, const _Pointer_adapter
<_Tp
>& __rhs
)
538 { return __rhs
.get() != reinterpret_cast<void*>(__lhs
); }
541 * Comparison operators for _Pointer_adapter defer to the base class'
542 * comparison operators, when possible.
544 template<typename _Tp
>
546 operator==(const _Pointer_adapter
<_Tp
>& __lhs
,
547 const _Pointer_adapter
<_Tp
>& __rhs
)
548 { return __lhs
._Tp::operator==(__rhs
); }
550 template<typename _Tp
>
552 operator<=(const _Pointer_adapter
<_Tp
>& __lhs
,
553 const _Pointer_adapter
<_Tp
>& __rhs
)
554 { return __lhs
._Tp::operator<(__rhs
) || __lhs
._Tp::operator==(__rhs
); }
556 template<typename _Tp
>
558 operator!=(const _Pointer_adapter
<_Tp
>& __lhs
,
559 const _Pointer_adapter
<_Tp
>& __rhs
)
560 { return !(__lhs
._Tp::operator==(__rhs
)); }
562 template<typename _Tp
>
564 operator>(const _Pointer_adapter
<_Tp
>& __lhs
,
565 const _Pointer_adapter
<_Tp
>& __rhs
)
566 { return !(__lhs
._Tp::operator<(__rhs
) || __lhs
._Tp::operator==(__rhs
)); }
568 template<typename _Tp
>
570 operator>=(const _Pointer_adapter
<_Tp
>& __lhs
,
571 const _Pointer_adapter
<_Tp
>& __rhs
)
572 { return !(__lhs
._Tp::operator<(__rhs
)); }
574 template<typename _CharT
, typename _Traits
, typename _StoreT
>
575 inline std::basic_ostream
<_CharT
, _Traits
>&
576 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
577 const _Pointer_adapter
<_StoreT
>& __p
)
578 { return (__os
<< __p
.get()); }
580 _GLIBCXX_END_NAMESPACE_VERSION
583 #if __cplusplus >= 201103L
584 namespace std
_GLIBCXX_VISIBILITY(default)
586 _GLIBCXX_BEGIN_NAMESPACE_VERSION
588 template<typename _Storage_policy
>
589 struct pointer_traits
<__gnu_cxx::_Pointer_adapter
<_Storage_policy
>>
592 typedef __gnu_cxx::_Pointer_adapter
<_Storage_policy
> pointer
;
593 /// The type pointed to
594 typedef typename
pointer::element_type element_type
;
595 /// Type used to represent the difference between two pointers
596 typedef typename
pointer::difference_type difference_type
;
598 template<typename _Up
>
599 using rebind
= typename
__gnu_cxx::_Pointer_adapter
<
600 typename pointer_traits
<_Storage_policy
>::template rebind
<_Up
>>;
602 static pointer
pointer_to(typename
pointer::reference __r
) noexcept
603 { return pointer(std::addressof(__r
)); }
606 #if __cpp_lib_concepts
607 template<typename _Policy
>
608 struct indirectly_readable_traits
<__gnu_cxx::_Pointer_adapter
<_Policy
>>
611 = typename
__gnu_cxx::_Pointer_adapter
<_Policy
>::value_type
;
614 _GLIBCXX_END_NAMESPACE_VERSION