move.hpp 4.7 KB

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