1 //-----------------------------------------------------------------------------
2 // boost variant/visitor_ptr.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
6 // Copyright (c) 2002-2003
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)
13 #ifndef BOOST_VARIANT_VISITOR_PTR_HPP
14 #define BOOST_VARIANT_VISITOR_PTR_HPP
16 #include <boost/variant/bad_visit.hpp>
17 #include <boost/variant/static_visitor.hpp>
19 #include <boost/mpl/eval_if.hpp>
20 #include <boost/mpl/identity.hpp>
21 #include <boost/throw_exception.hpp>
22 #include <boost/type_traits/add_reference.hpp>
23 #include <boost/type_traits/is_reference.hpp>
24 #include <boost/type_traits/is_void.hpp>
28 //////////////////////////////////////////////////////////////////////////
29 // function template visitor_ptr
31 // Adapts a function pointer for use as visitor capable of handling
32 // values of a single type. Throws bad_visit if inappropriately applied.
34 template <typename T, typename R>
36 : public static_visitor<R>
38 private: // representation
40 typedef R (*visitor_t)(T);
46 typedef R result_type;
48 private: // private typedefs
50 typedef typename mpl::eval_if<
53 , add_reference<const T>
54 >::type argument_fwd_type;
58 explicit visitor_ptr_t(visitor_t visitor) BOOST_NOEXCEPT
63 public: // static visitor interfaces
66 result_type operator()(const U&) const
68 boost::throw_exception(bad_visit());
71 #if !defined(BOOST_NO_VOID_RETURNS)
73 public: // static visitor interfaces, cont.
75 result_type operator()(argument_fwd_type operand) const
77 return visitor_(operand);
80 #else // defined(BOOST_NO_VOID_RETURNS)
82 private: // helpers, for static visitor interfaces (below)
84 result_type execute_impl(argument_fwd_type operand, mpl::false_) const
86 return visitor_(operand);
89 BOOST_VARIANT_AUX_RETURN_VOID_TYPE
90 execute_impl(argument_fwd_type operand, mpl::true_) const
93 BOOST_VARIANT_AUX_RETURN_VOID;
96 public: // static visitor interfaces, cont.
98 BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type)
99 operator()(argument_fwd_type operand) const
101 typedef typename is_void<result_type>::type has_void_result;
102 return execute_impl(operand, has_void_result());
105 #endif // BOOST_NO_VOID_RETURNS workaround
109 template <typename R, typename T>
110 inline visitor_ptr_t<T,R> visitor_ptr(R (*visitor)(T))
112 return visitor_ptr_t<T,R>(visitor);
117 #endif// BOOST_VISITOR_VISITOR_PTR_HPP