1 /*=============================================================================
2 Copyright (c) 2014 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #ifndef BOOST_SPIRIT_X3_DIRECTIVE_WITH_HPP
8 #define BOOST_SPIRIT_X3_DIRECTIVE_WITH_HPP
10 #include <boost/spirit/home/x3/support/unused.hpp>
11 #include <boost/spirit/home/x3/core/parser.hpp>
13 namespace boost { namespace spirit { namespace x3
15 ///////////////////////////////////////////////////////////////////////////
16 // with directive injects a value into the context prior to parsing.
17 ///////////////////////////////////////////////////////////////////////////
18 template <typename Subject, typename Derived, typename T>
19 struct with_value_holder
20 : unary_parser<Subject, Derived>
22 typedef unary_parser<Subject, Derived> base_type;
24 constexpr with_value_holder(Subject const& subject, T&& val)
26 , val(std::forward<T>(val)) {}
29 template <typename Subject, typename Derived, typename T>
30 struct with_value_holder<Subject, Derived, T&>
31 : unary_parser<Subject, Derived>
33 typedef unary_parser<Subject, Derived> base_type;
35 constexpr with_value_holder(Subject const& subject, T& val)
40 template <typename Subject, typename ID, typename T>
42 : with_value_holder<Subject, with_directive<Subject, ID, T>, T>
44 typedef with_value_holder<Subject, with_directive<Subject, ID, T>, T> base_type;
45 static bool const is_pass_through_unary = true;
46 static bool const handles_container = Subject::handles_container;
48 typedef Subject subject_type;
50 constexpr with_directive(Subject const& subject, T&& val)
51 : base_type(subject, std::forward<T>(val)) {}
53 template <typename Iterator, typename Context
54 , typename RContext, typename Attribute>
55 bool parse(Iterator& first, Iterator const& last
56 , Context const& context, RContext& rcontext, Attribute& attr) const
58 return this->subject.parse(
60 , make_context<ID>(this->val, context)
66 template <typename ID, typename T>
71 template <typename Subject>
72 constexpr with_directive<typename extension::as_parser<Subject>::value_type, ID, T>
73 operator[](Subject const& subject) const
75 return { as_parser(subject), std::forward<T>(val) };
79 template <typename ID, typename T>
80 constexpr with_gen<ID, T> with(T&& val)
82 return { std::forward<T>(val) };