Chris@16: // Copyright (c) 2001-2011 Hartmut Kaiser Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #if !defined(BOOST_SPIRIT_KARMA_UINT_FEB_23_2007_0840PM) Chris@16: #define BOOST_SPIRIT_KARMA_UINT_FEB_23_2007_0840PM Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace spirit Chris@16: { Chris@16: namespace tag Chris@16: { Chris@16: template Chris@16: struct uint_generator Chris@16: { Chris@16: BOOST_SPIRIT_IS_TAG() Chris@16: }; Chris@16: } Chris@16: Chris@16: namespace karma Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////// Chris@16: // This one is the class that the user can instantiate directly in Chris@16: // order to create a customized int generator Chris@16: template Chris@16: struct uint_generator Chris@16: : spirit::terminal > Chris@16: {}; Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Enablers Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template <> Chris@16: struct use_terminal // enables ushort_ Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> Chris@16: struct use_terminal // enables uint_ Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> Chris@16: struct use_terminal // enables ulong_ Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> Chris@16: struct use_terminal // enables bin Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> Chris@16: struct use_terminal // enables oct Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> Chris@16: struct use_terminal // enables hex Chris@16: : mpl::true_ {}; Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template <> Chris@16: struct use_terminal // enables ulong_long Chris@16: : mpl::true_ {}; Chris@16: #endif Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) Chris@16: template <> // enables lit(unsigned short(0)) Chris@16: struct use_terminal Chris@16: : mpl::true_ {}; Chris@16: #endif Chris@16: Chris@16: template <> // enables lit(0U) Chris@16: struct use_terminal Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> // enables lit(0UL) Chris@16: struct use_terminal Chris@16: : mpl::true_ {}; Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template <> // enables lit(0ULL) Chris@16: struct use_terminal Chris@16: : mpl::true_ {}; Chris@16: #endif Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template Chris@16: struct use_terminal > Chris@16: > : mpl::true_ {}; Chris@16: #endif Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template <> // enables *lazy* ushort_(...) Chris@16: struct use_lazy_terminal Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> // enables *lazy* uint_(...) Chris@16: struct use_lazy_terminal Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> // enables *lazy* ulong_(...) Chris@16: struct use_lazy_terminal Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> // enables *lazy* bin(...) Chris@16: struct use_lazy_terminal Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> // enables *lazy* oct(...) Chris@16: struct use_lazy_terminal Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> // enables *lazy* hex(...) Chris@16: struct use_lazy_terminal Chris@16: : mpl::true_ {}; Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template <> // enables *lazy* ulong_long(...) Chris@16: struct use_lazy_terminal Chris@16: : mpl::true_ {}; Chris@16: #endif Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // enables any custom uint_generator Chris@16: template Chris@16: struct use_terminal > Chris@16: : mpl::true_ {}; Chris@16: Chris@16: // enables any custom uint_generator(...) Chris@16: template Chris@16: struct use_terminal, fusion::vector1 > Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: // enables *lazy* custom uint_generator Chris@16: template Chris@16: struct use_lazy_terminal< Chris@16: karma::domain Chris@16: , tag::uint_generator Chris@16: , 1 // arity Chris@16: > : mpl::true_ {}; Chris@16: Chris@16: // enables lit(uint) Chris@16: template Chris@16: struct use_terminal > Chris@16: , typename enable_if >::type> Chris@16: : mpl::true_ {}; Chris@16: }} Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace boost { namespace spirit { namespace karma Chris@16: { Chris@16: #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS Chris@16: using spirit::ushort_; Chris@16: using spirit::uint_; Chris@16: using spirit::ulong_; Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: using spirit::ulong_long; Chris@16: #endif Chris@16: using spirit::bin; Chris@16: using spirit::oct; Chris@16: using spirit::hex; Chris@16: Chris@16: using spirit::lit; // lit(1U) is equivalent to 1U Chris@16: #endif Chris@16: Chris@16: using spirit::ushort_type; Chris@16: using spirit::uint_type; Chris@16: using spirit::ulong_type; Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: using spirit::ulong_long_type; Chris@16: #endif Chris@16: using spirit::bin_type; Chris@16: using spirit::oct_type; Chris@16: using spirit::hex_type; Chris@16: Chris@16: using spirit::lit_type; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // This specialization is used for unsigned int generators not having a Chris@16: // direct initializer: uint_, ulong_ etc. These generators must be used in Chris@16: // conjunction with an Attribute. Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct any_uint_generator Chris@16: : primitive_generator > Chris@16: { Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: typedef T type; Chris@16: }; Chris@16: Chris@16: // check template Attribute 'Radix' for validity Chris@16: BOOST_SPIRIT_ASSERT_MSG( Chris@16: Radix >= 2 && Radix <= 36, not_supported_radix, ()); Chris@16: Chris@16: BOOST_SPIRIT_ASSERT_MSG( Chris@16: // the following is a workaround for STLPort, where the simpler Chris@16: // `!std::numeric_limits::is_signed` wouldn't compile Chris@16: mpl::not_::is_signed> >::value, Chris@16: signed_unsigned_mismatch, ()); Chris@16: Chris@16: // int has a Attribute attached Chris@16: template Chris@16: static bool Chris@16: generate(OutputIterator& sink, Context& context, Delimiter const& d Chris@16: , Attribute const& attr) Chris@16: { Chris@16: if (!traits::has_optional_value(attr)) Chris@16: return false; // fail if it's an uninitialized optional Chris@16: Chris@16: return uint_inserter:: Chris@16: call(sink, traits::extract_from(attr, context)) && Chris@16: delimit_out(sink, d); // always do post-delimiting Chris@16: } Chris@16: Chris@16: // this int has no Attribute attached, it needs to have been Chris@16: // initialized from a direct literal Chris@16: template Chris@16: static bool Chris@16: generate(OutputIterator&, Context&, Delimiter const&, unused_type) Chris@16: { Chris@16: // It is not possible (doesn't make sense) to use numeric generators Chris@16: // without providing any attribute, as the generator doesn't 'know' Chris@16: // what to output. The following assertion fires if this situation Chris@16: // is detected in your code. Chris@16: BOOST_SPIRIT_ASSERT_FAIL(OutputIterator, uint_not_usable_without_attribute, ()); Chris@16: return false; Chris@16: } Chris@16: Chris@16: template Chris@16: static info what(Context const& /*context*/) Chris@16: { Chris@16: return info("unsigned-integer"); Chris@16: } Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // This specialization is used for unsigned int generators having a direct Chris@16: // initializer: uint_(10), ulong_(20) etc. Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template < Chris@16: typename T, typename CharEncoding, typename Tag, unsigned Radix Chris@16: , bool no_attribute> Chris@16: struct literal_uint_generator Chris@16: : primitive_generator > Chris@16: { Chris@16: template Chris@16: struct attribute Chris@16: : mpl::if_c Chris@16: {}; Chris@16: Chris@16: literal_uint_generator(typename add_const::type n) Chris@16: : n_(n) {} Chris@16: Chris@16: // check template Attribute 'Radix' for validity Chris@16: BOOST_SPIRIT_ASSERT_MSG( Chris@16: Radix >= 2 && Radix <= 36, not_supported_radix, ()); Chris@16: Chris@16: BOOST_SPIRIT_ASSERT_MSG( Chris@16: // the following is a workaround for STLPort, where the simpler Chris@16: // `!std::numeric_limits::is_signed wouldn't` compile Chris@16: mpl::not_::is_signed> >::value, Chris@16: signed_unsigned_mismatch, ()); Chris@16: Chris@16: // A uint(1U) which additionally has an associated attribute emits Chris@16: // its immediate literal only if it matches the attribute, otherwise Chris@16: // it fails. Chris@16: template Chris@16: bool generate(OutputIterator& sink, Context& context Chris@16: , Delimiter const& d, Attribute const& attr) const Chris@16: { Chris@16: typedef typename attribute::type attribute_type; Chris@16: if (!traits::has_optional_value(attr) || Chris@16: n_ != traits::extract_from(attr, context)) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: return uint_inserter::call(sink, n_) && Chris@16: delimit_out(sink, d); // always do post-delimiting Chris@16: } Chris@16: Chris@16: // A uint(1U) without any associated attribute just emits its Chris@16: // immediate literal Chris@16: template Chris@16: bool generate(OutputIterator& sink, Context&, Delimiter const& d Chris@16: , unused_type) const Chris@16: { Chris@16: return uint_inserter::call(sink, n_) && Chris@16: delimit_out(sink, d); // always do post-delimiting Chris@16: } Chris@16: Chris@16: template Chris@16: static info what(Context const& /*context*/) Chris@16: { Chris@16: return info("unsigned-integer"); Chris@16: } Chris@16: Chris@16: T n_; Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Generator generators: make_xxx function (objects) Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: struct make_uint Chris@16: { Chris@16: static bool const lower = Chris@16: has_modifier >::value; Chris@16: static bool const upper = Chris@16: has_modifier >::value; Chris@16: Chris@16: typedef any_uint_generator< Chris@16: T Chris@16: , typename spirit::detail::get_encoding_with_case< Chris@16: Modifiers, unused_type, lower || upper>::type Chris@16: , typename detail::get_casetag::type Chris@16: , Radix Chris@16: > result_type; Chris@16: Chris@16: result_type operator()(unused_type, unused_type) const Chris@16: { Chris@16: return result_type(); Chris@16: } Chris@16: }; Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::make_uint {}; Chris@16: Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::make_uint {}; Chris@16: Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::make_uint {}; Chris@16: Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::make_uint {}; Chris@16: Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::make_uint {}; Chris@16: Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::make_uint {}; Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::make_uint {}; Chris@16: #endif Chris@16: Chris@16: template Chris@16: struct make_primitive, Modifiers> Chris@16: : detail::make_uint::type, Modifiers, Radix> {}; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: struct make_uint_direct Chris@16: { Chris@16: static bool const lower = Chris@16: has_modifier >::value; Chris@16: static bool const upper = Chris@16: has_modifier >::value; Chris@16: Chris@16: typedef literal_uint_generator< Chris@16: T Chris@16: , typename spirit::detail::get_encoding_with_case< Chris@16: Modifiers, unused_type, lower || upper>::type Chris@16: , typename detail::get_casetag::type Chris@16: , Radix, false Chris@16: > result_type; Chris@16: Chris@16: template Chris@16: result_type operator()(Terminal const& term, unused_type) const Chris@16: { Chris@16: return result_type(fusion::at_c<0>(term.args)); Chris@16: } Chris@16: }; Chris@16: } Chris@16: Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : detail::make_uint_direct {}; Chris@16: Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : detail::make_uint_direct {}; Chris@16: Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : detail::make_uint_direct {}; Chris@16: Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : detail::make_uint_direct {}; Chris@16: Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : detail::make_uint_direct {}; Chris@16: Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : detail::make_uint_direct {}; Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex >, Modifiers> Chris@16: : detail::make_uint_direct {}; Chris@16: #endif Chris@16: Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex, fusion::vector1 > Chris@16: , Modifiers> Chris@16: : detail::make_uint_direct::type, Modifiers, Radix> Chris@16: {}; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: struct basic_uint_literal Chris@16: { Chris@16: static bool const lower = Chris@16: has_modifier >::value; Chris@16: static bool const upper = Chris@16: has_modifier >::value; Chris@16: Chris@16: typedef literal_uint_generator< Chris@16: T Chris@16: , typename spirit::detail::get_encoding_with_case< Chris@16: Modifiers, unused_type, lower || upper>::type Chris@16: , typename detail::get_casetag::type Chris@16: , 10, true Chris@16: > result_type; Chris@16: Chris@16: template Chris@16: result_type operator()(T_ i, unused_type) const Chris@16: { Chris@16: return result_type(i); Chris@16: } Chris@16: }; Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::basic_uint_literal {}; Chris@16: #endif Chris@16: Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::basic_uint_literal {}; Chris@16: Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::basic_uint_literal {}; Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template Chris@16: struct make_primitive Chris@16: : detail::basic_uint_literal {}; Chris@16: #endif Chris@16: Chris@16: // lit(uint) Chris@16: template Chris@16: struct make_primitive< Chris@16: terminal_ex > Chris@16: , Modifiers Chris@16: , typename enable_if >::type> Chris@16: { Chris@16: static bool const lower = Chris@16: has_modifier >::value; Chris@16: static bool const upper = Chris@16: has_modifier >::value; Chris@16: Chris@16: typedef literal_uint_generator< Chris@16: typename remove_const::type Chris@16: , typename spirit::detail::get_encoding_with_case< Chris@16: Modifiers, unused_type, lower || upper>::type Chris@16: , typename detail::get_casetag::type Chris@16: , 10, true Chris@16: > result_type; Chris@16: Chris@16: template Chris@16: result_type operator()(Terminal const& term, unused_type) const Chris@16: { Chris@16: return result_type(fusion::at_c<0>(term.args)); Chris@16: } Chris@16: }; Chris@16: }}} Chris@16: Chris@16: #endif