apply_visitor_delayed.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/apply_visitor_unary.hpp>
  15. #include <boost/variant/detail/apply_visitor_binary.hpp>
  16. #include <boost/variant/variant_fwd.hpp>
  17. #include <boost/variant/detail/has_result_type.hpp>
  18. #include <boost/core/enable_if.hpp>
  19. namespace boost {
  20. //////////////////////////////////////////////////////////////////////////
  21. // function template apply_visitor(visitor)
  22. //
  23. // Returns a function object, overloaded for unary and binary usage, that
  24. // visits its arguments using visitor (or a copy of visitor) via
  25. // * apply_visitor( visitor, [argument] )
  26. // under unary invocation, or
  27. // * apply_visitor( visitor, [argument1], [argument2] )
  28. // under binary invocation.
  29. //
  30. // NOTE: Unlike other apply_visitor forms, the visitor object must be
  31. // non-const; this prevents user from giving temporary, to disastrous
  32. // effect (i.e., returned function object would have dead reference).
  33. //
  34. template <typename Visitor>
  35. class apply_visitor_delayed_t
  36. {
  37. public: // visitor typedefs
  38. typedef typename Visitor::result_type
  39. result_type;
  40. private: // representation
  41. Visitor& visitor_;
  42. public: // structors
  43. explicit apply_visitor_delayed_t(Visitor& visitor) BOOST_NOEXCEPT
  44. : visitor_(visitor)
  45. {
  46. }
  47. public: // N-ary visitor interface
  48. template <typename... Visitables>
  49. result_type operator()(Visitables&... visitables) const
  50. {
  51. return apply_visitor(visitor_, visitables...);
  52. }
  53. private:
  54. apply_visitor_delayed_t& operator=(const apply_visitor_delayed_t&);
  55. };
  56. template <typename Visitor>
  57. inline typename boost::enable_if<
  58. boost::detail::variant::has_result_type<Visitor>,
  59. apply_visitor_delayed_t<Visitor>
  60. >::type apply_visitor(Visitor& visitor)
  61. {
  62. return apply_visitor_delayed_t<Visitor>(visitor);
  63. }
  64. #if !defined(BOOST_NO_CXX14_DECLTYPE_AUTO)
  65. template <typename Visitor>
  66. class apply_visitor_delayed_cpp14_t
  67. {
  68. private: // representation
  69. Visitor& visitor_;
  70. public: // structors
  71. explicit apply_visitor_delayed_cpp14_t(Visitor& visitor) BOOST_NOEXCEPT
  72. : visitor_(visitor)
  73. {
  74. }
  75. public: // N-ary visitor interface
  76. template <typename... Visitables>
  77. decltype(auto) operator()(Visitables&... visitables) const
  78. {
  79. return apply_visitor(visitor_, visitables...);
  80. }
  81. private:
  82. apply_visitor_delayed_cpp14_t& operator=(const apply_visitor_delayed_cpp14_t&);
  83. };
  84. template <typename Visitor>
  85. inline typename boost::disable_if<
  86. boost::detail::variant::has_result_type<Visitor>,
  87. apply_visitor_delayed_cpp14_t<Visitor>
  88. >::type apply_visitor(Visitor& visitor)
  89. {
  90. return apply_visitor_delayed_cpp14_t<Visitor>(visitor);
  91. }
  92. #endif // !defined(BOOST_NO_CXX14_DECLTYPE_AUTO)
  93. } // namespace boost
  94. #endif // BOOST_VARIANT_DETAIL_APPLY_VISITOR_DELAYED_HPP