• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2010 Joel de Guzman
3     Copyright (c) 2001-2010 Hartmut Kaiser
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_TEST_X3_REAL_HPP)
10 #define BOOST_SPIRIT_TEST_X3_REAL_HPP
11 
12 #include <climits>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <boost/spirit/home/x3/char.hpp>
15 #include <boost/spirit/home/x3/numeric.hpp>
16 #include <boost/spirit/home/x3/operator.hpp>
17 
18 #include "test.hpp"
19 
20 #include <boost/type_traits/type_identity.hpp>
21 
22 ///////////////////////////////////////////////////////////////////////////////
23 //  These policies can be used to parse thousand separated
24 //  numbers with at most 2 decimal digits after the decimal
25 //  point. e.g. 123,456,789.01
26 ///////////////////////////////////////////////////////////////////////////////
27 template <typename T>
28 struct ts_real_policies : boost::spirit::x3::ureal_policies<T>
29 {
30     //  2 decimal places Max
31     template <typename Iterator, typename Attribute>
32     static bool
parse_frac_nts_real_policies33     parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr)
34     {
35         namespace x3 = boost::spirit::x3;
36         return boost::spirit::x3::extract_uint<T, 10, 1, 2, true>::call(first, last, attr);
37     }
38 
39     //  No exponent
40     template <typename Iterator>
41     static bool
parse_expts_real_policies42     parse_exp(Iterator&, Iterator const&)
43     {
44         return false;
45     }
46 
47     //  No exponent
48     template <typename Iterator, typename Attribute>
49     static bool
parse_exp_nts_real_policies50     parse_exp_n(Iterator&, Iterator const&, Attribute&)
51     {
52         return false;
53     }
54 
55     //  Thousands separated numbers
56     template <typename Iterator, typename Accumulator>
57     static bool
parse_nts_real_policies58     parse_n(Iterator& first, Iterator const& last, Accumulator& result)
59     {
60         using boost::spirit::x3::uint_parser;
61         namespace x3 = boost::spirit::x3;
62 
63         uint_parser<unsigned, 10, 1, 3> uint3;
64         uint_parser<unsigned, 10, 3, 3> uint3_3;
65 
66         if (parse(first, last, uint3, result))
67         {
68             Accumulator n;
69             Iterator iter = first;
70 
71             while (x3::parse(iter, last, ',') && x3::parse(iter, last, uint3_3, n))
72             {
73                 result = result * 1000 + n;
74                 first = iter;
75             }
76 
77             return true;
78         }
79         return false;
80     }
81 };
82 
83 template <typename T>
84 struct no_trailing_dot_policy : boost::spirit::x3::real_policies<T>
85 {
86     static bool const allow_trailing_dot = false;
87 };
88 
89 template <typename T>
90 struct no_leading_dot_policy : boost::spirit::x3::real_policies<T>
91 {
92     static bool const allow_leading_dot = false;
93 };
94 
95 template <typename T>
compare(T n,boost::type_identity_t<T> expected)96 bool compare(T n, boost::type_identity_t<T> expected)
97 {
98     using std::abs;
99     using std::log10;
100     using std::pow;
101     T const eps = pow(T(10), -std::numeric_limits<T>::digits10 + log10(abs(expected)));
102     T delta = n - expected;
103     return (delta >= -eps) && (delta <= eps);
104 }
105 
106 ///////////////////////////////////////////////////////////////////////////////
107 // A custom real type
108 struct custom_real
109 {
110     double n;
custom_realcustom_real111     custom_real() : n(0) {}
custom_realcustom_real112     custom_real(double n_) : n(n_) {}
operator ==(custom_real a,custom_real b)113     friend bool operator==(custom_real a, custom_real b)
114         { return a.n == b.n; }
operator *(custom_real a,custom_real b)115     friend custom_real operator*(custom_real a, custom_real b)
116         { return custom_real(a.n * b.n); }
operator +(custom_real a,custom_real b)117     friend custom_real operator+(custom_real a, custom_real b)
118         { return custom_real(a.n + b.n); }
operator -(custom_real a,custom_real b)119     friend custom_real operator-(custom_real a, custom_real b)
120         { return custom_real(a.n - b.n); }
121 };
122 
123 #endif
124