move.h

Go to the documentation of this file.
00001 // Move, forward and identity for C++0x + swap -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008, 2009, 2010 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 move.h
00026  *  This is an internal header file, included by other library headers.
00027  *  You should not attempt to use it directly.
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 _GLIBCXX_BEGIN_NAMESPACE(std)
00037 
00038   // Used, in C++03 mode too, by allocators, etc.
00039   template<typename _Tp>
00040     inline _Tp*
00041     __addressof(_Tp& __r)
00042     {
00043       return reinterpret_cast<_Tp*>
00044     (&const_cast<char&>(reinterpret_cast<const volatile char&>(__r)));
00045     }
00046 
00047 _GLIBCXX_END_NAMESPACE
00048 
00049 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00050 #include <type_traits> // Brings in std::declval too.
00051 
00052 _GLIBCXX_BEGIN_NAMESPACE(std)
00053   
00054   /// forward (as per N2835)
00055   /// Forward lvalues as rvalues.
00056   template<typename _Tp>
00057     inline typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp&&>::type
00058     forward(typename std::common_type<_Tp>::type& __t)
00059     { return static_cast<_Tp&&>(__t); }
00060 
00061   /// Forward rvalues as rvalues.
00062   template<typename _Tp>
00063     inline typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp&&>::type
00064     forward(typename std::common_type<_Tp>::type&& __t)
00065     { return static_cast<_Tp&&>(__t); }
00066 
00067   // Forward lvalues as lvalues.
00068   template<typename _Tp>
00069     inline typename enable_if<is_lvalue_reference<_Tp>::value, _Tp>::type
00070     forward(typename std::common_type<_Tp>::type __t)
00071     { return __t; }
00072 
00073   // Prevent forwarding rvalues as const lvalues.
00074   template<typename _Tp>
00075     inline typename enable_if<is_lvalue_reference<_Tp>::value, _Tp>::type
00076     forward(typename std::remove_reference<_Tp>::type&& __t) = delete;
00077 
00078   /**
00079    *  @brief Move a value.
00080    *  @ingroup mutating_algorithms
00081    *  @param  __t  A thing of arbitrary type.
00082    *  @return Same, moved.
00083   */
00084   template<typename _Tp>
00085     inline typename std::remove_reference<_Tp>::type&&
00086     move(_Tp&& __t)
00087     { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
00088 
00089   /// declval, from type_traits.
00090 
00091   /**
00092    *  @brief Returns the actual address of the object or function
00093    *         referenced by r, even in the presence of an overloaded
00094    *         operator&.
00095    *  @param  __r  Reference to an object or function.
00096    *  @return   The actual address.
00097   */
00098   template<typename _Tp>
00099     inline _Tp*
00100     addressof(_Tp& __r)
00101     { return std::__addressof(__r); }
00102 
00103 _GLIBCXX_END_NAMESPACE
00104 
00105 #define _GLIBCXX_MOVE(__val) std::move(__val)
00106 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
00107 #else
00108 #define _GLIBCXX_MOVE(__val) (__val)
00109 #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
00110 #endif
00111 
00112 _GLIBCXX_BEGIN_NAMESPACE(std)
00113 
00114   /**
00115    *  @brief Swaps two values.
00116    *  @ingroup mutating_algorithms
00117    *  @param  __a  A thing of arbitrary type.
00118    *  @param  __b  Another thing of arbitrary type.
00119    *  @return   Nothing.
00120   */
00121   template<typename _Tp>
00122     inline void
00123     swap(_Tp& __a, _Tp& __b)
00124     {
00125       // concept requirements
00126       __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
00127 
00128       _Tp __tmp = _GLIBCXX_MOVE(__a);
00129       __a = _GLIBCXX_MOVE(__b);
00130       __b = _GLIBCXX_MOVE(__tmp);
00131     }
00132 
00133   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00134   // DR 809. std::swap should be overloaded for array types.
00135   template<typename _Tp, size_t _Nm>
00136     inline void
00137     swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
00138     {
00139       for (size_t __n = 0; __n < _Nm; ++__n)
00140     swap(__a[__n], __b[__n]);
00141     }
00142 
00143 _GLIBCXX_END_NAMESPACE
00144 
00145 #endif /* _MOVE_H */