| 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 | |
|---|
| 13 | #ifndef BOOST_VARIANT_VISITOR_PTR_HPP |
|---|
| 14 | #define BOOST_VARIANT_VISITOR_PTR_HPP |
|---|
| 15 | |
|---|
| 16 | #include "boost/variant/bad_visit.hpp" |
|---|
| 17 | #include "boost/variant/static_visitor.hpp" |
|---|
| 18 | |
|---|
| 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" |
|---|
| 24 | |
|---|
| 25 | namespace boost { |
|---|
| 26 | |
|---|
| 27 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 28 | // function template visitor_ptr |
|---|
| 29 | // |
|---|
| 30 | // Adapts a function pointer for use as visitor capable of handling |
|---|
| 31 | // values of a single type. Throws bad_visit if inappropriately applied. |
|---|
| 32 | // |
|---|
| 33 | template <typename T, typename R> |
|---|
| 34 | class visitor_ptr_t |
|---|
| 35 | : public static_visitor<R> |
|---|
| 36 | { |
|---|
| 37 | private: // representation |
|---|
| 38 | |
|---|
| 39 | typedef R (*visitor_t)(T); |
|---|
| 40 | |
|---|
| 41 | visitor_t visitor_; |
|---|
| 42 | |
|---|
| 43 | public: // typedefs |
|---|
| 44 | |
|---|
| 45 | typedef R result_type; |
|---|
| 46 | |
|---|
| 47 | private: // private typedefs |
|---|
| 48 | |
|---|
| 49 | typedef typename mpl::eval_if< |
|---|
| 50 | is_reference<T> |
|---|
| 51 | , mpl::identity<T> |
|---|
| 52 | , add_reference<const T> |
|---|
| 53 | >::type argument_fwd_type; |
|---|
| 54 | |
|---|
| 55 | public: // structors |
|---|
| 56 | |
|---|
| 57 | explicit visitor_ptr_t(visitor_t visitor) |
|---|
| 58 | : visitor_(visitor) |
|---|
| 59 | { |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public: // static visitor interfaces |
|---|
| 63 | |
|---|
| 64 | template <typename U> |
|---|
| 65 | result_type operator()(const U&) const |
|---|
| 66 | { |
|---|
| 67 | throw bad_visit(); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | #if !defined(BOOST_NO_VOID_RETURNS) |
|---|
| 71 | |
|---|
| 72 | public: // static visitor interfaces, cont. |
|---|
| 73 | |
|---|
| 74 | result_type operator()(argument_fwd_type operand) const |
|---|
| 75 | { |
|---|
| 76 | return visitor_(operand); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | #else // defined(BOOST_NO_VOID_RETURNS) |
|---|
| 80 | |
|---|
| 81 | private: // helpers, for static visitor interfaces (below) |
|---|
| 82 | |
|---|
| 83 | result_type execute_impl(argument_fwd_type operand, mpl::false_) const |
|---|
| 84 | { |
|---|
| 85 | return visitor_(operand); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | BOOST_VARIANT_AUX_RETURN_VOID_TYPE |
|---|
| 89 | execute_impl(argument_fwd_type operand, mpl::true_) const |
|---|
| 90 | { |
|---|
| 91 | visitor_(operand); |
|---|
| 92 | BOOST_VARIANT_AUX_RETURN_VOID; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | public: // static visitor interfaces, cont. |
|---|
| 96 | |
|---|
| 97 | BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) |
|---|
| 98 | operator()(argument_fwd_type operand) const |
|---|
| 99 | { |
|---|
| 100 | typedef typename is_void<result_type>::type has_void_result; |
|---|
| 101 | return execute_impl(operand, has_void_result()); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | #endif // BOOST_NO_VOID_RETURNS workaround |
|---|
| 105 | |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | template <typename R, typename T> |
|---|
| 109 | inline visitor_ptr_t<T,R> visitor_ptr(R (*visitor)(T)) |
|---|
| 110 | { |
|---|
| 111 | return visitor_ptr_t<T,R>(visitor); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | } // namespace boost |
|---|
| 115 | |
|---|
| 116 | #endif// BOOST_VISITOR_VISITOR_PTR_HPP |
|---|