| 1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #ifndef BOOST_SPIRIT_KARMA_DIRECTIVE_OMIT_HPP |
| 7 | #define BOOST_SPIRIT_KARMA_DIRECTIVE_OMIT_HPP |
| 8 | |
| 9 | #if defined(_MSC_VER) |
| 10 | #pragma once |
| 11 | #endif |
| 12 | |
| 13 | #include <boost/spirit/home/karma/meta_compiler.hpp> |
| 14 | #include <boost/spirit/home/karma/generator.hpp> |
| 15 | #include <boost/spirit/home/karma/domain.hpp> |
| 16 | #include <boost/spirit/home/support/unused.hpp> |
| 17 | #include <boost/spirit/home/support/info.hpp> |
| 18 | #include <boost/spirit/home/support/common_terminals.hpp> |
| 19 | #include <boost/spirit/home/support/has_semantic_action.hpp> |
| 20 | #include <boost/spirit/home/support/handles_container.hpp> |
| 21 | #include <boost/spirit/home/karma/detail/attributes.hpp> |
| 22 | #include <boost/spirit/home/karma/detail/output_iterator.hpp> |
| 23 | |
| 24 | namespace boost { namespace spirit |
| 25 | { |
| 26 | /////////////////////////////////////////////////////////////////////////// |
| 27 | // Enablers |
| 28 | /////////////////////////////////////////////////////////////////////////// |
| 29 | template <> |
| 30 | struct use_directive<karma::domain, tag::omit> // enables omit |
| 31 | : mpl::true_ {}; |
| 32 | |
| 33 | template <> |
| 34 | struct use_directive<karma::domain, tag::skip> // enables skip |
| 35 | : mpl::true_ {}; |
| 36 | }} |
| 37 | |
| 38 | namespace boost { namespace spirit { namespace karma |
| 39 | { |
| 40 | #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS |
| 41 | using spirit::omit; |
| 42 | using spirit::skip; |
| 43 | #endif |
| 44 | using spirit::omit_type; |
| 45 | using spirit::skip_type; |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////////////// |
| 48 | // omit_directive consumes the attribute of subject generator without |
| 49 | // generating anything |
| 50 | /////////////////////////////////////////////////////////////////////////// |
| 51 | template <typename Subject, bool Execute> |
| 52 | struct omit_directive : unary_generator<omit_directive<Subject, Execute> > |
| 53 | { |
| 54 | typedef Subject subject_type; |
| 55 | |
| 56 | typedef mpl::int_< |
| 57 | generator_properties::disabling | subject_type::properties::value |
| 58 | > properties; |
| 59 | |
| 60 | omit_directive(Subject const& subject) |
| 61 | : subject(subject) {} |
| 62 | |
| 63 | template <typename Context, typename Iterator = unused_type> |
| 64 | struct attribute |
| 65 | : traits::attribute_of<subject_type, Context, Iterator> |
| 66 | {}; |
| 67 | |
| 68 | template <typename OutputIterator, typename Context, typename Delimiter |
| 69 | , typename Attribute> |
| 70 | bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d |
| 71 | , Attribute const& attr) const |
| 72 | { |
| 73 | // We need to actually compile the output operation as we don't |
| 74 | // have any other means to verify, whether the passed attribute is |
| 75 | // compatible with the subject. |
| 76 | |
| 77 | #if defined(_MSC_VER) && _MSC_VER < 1900 |
| 78 | # pragma warning(push) |
| 79 | # pragma warning(disable: 4127) // conditional expression is constant |
| 80 | #endif |
| 81 | // omit[] will execute the code, while skip[] doesn't execute it |
| 82 | if (Execute) { |
| 83 | #if defined(_MSC_VER) && _MSC_VER < 1900 |
| 84 | # pragma warning(pop) |
| 85 | #endif |
| 86 | // wrap the given output iterator to avoid output |
| 87 | detail::disable_output<OutputIterator> disable(sink); |
| 88 | return subject.generate(sink, ctx, d, attr); |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | template <typename Context> |
| 94 | info what(Context& context) const |
| 95 | { |
| 96 | return info(Execute ? "omit" : "skip" , subject.what(context)); |
| 97 | } |
| 98 | |
| 99 | Subject subject; |
| 100 | }; |
| 101 | |
| 102 | /////////////////////////////////////////////////////////////////////////// |
| 103 | // Generator generators: make_xxx function (objects) |
| 104 | /////////////////////////////////////////////////////////////////////////// |
| 105 | template <typename Subject, typename Modifiers> |
| 106 | struct make_directive<tag::omit, Subject, Modifiers> |
| 107 | { |
| 108 | typedef omit_directive<Subject, true> result_type; |
| 109 | result_type operator()(unused_type, Subject const& subject |
| 110 | , unused_type) const |
| 111 | { |
| 112 | return result_type(subject); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | template <typename Subject, typename Modifiers> |
| 117 | struct make_directive<tag::skip, Subject, Modifiers> |
| 118 | { |
| 119 | typedef omit_directive<Subject, false> result_type; |
| 120 | result_type operator()(unused_type, Subject const& subject |
| 121 | , unused_type) const |
| 122 | { |
| 123 | return result_type(subject); |
| 124 | } |
| 125 | }; |
| 126 | }}} |
| 127 | |
| 128 | namespace boost { namespace spirit { namespace traits |
| 129 | { |
| 130 | /////////////////////////////////////////////////////////////////////////// |
| 131 | template <typename Subject, bool Execute> |
| 132 | struct has_semantic_action<karma::omit_directive<Subject, Execute> > |
| 133 | : unary_has_semantic_action<Subject> {}; |
| 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////// |
| 136 | template <typename Subject, bool Execute, typename Attribute |
| 137 | , typename Context, typename Iterator> |
| 138 | struct handles_container<karma::omit_directive<Subject, Execute>, Attribute |
| 139 | , Context, Iterator> |
| 140 | : unary_handles_container<Subject, Attribute, Context, Iterator> {}; |
| 141 | }}} |
| 142 | |
| 143 | #endif |
| 144 | |