pointer.h 18 KB

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