1 // Custom pointer adapter and sample storage policies
3 // Copyright (C) 2008, 2009, 2010 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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
46 # include <bits/ptr_traits.h>
49 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 * @brief A storage policy for use with _Pointer_adapter<> which yields a
57 * A _Storage_policy is required to provide 4 things:
58 * 1) A get() API for returning the stored pointer value.
59 * 2) An set() API for storing a pointer value.
60 * 3) An element_type typedef to define the type this points to.
61 * 4) An operator<() to support pointer comparison.
62 * 5) An operator==() to support pointer comparison.
64 template<typename _Tp>
65 class _Std_pointer_impl
68 // the type this pointer points to.
69 typedef _Tp element_type;
71 // A method to fetch the pointer value as a standard T* value;
76 // A method to set the pointer value, from a standard T* value;
78 set(element_type* __arg)
81 // Comparison of pointers
83 operator<(const _Std_pointer_impl& __rarg) const
84 { return (_M_value < __rarg._M_value); }
87 operator==(const _Std_pointer_impl& __rarg) const
88 { return (_M_value == __rarg._M_value); }
91 element_type* _M_value;
95 * @brief A storage policy for use with _Pointer_adapter<> which stores
96 * the pointer's address as an offset value which is relative to
99 * This is intended for pointers within shared memory regions which
100 * might be mapped at different addresses by different processes.
101 * For null pointers, a value of 1 is used. (0 is legitimate
102 * sometimes for nodes in circularly linked lists) This value was
103 * chosen as the least likely to generate an incorrect null, As
104 * there is no reason why any normal pointer would point 1 byte into
105 * its own pointer address.
107 template<typename _Tp>
108 class _Relative_pointer_impl
111 typedef _Tp element_type;
119 return reinterpret_cast<_Tp*>(reinterpret_cast<_UIntPtrType>(this)
129 _M_diff = reinterpret_cast<_UIntPtrType>(__arg)
130 - reinterpret_cast<_UIntPtrType>(this);
133 // Comparison of pointers
135 operator<(const _Relative_pointer_impl& __rarg) const
136 { return (reinterpret_cast<_UIntPtrType>(this->get())
137 < reinterpret_cast<_UIntPtrType>(__rarg.get())); }
140 operator==(const _Relative_pointer_impl& __rarg) const
141 { return (reinterpret_cast<_UIntPtrType>(this->get())
142 == reinterpret_cast<_UIntPtrType>(__rarg.get())); }
145 #ifdef _GLIBCXX_USE_LONG_LONG
146 typedef __gnu_cxx::__conditional_type<
147 (sizeof(unsigned long) >= sizeof(void*)),
148 unsigned long, unsigned long long>::__type _UIntPtrType;
150 typedef unsigned long _UIntPtrType;
152 _UIntPtrType _M_diff;
156 * Relative_pointer_impl needs a specialization for const T because of
157 * the casting done during pointer arithmetic.
159 template<typename _Tp>
160 class _Relative_pointer_impl<const _Tp>
163 typedef const _Tp element_type;
171 return reinterpret_cast<const _Tp*>
172 (reinterpret_cast<_UIntPtrType>(this) + _M_diff);
176 set(const _Tp* __arg)
181 _M_diff = reinterpret_cast<_UIntPtrType>(__arg)
182 - reinterpret_cast<_UIntPtrType>(this);
185 // Comparison of pointers
187 operator<(const _Relative_pointer_impl& __rarg) const
188 { return (reinterpret_cast<_UIntPtrType>(this->get())
189 < reinterpret_cast<_UIntPtrType>(__rarg.get())); }
192 operator==(const _Relative_pointer_impl& __rarg) const
193 { return (reinterpret_cast<_UIntPtrType>(this->get())
194 == reinterpret_cast<_UIntPtrType>(__rarg.get())); }
197 #ifdef _GLIBCXX_USE_LONG_LONG
198 typedef __gnu_cxx::__conditional_type<
199 (sizeof(unsigned long) >= sizeof(void*)),
200 unsigned long, unsigned long long>::__type _UIntPtrType;
202 typedef unsigned long _UIntPtrType;
204 _UIntPtrType _M_diff;
208 * The specialization on this type helps resolve the problem of
209 * reference to void, and eliminates the need to specialize
210 * _Pointer_adapter for cases of void*, const void*, and so on.
212 struct _Invalid_type { };
214 template<typename _Tp>
215 struct _Reference_type
216 { typedef _Tp& reference; };
219 struct _Reference_type<void>
220 { typedef _Invalid_type& reference; };
223 struct _Reference_type<const void>
224 { typedef const _Invalid_type& reference; };
227 struct _Reference_type<volatile void>
228 { typedef volatile _Invalid_type& reference; };
231 struct _Reference_type<volatile const void>
232 { typedef const volatile _Invalid_type& reference; };
235 * This structure accommodates the way in which
236 * std::iterator_traits<> is normally specialized for const T*, so
237 * that value_type is still T.
239 template<typename _Tp>
240 struct _Unqualified_type
241 { typedef _Tp type; };
243 template<typename _Tp>
244 struct _Unqualified_type<const _Tp>
245 { typedef _Tp type; };
248 * The following provides an 'alternative pointer' that works with
249 * the containers when specified as the pointer typedef of the
252 * The pointer type used with the containers doesn't have to be this
253 * class, but it must support the implicit conversions, pointer
254 * arithmetic, comparison operators, etc. that are supported by this
255 * class, and avoid raising compile-time ambiguities. Because
256 * creating a working pointer can be challenging, this pointer
257 * template was designed to wrapper an easier storage policy type,
258 * so that it becomes reusable for creating other pointer types.
260 * A key point of this class is also that it allows container
261 * writers to 'assume' Allocator::pointer is a typedef for a normal
262 * pointer. This class supports most of the conventions of a true
263 * pointer, and can, for instance handle implicit conversion to
264 * const and base class pointer types. The only impositions on
265 * container writers to support extended pointers are: 1) use the
266 * Allocator::pointer typedef appropriately for pointer types. 2)
267 * if you need pointer casting, use the __pointer_cast<> functions
268 * from ext/cast.h. This allows pointer cast operations to be
269 * overloaded as necessary by custom pointers.
271 * Note: The const qualifier works with this pointer adapter as
274 * _Tp* == _Pointer_adapter<_Std_pointer_impl<_Tp> >;
275 * const _Tp* == _Pointer_adapter<_Std_pointer_impl<const _Tp> >;
276 * _Tp* const == const _Pointer_adapter<_Std_pointer_impl<_Tp> >;
277 * const _Tp* const == const _Pointer_adapter<_Std_pointer_impl<const _Tp> >;
279 template<typename _Storage_policy>
280 class _Pointer_adapter : public _Storage_policy
283 typedef typename _Storage_policy::element_type element_type;
285 // These are needed for iterator_traits
286 typedef std::random_access_iterator_tag iterator_category;
287 typedef typename _Unqualified_type<element_type>::type value_type;
288 typedef std::ptrdiff_t difference_type;
289 typedef _Pointer_adapter pointer;
290 typedef typename _Reference_type<element_type>::reference reference;
292 // Reminder: 'const' methods mean that the method is valid when the
293 // pointer is immutable, and has nothing to do with whether the
294 // 'pointee' is const.
296 // Default Constructor (Convert from element_type*)
297 _Pointer_adapter(element_type* __arg = 0)
298 { _Storage_policy::set(__arg); }
300 // Copy constructor from _Pointer_adapter of same type.
301 _Pointer_adapter(const _Pointer_adapter& __arg)
302 { _Storage_policy::set(__arg.get()); }
304 // Convert from _Up* if conversion to element_type* is valid.
305 template<typename _Up>
306 _Pointer_adapter(_Up* __arg)
307 { _Storage_policy::set(__arg); }
309 // Conversion from another _Pointer_adapter if _Up if static cast is
311 template<typename _Up>
312 _Pointer_adapter(const _Pointer_adapter<_Up>& __arg)
313 { _Storage_policy::set(__arg.get()); }
316 ~_Pointer_adapter() { }
318 // Assignment operator
320 operator=(const _Pointer_adapter& __arg)
322 _Storage_policy::set(__arg.get());
326 template<typename _Up>
328 operator=(const _Pointer_adapter<_Up>& __arg)
330 _Storage_policy::set(__arg.get());
334 template<typename _Up>
336 operator=(_Up* __arg)
338 _Storage_policy::set(__arg);
342 // Operator*, returns element_type&
345 { return *(_Storage_policy::get()); }
347 // Operator->, returns element_type*
350 { return _Storage_policy::get(); }
352 // Operator[], returns a element_type& to the item at that loc.
354 operator[](std::ptrdiff_t __index) const
355 { return _Storage_policy::get()[__index]; }
357 // To allow implicit conversion to "bool", for "if (ptr)..."
359 typedef element_type*(_Pointer_adapter::*__unspecified_bool_type)() const;
362 operator __unspecified_bool_type() const
364 return _Storage_policy::get() == 0 ? 0 :
365 &_Pointer_adapter::operator->;
368 // ! operator (for: if (!ptr)...)
371 { return (_Storage_policy::get() == 0); }
373 // Pointer differences
374 inline friend std::ptrdiff_t
375 operator-(const _Pointer_adapter& __lhs, element_type* __rhs)
376 { return (__lhs.get() - __rhs); }
378 inline friend std::ptrdiff_t
379 operator-(element_type* __lhs, const _Pointer_adapter& __rhs)
380 { return (__lhs - __rhs.get()); }
382 template<typename _Up>
383 inline friend std::ptrdiff_t
384 operator-(const _Pointer_adapter& __lhs, _Up* __rhs)
385 { return (__lhs.get() - __rhs); }
387 template<typename _Up>
388 inline friend std::ptrdiff_t
389 operator-(_Up* __lhs, const _Pointer_adapter& __rhs)
390 { return (__lhs - __rhs.get()); }
392 template<typename _Up>
393 inline std::ptrdiff_t
394 operator-(const _Pointer_adapter<_Up>& __rhs) const
395 { return (_Storage_policy::get() - __rhs.get()); }
398 // Note: There is a reason for all this overloading based on different
399 // integer types. In some libstdc++-v3 test cases, a templated
400 // operator+ is declared which can match any types. This operator
401 // tends to "steal" the recognition of _Pointer_adapter's own operator+
402 // unless the integer type matches perfectly.
404 #define _CXX_POINTER_ARITH_OPERATOR_SET(INT_TYPE) \
405 inline friend _Pointer_adapter \
406 operator+(const _Pointer_adapter& __lhs, INT_TYPE __offset) \
407 { return _Pointer_adapter(__lhs.get() + __offset); } \
409 inline friend _Pointer_adapter \
410 operator+(INT_TYPE __offset, const _Pointer_adapter& __rhs) \
411 { return _Pointer_adapter(__rhs.get() + __offset); } \
413 inline friend _Pointer_adapter \
414 operator-(const _Pointer_adapter& __lhs, INT_TYPE __offset) \
415 { return _Pointer_adapter(__lhs.get() - __offset); } \
417 inline _Pointer_adapter& \
418 operator+=(INT_TYPE __offset) \
420 _Storage_policy::set(_Storage_policy::get() + __offset); \
424 inline _Pointer_adapter& \
425 operator-=(INT_TYPE __offset) \
427 _Storage_policy::set(_Storage_policy::get() - __offset); \
430 // END of _CXX_POINTER_ARITH_OPERATOR_SET macro
432 // Expand into the various pointer arithmetic operators needed.
433 _CXX_POINTER_ARITH_OPERATOR_SET(short);
434 _CXX_POINTER_ARITH_OPERATOR_SET(unsigned short);
435 _CXX_POINTER_ARITH_OPERATOR_SET(int);
436 _CXX_POINTER_ARITH_OPERATOR_SET(unsigned int);
437 _CXX_POINTER_ARITH_OPERATOR_SET(long);
438 _CXX_POINTER_ARITH_OPERATOR_SET(unsigned long);
440 // Mathematical Manipulators
441 inline _Pointer_adapter&
444 _Storage_policy::set(_Storage_policy::get() + 1);
448 inline _Pointer_adapter
451 _Pointer_adapter tmp(*this);
452 _Storage_policy::set(_Storage_policy::get() + 1);
456 inline _Pointer_adapter&
459 _Storage_policy::set(_Storage_policy::get() - 1);
463 inline _Pointer_adapter
466 _Pointer_adapter tmp(*this);
467 _Storage_policy::set(_Storage_policy::get() - 1);
471 }; // class _Pointer_adapter
474 #define _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(OPERATOR) \
475 template<typename _Tp1, typename _Tp2> \
477 operator OPERATOR(const _Pointer_adapter<_Tp1>& __lhs, _Tp2 __rhs) \
478 { return __lhs.get() OPERATOR __rhs; } \
480 template<typename _Tp1, typename _Tp2> \
482 operator OPERATOR(_Tp1 __lhs, const _Pointer_adapter<_Tp2>& __rhs) \
483 { return __lhs OPERATOR __rhs.get(); } \
485 template<typename _Tp1, typename _Tp2> \
487 operator OPERATOR(const _Pointer_adapter<_Tp1>& __lhs, \
488 const _Pointer_adapter<_Tp2>& __rhs) \
489 { return __lhs.get() OPERATOR __rhs.get(); } \
491 // End GCC_CXX_POINTER_COMPARISON_OPERATION_SET Macro
493 // Expand into the various comparison operators needed.
494 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(==)
495 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(!=)
496 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(<)
497 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(<=)
498 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(>)
499 _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(>=)
501 // These are here for expressions like "ptr == 0", "ptr != 0"
502 template<typename _Tp>
504 operator==(const _Pointer_adapter<_Tp>& __lhs, int __rhs)
505 { return __lhs.get() == reinterpret_cast<void*>(__rhs); }
507 template<typename _Tp>
509 operator==(int __lhs, const _Pointer_adapter<_Tp>& __rhs)
510 { return __rhs.get() == reinterpret_cast<void*>(__lhs); }
512 template<typename _Tp>
514 operator!=(const _Pointer_adapter<_Tp>& __lhs, int __rhs)
515 { return __lhs.get() != reinterpret_cast<void*>(__rhs); }
517 template<typename _Tp>
519 operator!=(int __lhs, const _Pointer_adapter<_Tp>& __rhs)
520 { return __rhs.get() != reinterpret_cast<void*>(__lhs); }
523 * Comparison operators for _Pointer_adapter defer to the base class'
524 * comparison operators, when possible.
526 template<typename _Tp>
528 operator==(const _Pointer_adapter<_Tp>& __lhs,
529 const _Pointer_adapter<_Tp>& __rhs)
530 { return __lhs._Tp::operator==(__rhs); }
532 template<typename _Tp>
534 operator<=(const _Pointer_adapter<_Tp>& __lhs,
535 const _Pointer_adapter<_Tp>& __rhs)
536 { return __lhs._Tp::operator<(__rhs) || __lhs._Tp::operator==(__rhs); }
538 template<typename _Tp>
540 operator!=(const _Pointer_adapter<_Tp>& __lhs,
541 const _Pointer_adapter<_Tp>& __rhs)
542 { return !(__lhs._Tp::operator==(__rhs)); }
544 template<typename _Tp>
546 operator>(const _Pointer_adapter<_Tp>& __lhs,
547 const _Pointer_adapter<_Tp>& __rhs)
548 { return !(__lhs._Tp::operator<(__rhs) || __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)); }
556 template<typename _CharT, typename _Traits, typename _StoreT>
557 inline std::basic_ostream<_CharT, _Traits>&
558 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
559 const _Pointer_adapter<_StoreT>& __p)
560 { return (__os << __p.get()); }
562 _GLIBCXX_END_NAMESPACE_VERSION
565 #ifdef __GXX_EXPERIMENTAL_CXX0X__
566 namespace std _GLIBCXX_VISIBILITY(default)
568 _GLIBCXX_BEGIN_NAMESPACE_VERSION
570 template<typename _Storage_policy>
571 struct pointer_traits<__gnu_cxx::_Pointer_adapter<_Storage_policy>>
574 typedef __gnu_cxx::_Pointer_adapter<_Storage_policy> pointer;
575 /// The type pointed to
576 typedef typename pointer::element_type element_type;
577 /// Type used to represent the difference between two pointers
578 typedef typename pointer::difference_type difference_type;
580 /* TODO: replace __rebind<U> with alias template rebind<U> */
582 template<typename _Up>
583 using rebind<_Up> = typename __gnu_cxx::_Pointer_adapter<
584 typename pointer_traits<_Storage_policy>::rebind<_Up>>
586 template<typename _Up>
589 typedef pointer_traits<_Storage_policy> _Policy_traits;
590 typedef typename _Policy_traits::template __rebind<_Up>::__type
593 typedef typename __gnu_cxx::_Pointer_adapter<_Rebound_policy> __type;
596 static pointer pointer_to(typename pointer::reference __r) noexcept
597 { return pointer(std::addressof(__r)); }
600 _GLIBCXX_END_NAMESPACE_VERSION