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   /// identity
00055   template<typename _Tp>
00056     struct identity
00057     {
00058       typedef _Tp type;
00059     };
00060 
00061   /// forward (as per N2835)
00062   /// Forward lvalues as rvalues.
00063   template<typename _Tp>
00064     inline typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp&&>::type
00065     forward(typename std::identity<_Tp>::type& __t)
00066     { return static_cast<_Tp&&>(__t); }
00067 
00068   /// Forward rvalues as rvalues.
00069   template<typename _Tp>
00070     inline typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp&&>::type
00071     forward(typename std::identity<_Tp>::type&& __t)
00072     { return static_cast<_Tp&&>(__t); }
00073 
00074   // Forward lvalues as lvalues.
00075   template<typename _Tp>
00076     inline typename enable_if<is_lvalue_reference<_Tp>::value, _Tp>::type
00077     forward(typename std::identity<_Tp>::type __t)
00078     { return __t; }
00079 
00080   // Prevent forwarding rvalues as const lvalues.
00081   template<typename _Tp>
00082     inline typename enable_if<is_lvalue_reference<_Tp>::value, _Tp>::type
00083     forward(typename std::remove_reference<_Tp>::type&& __t) = delete;
00084 
00085   /**
00086    *  @brief Move a value.
00087    *  @ingroup mutating_algorithms
00088    *  @param  __t  A thing of arbitrary type.
00089    *  @return Same, moved.
00090   */
00091   template<typename _Tp>
00092     inline typename std::remove_reference<_Tp>::type&&
00093     move(_Tp&& __t)
00094     { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
00095 
00096   /// declval, from type_traits.
00097 
00098   /**
00099    *  @brief Returns the actual address of the object or function
00100    *         referenced by r, even in the presence of an overloaded
00101    *         operator&.
00102    *  @param  __r  Reference to an object or function.
00103    *  @return   The actual address.
00104   */
00105   template<typename _Tp>
00106     inline _Tp*
00107     addressof(_Tp& __r)
00108     { return std::__addressof(__r); }
00109 
00110 _GLIBCXX_END_NAMESPACE
00111 
00112 #define _GLIBCXX_MOVE(_Tp) std::move(_Tp)
00113 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
00114 #else
00115 #define _GLIBCXX_MOVE(_Tp) (_Tp)
00116 #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
00117 #endif
00118 
00119 _GLIBCXX_BEGIN_NAMESPACE(std)
00120 
00121   /**
00122    *  @brief Swaps two values.
00123    *  @ingroup mutating_algorithms
00124    *  @param  __a  A thing of arbitrary type.
00125    *  @param  __b  Another thing of arbitrary type.
00126    *  @return   Nothing.
00127   */
00128   template<typename _Tp>
00129     inline void
00130     swap(_Tp& __a, _Tp& __b)
00131     {
00132       // concept requirements
00133       __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
00134 
00135       _Tp __tmp = _GLIBCXX_MOVE(__a);
00136       __a = _GLIBCXX_MOVE(__b);
00137       __b = _GLIBCXX_MOVE(__tmp);
00138     }
00139 
00140   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00141   // DR 809. std::swap should be overloaded for array types.
00142   template<typename _Tp, size_t _Nm>
00143     inline void
00144     swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
00145     {
00146       for (size_t __n = 0; __n < _Nm; ++__n)
00147     swap(__a[__n], __b[__n]);
00148     }
00149 
00150 _GLIBCXX_END_NAMESPACE
00151 
00152 #endif /* _MOVE_H */