swap.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2006 Dan Marsden
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_FUSION_SWAP_20070501_1956)
  8. #define BOOST_FUSION_SWAP_20070501_1956
  9. #include <boost/fusion/support/config.hpp>
  10. #include <algorithm>
  11. #include <boost/fusion/support/is_sequence.hpp>
  12. #include <boost/fusion/view/zip_view.hpp>
  13. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  14. #include <boost/fusion/sequence/intrinsic/front.hpp>
  15. #include <boost/fusion/sequence/intrinsic/back.hpp>
  16. #include <boost/core/enable_if.hpp>
  17. #include <boost/mpl/and.hpp>
  18. namespace boost { namespace fusion {
  19. namespace result_of
  20. {
  21. template<typename Seq1, typename Seq2>
  22. struct swap
  23. : enable_if<mpl::and_<
  24. traits::is_sequence<Seq1>,
  25. traits::is_sequence<Seq2>
  26. > > {};
  27. }
  28. namespace detail
  29. {
  30. struct swap
  31. {
  32. template<typename Elem>
  33. struct result
  34. {
  35. typedef void type;
  36. };
  37. template<typename Elem>
  38. BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  39. void operator()(Elem const& e) const
  40. {
  41. using std::swap;
  42. swap(front(e), back(e));
  43. }
  44. };
  45. }
  46. template<typename Seq1, typename Seq2>
  47. BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  48. inline typename result_of::swap<Seq1, Seq2>::type
  49. swap(Seq1& lhs, Seq2& rhs)
  50. {
  51. typedef vector<Seq1&, Seq2&> references;
  52. for_each(zip_view<references>(references(lhs, rhs)), detail::swap());
  53. }
  54. }}
  55. #endif