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 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 <cstddef>
00035 #include <bits/concept_check.h>
00036 
00037 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00038 #include <type_traits> // Brings in std::declval too.
00039 
00040 _GLIBCXX_BEGIN_NAMESPACE(std)
00041 
00042   /// identity
00043   template<typename _Tp>
00044     struct identity
00045     {
00046       typedef _Tp type;
00047     };
00048 
00049   /// forward (as per N2835)
00050   /// Forward lvalues as rvalues.
00051   template<typename _Tp>
00052     inline typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp&&>::type
00053     forward(typename std::identity<_Tp>::type& __t)
00054     { return static_cast<_Tp&&>(__t); }
00055 
00056   /// Forward rvalues as rvalues.
00057   template<typename _Tp>
00058     inline typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp&&>::type
00059     forward(typename std::identity<_Tp>::type&& __t)
00060     { return static_cast<_Tp&&>(__t); }
00061 
00062   // Forward lvalues as lvalues.
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 __t; }
00067 
00068   // Prevent forwarding rvalues as const lvalues.
00069   template<typename _Tp>
00070     inline typename enable_if<is_lvalue_reference<_Tp>::value, _Tp>::type
00071     forward(typename std::remove_reference<_Tp>::type&& __t) = delete;
00072 
00073   /**
00074    *  @brief Move a value.
00075    *  @ingroup mutating_algorithms
00076    *  @param  __t  A thing of arbitrary type.
00077    *  @return Same, moved.
00078   */
00079   template<typename _Tp>
00080     inline typename std::remove_reference<_Tp>::type&&
00081     move(_Tp&& __t)
00082     { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
00083 
00084   /// declval, from type_traits.
00085 
00086 _GLIBCXX_END_NAMESPACE
00087 
00088 #define _GLIBCXX_MOVE(_Tp) std::move(_Tp)
00089 #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val)
00090 #else
00091 #define _GLIBCXX_MOVE(_Tp) (_Tp)
00092 #define _GLIBCXX_FORWARD(_Tp, __val) (__val)
00093 #endif
00094 
00095 _GLIBCXX_BEGIN_NAMESPACE(std)
00096 
00097   /**
00098    *  @brief Swaps two values.
00099    *  @ingroup mutating_algorithms
00100    *  @param  __a  A thing of arbitrary type.
00101    *  @param  __b  Another thing of arbitrary type.
00102    *  @return   Nothing.
00103   */
00104   template<typename _Tp>
00105     inline void
00106     swap(_Tp& __a, _Tp& __b)
00107     {
00108       // concept requirements
00109       __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
00110 
00111       _Tp __tmp = _GLIBCXX_MOVE(__a);
00112       __a = _GLIBCXX_MOVE(__b);
00113       __b = _GLIBCXX_MOVE(__tmp);
00114     }
00115 
00116   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00117   // DR 809. std::swap should be overloaded for array types.
00118   template<typename _Tp, size_t _Nm>
00119     inline void
00120     swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
00121     {
00122       for (size_t __n = 0; __n < _Nm; ++__n)
00123     swap(__a[__n], __b[__n]);
00124     }
00125 
00126 _GLIBCXX_END_NAMESPACE
00127 
00128 #endif /* _MOVE_H */