swap_actor.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*=============================================================================
  2. Copyright (c) 2003 Jonathan de Halleux (dehalleux@pelikhan.com)
  3. http://spirit.sourceforge.net/
  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. #ifndef BOOST_SPIRIT_ACTOR_SWAP_ACTOR_HPP
  8. #define BOOST_SPIRIT_ACTOR_SWAP_ACTOR_HPP
  9. #include <boost/spirit/home/classic/namespace.hpp>
  10. #include <boost/spirit/home/classic/actor/ref_value_actor.hpp>
  11. namespace boost { namespace spirit {
  12. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  13. ///////////////////////////////////////////////////////////////////////////
  14. // Summary:
  15. // A semantic action policy that swaps values.
  16. // (This doc uses convention available in actors.hpp)
  17. //
  18. // Actions (what it does):
  19. // ref.swap( value_ref );
  20. //
  21. // Policy name:
  22. // swap_action
  23. //
  24. // Policy holder, corresponding helper method:
  25. // ref_value_actor, swap_a( ref );
  26. // ref_const_ref_actor, swap_a( ref, value_ref );
  27. //
  28. // () operators: both
  29. //
  30. // See also ref_value_actor and ref_const_ref_actor for more details.
  31. ///////////////////////////////////////////////////////////////////////////
  32. template<
  33. typename T
  34. >
  35. class swap_actor
  36. {
  37. private:
  38. T& ref;
  39. T& swap_ref;
  40. public:
  41. swap_actor(
  42. T& ref_,
  43. T& swap_ref_)
  44. : ref(ref_), swap_ref(swap_ref_)
  45. {};
  46. template<typename T2>
  47. void operator()(T2 const& /*val*/) const
  48. {
  49. ref.swap(swap_ref);
  50. }
  51. template<typename IteratorT>
  52. void operator()(
  53. IteratorT const& /*first*/,
  54. IteratorT const& /*last*/
  55. ) const
  56. {
  57. ref.swap(swap_ref);
  58. }
  59. };
  60. template<
  61. typename T
  62. >
  63. inline swap_actor<T> swap_a(
  64. T& ref_,
  65. T& swap_ref_
  66. )
  67. {
  68. return swap_actor<T>(ref_,swap_ref_);
  69. }
  70. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  71. }}
  72. #endif