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