libstdc++
refwrap.h
00001 // Implementation of std::reference_wrapper -*- C++ -*-
00002 
00003 // Copyright (C) 2004-2017 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/bind.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   /// If we have found a result_type, extract it.
00048   template<typename _Functor, typename = __void_t<>>
00049     struct _Maybe_get_result_type
00050     { };
00051 
00052   template<typename _Functor>
00053     struct _Maybe_get_result_type<_Functor,
00054                                   __void_t<typename _Functor::result_type>>
00055     { typedef typename _Functor::result_type result_type; };
00056 
00057   /**
00058    *  Base class for any function object that has a weak result type, as
00059    *  defined in 20.8.2 [func.require] of C++11.
00060   */
00061   template<typename _Functor>
00062     struct _Weak_result_type_impl
00063     : _Maybe_get_result_type<_Functor>
00064     { };
00065 
00066   /// Retrieve the result type for a function type.
00067   template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
00068     struct _Weak_result_type_impl<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
00069     { typedef _Res result_type; };
00070 
00071   template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
00072     struct _Weak_result_type_impl<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL>
00073     { typedef _Res result_type; };
00074 
00075   template<typename _Res, typename... _ArgTypes>
00076     struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
00077     { typedef _Res result_type; };
00078 
00079   template<typename _Res, typename... _ArgTypes>
00080     struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
00081     { typedef _Res result_type; };
00082 
00083   template<typename _Res, typename... _ArgTypes>
00084     struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
00085     { typedef _Res result_type; };
00086 
00087   template<typename _Res, typename... _ArgTypes>
00088     struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
00089     { typedef _Res result_type; };
00090 
00091   template<typename _Res, typename... _ArgTypes>
00092     struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
00093     { typedef _Res result_type; };
00094 
00095   template<typename _Res, typename... _ArgTypes>
00096     struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
00097     { typedef _Res result_type; };
00098 
00099   /// Retrieve the result type for a function reference.
00100   template<typename _Res, typename... _ArgTypes>
00101     struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
00102     { typedef _Res result_type; };
00103 
00104   template<typename _Res, typename... _ArgTypes>
00105     struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
00106     { typedef _Res result_type; };
00107 
00108   /// Retrieve the result type for a function pointer.
00109   template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
00110     struct _Weak_result_type_impl<_Res(*)(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL>
00111     { typedef _Res result_type; };
00112 
00113   template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
00114     struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)
00115                                   _GLIBCXX_NOEXCEPT_QUAL>
00116     { typedef _Res result_type; };
00117 
00118   /// Retrieve result type for a member function pointer.
00119   template<typename _Res, typename _Class, typename... _ArgTypes
00120            _GLIBCXX_NOEXCEPT_PARM>
00121     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
00122                                   _GLIBCXX_NOEXCEPT_QUAL>
00123     { typedef _Res result_type; };
00124 
00125   template<typename _Res, typename _Class, typename... _ArgTypes
00126            _GLIBCXX_NOEXCEPT_PARM>
00127     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
00128                                   _GLIBCXX_NOEXCEPT_QUAL>
00129     { typedef _Res result_type; };
00130 
00131   /// Retrieve result type for a const member function pointer.
00132   template<typename _Res, typename _Class, typename... _ArgTypes
00133            _GLIBCXX_NOEXCEPT_PARM>
00134     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const
00135                                   _GLIBCXX_NOEXCEPT_QUAL>
00136     { typedef _Res result_type; };
00137 
00138   template<typename _Res, typename _Class, typename... _ArgTypes
00139            _GLIBCXX_NOEXCEPT_PARM>
00140     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const
00141                                   _GLIBCXX_NOEXCEPT_QUAL>
00142     { typedef _Res result_type; };
00143 
00144   /// Retrieve result type for a volatile member function pointer.
00145   template<typename _Res, typename _Class, typename... _ArgTypes
00146            _GLIBCXX_NOEXCEPT_PARM>
00147     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile
00148                                   _GLIBCXX_NOEXCEPT_QUAL>
00149     { typedef _Res result_type; };
00150 
00151   template<typename _Res, typename _Class, typename... _ArgTypes
00152            _GLIBCXX_NOEXCEPT_PARM>
00153     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile
00154                                   _GLIBCXX_NOEXCEPT_QUAL>
00155     { typedef _Res result_type; };
00156 
00157   /// Retrieve result type for a const volatile member function pointer.
00158   template<typename _Res, typename _Class, typename... _ArgTypes
00159            _GLIBCXX_NOEXCEPT_PARM>
00160     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
00161                                   const volatile _GLIBCXX_NOEXCEPT_QUAL>
00162     { typedef _Res result_type; };
00163 
00164   template<typename _Res, typename _Class, typename... _ArgTypes
00165            _GLIBCXX_NOEXCEPT_PARM>
00166     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
00167                                   const volatile _GLIBCXX_NOEXCEPT_QUAL>
00168     { typedef _Res result_type; };
00169 
00170   /**
00171    *  Strip top-level cv-qualifiers from the function object and let
00172    *  _Weak_result_type_impl perform the real work.
00173   */
00174   template<typename _Functor>
00175     struct _Weak_result_type
00176     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
00177     { };
00178 
00179   // Detect nested argument_type.
00180   template<typename _Tp, typename = __void_t<>>
00181     struct _Refwrap_base_arg1
00182     { };
00183 
00184   // Nested argument_type.
00185   template<typename _Tp>
00186     struct _Refwrap_base_arg1<_Tp,
00187                               __void_t<typename _Tp::argument_type>>
00188     {
00189       typedef typename _Tp::argument_type argument_type;
00190     };
00191 
00192   // Detect nested first_argument_type and second_argument_type.
00193   template<typename _Tp, typename = __void_t<>>
00194     struct _Refwrap_base_arg2
00195     { };
00196 
00197   // Nested first_argument_type and second_argument_type.
00198   template<typename _Tp>
00199     struct _Refwrap_base_arg2<_Tp,
00200                               __void_t<typename _Tp::first_argument_type,
00201                                        typename _Tp::second_argument_type>>
00202     {
00203       typedef typename _Tp::first_argument_type first_argument_type;
00204       typedef typename _Tp::second_argument_type second_argument_type;
00205     };
00206 
00207   /**
00208    *  Derives from unary_function or binary_function when it
00209    *  can. Specializations handle all of the easy cases. The primary
00210    *  template determines what to do with a class type, which may
00211    *  derive from both unary_function and binary_function.
00212   */
00213   template<typename _Tp>
00214     struct _Reference_wrapper_base
00215     : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp>
00216     { };
00217 
00218   // - a function type (unary)
00219   template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
00220     struct _Reference_wrapper_base<_Res(_T1) _GLIBCXX_NOEXCEPT_QUAL>
00221     : unary_function<_T1, _Res>
00222     { };
00223 
00224   template<typename _Res, typename _T1>
00225     struct _Reference_wrapper_base<_Res(_T1) const>
00226     : unary_function<_T1, _Res>
00227     { };
00228 
00229   template<typename _Res, typename _T1>
00230     struct _Reference_wrapper_base<_Res(_T1) volatile>
00231     : unary_function<_T1, _Res>
00232     { };
00233 
00234   template<typename _Res, typename _T1>
00235     struct _Reference_wrapper_base<_Res(_T1) const volatile>
00236     : unary_function<_T1, _Res>
00237     { };
00238 
00239   // - a function type (binary)
00240   template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
00241     struct _Reference_wrapper_base<_Res(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
00242     : binary_function<_T1, _T2, _Res>
00243     { };
00244 
00245   template<typename _Res, typename _T1, typename _T2>
00246     struct _Reference_wrapper_base<_Res(_T1, _T2) const>
00247     : binary_function<_T1, _T2, _Res>
00248     { };
00249 
00250   template<typename _Res, typename _T1, typename _T2>
00251     struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
00252     : binary_function<_T1, _T2, _Res>
00253     { };
00254 
00255   template<typename _Res, typename _T1, typename _T2>
00256     struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
00257     : binary_function<_T1, _T2, _Res>
00258     { };
00259 
00260   // - a function pointer type (unary)
00261   template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
00262     struct _Reference_wrapper_base<_Res(*)(_T1) _GLIBCXX_NOEXCEPT_QUAL>
00263     : unary_function<_T1, _Res>
00264     { };
00265 
00266   // - a function pointer type (binary)
00267   template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
00268     struct _Reference_wrapper_base<_Res(*)(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL>
00269     : binary_function<_T1, _T2, _Res>
00270     { };
00271 
00272   // - a pointer to member function type (unary, no qualifiers)
00273   template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
00274     struct _Reference_wrapper_base<_Res (_T1::*)() _GLIBCXX_NOEXCEPT_QUAL>
00275     : unary_function<_T1*, _Res>
00276     { };
00277 
00278   // - a pointer to member function type (binary, no qualifiers)
00279   template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
00280     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) _GLIBCXX_NOEXCEPT_QUAL>
00281     : binary_function<_T1*, _T2, _Res>
00282     { };
00283 
00284   // - a pointer to member function type (unary, const)
00285   template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
00286     struct _Reference_wrapper_base<_Res (_T1::*)() const _GLIBCXX_NOEXCEPT_QUAL>
00287     : unary_function<const _T1*, _Res>
00288     { };
00289 
00290   // - a pointer to member function type (binary, const)
00291   template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
00292     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const _GLIBCXX_NOEXCEPT_QUAL>
00293     : binary_function<const _T1*, _T2, _Res>
00294     { };
00295 
00296   // - a pointer to member function type (unary, volatile)
00297   template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
00298     struct _Reference_wrapper_base<_Res (_T1::*)() volatile _GLIBCXX_NOEXCEPT_QUAL>
00299     : unary_function<volatile _T1*, _Res>
00300     { };
00301 
00302   // - a pointer to member function type (binary, volatile)
00303   template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
00304     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile _GLIBCXX_NOEXCEPT_QUAL>
00305     : binary_function<volatile _T1*, _T2, _Res>
00306     { };
00307 
00308   // - a pointer to member function type (unary, const volatile)
00309   template<typename _Res, typename _T1 _GLIBCXX_NOEXCEPT_PARM>
00310     struct _Reference_wrapper_base<_Res (_T1::*)() const volatile _GLIBCXX_NOEXCEPT_QUAL>
00311     : unary_function<const volatile _T1*, _Res>
00312     { };
00313 
00314   // - a pointer to member function type (binary, const volatile)
00315   template<typename _Res, typename _T1, typename _T2 _GLIBCXX_NOEXCEPT_PARM>
00316     struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile _GLIBCXX_NOEXCEPT_QUAL>
00317     : binary_function<const volatile _T1*, _T2, _Res>
00318     { };
00319 
00320   /**
00321    *  @brief Primary class template for reference_wrapper.
00322    *  @ingroup functors
00323    *  @{
00324    */
00325   template<typename _Tp>
00326     class reference_wrapper
00327     : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
00328     {
00329       _Tp* _M_data;
00330 
00331     public:
00332       typedef _Tp type;
00333 
00334       reference_wrapper(_Tp& __indata) noexcept
00335       : _M_data(std::__addressof(__indata))
00336       { }
00337 
00338       reference_wrapper(_Tp&&) = delete;
00339 
00340       reference_wrapper(const reference_wrapper&) = default;
00341 
00342       reference_wrapper&
00343       operator=(const reference_wrapper&) = default;
00344 
00345       operator _Tp&() const noexcept
00346       { return this->get(); }
00347 
00348       _Tp&
00349       get() const noexcept
00350       { return *_M_data; }
00351 
00352       template<typename... _Args>
00353         typename result_of<_Tp&(_Args&&...)>::type
00354         operator()(_Args&&... __args) const
00355         {
00356           return std::__invoke(get(), std::forward<_Args>(__args)...);
00357         }
00358     };
00359 
00360 
00361   /// Denotes a reference should be taken to a variable.
00362   template<typename _Tp>
00363     inline reference_wrapper<_Tp>
00364     ref(_Tp& __t) noexcept
00365     { return reference_wrapper<_Tp>(__t); }
00366 
00367   /// Denotes a const reference should be taken to a variable.
00368   template<typename _Tp>
00369     inline reference_wrapper<const _Tp>
00370     cref(const _Tp& __t) noexcept
00371     { return reference_wrapper<const _Tp>(__t); }
00372 
00373   template<typename _Tp>
00374     void ref(const _Tp&&) = delete;
00375 
00376   template<typename _Tp>
00377     void cref(const _Tp&&) = delete;
00378 
00379   /// Partial specialization.
00380   template<typename _Tp>
00381     inline reference_wrapper<_Tp>
00382     ref(reference_wrapper<_Tp> __t) noexcept
00383     { return ref(__t.get()); }
00384 
00385   /// Partial specialization.
00386   template<typename _Tp>
00387     inline reference_wrapper<const _Tp>
00388     cref(reference_wrapper<_Tp> __t) noexcept
00389     { return cref(__t.get()); }
00390 
00391   // @} group functors
00392 
00393 _GLIBCXX_END_NAMESPACE_VERSION
00394 } // namespace std
00395 
00396 #endif // C++11
00397 
00398 #endif // _GLIBCXX_REFWRAP_H