1 | //----------------------------------------------------------------------------- |
---|
2 | // boost variant/detail/move.hpp header file |
---|
3 | // See http://www.boost.org for updates, documentation, and revision history. |
---|
4 | //----------------------------------------------------------------------------- |
---|
5 | // |
---|
6 | // Copyright (c) 2002-2003 Eric Friedman |
---|
7 | // Copyright (c) 2002 by Andrei Alexandrescu |
---|
8 | // |
---|
9 | // Use, modification and distribution are subject to the |
---|
10 | // Boost Software License, Version 1.0. (See accompanying file |
---|
11 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
12 | // |
---|
13 | // This file derivative of MoJO. Much thanks to Andrei for his initial work. |
---|
14 | // See <http://www.cuj.com/experts/2102/alexandr.htm> for information on MOJO. |
---|
15 | // Re-issued here under the Boost Software License, with permission of the original |
---|
16 | // author (Andrei Alexandrescu). |
---|
17 | |
---|
18 | |
---|
19 | #ifndef BOOST_VARIANT_DETAIL_MOVE_HPP |
---|
20 | #define BOOST_VARIANT_DETAIL_MOVE_HPP |
---|
21 | |
---|
22 | #include <iterator> // for iterator_traits |
---|
23 | #include <new> // for placement new |
---|
24 | |
---|
25 | #include "boost/config.hpp" |
---|
26 | #include "boost/detail/workaround.hpp" |
---|
27 | #include "boost/mpl/if.hpp" |
---|
28 | #include "boost/type_traits/is_base_and_derived.hpp" |
---|
29 | |
---|
30 | namespace boost { |
---|
31 | namespace detail { namespace variant { |
---|
32 | |
---|
33 | ////////////////////////////////////////////////////////////////////////// |
---|
34 | // forward declares |
---|
35 | // |
---|
36 | // NOTE: Incomplete until (if?) Boost.Move becomes part of Boost. |
---|
37 | // |
---|
38 | template <typename Deriving> class moveable; |
---|
39 | template <typename T> class move_source; |
---|
40 | template <typename T> class move_return; |
---|
41 | |
---|
42 | namespace detail { |
---|
43 | |
---|
44 | // (detail) moveable_tag |
---|
45 | // |
---|
46 | // Concrete type from which moveable<T> derives. |
---|
47 | // |
---|
48 | // TODO: Move into moveable_fwd.hpp and define has_move_constructor. |
---|
49 | // |
---|
50 | template <typename Deriving> |
---|
51 | struct moveable_tag |
---|
52 | { |
---|
53 | }; |
---|
54 | |
---|
55 | } // namespace detail |
---|
56 | |
---|
57 | ////////////////////////////////////////////////////////////////////////// |
---|
58 | // function template move |
---|
59 | // |
---|
60 | // Takes a T& and returns, if T derives moveable<T>, a move_source<T> for |
---|
61 | // the object; else, returns the T&. |
---|
62 | // |
---|
63 | |
---|
64 | namespace detail { |
---|
65 | |
---|
66 | // (detail) class template move_type |
---|
67 | // |
---|
68 | // Metafunction that, given moveable T, provides move_source<T>, else T&. |
---|
69 | // |
---|
70 | template <typename T> |
---|
71 | struct move_type |
---|
72 | { |
---|
73 | public: // metafunction result |
---|
74 | |
---|
75 | typedef typename mpl::if_< |
---|
76 | is_base_and_derived<detail::moveable_tag<T>, T> |
---|
77 | , move_source<T> |
---|
78 | , T& |
---|
79 | >::type type; |
---|
80 | |
---|
81 | }; |
---|
82 | |
---|
83 | } // namespace detail |
---|
84 | |
---|
85 | template <typename T> |
---|
86 | inline |
---|
87 | typename detail::move_type<T>::type |
---|
88 | move(T& source) |
---|
89 | { |
---|
90 | typedef typename detail::move_type<T>::type |
---|
91 | move_t; |
---|
92 | |
---|
93 | return move_t(source); |
---|
94 | } |
---|
95 | |
---|
96 | ////////////////////////////////////////////////////////////////////////// |
---|
97 | // class template return_t |
---|
98 | // |
---|
99 | // Metafunction that, given moveable T, provides move_return<T>, else T. |
---|
100 | // |
---|
101 | template <typename T> |
---|
102 | struct return_t |
---|
103 | { |
---|
104 | public: // metafunction result |
---|
105 | |
---|
106 | typedef typename mpl::if_< |
---|
107 | is_base_and_derived<moveable<T>, T> |
---|
108 | , move_return<T> |
---|
109 | , T |
---|
110 | >::type type; |
---|
111 | |
---|
112 | }; |
---|
113 | |
---|
114 | ////////////////////////////////////////////////////////////////////////// |
---|
115 | // function template move_swap |
---|
116 | // |
---|
117 | // Swaps using Koenig lookup but falls back to move-swap for primitive |
---|
118 | // types and on non-conforming compilers. |
---|
119 | // |
---|
120 | |
---|
121 | #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) \ |
---|
122 | || BOOST_WORKAROUND(__GNUC__, BOOST_TESTED_AT(2)) |
---|
123 | |
---|
124 | // [Indicate that move_swap by overload is disabled...] |
---|
125 | #define BOOST_NO_MOVE_SWAP_BY_OVERLOAD |
---|
126 | |
---|
127 | // [...and provide straight swap-by-move implementation:] |
---|
128 | template <typename T> |
---|
129 | inline void move_swap(T& lhs, T& rhs) |
---|
130 | { |
---|
131 | T tmp( boost::detail::variant::move(lhs) ); |
---|
132 | lhs = boost::detail::variant::move(rhs); |
---|
133 | rhs = boost::detail::variant::move(tmp); |
---|
134 | } |
---|
135 | |
---|
136 | #else// !workaround |
---|
137 | |
---|
138 | namespace detail { namespace move_swap { |
---|
139 | |
---|
140 | template <typename T> |
---|
141 | inline void swap(T& lhs, T& rhs) |
---|
142 | { |
---|
143 | T tmp( boost::detail::variant::move(lhs) ); |
---|
144 | lhs = boost::detail::variant::move(rhs); |
---|
145 | rhs = boost::detail::variant::move(tmp); |
---|
146 | } |
---|
147 | |
---|
148 | }} // namespace detail::move_swap |
---|
149 | |
---|
150 | template <typename T> |
---|
151 | inline void move_swap(T& lhs, T& rhs) |
---|
152 | { |
---|
153 | using detail::move_swap::swap; |
---|
154 | |
---|
155 | swap(lhs, rhs); |
---|
156 | } |
---|
157 | |
---|
158 | #endif // workaround |
---|
159 | |
---|
160 | }} // namespace detail::variant |
---|
161 | } // namespace boost |
---|
162 | |
---|
163 | #endif // BOOST_VARIANT_DETAIL_MOVE_HPP |
---|
164 | |
---|
165 | |
---|
166 | |
---|