visitor_ptr.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //-----------------------------------------------------------------------------
  2. // boost variant/visitor_ptr.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_VISITOR_PTR_HPP
  13. #define BOOST_VARIANT_VISITOR_PTR_HPP
  14. #include <boost/variant/bad_visit.hpp>
  15. #include <boost/variant/static_visitor.hpp>
  16. #include <boost/mpl/eval_if.hpp>
  17. #include <boost/mpl/identity.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #include <boost/type_traits/add_reference.hpp>
  20. #include <boost/type_traits/is_reference.hpp>
  21. #include <boost/type_traits/is_void.hpp>
  22. namespace boost {
  23. //////////////////////////////////////////////////////////////////////////
  24. // function template visitor_ptr
  25. //
  26. // Adapts a function pointer for use as visitor capable of handling
  27. // values of a single type. Throws bad_visit if inappropriately applied.
  28. //
  29. template <typename T, typename R>
  30. class visitor_ptr_t
  31. : public static_visitor<R>
  32. {
  33. private: // representation
  34. typedef R (*visitor_t)(T);
  35. visitor_t visitor_;
  36. public: // typedefs
  37. typedef R result_type;
  38. private: // private typedefs
  39. typedef typename mpl::eval_if<
  40. is_reference<T>
  41. , mpl::identity<T>
  42. , add_reference<const T>
  43. >::type argument_fwd_type;
  44. public: // structors
  45. explicit visitor_ptr_t(visitor_t visitor) BOOST_NOEXCEPT
  46. : visitor_(visitor)
  47. {
  48. }
  49. public: // static visitor interfaces
  50. template <typename U>
  51. result_type operator()(const U&) const
  52. {
  53. boost::throw_exception(bad_visit());
  54. }
  55. public: // static visitor interfaces, cont.
  56. result_type operator()(argument_fwd_type operand) const
  57. {
  58. return visitor_(operand);
  59. }
  60. };
  61. template <typename R, typename T>
  62. inline visitor_ptr_t<T,R> visitor_ptr(R (*visitor)(T))
  63. {
  64. return visitor_ptr_t<T,R>(visitor);
  65. }
  66. } // namespace boost
  67. #endif// BOOST_VISITOR_VISITOR_PTR_HPP