libstdc++
|
00001 // Implementation of std::reference_wrapper -*- C++ -*- 00002 00003 // Copyright (C) 2004-2018 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/bits/refwrap.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{functional} 00028 */ 00029 00030 #ifndef _GLIBCXX_REFWRAP_H 00031 #define _GLIBCXX_REFWRAP_H 1 00032 00033 #pragma GCC system_header 00034 00035 #if __cplusplus < 201103L 00036 # include <bits/c++0x_warning.h> 00037 #else 00038 00039 #include <bits/move.h> 00040 #include <bits/invoke.h> 00041 #include <bits/stl_function.h> // for unary_function and binary_function 00042 00043 namespace std _GLIBCXX_VISIBILITY(default) 00044 { 00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00046 00047 /** 00048 * Derives from @c unary_function or @c binary_function, or perhaps 00049 * nothing, depending on the number of arguments provided. The 00050 * primary template is the basis case, which derives nothing. 00051 */ 00052 template<typename _Res, typename... _ArgTypes> 00053 struct _Maybe_unary_or_binary_function { }; 00054 00055 /// Derives from @c unary_function, as appropriate. 00056 template<typename _Res, typename _T1> 00057 struct _Maybe_unary_or_binary_function<_Res, _T1> 00058 : std::unary_function<_T1, _Res> { }; 00059 00060 /// Derives from @c binary_function, as appropriate. 00061 template<typename _Res, typename _T1, typename _T2> 00062 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> 00063 : std::binary_function<_T1, _T2, _Res> { }; 00064 00065 template<typename _Signature> 00066 struct _Mem_fn_traits; 00067 00068 template<typename _Res, typename _Class, typename... _ArgTypes> 00069 struct _Mem_fn_traits_base 00070 { 00071 using __result_type = _Res; 00072 using __maybe_type 00073 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>; 00074 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>; 00075 }; 00076 00077 #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \ 00078 template<typename _Res, typename _Class, typename... _ArgTypes> \ 00079 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \ 00080 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ 00081 { \ 00082 using __vararg = false_type; \ 00083 }; \ 00084 template<typename _Res, typename _Class, typename... _ArgTypes> \ 00085 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \ 00086 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ 00087 { \ 00088 using __vararg = true_type; \ 00089 }; 00090 00091 #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \ 00092 _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \ 00093 _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \ 00094 _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \ 00095 _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL) 00096 00097 _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type) 00098 _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type) 00099 _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) 00100 00101 #if __cplusplus > 201402L 00102 _GLIBCXX_MEM_FN_TRAITS(noexcept, true_type, true_type) 00103 _GLIBCXX_MEM_FN_TRAITS(& noexcept, true_type, false_type) 00104 _GLIBCXX_MEM_FN_TRAITS(&& noexcept, false_type, true_type) 00105 #endif 00106 00107 #undef _GLIBCXX_MEM_FN_TRAITS 00108 #undef _GLIBCXX_MEM_FN_TRAITS2 00109 00110 /// If we have found a result_type, extract it. 00111 template<typename _Functor, typename = __void_t<>> 00112 struct _Maybe_get_result_type 00113 { }; 00114 00115 template<typename _Functor> 00116 struct _Maybe_get_result_type<_Functor, 00117 __void_t<typename _Functor::result_type>> 00118 { typedef typename _Functor::result_type result_type; }; 00119 00120 /** 00121 * Base class for any function object that has a weak result type, as 00122 * defined in 20.8.2 [func.require] of C++11. 00123 */ 00124 template<typename _Functor> 00125 struct _Weak_result_type_impl 00126 : _Maybe_get_result_type<_Functor> 00127 { }; 00128 00129 /// Retrieve the result type for a function type. 00130 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM> 00131 struct _Weak_result_type_impl<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL> 00132 { typedef _Res result_type; }; 00133 00134 /// Retrieve the result type for a varargs function type. 00135 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM> 00136 struct _Weak_result_type_impl<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL> 00137 { typedef _Res result_type; }; 00138 00139 /// Retrieve the result type for a function pointer. 00140 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM> 00141 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL> 00142 { typedef _Res result_type; }; 00143 00144 /// Retrieve the result type for a varargs function pointer. 00145 template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM> 00146 struct 00147 _Weak_result_type_impl<_Res(*)(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL> 00148 { typedef _Res result_type; }; 00149 00150 // Let _Weak_result_type_impl perform the real work. 00151 template<typename _Functor, 00152 bool = is_member_function_pointer<_Functor>::value> 00153 struct _Weak_result_type_memfun 00154 : _Weak_result_type_impl<_Functor> 00155 { }; 00156 00157 // A pointer to member function has a weak result type. 00158 template<typename _MemFunPtr> 00159 struct _Weak_result_type_memfun<_MemFunPtr, true> 00160 { 00161 using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type; 00162 }; 00163 00164 // A pointer to data member doesn't have a weak result type. 00165 template<typename _Func, typename _Class> 00166 struct _Weak_result_type_memfun<_Func _Class::*, false> 00167 { }; 00168 00169 /** 00170 * Strip top-level cv-qualifiers from the function object and let 00171 * _Weak_result_type_memfun perform the real work. 00172 */ 00173 template<typename _Functor> 00174 struct _Weak_result_type 00175 : _Weak_result_type_memfun<typename remove_cv<_Functor>::type> 00176 { }; 00177 00178 // Detect nested argument_type. 00179 template<typename _Tp, typename = __void_t<>> 00180 struct _Refwrap_base_arg1 00181 { }; 00182 00183 // Nested argument_type. 00184 template<typename _Tp> 00185 struct _Refwrap_base_arg1<_Tp, 00186 __void_t<typename _Tp::argument_type>> 00187 { 00188 typedef typename _Tp::argument_type argument_type; 00189 }; 00190 00191 // Detect nested first_argument_type and second_argument_type. 00192 template<typename _Tp, typename = __void_t<>> 00193 struct _Refwrap_base_arg2 00194 { }; 00195 00196 // Nested first_argument_type and second_argument_type. 00197 template<typename _Tp> 00198 struct _Refwrap_base_arg2<_Tp, 00199 __void_t<typename _Tp::first_argument_type, 00200 typename _Tp::second_argument_type>> 00201 { 00202 typedef typename _Tp::first_argument_type first_argument_type; 00203 typedef typename _Tp::second_argument_type second_argument_type; 00204 }; 00205 00206 /** 00207 * Derives from unary_function or binary_function when it 00208 * can. Specializations handle all of the easy cases. The primary 00209 * template determines what to do with a class type, which may 00210 * derive from both unary_function and binary_function. 00211 */ 00212 template<typename _Tp> 00213 struct _Reference_wrapper_base 00214 : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp> 00215 { }; 00216 00217 // - a function type (unary) 00218 template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM> 00219 struct _Reference_wrapper_base<_Res(_T1) _GLIBCXX_NOEXCEPT_QUAL> 00220 : unary_function<_T1, _Res> 00221 { }; 00222 00223 template<typename _Res, typename _T1> 00224 struct _Reference_wrapper_base<_Res(_T1) const> 00225 : unary_function<_T1, _Res> 00226 { }; 00227 00228 template<typename _Res, typename _T1> 00229 struct _Reference_wrapper_base<_Res(_T1) volatile> 00230 : unary_function<_T1, _Res> 00231 { }; 00232 00233 template<typename _Res, typename _T1> 00234 struct _Reference_wrapper_base<_Res(_T1) const volatile> 00235 : unary_function<_T1, _Res> 00236 { }; 00237 00238 // - a function type (binary) 00239 template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM> 00240 struct _Reference_wrapper_base<_Res(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL> 00241 : binary_function<_T1, _T2, _Res> 00242 { }; 00243 00244 template<typename _Res, typename _T1, typename _T2> 00245 struct _Reference_wrapper_base<_Res(_T1, _T2) const> 00246 : binary_function<_T1, _T2, _Res> 00247 { }; 00248 00249 template<typename _Res, typename _T1, typename _T2> 00250 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile> 00251 : binary_function<_T1, _T2, _Res> 00252 { }; 00253 00254 template<typename _Res, typename _T1, typename _T2> 00255 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile> 00256 : binary_function<_T1, _T2, _Res> 00257 { }; 00258 00259 // - a function pointer type (unary) 00260 template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM> 00261 struct _Reference_wrapper_base<_Res(*)(_T1) _GLIBCXX_NOEXCEPT_QUAL> 00262 : unary_function<_T1, _Res> 00263 { }; 00264 00265 // - a function pointer type (binary) 00266 template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM> 00267 struct _Reference_wrapper_base<_Res(*)(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL> 00268 : binary_function<_T1, _T2, _Res> 00269 { }; 00270 00271 template<typename _Tp, bool = is_member_function_pointer<_Tp>::value> 00272 struct _Reference_wrapper_base_memfun 00273 : _Reference_wrapper_base<_Tp> 00274 { }; 00275 00276 template<typename _MemFunPtr> 00277 struct _Reference_wrapper_base_memfun<_MemFunPtr, true> 00278 : _Mem_fn_traits<_MemFunPtr>::__maybe_type 00279 { 00280 using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type; 00281 }; 00282 00283 /** 00284 * @brief Primary class template for reference_wrapper. 00285 * @ingroup functors 00286 * @{ 00287 */ 00288 template<typename _Tp> 00289 class reference_wrapper 00290 : public _Reference_wrapper_base_memfun<typename remove_cv<_Tp>::type> 00291 { 00292 _Tp* _M_data; 00293 00294 public: 00295 typedef _Tp type; 00296 00297 reference_wrapper(_Tp& __indata) noexcept 00298 : _M_data(std::__addressof(__indata)) 00299 { } 00300 00301 reference_wrapper(_Tp&&) = delete; 00302 00303 reference_wrapper(const reference_wrapper&) = default; 00304 00305 reference_wrapper& 00306 operator=(const reference_wrapper&) = default; 00307 00308 operator _Tp&() const noexcept 00309 { return this->get(); } 00310 00311 _Tp& 00312 get() const noexcept 00313 { return *_M_data; } 00314 00315 template<typename... _Args> 00316 typename result_of<_Tp&(_Args&&...)>::type 00317 operator()(_Args&&... __args) const 00318 { 00319 return std::__invoke(get(), std::forward<_Args>(__args)...); 00320 } 00321 }; 00322 00323 00324 /// Denotes a reference should be taken to a variable. 00325 template<typename _Tp> 00326 inline reference_wrapper<_Tp> 00327 ref(_Tp& __t) noexcept 00328 { return reference_wrapper<_Tp>(__t); } 00329 00330 /// Denotes a const reference should be taken to a variable. 00331 template<typename _Tp> 00332 inline reference_wrapper<const _Tp> 00333 cref(const _Tp& __t) noexcept 00334 { return reference_wrapper<const _Tp>(__t); } 00335 00336 template<typename _Tp> 00337 void ref(const _Tp&&) = delete; 00338 00339 template<typename _Tp> 00340 void cref(const _Tp&&) = delete; 00341 00342 /// std::ref overload to prevent wrapping a reference_wrapper 00343 template<typename _Tp> 00344 inline reference_wrapper<_Tp> 00345 ref(reference_wrapper<_Tp> __t) noexcept 00346 { return __t; } 00347 00348 /// std::cref overload to prevent wrapping a reference_wrapper 00349 template<typename _Tp> 00350 inline reference_wrapper<const _Tp> 00351 cref(reference_wrapper<_Tp> __t) noexcept 00352 { return { __t.get() }; } 00353 00354 // @} group functors 00355 00356 _GLIBCXX_END_NAMESPACE_VERSION 00357 } // namespace std 00358 00359 #endif // C++11 00360 00361 #endif // _GLIBCXX_REFWRAP_H