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/type_traits/add_reference.hpp"
22 #include "boost/type_traits/is_reference.hpp"
23 #include "boost/type_traits/is_void.hpp"
27 //////////////////////////////////////////////////////////////////////////
28 // function template visitor_ptr
30 // Adapts a function pointer for use as visitor capable of handling
31 // values of a single type. Throws bad_visit if inappropriately applied.
33 template <typename T
, typename R
>
35 : public static_visitor
<R
>
37 private: // representation
39 typedef R (*visitor_t
)(T
);
45 typedef R result_type
;
47 private: // private typedefs
49 typedef typename
mpl::eval_if
<
52 , add_reference
<const T
>
53 >::type argument_fwd_type
;
57 explicit visitor_ptr_t(visitor_t visitor
)
62 public: // static visitor interfaces
65 result_type
operator()(const U
&) const
70 #if !defined(BOOST_NO_VOID_RETURNS)
72 public: // static visitor interfaces, cont.
74 result_type
operator()(argument_fwd_type operand
) const
76 return visitor_(operand
);
79 #else // defined(BOOST_NO_VOID_RETURNS)
81 private: // helpers, for static visitor interfaces (below)
83 result_type
execute_impl(argument_fwd_type operand
, mpl::false_
) const
85 return visitor_(operand
);
88 BOOST_VARIANT_AUX_RETURN_VOID_TYPE
89 execute_impl(argument_fwd_type operand
, mpl::true_
) const
92 BOOST_VARIANT_AUX_RETURN_VOID
;
95 public: // static visitor interfaces, cont.
97 BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type
)
98 operator()(argument_fwd_type operand
) const
100 typedef typename is_void
<result_type
>::type has_void_result
;
101 return execute_impl(operand
, has_void_result());
104 #endif // BOOST_NO_VOID_RETURNS workaround
108 template <typename R
, typename T
>
109 inline visitor_ptr_t
<T
,R
> visitor_ptr(R (*visitor
)(T
))
111 return visitor_ptr_t
<T
,R
>(visitor
);
116 #endif// BOOST_VISITOR_VISITOR_PTR_HPP