swap.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <algorithm>
  10. #include <boost/fusion/support/is_sequence.hpp>
  11. #include <boost/fusion/view/zip_view.hpp>
  12. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  13. #include <boost/utility/enable_if.hpp>
  14. #include <boost/fusion/sequence/intrinsic/front.hpp>
  15. #include <boost/fusion/sequence/intrinsic/back.hpp>
  16. #include <boost/mpl/and.hpp>
  17. namespace boost { namespace fusion {
  18. namespace result_of
  19. {
  20. template<typename Seq1, typename Seq2>
  21. struct swap
  22. {
  23. typedef void type;
  24. };
  25. }
  26. namespace detail
  27. {
  28. struct swap
  29. {
  30. template<typename Elem>
  31. struct result
  32. {
  33. typedef void type;
  34. };
  35. template<typename Elem>
  36. void operator()(Elem const& e) const
  37. {
  38. using std::swap;
  39. swap(front(e), back(e));
  40. }
  41. };
  42. }
  43. template<typename Seq1, typename Seq2>
  44. typename enable_if<mpl::and_<traits::is_sequence<Seq1>, traits::is_sequence<Seq2> >, void>::type
  45. swap(Seq1& lhs, Seq2& rhs)
  46. {
  47. typedef vector<Seq1&, Seq2&> references;
  48. for_each(zip_view<references>(references(lhs, rhs)), detail::swap());
  49. }
  50. }}
  51. #endif