pointer.h 20 KB

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