libstdc++
|
00001 // Move, forward and identity for C++0x + swap -*- C++ -*- 00002 00003 // Copyright (C) 2007-2015 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 bits/move.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{utility} 00028 */ 00029 00030 #ifndef _MOVE_H 00031 #define _MOVE_H 1 00032 00033 #include <bits/c++config.h> 00034 #include <bits/concept_check.h> 00035 00036 namespace std _GLIBCXX_VISIBILITY(default) 00037 { 00038 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00039 00040 // Used, in C++03 mode too, by allocators, etc. 00041 /** 00042 * @brief Same as C++11 std::addressof 00043 * @ingroup utilities 00044 */ 00045 template<typename _Tp> 00046 inline _Tp* 00047 __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT 00048 { 00049 return reinterpret_cast<_Tp*> 00050 (&const_cast<char&>(reinterpret_cast<const volatile char&>(__r))); 00051 } 00052 00053 _GLIBCXX_END_NAMESPACE_VERSION 00054 } // namespace 00055 00056 #if __cplusplus >= 201103L 00057 #include <type_traits> // Brings in std::declval too. 00058 00059 namespace std _GLIBCXX_VISIBILITY(default) 00060 { 00061 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00062 00063 /** 00064 * @addtogroup utilities 00065 * @{ 00066 */ 00067 00068 /** 00069 * @brief Forward an lvalue. 00070 * @return The parameter cast to the specified type. 00071 * 00072 * This function is used to implement "perfect forwarding". 00073 */ 00074 template<typename _Tp> 00075 constexpr _Tp&& 00076 forward(typename std::remove_reference<_Tp>::type& __t) noexcept 00077 { return static_cast<_Tp&&>(__t); } 00078 00079 /** 00080 * @brief Forward an rvalue. 00081 * @return The parameter cast to the specified type. 00082 * 00083 * This function is used to implement "perfect forwarding". 00084 */ 00085 template<typename _Tp> 00086 constexpr _Tp&& 00087 forward(typename std::remove_reference<_Tp>::type&& __t) noexcept 00088 { 00089 static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument" 00090 " substituting _Tp is an lvalue reference type"); 00091 return static_cast<_Tp&&>(__t); 00092 } 00093 00094 /** 00095 * @brief Convert a value to an rvalue. 00096 * @param __t A thing of arbitrary type. 00097 * @return The parameter cast to an rvalue-reference to allow moving it. 00098 */ 00099 template<typename _Tp> 00100 constexpr typename std::remove_reference<_Tp>::type&& 00101 move(_Tp&& __t) noexcept 00102 { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } 00103 00104 00105 template<typename _Tp> 00106 struct __move_if_noexcept_cond 00107 : public __and_<__not_<is_nothrow_move_constructible<_Tp>>, 00108 is_copy_constructible<_Tp>>::type { }; 00109 00110 /** 00111 * @brief Conditionally convert a value to an rvalue. 00112 * @param __x A thing of arbitrary type. 00113 * @return The parameter, possibly cast to an rvalue-reference. 00114 * 00115 * Same as std::move unless the type's move constructor could throw and the 00116 * type is copyable, in which case an lvalue-reference is returned instead. 00117 */ 00118 template<typename _Tp> 00119 constexpr typename 00120 conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type 00121 move_if_noexcept(_Tp& __x) noexcept 00122 { return std::move(__x); } 00123 00124 // declval, from type_traits. 00125 00126 /** 00127 * @brief Returns the actual address of the object or function 00128 * referenced by r, even in the presence of an overloaded 00129 * operator&. 00130 * @param __r Reference to an object or function. 00131 * @return The actual address. 00132 */ 00133 template<typename _Tp> 00134 inline _Tp* 00135 addressof(_Tp& __r) noexcept 00136 { return std::__addressof(__r); } 00137 00138 // C++11 version of std::exchange for internal use. 00139 template <typename _Tp, typename _Up = _Tp> 00140 inline _Tp 00141 __exchange(_Tp& __obj, _Up&& __new_val) 00142 { 00143 _Tp __old_val = std::move(__obj); 00144 __obj = std::forward<_Up>(__new_val); 00145 return __old_val; 00146 } 00147 00148 /// @} group utilities 00149 _GLIBCXX_END_NAMESPACE_VERSION 00150 } // namespace 00151 00152 #define _GLIBCXX_MOVE(__val) std::move(__val) 00153 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) 00154 #else 00155 #define _GLIBCXX_MOVE(__val) (__val) 00156 #define _GLIBCXX_FORWARD(_Tp, __val) (__val) 00157 #endif 00158 00159 namespace std _GLIBCXX_VISIBILITY(default) 00160 { 00161 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00162 00163 /** 00164 * @addtogroup utilities 00165 * @{ 00166 */ 00167 00168 /** 00169 * @brief Swaps two values. 00170 * @param __a A thing of arbitrary type. 00171 * @param __b Another thing of arbitrary type. 00172 * @return Nothing. 00173 */ 00174 template<typename _Tp> 00175 inline void 00176 swap(_Tp& __a, _Tp& __b) 00177 #if __cplusplus >= 201103L 00178 noexcept(__and_<is_nothrow_move_constructible<_Tp>, 00179 is_nothrow_move_assignable<_Tp>>::value) 00180 #endif 00181 { 00182 // concept requirements 00183 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) 00184 00185 _Tp __tmp = _GLIBCXX_MOVE(__a); 00186 __a = _GLIBCXX_MOVE(__b); 00187 __b = _GLIBCXX_MOVE(__tmp); 00188 } 00189 00190 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00191 // DR 809. std::swap should be overloaded for array types. 00192 /// Swap the contents of two arrays. 00193 template<typename _Tp, size_t _Nm> 00194 inline void 00195 swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) 00196 #if __cplusplus >= 201103L 00197 noexcept(noexcept(swap(*__a, *__b))) 00198 #endif 00199 { 00200 for (size_t __n = 0; __n < _Nm; ++__n) 00201 swap(__a[__n], __b[__n]); 00202 } 00203 00204 /// @} group utilities 00205 _GLIBCXX_END_NAMESPACE_VERSION 00206 } // namespace 00207 00208 #endif /* _MOVE_H */