apply_visitor_delayed.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //-----------------------------------------------------------------------------
  2. // boost variant/detail/apply_visitor_delayed.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2002-2003
  7. // Eric Friedman
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_VARIANT_DETAIL_APPLY_VISITOR_DELAYED_HPP
  13. #define BOOST_VARIANT_DETAIL_APPLY_VISITOR_DELAYED_HPP
  14. #include "boost/variant/detail/generic_result_type.hpp"
  15. #include "boost/variant/detail/apply_visitor_unary.hpp"
  16. #include "boost/variant/detail/apply_visitor_binary.hpp"
  17. namespace boost {
  18. //////////////////////////////////////////////////////////////////////////
  19. // function template apply_visitor(visitor)
  20. //
  21. // Returns a function object, overloaded for unary and binary usage, that
  22. // visits its arguments using visitor (or a copy of visitor) via
  23. // * apply_visitor( visitor, [argument] )
  24. // under unary invocation, or
  25. // * apply_visitor( visitor, [argument1], [argument2] )
  26. // under binary invocation.
  27. //
  28. // NOTE: Unlike other apply_visitor forms, the visitor object must be
  29. // non-const; this prevents user from giving temporary, to disastrous
  30. // effect (i.e., returned function object would have dead reference).
  31. //
  32. template <typename Visitor>
  33. class apply_visitor_delayed_t
  34. {
  35. public: // visitor typedefs
  36. typedef typename Visitor::result_type
  37. result_type;
  38. private: // representation
  39. Visitor& visitor_;
  40. public: // structors
  41. explicit apply_visitor_delayed_t(Visitor& visitor)
  42. : visitor_(visitor)
  43. {
  44. }
  45. public: // unary visitor interface
  46. template <typename Visitable>
  47. BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type)
  48. operator()(Visitable& visitable) const
  49. {
  50. return apply_visitor(visitor_, visitable);
  51. }
  52. public: // binary visitor interface
  53. template <typename Visitable1, typename Visitable2>
  54. BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type)
  55. operator()(Visitable1& visitable1, Visitable2& visitable2) const
  56. {
  57. return apply_visitor(visitor_, visitable1, visitable2);
  58. }
  59. private:
  60. apply_visitor_delayed_t& operator=(const apply_visitor_delayed_t&);
  61. };
  62. template <typename Visitor>
  63. inline apply_visitor_delayed_t<Visitor> apply_visitor(Visitor& visitor)
  64. {
  65. return apply_visitor_delayed_t<Visitor>(visitor);
  66. }
  67. } // namespace boost
  68. #endif // BOOST_VARIANT_DETAIL_APPLY_VISITOR_DELAYED_HPP