📄 numerics.hpp
字号:
/*=============================================================================
Numeric parsers
Spirit V1.3.1
Copyright (c) 2001, Joel de Guzman
This software is provided 'as-is', without any express or implied
warranty. In no event will the copyright holder be held liable for
any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Acknowledgements:
Special thanks to Dan Nuffer, John (EBo) David, Chris Uzdavinis,
and Doug Gregor. These people are most instrumental in steering
Spirit in the right direction.
Special thanks also to people who have contributed to the code base
and sample code, ported Spirit to various platforms and compilers,
gave suggestions, reported and provided bug fixes. Alexander
Hirner, Andy Elvey, Bogdan Kushnir, Brett Calcott, Bruce Florman,
Changzhe Han, Colin McPhail, Hakki Dogusan, Jan Bares, Joseph
Smith, Martijn W. van der Lee, Raghavendra Satish, Remi Delcos, Tom
Spilman, Vladimir Prus, W. Scott Dillman, David A. Greene, Bob
Bailey, Hartmut Kaiser.
Finally special thanks also to people who gave feedback and
valuable comments, particularly members of Spirit's Source Forge
mailing list and boost.org.
URL: http://spirit.sourceforge.net/
=============================================================================*/
#ifndef SPIRIT_NUMERICS_HPP
#define SPIRIT_NUMERICS_HPP
#include "boost/spirit/MSVC/ps_helper.hpp"
#include "boost/config.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace spirit {
///////////////////////////////////////////////////////////////////////////////
//
// numeric_action class
//
// Links a numeric parser with a user defined semantic action.
// The semantic action may be a function or a functor. A function
// should be compatible with the interface:
//
// void f(NumT num);
//
// A functor should have a member operator() with a compatible
// signature as above. The matching number is passed into the
// function/functor. This is the default class that numeric
// parsers use when dealing with the construct:
//
// p[f]
//
// where p is a parser and f is a function or functor. The header
// file <actions.hpp> has some useful predefined generic functors
// that may be used to directly link variables with numeric parsers
// (see reference_wrapper and context_wrapper classes.
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT>
class numeric_action
: public unary<ParserT>,
public parser<numeric_action<ParserT, ActionT> > {
public:
typedef action_parser_category parser_category;
numeric_action(ActionT const& actor_)
: unary<ParserT>(ParserT(0)), actor(actor_) {}
template <typename IteratorT>
match
parse(IteratorT& first, IteratorT const& last) const
{
match hit = this->subject().parse(first, last);
if (hit)
actor(this->subject().n);
return hit;
}
ActionT const &predicate() const { return actor; }
private:
typename embed_trait<ActionT>::type actor;
};
///////////////////////////////////////////////////////////////////////////////
// Simple traits class for customizing locale specifics. This allows for
// customization of the parsing algorithm in dependency of the used locale,
// format of the number etc.
// Use this traits template, if you need single character delimiters.
// The default separators used here are for parsing C-style numbers.
template<const int ds = '.', const int ts = '\0', const int exp = 'e'>
struct numeric_parser_traits
{
enum real_parser_chars {
decimal_sep_char = ds, // character used as decimal point
thousands_sep_char = ts, // character used as thousands separator
exponent_char = exp // character used as delimiter before exponent
};
typedef const int DecimalSepT;
typedef const int ThousandsSepT;
typedef const int ExponentSepT;
};
///////////////////////////////////////////////////////////////////////////////
// More complex traits class for customizing locale specifics. This allows for
// customization of the parsing algorithm in dependency of the used locale,
// format of the number etc.
// Use this traits template, if you need more complex delimiters such as static
// parsers et.al.
template<typename DecimalParserT, typename ThousandsParserT,
typename ExponentParserT>
struct numeric_parser_traits_ex
{
typedef DecimalParserT DecimalSepT;
typedef ThousandsParserT ThousandsSepT;
typedef ExponentParserT ExponentSepT;
static DecimalSepT decimal_sep_char; // delimiter used as decimal point
static ThousandsSepT thousands_sep_char; // delimiter used as thousands
// separator
static ExponentSepT exponent_char; // delimiter used before exponent
};
template<typename DecimalParserT, typename ThousandsParserT,
typename ExponentParserT>
numeric_parser_traits_ex<DecimalParserT, ThousandsParserT, ExponentParserT>
::DecimalSepT
numeric_parser_traits_ex<DecimalParserT, ThousandsParserT, ExponentParserT>
::decimal_sep_char;
template<typename DecimalParserT, typename ThousandsParserT,
typename ExponentParserT>
numeric_parser_traits_ex<DecimalParserT, ThousandsParserT, ExponentParserT>
::ThousandsSepT
numeric_parser_traits_ex<DecimalParserT, ThousandsParserT, ExponentParserT>
::thousands_sep_char;
template<typename DecimalParserT, typename ThousandsParserT,
typename ExponentParserT>
numeric_parser_traits_ex<DecimalParserT, ThousandsParserT, ExponentParserT>
::ExponentSepT
numeric_parser_traits_ex<DecimalParserT, ThousandsParserT, ExponentParserT>
::exponent_char;
///////////////////////////////////////////////////////////////////////////////
// forward declarations
template <typename T = unsigned, const int Radix = 10> struct uint_parser;
template <typename T = int, const int Radix = 10> struct int_parser;
template <typename T = double, typename TraitsT = numeric_parser_traits<> >
struct ureal_parser;
template <typename T = double, typename TraitsT = numeric_parser_traits<> >
struct real_parser;
///////////////////////////////////////////////////////////////////////////////
// various workaorunds to support numeric traits
///////////////////////////////////////////////////////////////////////////////
namespace impl
{
//iat - int_action_traits
//rat - real_action_traits
//////////////////////////////////////////////////////////////////////////
template< typename T>
selector1 iat_helper(void(*)(T) );
template< typename T>
selector2 iat_helper(reference_wrapper<T> );
selector3 iat_helper(...);
template<int i>
struct iat_selector
{
//functor
template<class Type , class T, const int Radix>
struct local {
typedef typename T::argument_type arg_type;
typedef numeric_action<uint_parser<arg_type, Radix>, T> uint_action;
typedef numeric_action<int_parser<arg_type, Radix>, T> int_action;
};
};
template<>
struct iat_selector<1>
{
//function
template<class Type, class T, const int Radix>
struct local {
typedef numeric_action<uint_parser<Type, Radix>, T> uint_action;
typedef numeric_action<int_parser<Type, Radix>, T> int_action;
};
};
template<>
struct iat_selector<2>
{
//reference_wrapper
template<class Type ,class T, const int Radix>
struct local {
typedef uint_parser<T, Radix> uint_action;
typedef int_parser<T, Radix> int_action;
};
};
////////////////////////////////////////////////////////////////////////
template< typename T>
selector1 rat_helper(void(*)(T) );
template< typename T>
selector2 rat_helper(reference_wrapper<T> const & );
selector3 rat_helper(...);
template<int i>
struct rat_selector
{
template<class Type, class T, class Radix>
struct local {
//functor
typedef typename T::argument_type arg_type;
typedef numeric_action<ureal_parser<arg_type, Radix>, T> ureal_action;
typedef numeric_action<real_parser<arg_type, Radix>, T> real_action;
};
};
template<>
struct rat_selector<1>
{
template<class Type, class T, class Radix>
struct local {
//function
typedef numeric_action<ureal_parser<Type, Radix>, T> ureal_action;
typedef numeric_action<real_parser<Type, Radix>, T> real_action;
};
};
template<>
struct rat_selector<2>
{
template<class Type, class T, class Radix>
struct local {
//reference wrapper
typedef ureal_parser<T, Radix> ureal_action;
typedef real_parser<T, Radix> real_action;
};
};
} //end namespace impl
template <typename Type, typename T, const int Radix = 10>
struct int_numeric_action_traits {
static T t();
typedef typename impl::iat_selector<(sizeof(impl::iat_helper(t())))>::
template local<Type, T,Radix> iat;
typedef typename iat::uint_action uint_action;
typedef typename iat::int_action int_action;
};
template <typename Type , typename T, typename TraitsT = numeric_parser_traits<> >
struct real_numeric_action_traits {
static T t();
typedef typename impl::rat_selector<(sizeof(impl::rat_helper(t())))>::
template local<Type,T,TraitsT> rat;
typedef typename rat::ureal_action ureal_action;
typedef typename rat::real_action real_action;
};
} //end namespace spirit
#include "boost/spirit/MSVC/impl/numerics.ipp"
namespace spirit
{
///////////////////////////////////////////////////////////////////////////////
//
// uint_parser class
//
// Parses unsigned integers. A uint_parser is never directly
// instantiated. An instance of the uint_parser_gen, uint_p,
// generates a uint_parser through its [] operator:
//
// uint_p[f]
//
// where f is a semantic action which may be a function or a
// functor. A function should be compatible with the interface:
//
// void f(NumT num);
//
// A functor should have a member operator() with a compatible
// signature as above. The matching number is passed into the
// function/functor (see numeric_action class above).
//
///////////////////////////////////////////////////////////////////////////////
template <typename T = unsigned , const int Radix = 10>
struct uint_parser : public parser<uint_parser<T, Radix> > {
typedef typename impl::IF<is_reference_wrapper<T>::value,
typename unwrap_reference_wrapper<T>::type&,
T>::RET actual_type;
uint_parser(actual_type n_)
: n(n_) {}
template <typename IteratorT>
match
parse(IteratorT& first, IteratorT const& last) const
{
n = 0;
if (first == last)
return match();
typedef typename impl::strip_scanner<IteratorT>
::iterator_type plain_iter;
plain_iter i1 = impl::strip_scanner<IteratorT>::get(first);
plain_iter i2 = impl::strip_scanner<IteratorT>::get(last);
unsigned count = 0;
if (!impl::radix_traits<Radix>::isok(*i1))
return match();
if (impl::extract<Radix>::uint(i1, i2, n, count)) {
first = i1;
return match(count);
}
return match(); // return non-match if uint overflows
}
mutable actual_type n;
};
///////////////////////////////////////////////////////////////////////////////
template<const int Radix>
struct uint_parser_gen : public uint_parser<unsigned, Radix> {
uint_parser_gen()
: uint_parser<unsigned, Radix>(0) {}
template <typename T>
typename int_numeric_action_traits<T, void (*)(T), Radix>::uint_action
operator [] (void (*actor)(T)) const
{
// Borland 5.5 reports an internal compiler
// error if this is not defined here.
typedef typename int_numeric_action_traits<T, void(*)(T), Radix>::uint_action
action_t;
return action_t(actor);
}
template <typename T>
typename int_numeric_action_traits<typename unwrap_reference_wrapper<T>::type,
T, Radix>::uint_action
operator [] (T const& actor) const
{
// Borland 5.5 reports an internal compiler
// error if this is not defined here.
typedef typename int_numeric_action_traits<unwrap_reference_wrapper<T>::type,
T, Radix>::uint_action action_t;
return action_t(actor);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -