You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.5KB

  1. // Move, forward and identity for C++11 + swap -*- C++ -*-
  2. // Copyright (C) 2007-2020 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 bits/move.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{utility}
  23. */
  24. #ifndef _MOVE_H
  25. #define _MOVE_H 1
  26. #include <bits/c++config.h>
  27. #if __cplusplus < 201103L
  28. # include <bits/concept_check.h>
  29. #endif
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. // Used, in C++03 mode too, by allocators, etc.
  34. /**
  35. * @brief Same as C++11 std::addressof
  36. * @ingroup utilities
  37. */
  38. template<typename _Tp>
  39. inline _GLIBCXX_CONSTEXPR _Tp*
  40. __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT
  41. { return __builtin_addressof(__r); }
  42. #if __cplusplus >= 201103L
  43. _GLIBCXX_END_NAMESPACE_VERSION
  44. } // namespace
  45. #include <type_traits> // Brings in std::declval too.
  46. namespace std _GLIBCXX_VISIBILITY(default)
  47. {
  48. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  49. /**
  50. * @addtogroup utilities
  51. * @{
  52. */
  53. /**
  54. * @brief Forward an lvalue.
  55. * @return The parameter cast to the specified type.
  56. *
  57. * This function is used to implement "perfect forwarding".
  58. */
  59. template<typename _Tp>
  60. constexpr _Tp&&
  61. forward(typename std::remove_reference<_Tp>::type& __t) noexcept
  62. { return static_cast<_Tp&&>(__t); }
  63. /**
  64. * @brief Forward an rvalue.
  65. * @return The parameter cast to the specified type.
  66. *
  67. * This function is used to implement "perfect forwarding".
  68. */
  69. template<typename _Tp>
  70. constexpr _Tp&&
  71. forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
  72. {
  73. static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument"
  74. " substituting _Tp is an lvalue reference type");
  75. return static_cast<_Tp&&>(__t);
  76. }
  77. /**
  78. * @brief Convert a value to an rvalue.
  79. * @param __t A thing of arbitrary type.
  80. * @return The parameter cast to an rvalue-reference to allow moving it.
  81. */
  82. template<typename _Tp>
  83. constexpr typename std::remove_reference<_Tp>::type&&
  84. move(_Tp&& __t) noexcept
  85. { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
  86. template<typename _Tp>
  87. struct __move_if_noexcept_cond
  88. : public __and_<__not_<is_nothrow_move_constructible<_Tp>>,
  89. is_copy_constructible<_Tp>>::type { };
  90. /**
  91. * @brief Conditionally convert a value to an rvalue.
  92. * @param __x A thing of arbitrary type.
  93. * @return The parameter, possibly cast to an rvalue-reference.
  94. *
  95. * Same as std::move unless the type's move constructor could throw and the
  96. * type is copyable, in which case an lvalue-reference is returned instead.
  97. */
  98. template<typename _Tp>
  99. constexpr typename
  100. conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type
  101. move_if_noexcept(_Tp& __x) noexcept
  102. { return std::move(__x); }
  103. // declval, from type_traits.
  104. #if __cplusplus > 201402L
  105. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  106. // 2296. std::addressof should be constexpr
  107. # define __cpp_lib_addressof_constexpr 201603
  108. #endif
  109. /**
  110. * @brief Returns the actual address of the object or function
  111. * referenced by r, even in the presence of an overloaded
  112. * operator&.
  113. * @param __r Reference to an object or function.
  114. * @return The actual address.
  115. */
  116. template<typename _Tp>
  117. inline _GLIBCXX17_CONSTEXPR _Tp*
  118. addressof(_Tp& __r) noexcept
  119. { return std::__addressof(__r); }
  120. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  121. // 2598. addressof works on temporaries
  122. template<typename _Tp>
  123. const _Tp* addressof(const _Tp&&) = delete;
  124. // C++11 version of std::exchange for internal use.
  125. template <typename _Tp, typename _Up = _Tp>
  126. _GLIBCXX20_CONSTEXPR
  127. inline _Tp
  128. __exchange(_Tp& __obj, _Up&& __new_val)
  129. {
  130. _Tp __old_val = std::move(__obj);
  131. __obj = std::forward<_Up>(__new_val);
  132. return __old_val;
  133. }
  134. /// @} group utilities
  135. #define _GLIBCXX_MOVE(__val) std::move(__val)
  136. #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
  137. #else
  138. #define _GLIBCXX_MOVE(__val) (__val)
  139. #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
  140. #endif
  141. /**
  142. * @addtogroup utilities
  143. * @{
  144. */
  145. /**
  146. * @brief Swaps two values.
  147. * @param __a A thing of arbitrary type.
  148. * @param __b Another thing of arbitrary type.
  149. * @return Nothing.
  150. */
  151. template<typename _Tp>
  152. _GLIBCXX20_CONSTEXPR
  153. inline
  154. #if __cplusplus >= 201103L
  155. typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>,
  156. is_move_constructible<_Tp>,
  157. is_move_assignable<_Tp>>::value>::type
  158. #else
  159. void
  160. #endif
  161. swap(_Tp& __a, _Tp& __b)
  162. _GLIBCXX_NOEXCEPT_IF(__and_<is_nothrow_move_constructible<_Tp>,
  163. is_nothrow_move_assignable<_Tp>>::value)
  164. {
  165. #if __cplusplus < 201103L
  166. // concept requirements
  167. __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
  168. #endif
  169. _Tp __tmp = _GLIBCXX_MOVE(__a);
  170. __a = _GLIBCXX_MOVE(__b);
  171. __b = _GLIBCXX_MOVE(__tmp);
  172. }
  173. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  174. // DR 809. std::swap should be overloaded for array types.
  175. /// Swap the contents of two arrays.
  176. template<typename _Tp, size_t _Nm>
  177. _GLIBCXX20_CONSTEXPR
  178. inline
  179. #if __cplusplus >= 201103L
  180. typename enable_if<__is_swappable<_Tp>::value>::type
  181. #else
  182. void
  183. #endif
  184. swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
  185. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Tp>::value)
  186. {
  187. for (size_t __n = 0; __n < _Nm; ++__n)
  188. swap(__a[__n], __b[__n]);
  189. }
  190. /// @} group utilities
  191. _GLIBCXX_END_NAMESPACE_VERSION
  192. } // namespace
  193. #endif /* _MOVE_H */