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.

402 lines
13KB

  1. // Implementation of std::reference_wrapper -*- C++ -*-
  2. // Copyright (C) 2004-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 include/bits/refwrap.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{functional}
  23. */
  24. #ifndef _GLIBCXX_REFWRAP_H
  25. #define _GLIBCXX_REFWRAP_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <bits/move.h>
  31. #include <bits/invoke.h>
  32. #include <bits/stl_function.h> // for unary_function and binary_function
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. /// @cond undocumented
  37. /**
  38. * Derives from @c unary_function or @c binary_function, or perhaps
  39. * nothing, depending on the number of arguments provided. The
  40. * primary template is the basis case, which derives nothing.
  41. */
  42. template<typename _Res, typename... _ArgTypes>
  43. struct _Maybe_unary_or_binary_function { };
  44. /// Derives from @c unary_function, as appropriate.
  45. template<typename _Res, typename _T1>
  46. struct _Maybe_unary_or_binary_function<_Res, _T1>
  47. : std::unary_function<_T1, _Res> { };
  48. /// Derives from @c binary_function, as appropriate.
  49. template<typename _Res, typename _T1, typename _T2>
  50. struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
  51. : std::binary_function<_T1, _T2, _Res> { };
  52. template<typename _Signature>
  53. struct _Mem_fn_traits;
  54. template<typename _Res, typename _Class, typename... _ArgTypes>
  55. struct _Mem_fn_traits_base
  56. {
  57. using __result_type = _Res;
  58. using __maybe_type
  59. = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
  60. using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
  61. };
  62. #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
  63. template<typename _Res, typename _Class, typename... _ArgTypes> \
  64. struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
  65. : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
  66. { \
  67. using __vararg = false_type; \
  68. }; \
  69. template<typename _Res, typename _Class, typename... _ArgTypes> \
  70. struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
  71. : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
  72. { \
  73. using __vararg = true_type; \
  74. };
  75. #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
  76. _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
  77. _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
  78. _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
  79. _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
  80. _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
  81. _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
  82. _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
  83. #if __cplusplus > 201402L
  84. _GLIBCXX_MEM_FN_TRAITS(noexcept, true_type, true_type)
  85. _GLIBCXX_MEM_FN_TRAITS(& noexcept, true_type, false_type)
  86. _GLIBCXX_MEM_FN_TRAITS(&& noexcept, false_type, true_type)
  87. #endif
  88. #undef _GLIBCXX_MEM_FN_TRAITS
  89. #undef _GLIBCXX_MEM_FN_TRAITS2
  90. /// If we have found a result_type, extract it.
  91. template<typename _Functor, typename = __void_t<>>
  92. struct _Maybe_get_result_type
  93. { };
  94. template<typename _Functor>
  95. struct _Maybe_get_result_type<_Functor,
  96. __void_t<typename _Functor::result_type>>
  97. { typedef typename _Functor::result_type result_type; };
  98. /**
  99. * Base class for any function object that has a weak result type, as
  100. * defined in 20.8.2 [func.require] of C++11.
  101. */
  102. template<typename _Functor>
  103. struct _Weak_result_type_impl
  104. : _Maybe_get_result_type<_Functor>
  105. { };
  106. /// Retrieve the result type for a function type.
  107. template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  108. struct _Weak_result_type_impl<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
  109. { typedef _Res result_type; };
  110. /// Retrieve the result type for a varargs function type.
  111. template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  112. struct _Weak_result_type_impl<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
  113. { typedef _Res result_type; };
  114. /// Retrieve the result type for a function pointer.
  115. template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  116. struct _Weak_result_type_impl<_Res(*)(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
  117. { typedef _Res result_type; };
  118. /// Retrieve the result type for a varargs function pointer.
  119. template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  120. struct
  121. _Weak_result_type_impl<_Res(*)(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
  122. { typedef _Res result_type; };
  123. // Let _Weak_result_type_impl perform the real work.
  124. template<typename _Functor,
  125. bool = is_member_function_pointer<_Functor>::value>
  126. struct _Weak_result_type_memfun
  127. : _Weak_result_type_impl<_Functor>
  128. { };
  129. // A pointer to member function has a weak result type.
  130. template<typename _MemFunPtr>
  131. struct _Weak_result_type_memfun<_MemFunPtr, true>
  132. {
  133. using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type;
  134. };
  135. // A pointer to data member doesn't have a weak result type.
  136. template<typename _Func, typename _Class>
  137. struct _Weak_result_type_memfun<_Func _Class::*, false>
  138. { };
  139. /**
  140. * Strip top-level cv-qualifiers from the function object and let
  141. * _Weak_result_type_memfun perform the real work.
  142. */
  143. template<typename _Functor>
  144. struct _Weak_result_type
  145. : _Weak_result_type_memfun<typename remove_cv<_Functor>::type>
  146. { };
  147. #if __cplusplus <= 201703L
  148. // Detect nested argument_type.
  149. template<typename _Tp, typename = __void_t<>>
  150. struct _Refwrap_base_arg1
  151. { };
  152. // Nested argument_type.
  153. template<typename _Tp>
  154. struct _Refwrap_base_arg1<_Tp,
  155. __void_t<typename _Tp::argument_type>>
  156. {
  157. typedef typename _Tp::argument_type argument_type;
  158. };
  159. // Detect nested first_argument_type and second_argument_type.
  160. template<typename _Tp, typename = __void_t<>>
  161. struct _Refwrap_base_arg2
  162. { };
  163. // Nested first_argument_type and second_argument_type.
  164. template<typename _Tp>
  165. struct _Refwrap_base_arg2<_Tp,
  166. __void_t<typename _Tp::first_argument_type,
  167. typename _Tp::second_argument_type>>
  168. {
  169. typedef typename _Tp::first_argument_type first_argument_type;
  170. typedef typename _Tp::second_argument_type second_argument_type;
  171. };
  172. /**
  173. * Derives from unary_function or binary_function when it
  174. * can. Specializations handle all of the easy cases. The primary
  175. * template determines what to do with a class type, which may
  176. * derive from both unary_function and binary_function.
  177. */
  178. template<typename _Tp>
  179. struct _Reference_wrapper_base
  180. : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp>
  181. { };
  182. // - a function type (unary)
  183. template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
  184. struct _Reference_wrapper_base<_Res(_T1) _GLIBCXX_NOEXCEPT_QUAL>
  185. : unary_function<_T1, _Res>
  186. { };
  187. template<typename _Res, typename _T1>
  188. struct _Reference_wrapper_base<_Res(_T1) const>
  189. : unary_function<_T1, _Res>
  190. { };
  191. template<typename _Res, typename _T1>
  192. struct _Reference_wrapper_base<_Res(_T1) volatile>
  193. : unary_function<_T1, _Res>
  194. { };
  195. template<typename _Res, typename _T1>
  196. struct _Reference_wrapper_base<_Res(_T1) const volatile>
  197. : unary_function<_T1, _Res>
  198. { };
  199. // - a function type (binary)
  200. template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
  201. struct _Reference_wrapper_base<_Res(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
  202. : binary_function<_T1, _T2, _Res>
  203. { };
  204. template<typename _Res, typename _T1, typename _T2>
  205. struct _Reference_wrapper_base<_Res(_T1, _T2) const>
  206. : binary_function<_T1, _T2, _Res>
  207. { };
  208. template<typename _Res, typename _T1, typename _T2>
  209. struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
  210. : binary_function<_T1, _T2, _Res>
  211. { };
  212. template<typename _Res, typename _T1, typename _T2>
  213. struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
  214. : binary_function<_T1, _T2, _Res>
  215. { };
  216. // - a function pointer type (unary)
  217. template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
  218. struct _Reference_wrapper_base<_Res(*)(_T1) _GLIBCXX_NOEXCEPT_QUAL>
  219. : unary_function<_T1, _Res>
  220. { };
  221. // - a function pointer type (binary)
  222. template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
  223. struct _Reference_wrapper_base<_Res(*)(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
  224. : binary_function<_T1, _T2, _Res>
  225. { };
  226. template<typename _Tp, bool = is_member_function_pointer<_Tp>::value>
  227. struct _Reference_wrapper_base_memfun
  228. : _Reference_wrapper_base<_Tp>
  229. { };
  230. template<typename _MemFunPtr>
  231. struct _Reference_wrapper_base_memfun<_MemFunPtr, true>
  232. : _Mem_fn_traits<_MemFunPtr>::__maybe_type
  233. {
  234. using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type;
  235. };
  236. #endif // ! C++20
  237. /// @endcond
  238. /**
  239. * @brief Primary class template for reference_wrapper.
  240. * @ingroup functors
  241. */
  242. template<typename _Tp>
  243. class reference_wrapper
  244. #if __cplusplus <= 201703L
  245. // In C++20 std::reference_wrapper<T> allows T to be incomplete,
  246. // so checking for nested types could result in ODR violations.
  247. : public _Reference_wrapper_base_memfun<typename remove_cv<_Tp>::type>
  248. #endif
  249. {
  250. _Tp* _M_data;
  251. _GLIBCXX20_CONSTEXPR
  252. static _Tp* _S_fun(_Tp& __r) noexcept { return std::__addressof(__r); }
  253. static void _S_fun(_Tp&&) = delete;
  254. template<typename _Up, typename _Up2 = __remove_cvref_t<_Up>>
  255. using __not_same
  256. = typename enable_if<!is_same<reference_wrapper, _Up2>::value>::type;
  257. public:
  258. typedef _Tp type;
  259. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  260. // 2993. reference_wrapper<T> conversion from T&&
  261. // 3041. Unnecessary decay in reference_wrapper
  262. template<typename _Up, typename = __not_same<_Up>, typename
  263. = decltype(reference_wrapper::_S_fun(std::declval<_Up>()))>
  264. _GLIBCXX20_CONSTEXPR
  265. reference_wrapper(_Up&& __uref)
  266. noexcept(noexcept(reference_wrapper::_S_fun(std::declval<_Up>())))
  267. : _M_data(reference_wrapper::_S_fun(std::forward<_Up>(__uref)))
  268. { }
  269. reference_wrapper(const reference_wrapper&) = default;
  270. reference_wrapper&
  271. operator=(const reference_wrapper&) = default;
  272. _GLIBCXX20_CONSTEXPR
  273. operator _Tp&() const noexcept
  274. { return this->get(); }
  275. _GLIBCXX20_CONSTEXPR
  276. _Tp&
  277. get() const noexcept
  278. { return *_M_data; }
  279. template<typename... _Args>
  280. _GLIBCXX20_CONSTEXPR
  281. typename result_of<_Tp&(_Args&&...)>::type
  282. operator()(_Args&&... __args) const
  283. {
  284. #if __cplusplus > 201703L
  285. if constexpr (is_object_v<type>)
  286. static_assert(sizeof(type), "type must be complete");
  287. #endif
  288. return std::__invoke(get(), std::forward<_Args>(__args)...);
  289. }
  290. };
  291. #if __cpp_deduction_guides
  292. template<typename _Tp>
  293. reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
  294. #endif
  295. /// @relates reference_wrapper @{
  296. /// Denotes a reference should be taken to a variable.
  297. template<typename _Tp>
  298. _GLIBCXX20_CONSTEXPR
  299. inline reference_wrapper<_Tp>
  300. ref(_Tp& __t) noexcept
  301. { return reference_wrapper<_Tp>(__t); }
  302. /// Denotes a const reference should be taken to a variable.
  303. template<typename _Tp>
  304. _GLIBCXX20_CONSTEXPR
  305. inline reference_wrapper<const _Tp>
  306. cref(const _Tp& __t) noexcept
  307. { return reference_wrapper<const _Tp>(__t); }
  308. template<typename _Tp>
  309. void ref(const _Tp&&) = delete;
  310. template<typename _Tp>
  311. void cref(const _Tp&&) = delete;
  312. /// std::ref overload to prevent wrapping a reference_wrapper
  313. template<typename _Tp>
  314. _GLIBCXX20_CONSTEXPR
  315. inline reference_wrapper<_Tp>
  316. ref(reference_wrapper<_Tp> __t) noexcept
  317. { return __t; }
  318. /// std::cref overload to prevent wrapping a reference_wrapper
  319. template<typename _Tp>
  320. _GLIBCXX20_CONSTEXPR
  321. inline reference_wrapper<const _Tp>
  322. cref(reference_wrapper<_Tp> __t) noexcept
  323. { return { __t.get() }; }
  324. // @}
  325. _GLIBCXX_END_NAMESPACE_VERSION
  326. } // namespace std
  327. #endif // C++11
  328. #endif // _GLIBCXX_REFWRAP_H