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_QI_REAL_HPP)
10 #define BOOST_SPIRIT_TEST_QI_REAL_HPP
11
12 #include <climits>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <boost/spirit/include/qi_char.hpp>
15 #include <boost/spirit/include/qi_numeric.hpp>
16 #include <boost/spirit/include/qi_operator.hpp>
17
18 #include "test.hpp"
19
20 #include <boost/core/cmath.hpp>
21
22 #ifndef BOOST_NO_CXX11_SFINAE_EXPR
23 # include <boost/math/concepts/real_concept.hpp>
24 #else
25 # define BOOST_SPIRIT_NO_MATH_REAL_CONCEPT
26 #endif
27
28 ///////////////////////////////////////////////////////////////////////////////
29 // These policies can be used to parse thousand separated
30 // numbers with at most 2 decimal digits after the decimal
31 // point. e.g. 123,456,789.01
32 ///////////////////////////////////////////////////////////////////////////////
33 template <typename T>
34 struct ts_real_policies : boost::spirit::qi::ureal_policies<T>
35 {
36 // 2 decimal places Max
37 template <typename Iterator, typename Attribute>
38 static bool
parse_frac_nts_real_policies39 parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr, int& frac_digits)
40 {
41 namespace qi = boost::spirit::qi;
42 Iterator savef = first;
43 bool r = qi::extract_uint<Attribute, 10, 1, 2, true>::call(first, last, attr);
44 frac_digits = static_cast<int>(std::distance(savef, first));
45 return r;
46 }
47
48 // No exponent
49 template <typename Iterator>
50 static bool
parse_expts_real_policies51 parse_exp(Iterator&, Iterator const&)
52 {
53 return false;
54 }
55
56 // No exponent
57 template <typename Iterator, typename Attribute>
58 static bool
parse_exp_nts_real_policies59 parse_exp_n(Iterator&, Iterator const&, Attribute&)
60 {
61 return false;
62 }
63
64 // Thousands separated numbers
65 template <typename Iterator, typename Accumulator>
66 static bool
parse_nts_real_policies67 parse_n(Iterator& first, Iterator const& last, Accumulator& result)
68 {
69 using boost::spirit::qi::uint_parser;
70 namespace qi = boost::spirit::qi;
71
72 uint_parser<unsigned, 10, 1, 3> uint3;
73 uint_parser<unsigned, 10, 3, 3> uint3_3;
74
75 if (parse(first, last, uint3, result))
76 {
77 Accumulator n;
78 Iterator iter = first;
79
80 while (qi::parse(iter, last, ',') && qi::parse(iter, last, uint3_3, n))
81 {
82 result = result * 1000 + n;
83 first = iter;
84 }
85
86 return true;
87 }
88 return false;
89 }
90 };
91
92 template <typename T>
93 struct no_trailing_dot_policy : boost::spirit::qi::real_policies<T>
94 {
95 static bool const allow_trailing_dot = false;
96 };
97
98 template <typename T>
99 struct no_leading_dot_policy : boost::spirit::qi::real_policies<T>
100 {
101 static bool const allow_leading_dot = false;
102 };
103
104 template <typename T>
105 bool
compare(T n,double expected,T const eps=std::pow (10.0,-std::numeric_limits<T>::digits10))106 compare(T n, double expected
107 , T const eps = std::pow(10.0, -std::numeric_limits<T>::digits10))
108 {
109 T delta = n - expected;
110 return (delta >= -eps) && (delta <= eps);
111 }
112
113 ///////////////////////////////////////////////////////////////////////////////
114 // A custom real type
115 struct custom_real
116 {
117 double n;
custom_realcustom_real118 custom_real() : n(0) {}
custom_realcustom_real119 custom_real(double n_) : n(n_) {}
operator ==(custom_real a,custom_real b)120 friend bool operator==(custom_real a, custom_real b)
121 { return a.n == b.n; }
operator *(custom_real a,custom_real b)122 friend custom_real operator*(custom_real a, custom_real b)
123 { return custom_real(a.n * b.n); }
operator +(custom_real a,custom_real b)124 friend custom_real operator+(custom_real a, custom_real b)
125 { return custom_real(a.n + b.n); }
operator -(custom_real a,custom_real b)126 friend custom_real operator-(custom_real a, custom_real b)
127 { return custom_real(a.n - b.n); }
128 };
129
130 #endif
131