/*============================================================================= Copyright (c) 2001-2011 Hartmut Kaiser Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #if !defined(SPIRIT_ALTERNATIVE_FUNCTION_APRIL_23_2007_1046AM) #define SPIRIT_ALTERNATIVE_FUNCTION_APRIL_23_2007_1046AM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #include #include namespace boost { namespace spirit { namespace qi { namespace detail { template struct find_substitute { // Get the typr from the variant that can be a substitute for Expected. // If none is found, just return Expected typedef Variant variant_type; typedef typename variant_type::types types; typedef typename mpl::end::type end; typedef typename mpl::find_if >::type iter_1; typedef typename mpl::eval_if< is_same, mpl::find_if >, mpl::identity >::type iter; typedef typename mpl::eval_if< is_same, mpl::identity, mpl::deref >::type type; }; template struct alternative_function { alternative_function( Iterator& first, Iterator const& last, Context& context, Skipper const& skipper, Attribute& attr) : first(first), last(last), context(context), skipper(skipper), attr(attr) { } template bool call(Component const& component, mpl::true_) const { // if Attribute is not a variant, then pass it as-is return component.parse(first, last, context, skipper, attr); } template bool call_optional_or_variant(Component const& component, mpl::true_) const { // If Attribute is an optional, then create an attribute for the Component // with the type optional::value_type. If the expected attribute is unused type, // use it instead. typedef typename traits::attribute_of::type expected_type; typename mpl::if_< is_same, unused_type, typename Attribute::value_type>::type val; if (component.parse(first, last, context, skipper, val)) { traits::assign_to(val, attr); return true; } return false; } template bool call_variant(Component const& component, mpl::false_) const { // If Attribute is a variant, then search the variant types for a // suitable substitute type. typename find_substitute::type >::type val; if (component.parse(first, last, context, skipper, val)) { traits::assign_to(val, attr); return true; } return false; } template bool call_variant(Component const& component, mpl::true_) const { // If Attribute is a variant and the expected attribute is // the same type (pass the variant as-is). return component.parse(first, last, context, skipper, attr); } template bool call_optional_or_variant(Component const& component, mpl::false_) const { // Attribute is a variant... typedef typename traits::attribute_of::type expected; return call_variant(component, is_same()); } template bool call(Component const& component, mpl::false_) const { return call_optional_or_variant( component, spirit::traits::not_is_variant()); } template bool call_unused(Component const& component, mpl::true_) const { // return true if the parser succeeds return call(component, mpl::and_< spirit::traits::not_is_variant, spirit::traits::not_is_optional >()); } template bool call_unused(Component const& component, mpl::false_) const { return component.parse(first, last, context, skipper, unused); } template bool operator()(Component const& component) const { // return true if the parser succeeds typedef typename traits::not_is_unused< typename traits::attribute_of::type >::type predicate; return call_unused(component, predicate()); } Iterator& first; Iterator const& last; Context& context; Skipper const& skipper; Attribute& attr; private: // silence MSVC warning C4512: assignment operator could not be generated alternative_function& operator= (alternative_function const&); }; template struct alternative_function { alternative_function( Iterator& first, Iterator const& last, Context& context, Skipper const& skipper, unused_type) : first(first), last(last), context(context), skipper(skipper) { } template bool operator()(Component const& component) { // return true if the parser succeeds return component.parse(first, last, context, skipper, unused); } Iterator& first; Iterator const& last; Context& context; Skipper const& skipper; private: // silence MSVC warning C4512: assignment operator could not be generated alternative_function& operator= (alternative_function const&); }; }}}} #endif