You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

619 lines
20KB

  1. // Custom pointer adapter and sample storage policies
  2. // Copyright (C) 2008-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /**
  21. * @file ext/pointer.h
  22. * This file is a GNU extension to the Standard C++ Library.
  23. *
  24. * @author Bob Walters
  25. *
  26. * Provides reusable _Pointer_adapter for assisting in the development of
  27. * custom pointer types that can be used with the standard containers via
  28. * the allocator::pointer and allocator::const_pointer typedefs.
  29. */
  30. #ifndef _POINTER_H
  31. #define _POINTER_H 1
  32. #pragma GCC system_header
  33. #include <iosfwd>
  34. #include <bits/stl_iterator_base_types.h>
  35. #include <ext/cast.h>
  36. #include <ext/type_traits.h>
  37. #if __cplusplus >= 201103L
  38. # include <bits/move.h>
  39. # include <bits/ptr_traits.h>
  40. #endif
  41. #if __cplusplus > 201703L
  42. # include <iterator> // for indirectly_readable_traits
  43. #endif
  44. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  45. {
  46. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  47. /**
  48. * @brief A storage policy for use with _Pointer_adapter<> which yields a
  49. * standard pointer.
  50. *
  51. * A _Storage_policy is required to provide 4 things:
  52. * 1) A get() API for returning the stored pointer value.
  53. * 2) An set() API for storing a pointer value.
  54. * 3) An element_type typedef to define the type this points to.
  55. * 4) An operator<() to support pointer comparison.
  56. * 5) An operator==() to support pointer comparison.
  57. */
  58. template<typename _Tp>
  59. class _Std_pointer_impl
  60. {
  61. public:
  62. // the type this pointer points to.
  63. typedef _Tp element_type;
  64. // A method to fetch the pointer value as a standard T* value;
  65. inline _Tp*
  66. get() const
  67. { return _M_value; }
  68. // A method to set the pointer value, from a standard T* value;
  69. inline void
  70. set(element_type* __arg)
  71. { _M_value = __arg; }
  72. // Comparison of pointers
  73. inline bool
  74. operator<(const _Std_pointer_impl& __rarg) const
  75. { return (_M_value < __rarg._M_value); }
  76. inline bool
  77. operator==(const _Std_pointer_impl& __rarg) const
  78. { return (_M_value == __rarg._M_value); }
  79. private:
  80. element_type* _M_value;
  81. };
  82. /**
  83. * @brief A storage policy for use with _Pointer_adapter<> which stores
  84. * the pointer's address as an offset value which is relative to
  85. * its own address.
  86. *
  87. * This is intended for pointers within shared memory regions which
  88. * might be mapped at different addresses by different processes.
  89. * For null pointers, a value of 1 is used. (0 is legitimate
  90. * sometimes for nodes in circularly linked lists) This value was
  91. * chosen as the least likely to generate an incorrect null, As
  92. * there is no reason why any normal pointer would point 1 byte into
  93. * its own pointer address.
  94. */
  95. template<typename _Tp>
  96. class _Relative_pointer_impl
  97. {
  98. public:
  99. typedef _Tp element_type;
  100. _Tp*
  101. get() const
  102. {
  103. if (_M_diff == 1)
  104. return 0;
  105. else
  106. return reinterpret_cast<_Tp*>(reinterpret_cast<_UIntPtrType>(this)
  107. + _M_diff);
  108. }
  109. void
  110. set(_Tp* __arg)
  111. {
  112. if (!__arg)
  113. _M_diff = 1;
  114. else
  115. _M_diff = reinterpret_cast<_UIntPtrType>(__arg)
  116. - reinterpret_cast<_UIntPtrType>(this);
  117. }
  118. // Comparison of pointers
  119. inline bool
  120. operator<(const _Relative_pointer_impl& __rarg) const
  121. { return (reinterpret_cast<_UIntPtrType>(this->get())
  122. < reinterpret_cast<_UIntPtrType>(__rarg.get())); }
  123. inline bool
  124. operator==(const _Relative_pointer_impl& __rarg) const
  125. { return (reinterpret_cast<_UIntPtrType>(this->get())
  126. == reinterpret_cast<_UIntPtrType>(__rarg.get())); }
  127. private:
  128. #ifdef _GLIBCXX_USE_LONG_LONG
  129. typedef __gnu_cxx::__conditional_type<
  130. (sizeof(unsigned long) >= sizeof(void*)),
  131. unsigned long, unsigned long long>::__type _UIntPtrType;
  132. #else
  133. typedef unsigned long _UIntPtrType;
  134. #endif
  135. _UIntPtrType _M_diff;
  136. };
  137. /**
  138. * Relative_pointer_impl needs a specialization for const T because of
  139. * the casting done during pointer arithmetic.
  140. */
  141. template<typename _Tp>
  142. class _Relative_pointer_impl<const _Tp>
  143. {
  144. public:
  145. typedef const _Tp element_type;
  146. const _Tp*
  147. get() const
  148. {
  149. if (_M_diff == 1)
  150. return 0;
  151. else
  152. return reinterpret_cast<const _Tp*>
  153. (reinterpret_cast<_UIntPtrType>(this) + _M_diff);
  154. }
  155. void
  156. set(const _Tp* __arg)
  157. {
  158. if (!__arg)
  159. _M_diff = 1;
  160. else
  161. _M_diff = reinterpret_cast<_UIntPtrType>(__arg)
  162. - reinterpret_cast<_UIntPtrType>(this);
  163. }
  164. // Comparison of pointers
  165. inline bool
  166. operator<(const _Relative_pointer_impl& __rarg) const
  167. { return (reinterpret_cast<_UIntPtrType>(this->get())
  168. < reinterpret_cast<_UIntPtrType>(__rarg.get())); }
  169. inline bool
  170. operator==(const _Relative_pointer_impl& __rarg) const
  171. { return (reinterpret_cast<_UIntPtrType>(this->get())
  172. == reinterpret_cast<_UIntPtrType>(__rarg.get())); }
  173. private:
  174. #ifdef _GLIBCXX_USE_LONG_LONG
  175. typedef __gnu_cxx::__conditional_type<
  176. (sizeof(unsigned long) >= sizeof(void*)),
  177. unsigned long, unsigned long long>::__type _UIntPtrType;
  178. #else
  179. typedef unsigned long _UIntPtrType;
  180. #endif
  181. _UIntPtrType _M_diff;
  182. };
  183. /**
  184. * The specialization on this type helps resolve the problem of
  185. * reference to void, and eliminates the need to specialize
  186. * _Pointer_adapter for cases of void*, const void*, and so on.
  187. */
  188. struct _Invalid_type { };
  189. template<typename _Tp>
  190. struct _Reference_type
  191. { typedef _Tp& reference; };
  192. template<>
  193. struct _Reference_type<void>
  194. { typedef _Invalid_type& reference; };
  195. template<>
  196. struct _Reference_type<const void>
  197. { typedef const _Invalid_type& reference; };
  198. template<>
  199. struct _Reference_type<volatile void>
  200. { typedef volatile _Invalid_type& reference; };
  201. template<>
  202. struct _Reference_type<volatile const void>
  203. { typedef const volatile _Invalid_type& reference; };
  204. /**
  205. * This structure accommodates the way in which
  206. * std::iterator_traits<> is normally specialized for const T*, so
  207. * that value_type is still T.
  208. */
  209. template<typename _Tp>
  210. struct _Unqualified_type
  211. { typedef _Tp type; };
  212. template<typename _Tp>
  213. struct _Unqualified_type<const _Tp>
  214. { typedef _Tp type; };
  215. /**
  216. * The following provides an 'alternative pointer' that works with
  217. * the containers when specified as the pointer typedef of the
  218. * allocator.
  219. *
  220. * The pointer type used with the containers doesn't have to be this
  221. * class, but it must support the implicit conversions, pointer
  222. * arithmetic, comparison operators, etc. that are supported by this
  223. * class, and avoid raising compile-time ambiguities. Because
  224. * creating a working pointer can be challenging, this pointer
  225. * template was designed to wrapper an easier storage policy type,
  226. * so that it becomes reusable for creating other pointer types.
  227. *
  228. * A key point of this class is also that it allows container
  229. * writers to 'assume' Allocator::pointer is a typedef for a normal
  230. * pointer. This class supports most of the conventions of a true
  231. * pointer, and can, for instance handle implicit conversion to
  232. * const and base class pointer types. The only impositions on
  233. * container writers to support extended pointers are: 1) use the
  234. * Allocator::pointer typedef appropriately for pointer types. 2)
  235. * if you need pointer casting, use the __pointer_cast<> functions
  236. * from ext/cast.h. This allows pointer cast operations to be
  237. * overloaded as necessary by custom pointers.
  238. *
  239. * Note: The const qualifier works with this pointer adapter as
  240. * follows:
  241. *
  242. * _Tp* == _Pointer_adapter<_Std_pointer_impl<_Tp> >;
  243. * const _Tp* == _Pointer_adapter<_Std_pointer_impl<const _Tp> >;
  244. * _Tp* const == const _Pointer_adapter<_Std_pointer_impl<_Tp> >;
  245. * const _Tp* const == const _Pointer_adapter<_Std_pointer_impl<const _Tp> >;
  246. */
  247. template<typename _Storage_policy>
  248. class _Pointer_adapter : public _Storage_policy
  249. {
  250. public:
  251. typedef typename _Storage_policy::element_type element_type;
  252. // These are needed for iterator_traits
  253. typedef std::random_access_iterator_tag iterator_category;
  254. typedef typename _Unqualified_type<element_type>::type value_type;
  255. typedef std::ptrdiff_t difference_type;
  256. typedef _Pointer_adapter pointer;
  257. typedef typename _Reference_type<element_type>::reference reference;
  258. // Reminder: 'const' methods mean that the method is valid when the
  259. // pointer is immutable, and has nothing to do with whether the
  260. // 'pointee' is const.
  261. // Default Constructor (Convert from element_type*)
  262. _Pointer_adapter(element_type* __arg = 0)
  263. { _Storage_policy::set(__arg); }
  264. // Copy constructor from _Pointer_adapter of same type.
  265. _Pointer_adapter(const _Pointer_adapter& __arg)
  266. { _Storage_policy::set(__arg.get()); }
  267. // Convert from _Up* if conversion to element_type* is valid.
  268. template<typename _Up>
  269. _Pointer_adapter(_Up* __arg)
  270. { _Storage_policy::set(__arg); }
  271. // Conversion from another _Pointer_adapter if _Up if static cast is
  272. // valid.
  273. template<typename _Up>
  274. _Pointer_adapter(const _Pointer_adapter<_Up>& __arg)
  275. { _Storage_policy::set(__arg.get()); }
  276. // Destructor
  277. ~_Pointer_adapter() { }
  278. // Assignment operator
  279. _Pointer_adapter&
  280. operator=(const _Pointer_adapter& __arg)
  281. {
  282. _Storage_policy::set(__arg.get());
  283. return *this;
  284. }
  285. template<typename _Up>
  286. _Pointer_adapter&
  287. operator=(const _Pointer_adapter<_Up>& __arg)
  288. {
  289. _Storage_policy::set(__arg.get());
  290. return *this;
  291. }
  292. template<typename _Up>
  293. _Pointer_adapter&
  294. operator=(_Up* __arg)
  295. {
  296. _Storage_policy::set(__arg);
  297. return *this;
  298. }
  299. // Operator*, returns element_type&
  300. inline reference
  301. operator*() const
  302. { return *(_Storage_policy::get()); }
  303. // Operator->, returns element_type*
  304. inline element_type*
  305. operator->() const
  306. { return _Storage_policy::get(); }
  307. // Operator[], returns a element_type& to the item at that loc.
  308. inline reference
  309. operator[](std::ptrdiff_t __index) const
  310. { return _Storage_policy::get()[__index]; }
  311. // To allow implicit conversion to "bool", for "if (ptr)..."
  312. #if __cplusplus >= 201103L
  313. explicit operator bool() const { return _Storage_policy::get() != 0; }
  314. #else
  315. private:
  316. typedef element_type*(_Pointer_adapter::*__unspecified_bool_type)() const;
  317. public:
  318. operator __unspecified_bool_type() const
  319. {
  320. return _Storage_policy::get() == 0 ? 0 :
  321. &_Pointer_adapter::operator->;
  322. }
  323. // ! operator (for: if (!ptr)...)
  324. inline bool
  325. operator!() const
  326. { return (_Storage_policy::get() == 0); }
  327. #endif
  328. // Pointer differences
  329. inline friend std::ptrdiff_t
  330. operator-(const _Pointer_adapter& __lhs, element_type* __rhs)
  331. { return (__lhs.get() - __rhs); }
  332. inline friend std::ptrdiff_t
  333. operator-(element_type* __lhs, const _Pointer_adapter& __rhs)
  334. { return (__lhs - __rhs.get()); }
  335. template<typename _Up>
  336. inline friend std::ptrdiff_t
  337. operator-(const _Pointer_adapter& __lhs, _Up* __rhs)
  338. { return (__lhs.get() - __rhs); }
  339. template<typename _Up>
  340. inline friend std::ptrdiff_t
  341. operator-(_Up* __lhs, const _Pointer_adapter& __rhs)
  342. { return (__lhs - __rhs.get()); }
  343. template<typename _Up>
  344. inline std::ptrdiff_t
  345. operator-(const _Pointer_adapter<_Up>& __rhs) const
  346. { return (_Storage_policy::get() - __rhs.get()); }
  347. // Pointer math
  348. // Note: There is a reason for all this overloading based on different
  349. // integer types. In some libstdc++-v3 test cases, a templated
  350. // operator+ is declared which can match any types. This operator
  351. // tends to "steal" the recognition of _Pointer_adapter's own operator+
  352. // unless the integer type matches perfectly.
  353. #define _CXX_POINTER_ARITH_OPERATOR_SET(INT_TYPE) \
  354. inline friend _Pointer_adapter \
  355. operator+(const _Pointer_adapter& __lhs, INT_TYPE __offset) \
  356. { return _Pointer_adapter(__lhs.get() + __offset); } \
  357. \
  358. inline friend _Pointer_adapter \
  359. operator+(INT_TYPE __offset, const _Pointer_adapter& __rhs) \
  360. { return _Pointer_adapter(__rhs.get() + __offset); } \
  361. \
  362. inline friend _Pointer_adapter \
  363. operator-(const _Pointer_adapter& __lhs, INT_TYPE __offset) \
  364. { return _Pointer_adapter(__lhs.get() - __offset); } \
  365. \
  366. inline _Pointer_adapter& \
  367. operator+=(INT_TYPE __offset) \
  368. { \
  369. _Storage_policy::set(_Storage_policy::get() + __offset); \
  370. return *this; \
  371. } \
  372. \
  373. inline _Pointer_adapter& \
  374. operator-=(INT_TYPE __offset) \
  375. { \
  376. _Storage_policy::set(_Storage_policy::get() - __offset); \
  377. return *this; \
  378. } \
  379. // END of _CXX_POINTER_ARITH_OPERATOR_SET macro
  380. // Expand into the various pointer arithmetic operators needed.
  381. _CXX_POINTER_ARITH_OPERATOR_SET(short);
  382. _CXX_POINTER_ARITH_OPERATOR_SET(unsigned short);
  383. _CXX_POINTER_ARITH_OPERATOR_SET(int);
  384. _CXX_POINTER_ARITH_OPERATOR_SET(unsigned int);
  385. _CXX_POINTER_ARITH_OPERATOR_SET(long);
  386. _CXX_POINTER_ARITH_OPERATOR_SET(unsigned long);
  387. #ifdef _GLIBCXX_USE_LONG_LONG
  388. _CXX_POINTER_ARITH_OPERATOR_SET(long long);
  389. _CXX_POINTER_ARITH_OPERATOR_SET(unsigned long long);
  390. #endif
  391. // Mathematical Manipulators
  392. inline _Pointer_adapter&
  393. operator++()
  394. {
  395. _Storage_policy::set(_Storage_policy::get() + 1);
  396. return *this;
  397. }
  398. inline _Pointer_adapter
  399. operator++(int)
  400. {
  401. _Pointer_adapter __tmp(*this);
  402. _Storage_policy::set(_Storage_policy::get() + 1);
  403. return __tmp;
  404. }
  405. inline _Pointer_adapter&
  406. operator--()
  407. {
  408. _Storage_policy::set(_Storage_policy::get() - 1);
  409. return *this;
  410. }
  411. inline _Pointer_adapter
  412. operator--(int)
  413. {
  414. _Pointer_adapter __tmp(*this);
  415. _Storage_policy::set(_Storage_policy::get() - 1);
  416. return __tmp;
  417. }
  418. #if __cpp_lib_three_way_comparison
  419. friend std::strong_ordering
  420. operator<=>(const _Pointer_adapter& __lhs, const _Pointer_adapter& __rhs)
  421. noexcept
  422. { return __lhs.get() <=> __rhs.get(); }
  423. #endif
  424. }; // class _Pointer_adapter
  425. #define _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(OPERATOR) \
  426. template<typename _Tp1, typename _Tp2> \
  427. inline bool \
  428. operator OPERATOR(const _Pointer_adapter<_Tp1>& __lhs, _Tp2 __rhs) \
  429. { return __lhs.get() OPERATOR __rhs; } \
  430. \
  431. template<typename _Tp1, typename _Tp2> \
  432. inline bool \
  433. operator OPERATOR(_Tp1 __lhs, const _Pointer_adapter<_Tp2>& __rhs) \
  434. { return __lhs OPERATOR __rhs.get(); } \
  435. \
  436. template<typename _Tp1, typename _Tp2> \
  437. inline bool \
  438. operator OPERATOR(const _Pointer_adapter<_Tp1>& __lhs, \
  439. const _Pointer_adapter<_Tp2>& __rhs) \
  440. { return __lhs.get() OPERATOR __rhs.get(); } \
  441. \
  442. // End GCC_CXX_POINTER_COMPARISON_OPERATION_SET Macro
  443. // Expand into the various comparison operators needed.
  444. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(==)
  445. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(!=)
  446. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(<)
  447. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(<=)
  448. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(>)
  449. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(>=)
  450. // These are here for expressions like "ptr == 0", "ptr != 0"
  451. template<typename _Tp>
  452. inline bool
  453. operator==(const _Pointer_adapter<_Tp>& __lhs, int __rhs)
  454. { return __lhs.get() == reinterpret_cast<void*>(__rhs); }
  455. template<typename _Tp>
  456. inline bool
  457. operator==(int __lhs, const _Pointer_adapter<_Tp>& __rhs)
  458. { return __rhs.get() == reinterpret_cast<void*>(__lhs); }
  459. template<typename _Tp>
  460. inline bool
  461. operator!=(const _Pointer_adapter<_Tp>& __lhs, int __rhs)
  462. { return __lhs.get() != reinterpret_cast<void*>(__rhs); }
  463. template<typename _Tp>
  464. inline bool
  465. operator!=(int __lhs, const _Pointer_adapter<_Tp>& __rhs)
  466. { return __rhs.get() != reinterpret_cast<void*>(__lhs); }
  467. /**
  468. * Comparison operators for _Pointer_adapter defer to the base class'
  469. * comparison operators, when possible.
  470. */
  471. template<typename _Tp>
  472. inline bool
  473. operator==(const _Pointer_adapter<_Tp>& __lhs,
  474. const _Pointer_adapter<_Tp>& __rhs)
  475. { return __lhs._Tp::operator==(__rhs); }
  476. template<typename _Tp>
  477. inline bool
  478. operator<=(const _Pointer_adapter<_Tp>& __lhs,
  479. const _Pointer_adapter<_Tp>& __rhs)
  480. { return __lhs._Tp::operator<(__rhs) || __lhs._Tp::operator==(__rhs); }
  481. template<typename _Tp>
  482. inline bool
  483. operator!=(const _Pointer_adapter<_Tp>& __lhs,
  484. const _Pointer_adapter<_Tp>& __rhs)
  485. { return !(__lhs._Tp::operator==(__rhs)); }
  486. template<typename _Tp>
  487. inline bool
  488. operator>(const _Pointer_adapter<_Tp>& __lhs,
  489. const _Pointer_adapter<_Tp>& __rhs)
  490. { return !(__lhs._Tp::operator<(__rhs) || __lhs._Tp::operator==(__rhs)); }
  491. template<typename _Tp>
  492. inline bool
  493. operator>=(const _Pointer_adapter<_Tp>& __lhs,
  494. const _Pointer_adapter<_Tp>& __rhs)
  495. { return !(__lhs._Tp::operator<(__rhs)); }
  496. template<typename _CharT, typename _Traits, typename _StoreT>
  497. inline std::basic_ostream<_CharT, _Traits>&
  498. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  499. const _Pointer_adapter<_StoreT>& __p)
  500. { return (__os << __p.get()); }
  501. _GLIBCXX_END_NAMESPACE_VERSION
  502. } // namespace
  503. #if __cplusplus >= 201103L
  504. namespace std _GLIBCXX_VISIBILITY(default)
  505. {
  506. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  507. template<typename _Storage_policy>
  508. struct pointer_traits<__gnu_cxx::_Pointer_adapter<_Storage_policy>>
  509. {
  510. /// The pointer type
  511. typedef __gnu_cxx::_Pointer_adapter<_Storage_policy> pointer;
  512. /// The type pointed to
  513. typedef typename pointer::element_type element_type;
  514. /// Type used to represent the difference between two pointers
  515. typedef typename pointer::difference_type difference_type;
  516. template<typename _Up>
  517. using rebind = typename __gnu_cxx::_Pointer_adapter<
  518. typename pointer_traits<_Storage_policy>::template rebind<_Up>>;
  519. static pointer pointer_to(typename pointer::reference __r) noexcept
  520. { return pointer(std::addressof(__r)); }
  521. };
  522. #if __cpp_lib_concepts
  523. template<typename _Policy>
  524. struct indirectly_readable_traits<__gnu_cxx::_Pointer_adapter<_Policy>>
  525. {
  526. using value_type
  527. = typename __gnu_cxx::_Pointer_adapter<_Policy>::value_type;
  528. };
  529. #endif
  530. _GLIBCXX_END_NAMESPACE_VERSION
  531. } // namespace
  532. #endif
  533. #endif // _POINTER_H