pointer.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // Custom pointer adapter and sample storage policies
  2. // Copyright (C) 2008-2022 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<uintptr_t>(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<uintptr_t>(__arg)
  116. - reinterpret_cast<uintptr_t>(this);
  117. }
  118. // Comparison of pointers
  119. inline bool
  120. operator<(const _Relative_pointer_impl& __rarg) const
  121. { return (reinterpret_cast<uintptr_t>(this->get())
  122. < reinterpret_cast<uintptr_t>(__rarg.get())); }
  123. inline bool
  124. operator==(const _Relative_pointer_impl& __rarg) const
  125. { return (reinterpret_cast<uintptr_t>(this->get())
  126. == reinterpret_cast<uintptr_t>(__rarg.get())); }
  127. private:
  128. typedef __UINTPTR_TYPE__ uintptr_t;
  129. uintptr_t _M_diff;
  130. };
  131. /**
  132. * Relative_pointer_impl needs a specialization for const T because of
  133. * the casting done during pointer arithmetic.
  134. */
  135. template<typename _Tp>
  136. class _Relative_pointer_impl<const _Tp>
  137. {
  138. public:
  139. typedef const _Tp element_type;
  140. const _Tp*
  141. get() const
  142. {
  143. if (_M_diff == 1)
  144. return 0;
  145. else
  146. return reinterpret_cast<const _Tp*>
  147. (reinterpret_cast<uintptr_t>(this) + _M_diff);
  148. }
  149. void
  150. set(const _Tp* __arg)
  151. {
  152. if (!__arg)
  153. _M_diff = 1;
  154. else
  155. _M_diff = reinterpret_cast<uintptr_t>(__arg)
  156. - reinterpret_cast<uintptr_t>(this);
  157. }
  158. // Comparison of pointers
  159. inline bool
  160. operator<(const _Relative_pointer_impl& __rarg) const
  161. { return (reinterpret_cast<uintptr_t>(this->get())
  162. < reinterpret_cast<uintptr_t>(__rarg.get())); }
  163. inline bool
  164. operator==(const _Relative_pointer_impl& __rarg) const
  165. { return (reinterpret_cast<uintptr_t>(this->get())
  166. == reinterpret_cast<uintptr_t>(__rarg.get())); }
  167. private:
  168. typedef __UINTPTR_TYPE__ uintptr_t;
  169. uintptr_t _M_diff;
  170. };
  171. /**
  172. * The specialization on this type helps resolve the problem of
  173. * reference to void, and eliminates the need to specialize
  174. * _Pointer_adapter for cases of void*, const void*, and so on.
  175. */
  176. struct _Invalid_type { };
  177. template<typename _Tp>
  178. struct _Reference_type
  179. { typedef _Tp& reference; };
  180. template<>
  181. struct _Reference_type<void>
  182. { typedef _Invalid_type& reference; };
  183. template<>
  184. struct _Reference_type<const void>
  185. { typedef const _Invalid_type& reference; };
  186. template<>
  187. struct _Reference_type<volatile void>
  188. { typedef volatile _Invalid_type& reference; };
  189. template<>
  190. struct _Reference_type<volatile const void>
  191. { typedef const volatile _Invalid_type& reference; };
  192. /**
  193. * This structure accommodates the way in which
  194. * std::iterator_traits<> is normally specialized for const T*, so
  195. * that value_type is still T.
  196. */
  197. template<typename _Tp>
  198. struct _Unqualified_type
  199. { typedef _Tp type; };
  200. template<typename _Tp>
  201. struct _Unqualified_type<const _Tp>
  202. { typedef _Tp type; };
  203. /**
  204. * The following provides an 'alternative pointer' that works with
  205. * the containers when specified as the pointer typedef of the
  206. * allocator.
  207. *
  208. * The pointer type used with the containers doesn't have to be this
  209. * class, but it must support the implicit conversions, pointer
  210. * arithmetic, comparison operators, etc. that are supported by this
  211. * class, and avoid raising compile-time ambiguities. Because
  212. * creating a working pointer can be challenging, this pointer
  213. * template was designed to wrapper an easier storage policy type,
  214. * so that it becomes reusable for creating other pointer types.
  215. *
  216. * A key point of this class is also that it allows container
  217. * writers to 'assume' Allocator::pointer is a typedef for a normal
  218. * pointer. This class supports most of the conventions of a true
  219. * pointer, and can, for instance handle implicit conversion to
  220. * const and base class pointer types. The only impositions on
  221. * container writers to support extended pointers are: 1) use the
  222. * Allocator::pointer typedef appropriately for pointer types. 2)
  223. * if you need pointer casting, use the __pointer_cast<> functions
  224. * from ext/cast.h. This allows pointer cast operations to be
  225. * overloaded as necessary by custom pointers.
  226. *
  227. * Note: The const qualifier works with this pointer adapter as
  228. * follows:
  229. *
  230. * _Tp* == _Pointer_adapter<_Std_pointer_impl<_Tp> >;
  231. * const _Tp* == _Pointer_adapter<_Std_pointer_impl<const _Tp> >;
  232. * _Tp* const == const _Pointer_adapter<_Std_pointer_impl<_Tp> >;
  233. * const _Tp* const == const _Pointer_adapter<_Std_pointer_impl<const _Tp> >;
  234. */
  235. template<typename _Storage_policy>
  236. class _Pointer_adapter : public _Storage_policy
  237. {
  238. public:
  239. typedef typename _Storage_policy::element_type element_type;
  240. // These are needed for iterator_traits
  241. typedef std::random_access_iterator_tag iterator_category;
  242. typedef typename _Unqualified_type<element_type>::type value_type;
  243. typedef std::ptrdiff_t difference_type;
  244. typedef _Pointer_adapter pointer;
  245. typedef typename _Reference_type<element_type>::reference reference;
  246. // Reminder: 'const' methods mean that the method is valid when the
  247. // pointer is immutable, and has nothing to do with whether the
  248. // 'pointee' is const.
  249. // Default Constructor (Convert from element_type*)
  250. _Pointer_adapter(element_type* __arg = 0)
  251. { _Storage_policy::set(__arg); }
  252. // Copy constructor from _Pointer_adapter of same type.
  253. _Pointer_adapter(const _Pointer_adapter& __arg)
  254. { _Storage_policy::set(__arg.get()); }
  255. // Convert from _Up* if conversion to element_type* is valid.
  256. template<typename _Up>
  257. _Pointer_adapter(_Up* __arg)
  258. { _Storage_policy::set(__arg); }
  259. // Conversion from another _Pointer_adapter if _Up if static cast is
  260. // valid.
  261. template<typename _Up>
  262. _Pointer_adapter(const _Pointer_adapter<_Up>& __arg)
  263. { _Storage_policy::set(__arg.get()); }
  264. // Destructor
  265. ~_Pointer_adapter() { }
  266. // Assignment operator
  267. _Pointer_adapter&
  268. operator=(const _Pointer_adapter& __arg)
  269. {
  270. _Storage_policy::set(__arg.get());
  271. return *this;
  272. }
  273. template<typename _Up>
  274. _Pointer_adapter&
  275. operator=(const _Pointer_adapter<_Up>& __arg)
  276. {
  277. _Storage_policy::set(__arg.get());
  278. return *this;
  279. }
  280. template<typename _Up>
  281. _Pointer_adapter&
  282. operator=(_Up* __arg)
  283. {
  284. _Storage_policy::set(__arg);
  285. return *this;
  286. }
  287. // Operator*, returns element_type&
  288. inline reference
  289. operator*() const
  290. { return *(_Storage_policy::get()); }
  291. // Operator->, returns element_type*
  292. inline element_type*
  293. operator->() const
  294. { return _Storage_policy::get(); }
  295. // Operator[], returns a element_type& to the item at that loc.
  296. inline reference
  297. operator[](std::ptrdiff_t __index) const
  298. { return _Storage_policy::get()[__index]; }
  299. // To allow implicit conversion to "bool", for "if (ptr)..."
  300. #if __cplusplus >= 201103L
  301. explicit operator bool() const { return _Storage_policy::get() != 0; }
  302. #else
  303. private:
  304. typedef element_type*(_Pointer_adapter::*__unspecified_bool_type)() const;
  305. public:
  306. operator __unspecified_bool_type() const
  307. {
  308. return _Storage_policy::get() == 0 ? 0 :
  309. &_Pointer_adapter::operator->;
  310. }
  311. // ! operator (for: if (!ptr)...)
  312. inline bool
  313. operator!() const
  314. { return (_Storage_policy::get() == 0); }
  315. #endif
  316. // Pointer differences
  317. inline friend std::ptrdiff_t
  318. operator-(const _Pointer_adapter& __lhs, element_type* __rhs)
  319. { return (__lhs.get() - __rhs); }
  320. inline friend std::ptrdiff_t
  321. operator-(element_type* __lhs, const _Pointer_adapter& __rhs)
  322. { return (__lhs - __rhs.get()); }
  323. template<typename _Up>
  324. inline friend std::ptrdiff_t
  325. operator-(const _Pointer_adapter& __lhs, _Up* __rhs)
  326. { return (__lhs.get() - __rhs); }
  327. template<typename _Up>
  328. inline friend std::ptrdiff_t
  329. operator-(_Up* __lhs, const _Pointer_adapter& __rhs)
  330. { return (__lhs - __rhs.get()); }
  331. template<typename _Up>
  332. inline std::ptrdiff_t
  333. operator-(const _Pointer_adapter<_Up>& __rhs) const
  334. { return (_Storage_policy::get() - __rhs.get()); }
  335. // Pointer math
  336. // Note: There is a reason for all this overloading based on different
  337. // integer types. In some libstdc++-v3 test cases, a templated
  338. // operator+ is declared which can match any types. This operator
  339. // tends to "steal" the recognition of _Pointer_adapter's own operator+
  340. // unless the integer type matches perfectly.
  341. #define _CXX_POINTER_ARITH_OPERATOR_SET(INT_TYPE) \
  342. inline friend _Pointer_adapter \
  343. operator+(const _Pointer_adapter& __lhs, INT_TYPE __offset) \
  344. { return _Pointer_adapter(__lhs.get() + __offset); } \
  345. \
  346. inline friend _Pointer_adapter \
  347. operator+(INT_TYPE __offset, const _Pointer_adapter& __rhs) \
  348. { return _Pointer_adapter(__rhs.get() + __offset); } \
  349. \
  350. inline friend _Pointer_adapter \
  351. operator-(const _Pointer_adapter& __lhs, INT_TYPE __offset) \
  352. { return _Pointer_adapter(__lhs.get() - __offset); } \
  353. \
  354. inline _Pointer_adapter& \
  355. operator+=(INT_TYPE __offset) \
  356. { \
  357. _Storage_policy::set(_Storage_policy::get() + __offset); \
  358. return *this; \
  359. } \
  360. \
  361. inline _Pointer_adapter& \
  362. operator-=(INT_TYPE __offset) \
  363. { \
  364. _Storage_policy::set(_Storage_policy::get() - __offset); \
  365. return *this; \
  366. } \
  367. // END of _CXX_POINTER_ARITH_OPERATOR_SET macro
  368. // Expand into the various pointer arithmetic operators needed.
  369. _CXX_POINTER_ARITH_OPERATOR_SET(short);
  370. _CXX_POINTER_ARITH_OPERATOR_SET(unsigned short);
  371. _CXX_POINTER_ARITH_OPERATOR_SET(int);
  372. _CXX_POINTER_ARITH_OPERATOR_SET(unsigned int);
  373. _CXX_POINTER_ARITH_OPERATOR_SET(long);
  374. _CXX_POINTER_ARITH_OPERATOR_SET(unsigned long);
  375. #ifdef _GLIBCXX_USE_LONG_LONG
  376. _CXX_POINTER_ARITH_OPERATOR_SET(long long);
  377. _CXX_POINTER_ARITH_OPERATOR_SET(unsigned long long);
  378. #endif
  379. // Mathematical Manipulators
  380. inline _Pointer_adapter&
  381. operator++()
  382. {
  383. _Storage_policy::set(_Storage_policy::get() + 1);
  384. return *this;
  385. }
  386. inline _Pointer_adapter
  387. operator++(int)
  388. {
  389. _Pointer_adapter __tmp(*this);
  390. _Storage_policy::set(_Storage_policy::get() + 1);
  391. return __tmp;
  392. }
  393. inline _Pointer_adapter&
  394. operator--()
  395. {
  396. _Storage_policy::set(_Storage_policy::get() - 1);
  397. return *this;
  398. }
  399. inline _Pointer_adapter
  400. operator--(int)
  401. {
  402. _Pointer_adapter __tmp(*this);
  403. _Storage_policy::set(_Storage_policy::get() - 1);
  404. return __tmp;
  405. }
  406. #if __cpp_lib_three_way_comparison
  407. friend std::strong_ordering
  408. operator<=>(const _Pointer_adapter& __lhs, const _Pointer_adapter& __rhs)
  409. noexcept
  410. { return __lhs.get() <=> __rhs.get(); }
  411. #endif
  412. }; // class _Pointer_adapter
  413. #define _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(OPERATOR) \
  414. template<typename _Tp1, typename _Tp2> \
  415. inline bool \
  416. operator OPERATOR(const _Pointer_adapter<_Tp1>& __lhs, _Tp2 __rhs) \
  417. { return __lhs.get() OPERATOR __rhs; } \
  418. \
  419. template<typename _Tp1, typename _Tp2> \
  420. inline bool \
  421. operator OPERATOR(_Tp1 __lhs, const _Pointer_adapter<_Tp2>& __rhs) \
  422. { return __lhs OPERATOR __rhs.get(); } \
  423. \
  424. template<typename _Tp1, typename _Tp2> \
  425. inline bool \
  426. operator OPERATOR(const _Pointer_adapter<_Tp1>& __lhs, \
  427. const _Pointer_adapter<_Tp2>& __rhs) \
  428. { return __lhs.get() OPERATOR __rhs.get(); } \
  429. \
  430. // End GCC_CXX_POINTER_COMPARISON_OPERATION_SET Macro
  431. // Expand into the various comparison operators needed.
  432. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(==)
  433. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(!=)
  434. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(<)
  435. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(<=)
  436. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(>)
  437. _GCC_CXX_POINTER_COMPARISON_OPERATION_SET(>=)
  438. // These are here for expressions like "ptr == 0", "ptr != 0"
  439. template<typename _Tp>
  440. inline bool
  441. operator==(const _Pointer_adapter<_Tp>& __lhs, int __rhs)
  442. { return __lhs.get() == reinterpret_cast<void*>(__rhs); }
  443. template<typename _Tp>
  444. inline bool
  445. operator==(int __lhs, const _Pointer_adapter<_Tp>& __rhs)
  446. { return __rhs.get() == reinterpret_cast<void*>(__lhs); }
  447. template<typename _Tp>
  448. inline bool
  449. operator!=(const _Pointer_adapter<_Tp>& __lhs, int __rhs)
  450. { return __lhs.get() != reinterpret_cast<void*>(__rhs); }
  451. template<typename _Tp>
  452. inline bool
  453. operator!=(int __lhs, const _Pointer_adapter<_Tp>& __rhs)
  454. { return __rhs.get() != reinterpret_cast<void*>(__lhs); }
  455. /**
  456. * Comparison operators for _Pointer_adapter defer to the base class'
  457. * comparison operators, when possible.
  458. */
  459. template<typename _Tp>
  460. inline bool
  461. operator==(const _Pointer_adapter<_Tp>& __lhs,
  462. const _Pointer_adapter<_Tp>& __rhs)
  463. { return __lhs._Tp::operator==(__rhs); }
  464. template<typename _Tp>
  465. inline bool
  466. operator<=(const _Pointer_adapter<_Tp>& __lhs,
  467. const _Pointer_adapter<_Tp>& __rhs)
  468. { return __lhs._Tp::operator<(__rhs) || __lhs._Tp::operator==(__rhs); }
  469. template<typename _Tp>
  470. inline bool
  471. operator!=(const _Pointer_adapter<_Tp>& __lhs,
  472. const _Pointer_adapter<_Tp>& __rhs)
  473. { return !(__lhs._Tp::operator==(__rhs)); }
  474. template<typename _Tp>
  475. inline bool
  476. operator>(const _Pointer_adapter<_Tp>& __lhs,
  477. const _Pointer_adapter<_Tp>& __rhs)
  478. { return !(__lhs._Tp::operator<(__rhs) || __lhs._Tp::operator==(__rhs)); }
  479. template<typename _Tp>
  480. inline bool
  481. operator>=(const _Pointer_adapter<_Tp>& __lhs,
  482. const _Pointer_adapter<_Tp>& __rhs)
  483. { return !(__lhs._Tp::operator<(__rhs)); }
  484. template<typename _CharT, typename _Traits, typename _StoreT>
  485. inline std::basic_ostream<_CharT, _Traits>&
  486. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  487. const _Pointer_adapter<_StoreT>& __p)
  488. { return (__os << __p.get()); }
  489. _GLIBCXX_END_NAMESPACE_VERSION
  490. } // namespace
  491. #if __cplusplus >= 201103L
  492. namespace std _GLIBCXX_VISIBILITY(default)
  493. {
  494. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  495. template<typename _Storage_policy>
  496. struct pointer_traits<__gnu_cxx::_Pointer_adapter<_Storage_policy>>
  497. {
  498. /// The pointer type
  499. typedef __gnu_cxx::_Pointer_adapter<_Storage_policy> pointer;
  500. /// The type pointed to
  501. typedef typename pointer::element_type element_type;
  502. /// Type used to represent the difference between two pointers
  503. typedef typename pointer::difference_type difference_type;
  504. template<typename _Up>
  505. using rebind = typename __gnu_cxx::_Pointer_adapter<
  506. typename pointer_traits<_Storage_policy>::template rebind<_Up>>;
  507. static pointer pointer_to(typename pointer::reference __r) noexcept
  508. { return pointer(std::addressof(__r)); }
  509. };
  510. #if __cpp_lib_concepts
  511. template<typename _Policy>
  512. struct indirectly_readable_traits<__gnu_cxx::_Pointer_adapter<_Policy>>
  513. {
  514. using value_type
  515. = typename __gnu_cxx::_Pointer_adapter<_Policy>::value_type;
  516. };
  517. #endif
  518. _GLIBCXX_END_NAMESPACE_VERSION
  519. } // namespace
  520. #endif
  521. #endif // _POINTER_H