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