]>
Commit | Line | Data |
---|---|---|
e14e932b | 1 | // Move, forward and identity for C++0x + swap -*- C++ -*- |
1476e59c | 2 | |
748086b7 | 3 | // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. |
1476e59c PC |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
1476e59c PC |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
1476e59c | 24 | |
caa8b3c6 | 25 | /** @file move.h |
1476e59c PC |
26 | * This is an internal header file, included by other library headers. |
27 | * You should not attempt to use it directly. | |
28 | */ | |
29 | ||
caa8b3c6 PC |
30 | #ifndef _MOVE_H |
31 | #define _MOVE_H 1 | |
1476e59c | 32 | |
e14e932b | 33 | #include <bits/c++config.h> |
caa8b3c6 | 34 | #include <cstddef> |
e14e932b PC |
35 | #include <bits/concept_check.h> |
36 | ||
1476e59c | 37 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
7274deff | 38 | #include <type_traits> // Brings in std::declval too. |
1476e59c PC |
39 | |
40 | _GLIBCXX_BEGIN_NAMESPACE(std) | |
41 | ||
fd9380a6 | 42 | /// identity |
1476e59c PC |
43 | template<typename _Tp> |
44 | struct identity | |
45 | { | |
46 | typedef _Tp type; | |
47 | }; | |
48 | ||
e7f1930f JM |
49 | /// forward (as per N2835) |
50 | /// Forward lvalues as rvalues. | |
53a381dc | 51 | template<typename _Tp> |
e7f1930f JM |
52 | inline typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp&&>::type |
53 | forward(typename std::identity<_Tp>::type& __t) | |
54 | { return static_cast<_Tp&&>(__t); } | |
55 | ||
56 | /// Forward rvalues as rvalues. | |
53a381dc | 57 | template<typename _Tp> |
e7f1930f | 58 | inline typename enable_if<!is_lvalue_reference<_Tp>::value, _Tp&&>::type |
1476e59c | 59 | forward(typename std::identity<_Tp>::type&& __t) |
e7f1930f JM |
60 | { return static_cast<_Tp&&>(__t); } |
61 | ||
62 | // Forward lvalues as lvalues. | |
53a381dc | 63 | template<typename _Tp> |
e7f1930f JM |
64 | inline typename enable_if<is_lvalue_reference<_Tp>::value, _Tp>::type |
65 | forward(typename std::identity<_Tp>::type __t) | |
1476e59c PC |
66 | { return __t; } |
67 | ||
e7f1930f | 68 | // Prevent forwarding rvalues as const lvalues. |
53a381dc | 69 | template<typename _Tp> |
e7f1930f | 70 | inline typename enable_if<is_lvalue_reference<_Tp>::value, _Tp>::type |
53a381dc | 71 | forward(typename std::remove_reference<_Tp>::type&& __t) = delete; |
e7f1930f | 72 | |
fd9380a6 BK |
73 | /** |
74 | * @brief Move a value. | |
75 | * @ingroup mutating_algorithms | |
76 | * @param __t A thing of arbitrary type. | |
77 | * @return Same, moved. | |
78 | */ | |
1476e59c PC |
79 | template<typename _Tp> |
80 | inline typename std::remove_reference<_Tp>::type&& | |
81 | move(_Tp&& __t) | |
e7f1930f | 82 | { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); } |
1476e59c | 83 | |
7274deff PC |
84 | /// declval, defined in <type_traits>. |
85 | ||
1476e59c PC |
86 | _GLIBCXX_END_NAMESPACE |
87 | ||
d98f312c | 88 | #define _GLIBCXX_MOVE(_Tp) std::move(_Tp) |
55dd8445 | 89 | #define _GLIBCXX_FORWARD(_Tp, __val) std::forward<_Tp>(__val) |
d98f312c | 90 | #else |
85799c60 | 91 | #define _GLIBCXX_MOVE(_Tp) (_Tp) |
55dd8445 | 92 | #define _GLIBCXX_FORWARD(_Tp, __val) (__val) |
1476e59c PC |
93 | #endif |
94 | ||
e14e932b PC |
95 | _GLIBCXX_BEGIN_NAMESPACE(std) |
96 | ||
97 | /** | |
98 | * @brief Swaps two values. | |
fd9380a6 BK |
99 | * @ingroup mutating_algorithms |
100 | * @param __a A thing of arbitrary type. | |
101 | * @param __b Another thing of arbitrary type. | |
e14e932b PC |
102 | * @return Nothing. |
103 | */ | |
104 | template<typename _Tp> | |
105 | inline void | |
106 | swap(_Tp& __a, _Tp& __b) | |
107 | { | |
108 | // concept requirements | |
109 | __glibcxx_function_requires(_SGIAssignableConcept<_Tp>) | |
110 | ||
111 | _Tp __tmp = _GLIBCXX_MOVE(__a); | |
112 | __a = _GLIBCXX_MOVE(__b); | |
113 | __b = _GLIBCXX_MOVE(__tmp); | |
114 | } | |
115 | ||
caa8b3c6 PC |
116 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
117 | // DR 809. std::swap should be overloaded for array types. | |
118 | template<typename _Tp, size_t _Nm> | |
119 | inline void | |
120 | swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm]) | |
121 | { | |
122 | for (size_t __n = 0; __n < _Nm; ++__n) | |
123 | swap(__a[__n], __b[__n]); | |
124 | } | |
125 | ||
e14e932b PC |
126 | _GLIBCXX_END_NAMESPACE |
127 | ||
caa8b3c6 | 128 | #endif /* _MOVE_H */ |