pointer.h 19 KB

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