Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2012-2012. |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // See http://www.boost.org/libs/move for documentation. |
| 9 | // |
| 10 | ////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | //! \file |
| 13 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 14 | #ifndef NDNBOOST_MOVE_MOVE_UTILITY_HPP |
| 15 | #define NDNBOOST_MOVE_MOVE_UTILITY_HPP |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 16 | |
| 17 | #include <ndnboost/move/detail/config_begin.hpp> |
| 18 | #include <ndnboost/move/core.hpp> |
| 19 | #include <ndnboost/move/detail/meta_utils.hpp> |
| 20 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 21 | #if defined(NDNBOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(NDNBOOST_MOVE_DOXYGEN_INVOKED) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 22 | |
| 23 | namespace ndnboost { |
| 24 | |
| 25 | template<class T> |
| 26 | struct enable_move_utility_emulation |
| 27 | { |
| 28 | static const bool value = true; |
| 29 | }; |
| 30 | |
| 31 | ////////////////////////////////////////////////////////////////////////////// |
| 32 | // |
| 33 | // move() |
| 34 | // |
| 35 | ////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | template <class T> |
| 38 | inline typename ::ndnboost::move_detail::enable_if_c |
| 39 | < enable_move_utility_emulation<T>::value && !has_move_emulation_enabled<T>::value, T&>::type |
| 40 | move(T& x) |
| 41 | { |
| 42 | return x; |
| 43 | } |
| 44 | |
| 45 | template <class T> |
| 46 | inline typename ::ndnboost::move_detail::enable_if_c |
| 47 | < enable_move_utility_emulation<T>::value && has_move_emulation_enabled<T>::value, rv<T>&>::type |
| 48 | move(T& x) |
| 49 | { |
| 50 | return *static_cast<rv<T>* >(::ndnboost::move_detail::addressof(x)); |
| 51 | } |
| 52 | |
| 53 | template <class T> |
| 54 | inline typename ::ndnboost::move_detail::enable_if_c |
| 55 | < enable_move_utility_emulation<T>::value && has_move_emulation_enabled<T>::value, rv<T>&>::type |
| 56 | move(rv<T>& x) |
| 57 | { |
| 58 | return x; |
| 59 | } |
| 60 | |
| 61 | ////////////////////////////////////////////////////////////////////////////// |
| 62 | // |
| 63 | // forward() |
| 64 | // |
| 65 | ////////////////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | template <class T> |
| 68 | inline typename ::ndnboost::move_detail::enable_if_c |
| 69 | < enable_move_utility_emulation<T>::value && ::ndnboost::move_detail::is_rv<T>::value, T &>::type |
| 70 | forward(const typename ::ndnboost::move_detail::identity<T>::type &x) |
| 71 | { |
| 72 | return const_cast<T&>(x); |
| 73 | } |
| 74 | |
| 75 | template <class T> |
| 76 | inline typename ::ndnboost::move_detail::enable_if_c |
| 77 | < enable_move_utility_emulation<T>::value && !::ndnboost::move_detail::is_rv<T>::value, const T &>::type |
| 78 | forward(const typename ::ndnboost::move_detail::identity<T>::type &x) |
| 79 | { |
| 80 | return x; |
| 81 | } |
| 82 | |
| 83 | } //namespace ndnboost |
| 84 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 85 | #else //#if defined(NDNBOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(NDNBOOST_MOVE_DOXYGEN_INVOKED) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 86 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 87 | #if defined(NDNBOOST_MOVE_USE_STANDARD_LIBRARY_MOVE) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 88 | #include <utility> |
| 89 | |
| 90 | namespace ndnboost{ |
| 91 | |
| 92 | using ::std::move; |
| 93 | using ::std::forward; |
| 94 | |
| 95 | } //namespace ndnboost |
| 96 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 97 | #else //!NDNBOOST_MOVE_USE_STANDARD_LIBRARY_MOVE |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 98 | |
| 99 | #include <ndnboost/type_traits/remove_reference.hpp> |
| 100 | |
| 101 | namespace ndnboost { |
| 102 | |
| 103 | //! This trait's internal boolean `value` is false in compilers with rvalue references |
| 104 | //! and true in compilers without rvalue references. |
| 105 | //! |
| 106 | //! A user can specialize this trait for a type T to false to SFINAE out `move` and `forward` |
| 107 | //! so that the user can define a different move emulation for that type in namespace ndnboost |
| 108 | //! (e.g. another Boost library for its types) and avoid any overload ambiguity. |
| 109 | template<class T> |
| 110 | struct enable_move_utility_emulation |
| 111 | { |
| 112 | static const bool value = false; |
| 113 | }; |
| 114 | |
| 115 | ////////////////////////////////////////////////////////////////////////////// |
| 116 | // |
| 117 | // move |
| 118 | // |
| 119 | ////////////////////////////////////////////////////////////////////////////// |
| 120 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 121 | #if defined(NDNBOOST_MOVE_DOXYGEN_INVOKED) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 122 | //! This function provides a way to convert a reference into a rvalue reference |
| 123 | //! in compilers with rvalue references. For other compilers converts T & into |
| 124 | //! <i>::ndnboost::rv<T> &</i> so that move emulation is activated. |
| 125 | template <class T> |
| 126 | rvalue_reference move (input_reference); |
| 127 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 128 | #elif defined(NDNBOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 129 | |
| 130 | //Old move approach, lvalues could bind to rvalue references |
| 131 | template <class T> |
| 132 | inline typename remove_reference<T>::type && move(T&& t) |
| 133 | { return t; } |
| 134 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 135 | #else //NDNBOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 136 | |
| 137 | template <class T> |
| 138 | inline typename remove_reference<T>::type && move(T&& t) |
| 139 | { return static_cast<typename remove_reference<T>::type &&>(t); } |
| 140 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 141 | #endif //NDNBOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 142 | |
| 143 | ////////////////////////////////////////////////////////////////////////////// |
| 144 | // |
| 145 | // forward |
| 146 | // |
| 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | |
| 149 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 150 | #if defined(NDNBOOST_MOVE_DOXYGEN_INVOKED) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 151 | //! This function provides limited form of forwarding that is usually enough for |
| 152 | //! in-place construction and avoids the exponential overloading for |
| 153 | //! achieve the limited forwarding in C++03. |
| 154 | //! |
| 155 | //! For compilers with rvalue references this function provides perfect forwarding. |
| 156 | //! |
| 157 | //! Otherwise: |
| 158 | //! * If input_reference binds to const ::ndnboost::rv<T> & then it output_reference is |
| 159 | //! ::ndnboost::rv<T> & |
| 160 | //! |
| 161 | //! * Else, output_reference is equal to input_reference. |
| 162 | template <class T> output_reference forward(input_reference); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 163 | #elif defined(NDNBOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 164 | |
| 165 | //Old move approach, lvalues could bind to rvalue references |
| 166 | |
| 167 | template <class T> |
| 168 | inline T&& forward (typename ::ndnboost::move_detail::identity<T>::type&& t) |
| 169 | { return t; } |
| 170 | |
| 171 | #else //Old move |
| 172 | |
| 173 | //Implementation #5 from N2951, thanks to Howard Hinnant |
| 174 | |
| 175 | template <class T, class U> |
| 176 | inline T&& forward(U&& t |
| 177 | , typename ::ndnboost::move_detail::enable_if_c< |
| 178 | move_detail::is_lvalue_reference<T>::value ? move_detail::is_lvalue_reference<U>::value : true>::type * = 0/* |
| 179 | , typename ::ndnboost::move_detail::enable_if_c< |
| 180 | move_detail::is_convertible |
| 181 | <typename remove_reference<U>::type*, typename remove_reference<T>::type*>::value>::type * = 0*/) |
| 182 | { return static_cast<T&&>(t); } |
| 183 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 184 | #endif //NDNBOOST_MOVE_DOXYGEN_INVOKED |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 185 | |
| 186 | } //namespace ndnboost { |
| 187 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 188 | #endif //#if defined(NDNBOOST_MOVE_USE_STANDARD_LIBRARY_MOVE) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 189 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 190 | #endif //NDNBOOST_NO_CXX11_RVALUE_REFERENCES |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 191 | |
| 192 | #include <ndnboost/move/detail/config_end.hpp> |
| 193 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 194 | #endif //#ifndef NDNBOOST_MOVE_MOVE_UTILITY_HPP |