// 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(BOOST_SPIRIT_KARMA_REAL_FEB_26_2007_0512PM) #define BOOST_SPIRIT_KARMA_REAL_FEB_26_2007_0512PM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { namespace karma { /////////////////////////////////////////////////////////////////////// // forward declaration only template struct real_policies; /////////////////////////////////////////////////////////////////////// // This is the class that the user can instantiate directly in // order to create a customized real generator template > struct real_generator : spirit::terminal > { typedef tag::stateful_tag tag_type; real_generator() {} real_generator(Policies const& p) : spirit::terminal(p) {} }; } /////////////////////////////////////////////////////////////////////////// // Enablers /////////////////////////////////////////////////////////////////////////// template <> struct use_terminal // enables float_ : mpl::true_ {}; template <> struct use_terminal // enables double_ : mpl::true_ {}; template <> struct use_terminal // enables long_double : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// template <> struct use_terminal // enables lit(1.0f) : mpl::true_ {}; template <> struct use_terminal // enables lit(1.0) : mpl::true_ {}; template <> struct use_terminal // enables lit(1.0l) : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// template struct use_terminal > > : mpl::true_ {}; template struct use_terminal > > : mpl::true_ {}; template struct use_terminal > > : mpl::true_ {}; // lazy float_(...), double_(...), long_double(...) template <> struct use_lazy_terminal : mpl::true_ {}; template <> struct use_lazy_terminal : mpl::true_ {}; template <> struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// // enables custom real generator template struct use_terminal > : mpl::true_ {}; template struct use_terminal , fusion::vector1 > > : mpl::true_ {}; // enables *lazy* custom real generator template struct use_lazy_terminal< karma::domain , tag::stateful_tag , 1 // arity > : mpl::true_ {}; // enables lit(double) template struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; }} /////////////////////////////////////////////////////////////////////////////// namespace boost { namespace spirit { namespace karma { #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS using spirit::float_; using spirit::double_; using spirit::long_double; #endif using spirit::float_type; using spirit::double_type; using spirit::long_double_type; /////////////////////////////////////////////////////////////////////////// // This specialization is used for real generators not having a direct // initializer: float_, double_ etc. These generators must be used in // conjunction with an attribute. /////////////////////////////////////////////////////////////////////////// template < typename T, typename Policies, typename CharEncoding, typename Tag> struct any_real_generator : primitive_generator > { typedef typename Policies::properties properties; template struct attribute { typedef T type; }; any_real_generator(Policies const& policies = Policies()) : p_(policies) {} // double_/float_/etc. has an attached attribute template bool generate(OutputIterator& sink, Context& context , Delimiter const& d, Attribute const& attr) const { if (!traits::has_optional_value(attr)) return false; // fail if it's an uninitialized optional typedef real_inserter inserter_type; return inserter_type::call(sink, traits::extract_from(attr, context), p_) && karma::delimit_out(sink, d); // always do post-delimiting } // this double_/float_/etc. has no attribute attached, it needs to have // been initialized from a direct literal template static bool generate(OutputIterator&, Context&, Delimiter const& , unused_type) { // It is not possible (doesn't make sense) to use numeric generators // without providing any attribute, as the generator doesn't 'know' // what to output. The following assertion fires if this situation // is detected in your code. BOOST_SPIRIT_ASSERT_FAIL(OutputIterator, real_not_usable_without_attribute, ()); return false; } template static info what(Context const& /*context*/) { return info("real"); } Policies p_; }; /////////////////////////////////////////////////////////////////////////// // This specialization is used for real generators having a direct // initializer: float_(10.), double_(20.) etc. /////////////////////////////////////////////////////////////////////////// template < typename T, typename Policies, typename CharEncoding, typename Tag , bool no_attribute> struct literal_real_generator : primitive_generator > { typedef typename Policies::properties properties; template struct attribute : mpl::if_c {}; literal_real_generator(typename add_const::type n , Policies const& policies = Policies()) : n_(n), p_(policies) {} // A double_(1.0) which additionally has an associated attribute emits // its immediate literal only if it matches the attribute, otherwise // it fails. template bool generate(OutputIterator& sink, Context& context , Delimiter const& d, Attribute const& attr) const { typedef typename attribute::type attribute_type; if (!traits::has_optional_value(attr) || n_ != traits::extract_from(attr, context)) { return false; } typedef real_inserter inserter_type; return inserter_type::call(sink, n_, p_) && karma::delimit_out(sink, d); // always do post-delimiting } // A double_(1.0) without any associated attribute just emits its // immediate literal template bool generate(OutputIterator& sink, Context&, Delimiter const& d , unused_type) const { typedef real_inserter inserter_type; return inserter_type::call(sink, n_, p_) && karma::delimit_out(sink, d); // always do post-delimiting } template static info what(Context const& /*context*/) { return info("real"); } T n_; Policies p_; }; /////////////////////////////////////////////////////////////////////////// // Generator generators: make_xxx function (objects) /////////////////////////////////////////////////////////////////////////// namespace detail { template > struct make_real { static bool const lower = has_modifier >::value; static bool const upper = has_modifier >::value; typedef any_real_generator< T, Policies , typename spirit::detail::get_encoding_with_case< Modifiers, unused_type, lower || upper>::type , typename detail::get_casetag::type > result_type; template result_type operator()(Terminal const& term, unused_type) const { typedef tag::stateful_tag tag_type; using spirit::detail::get_stateful_data; return result_type(get_stateful_data::call(term)); } }; } template struct make_primitive : detail::make_real {}; template struct make_primitive : detail::make_real {}; template struct make_primitive : detail::make_real {}; /////////////////////////////////////////////////////////////////////////// template struct make_primitive< tag::stateful_tag, Modifiers> : detail::make_real::type , Modifiers, Policies> {}; /////////////////////////////////////////////////////////////////////////// namespace detail { template > struct make_real_direct { static bool const lower = has_modifier >::value; static bool const upper = has_modifier >::value; typedef literal_real_generator< T, Policies , typename spirit::detail::get_encoding_with_case< Modifiers, unused_type, lower || upper>::type , typename detail::get_casetag::type , false > result_type; template result_type operator()(Terminal const& term, unused_type) const { typedef tag::stateful_tag tag_type; using spirit::detail::get_stateful_data; return result_type(T(fusion::at_c<0>(term.args)) , get_stateful_data::call(term.term)); } }; } template struct make_primitive< terminal_ex >, Modifiers> : detail::make_real_direct {}; template struct make_primitive< terminal_ex >, Modifiers> : detail::make_real_direct {}; template struct make_primitive< terminal_ex >, Modifiers> : detail::make_real_direct {}; /////////////////////////////////////////////////////////////////////////// template struct make_primitive< terminal_ex , fusion::vector1 > , Modifiers> : detail::make_real_direct::type , Modifiers, Policies> {}; /////////////////////////////////////////////////////////////////////////// namespace detail { template struct basic_real_literal { static bool const lower = has_modifier >::value; static bool const upper = has_modifier >::value; typedef literal_real_generator< T, real_policies , typename spirit::detail::get_encoding_with_case< Modifiers, unused_type, lower || upper>::type , typename detail::get_casetag::type , true > result_type; template result_type operator()(T_ i, unused_type) const { return result_type(T(i)); } }; } template struct make_primitive : detail::basic_real_literal {}; template struct make_primitive : detail::basic_real_literal {}; template struct make_primitive : detail::basic_real_literal {}; // lit(double) template struct make_primitive< terminal_ex > , Modifiers , typename enable_if >::type> { static bool const lower = has_modifier >::value; static bool const upper = has_modifier >::value; typedef literal_real_generator< typename remove_const::type, real_policies , typename spirit::detail::get_encoding_with_case< Modifiers, unused_type, lower || upper>::type , typename detail::get_casetag::type , true > result_type; template result_type operator()(Terminal const& term, unused_type) const { return result_type(fusion::at_c<0>(term.args)); } }; }}} #endif // defined(BOOST_SPIRIT_KARMA_REAL_FEB_26_2007_0512PM)