move.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-2016.
  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. //! \file
  12. #ifndef BOOST_MOVE_ALGO_MOVE_HPP
  13. #define BOOST_MOVE_ALGO_MOVE_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. #include <boost/move/detail/config_begin.hpp>
  22. #include <boost/move/utility_core.hpp>
  23. #include <boost/move/detail/iterator_traits.hpp>
  24. #include <boost/move/detail/iterator_to_raw_pointer.hpp>
  25. #include <boost/move/detail/addressof.hpp>
  26. #include <boost/core/no_exceptions_support.hpp>
  27. #if defined(BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE)
  28. #include <algorithm>
  29. #endif
  30. namespace boost {
  31. //////////////////////////////////////////////////////////////////////////////
  32. //
  33. // move
  34. //
  35. //////////////////////////////////////////////////////////////////////////////
  36. #if !defined(BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE)
  37. //! <b>Effects</b>: Moves elements in the range [first,last) into the range [result,result + (last -
  38. //! first)) starting from first and proceeding to last. For each non-negative integer n < (last-first),
  39. //! performs *(result + n) = ::boost::move (*(first + n)).
  40. //!
  41. //! <b>Effects</b>: result + (last - first).
  42. //!
  43. //! <b>Requires</b>: result shall not be in the range [first,last).
  44. //!
  45. //! <b>Complexity</b>: Exactly last - first move assignments.
  46. template <typename I, // I models InputIterator
  47. typename O> // O models OutputIterator
  48. O move(I f, I l, O result)
  49. {
  50. while (f != l) {
  51. *result = ::boost::move(*f);
  52. ++f; ++result;
  53. }
  54. return result;
  55. }
  56. //////////////////////////////////////////////////////////////////////////////
  57. //
  58. // move_backward
  59. //
  60. //////////////////////////////////////////////////////////////////////////////
  61. //! <b>Effects</b>: Moves elements in the range [first,last) into the range
  62. //! [result - (last-first),result) starting from last - 1 and proceeding to
  63. //! first. For each positive integer n <= (last - first),
  64. //! performs *(result - n) = ::boost::move(*(last - n)).
  65. //!
  66. //! <b>Requires</b>: result shall not be in the range [first,last).
  67. //!
  68. //! <b>Returns</b>: result - (last - first).
  69. //!
  70. //! <b>Complexity</b>: Exactly last - first assignments.
  71. template <typename I, // I models BidirectionalIterator
  72. typename O> // O models BidirectionalIterator
  73. O move_backward(I f, I l, O result)
  74. {
  75. while (f != l) {
  76. --l; --result;
  77. *result = ::boost::move(*l);
  78. }
  79. return result;
  80. }
  81. #else
  82. using ::std::move_backward;
  83. #endif //!defined(BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE)
  84. //////////////////////////////////////////////////////////////////////////////
  85. //
  86. // uninitialized_move
  87. //
  88. //////////////////////////////////////////////////////////////////////////////
  89. //! <b>Effects</b>:
  90. //! \code
  91. //! for (; first != last; ++result, ++first)
  92. //! new (static_cast<void*>(&*result))
  93. //! typename iterator_traits<ForwardIterator>::value_type(boost::move(*first));
  94. //! \endcode
  95. //!
  96. //! <b>Returns</b>: result
  97. template
  98. <typename I, // I models InputIterator
  99. typename F> // F models ForwardIterator
  100. F uninitialized_move(I f, I l, F r
  101. /// @cond
  102. // ,typename ::boost::move_detail::enable_if<has_move_emulation_enabled<typename boost::movelib::iterator_traits<I>::value_type> >::type* = 0
  103. /// @endcond
  104. )
  105. {
  106. typedef typename boost::movelib::iterator_traits<I>::value_type input_value_type;
  107. F back = r;
  108. BOOST_TRY{
  109. while (f != l) {
  110. void * const addr = static_cast<void*>(::boost::move_detail::addressof(*r));
  111. ::new(addr) input_value_type(::boost::move(*f));
  112. ++f; ++r;
  113. }
  114. }
  115. BOOST_CATCH(...){
  116. for (; back != r; ++back){
  117. boost::movelib::iterator_to_raw_pointer(back)->~input_value_type();
  118. }
  119. BOOST_RETHROW;
  120. }
  121. BOOST_CATCH_END
  122. return r;
  123. }
  124. /// @cond
  125. /*
  126. template
  127. <typename I, // I models InputIterator
  128. typename F> // F models ForwardIterator
  129. F uninitialized_move(I f, I l, F r,
  130. typename ::boost::move_detail::disable_if<has_move_emulation_enabled<typename boost::movelib::iterator_traits<I>::value_type> >::type* = 0)
  131. {
  132. return std::uninitialized_copy(f, l, r);
  133. }
  134. */
  135. /// @endcond
  136. } //namespace boost {
  137. #include <boost/move/detail/config_end.hpp>
  138. #endif //#ifndef BOOST_MOVE_ALGO_MOVE_HPP