alloc_traits.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Allocator traits -*- C++ -*-
  2. // Copyright (C) 2011-2018 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. /** @file ext/alloc_traits.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _EXT_ALLOC_TRAITS_H
  24. #define _EXT_ALLOC_TRAITS_H 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201103L
  27. # include <bits/move.h>
  28. # include <bits/alloc_traits.h>
  29. #else
  30. # include <bits/allocator.h> // for __alloc_swap
  31. #endif
  32. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @brief Uniform interface to C++98 and C++11 allocators.
  37. * @ingroup allocators
  38. */
  39. template<typename _Alloc, typename = typename _Alloc::value_type>
  40. struct __alloc_traits
  41. #if __cplusplus >= 201103L
  42. : std::allocator_traits<_Alloc>
  43. #endif
  44. {
  45. typedef _Alloc allocator_type;
  46. #if __cplusplus >= 201103L
  47. typedef std::allocator_traits<_Alloc> _Base_type;
  48. typedef typename _Base_type::value_type value_type;
  49. typedef typename _Base_type::pointer pointer;
  50. typedef typename _Base_type::const_pointer const_pointer;
  51. typedef typename _Base_type::size_type size_type;
  52. typedef typename _Base_type::difference_type difference_type;
  53. // C++11 allocators do not define reference or const_reference
  54. typedef value_type& reference;
  55. typedef const value_type& const_reference;
  56. using _Base_type::allocate;
  57. using _Base_type::deallocate;
  58. using _Base_type::construct;
  59. using _Base_type::destroy;
  60. using _Base_type::max_size;
  61. private:
  62. template<typename _Ptr>
  63. using __is_custom_pointer
  64. = std::__and_<std::is_same<pointer, _Ptr>,
  65. std::__not_<std::is_pointer<_Ptr>>>;
  66. public:
  67. // overload construct for non-standard pointer types
  68. template<typename _Ptr, typename... _Args>
  69. static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
  70. construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
  71. {
  72. _Base_type::construct(__a, std::__to_address(__p),
  73. std::forward<_Args>(__args)...);
  74. }
  75. // overload destroy for non-standard pointer types
  76. template<typename _Ptr>
  77. static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
  78. destroy(_Alloc& __a, _Ptr __p)
  79. { _Base_type::destroy(__a, std::__to_address(__p)); }
  80. static _Alloc _S_select_on_copy(const _Alloc& __a)
  81. { return _Base_type::select_on_container_copy_construction(__a); }
  82. static void _S_on_swap(_Alloc& __a, _Alloc& __b)
  83. { std::__alloc_on_swap(__a, __b); }
  84. static constexpr bool _S_propagate_on_copy_assign()
  85. { return _Base_type::propagate_on_container_copy_assignment::value; }
  86. static constexpr bool _S_propagate_on_move_assign()
  87. { return _Base_type::propagate_on_container_move_assignment::value; }
  88. static constexpr bool _S_propagate_on_swap()
  89. { return _Base_type::propagate_on_container_swap::value; }
  90. static constexpr bool _S_always_equal()
  91. { return _Base_type::is_always_equal::value; }
  92. static constexpr bool _S_nothrow_move()
  93. { return _S_propagate_on_move_assign() || _S_always_equal(); }
  94. template<typename _Tp>
  95. struct rebind
  96. { typedef typename _Base_type::template rebind_alloc<_Tp> other; };
  97. #else
  98. typedef typename _Alloc::pointer pointer;
  99. typedef typename _Alloc::const_pointer const_pointer;
  100. typedef typename _Alloc::value_type value_type;
  101. typedef typename _Alloc::reference reference;
  102. typedef typename _Alloc::const_reference const_reference;
  103. typedef typename _Alloc::size_type size_type;
  104. typedef typename _Alloc::difference_type difference_type;
  105. static pointer
  106. allocate(_Alloc& __a, size_type __n)
  107. { return __a.allocate(__n); }
  108. static void deallocate(_Alloc& __a, pointer __p, size_type __n)
  109. { __a.deallocate(__p, __n); }
  110. template<typename _Tp>
  111. static void construct(_Alloc& __a, pointer __p, const _Tp& __arg)
  112. { __a.construct(__p, __arg); }
  113. static void destroy(_Alloc& __a, pointer __p)
  114. { __a.destroy(__p); }
  115. static size_type max_size(const _Alloc& __a)
  116. { return __a.max_size(); }
  117. static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
  118. static void _S_on_swap(_Alloc& __a, _Alloc& __b)
  119. {
  120. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  121. // 431. Swapping containers with unequal allocators.
  122. std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
  123. }
  124. template<typename _Tp>
  125. struct rebind
  126. { typedef typename _Alloc::template rebind<_Tp>::other other; };
  127. #endif
  128. };
  129. _GLIBCXX_END_NAMESPACE_VERSION
  130. } // namespace __gnu_cxx
  131. #endif