Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-19 09:47:36

0001 //  Copyright (c) 2001-2011 Hartmut Kaiser
0002 //  Copyright (c)      2010 Bryce Lelbach
0003 //
0004 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_SPIRIT_KARMA_DIRECTIVE_AS_HPP
0008 #define BOOST_SPIRIT_KARMA_DIRECTIVE_AS_HPP
0009 
0010 #if defined(_MSC_VER)
0011 #pragma once
0012 #endif
0013 
0014 #include <boost/spirit/home/karma/meta_compiler.hpp>
0015 #include <boost/spirit/home/karma/generator.hpp>
0016 #include <boost/spirit/home/karma/delimit_out.hpp>
0017 #include <boost/spirit/home/karma/domain.hpp>
0018 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
0019 #include <boost/spirit/home/karma/detail/as.hpp>
0020 #include <boost/spirit/home/support/unused.hpp>
0021 #include <boost/spirit/home/support/info.hpp>
0022 #include <boost/spirit/home/support/common_terminals.hpp>
0023 #include <boost/spirit/home/support/has_semantic_action.hpp>
0024 #include <boost/spirit/home/support/handles_container.hpp>
0025 #include <boost/spirit/home/support/assert_msg.hpp>
0026 #include <boost/spirit/home/support/container.hpp>
0027 #include <boost/spirit/home/karma/detail/attributes.hpp>
0028 
0029 namespace boost { namespace spirit { namespace karma
0030 {
0031     template <typename T>
0032     struct as
0033       : stateful_tag_type<T, tag::as>
0034     {
0035         BOOST_SPIRIT_ASSERT_MSG(
0036             (traits::is_container<T>::type::value),
0037             error_type_must_be_a_container,
0038             (T));
0039     };
0040 }}}
0041 
0042 namespace boost { namespace spirit
0043 {
0044     ///////////////////////////////////////////////////////////////////////////
0045     // Enablers
0046     ///////////////////////////////////////////////////////////////////////////
0047     // enables as_string[...]
0048     template <>
0049     struct use_directive<karma::domain, tag::as_string> 
0050       : mpl::true_ {};
0051 
0052     // enables as_wstring[...]
0053     template <>
0054     struct use_directive<karma::domain, tag::as_wstring> 
0055       : mpl::true_ {};
0056 
0057     // enables as<T>[...]
0058     template <typename T>
0059     struct use_directive<karma::domain, tag::stateful_tag<T, tag::as> > 
0060       : mpl::true_ 
0061     {};
0062 }}
0063 
0064 namespace boost { namespace spirit { namespace karma
0065 {
0066 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
0067     using spirit::as_string;
0068     using spirit::as_wstring;
0069 #endif
0070     using spirit::as_string_type;
0071     using spirit::as_wstring_type;
0072 
0073     ///////////////////////////////////////////////////////////////////////////
0074     // as_directive allows to hook custom conversions to string into the
0075     // output generation process
0076     ///////////////////////////////////////////////////////////////////////////
0077     template <typename Subject, typename T>
0078     struct as_directive 
0079       : unary_generator<as_directive<Subject, T> >
0080     {
0081         typedef Subject subject_type;
0082         typedef typename subject_type::properties properties;
0083 
0084         as_directive(Subject const& subject)
0085           : subject(subject) {}
0086 
0087         template <typename Context, typename Iterator>
0088         struct attribute
0089         {
0090             typedef T type;
0091         };
0092 
0093         template <typename OutputIterator, typename Context, typename Delimiter
0094           , typename Attribute>
0095         bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
0096           , Attribute const& attr) const
0097         {
0098             if (!traits::valid_as<T>(attr))
0099                 return false;
0100 
0101             return subject.generate(sink, ctx, d, traits::as<T>(attr)) &&
0102                     karma::delimit_out(sink, d); // always do post-delimiting
0103         }
0104 
0105         template <typename Context>
0106         info what(Context& context) const
0107         {
0108             return info("as", subject.what(context));
0109         }
0110 
0111         Subject subject;
0112     };
0113 
0114     ///////////////////////////////////////////////////////////////////////////
0115     // Generator generators: make_xxx function (objects)
0116     ///////////////////////////////////////////////////////////////////////////
0117     template <typename Subject, typename Modifiers>
0118     struct make_directive<tag::as_string, Subject, Modifiers>
0119     {
0120         typedef as_directive<Subject, std::string> result_type;
0121         result_type operator()(unused_type, Subject const& subject
0122           , unused_type) const
0123         {
0124             return result_type(subject);
0125         }
0126     };
0127 
0128     template <typename Subject, typename Modifiers>
0129     struct make_directive<tag::as_wstring, Subject, Modifiers>
0130     {
0131         typedef as_directive<Subject, std::basic_string<wchar_t> > result_type;
0132         result_type operator()(unused_type, Subject const& subject
0133           , unused_type) const
0134         {
0135             return result_type(subject);
0136         }
0137     };
0138     
0139     template <typename T, typename Subject, typename Modifiers>
0140     struct make_directive<tag::stateful_tag<T, tag::as>, Subject, Modifiers>
0141     {
0142         typedef as_directive<Subject, T> result_type;
0143         result_type operator()(unused_type, Subject const& subject
0144           , unused_type) const
0145         {
0146             return result_type(subject);
0147         }
0148     };
0149 }}}
0150 
0151 namespace boost { namespace spirit { namespace traits
0152 {
0153     ///////////////////////////////////////////////////////////////////////////
0154     template <typename Subject, typename T>
0155     struct has_semantic_action<karma::as_directive<Subject, T> >
0156       : unary_has_semantic_action<Subject> {};
0157 
0158     ///////////////////////////////////////////////////////////////////////////
0159     template <typename Subject, typename T, typename Attribute
0160       , typename Context, typename Iterator>
0161     struct handles_container<karma::as_directive<Subject, T>, Attribute
0162       , Context, Iterator>
0163       : mpl::false_ {};   // always dereference attribute if used in sequences
0164 }}}
0165 
0166 #endif