carl  24.04
Computer ARithmetic Library
settings_utils.cpp
Go to the documentation of this file.
1 #include "settings_utils.h"
2 
3 #include <boost/any.hpp>
4 #include <boost/program_options.hpp>
5 #include <boost/spirit/include/qi.hpp>
6 
7 namespace carl::settings {
8 
9 // Adapted from https://stackoverflow.com/questions/45071699/
10 void validate(boost::any& v, const std::vector<std::string>& values, carl::settings::duration* /*unused*/, int /*unused*/) {
11  namespace pov = boost::program_options::validators;
12  namespace qi = boost::spirit::qi;
13 
14  std::string s(pov::get_single_string(values));
15 
16  long value = 0;
17  std::chrono::nanoseconds factor;
18  qi::symbols<char, std::chrono::nanoseconds> unit;
19  unit.add
20  ("ns", std::chrono::nanoseconds(1))
21  ("µs", std::chrono::microseconds(1))
22  ("us", std::chrono::microseconds(1))
23  ("ms", std::chrono::milliseconds(1))
24  ("s", std::chrono::seconds(1))
25  ("m", std::chrono::minutes(1))
26  ("h", std::chrono::hours(1))
27  ;
28  if (qi::parse(s.begin(), s.end(), qi::long_ >> unit >> qi::eoi, value, factor)) {
29  v = duration(value * factor);
30  } else {
31  throw boost::program_options::invalid_option_value(s);
32  }
33 }
34 
35 void validate(boost::any& v, const std::vector<std::string>& values, carl::settings::binary_quantity* /*unused*/, int /*unused*/) {
36  namespace pov = boost::program_options::validators;
37  namespace qi = boost::spirit::qi;
38 
39  std::string s(pov::get_single_string(values));
40 
41  std::size_t value = 0;
42  std::size_t factor = 1;
43  qi::symbols<char, std::size_t> unit;
44  unit.add
45  ("K", static_cast<std::size_t>(1) << 10U)
46  ("Ki", static_cast<std::size_t>(1) << 10U)
47  ("M", static_cast<std::size_t>(1) << 20U)
48  ("Mi", static_cast<std::size_t>(1) << 20U)
49  ("G", static_cast<std::size_t>(1) << 30U)
50  ("Gi", static_cast<std::size_t>(1) << 30U)
51  ("T", static_cast<std::size_t>(1) << 40U)
52  ("Ti", static_cast<std::size_t>(1) << 40U)
53  ("P", static_cast<std::size_t>(1) << 50U)
54  ("Pi", static_cast<std::size_t>(1) << 50U)
55  ("E", static_cast<std::size_t>(1) << 60U)
56  ("Ei", static_cast<std::size_t>(1) << 60U)
57  ;
58  if (qi::parse(s.begin(), s.end(), qi::ulong_long >> -unit >> qi::eoi, value, factor)) {
59  v = binary_quantity(value * factor);
60  } else {
61  throw boost::program_options::invalid_option_value(s);
62  }
63 }
64 
65 void validate(boost::any& v, const std::vector<std::string>& values, carl::settings::metric_quantity* /*unused*/, int /*unused*/) {
66  namespace pov = boost::program_options::validators;
67  namespace qi = boost::spirit::qi;
68 
69  std::string s(pov::get_single_string(values));
70 
71  std::size_t value = 0;
72  std::size_t factor = 1;
73  qi::symbols<char, std::size_t> unit;
74  unit.add
75  ("K", 1000)
76  ("M", 1000000)
77  ("G", 1000000000)
78  ("T", 1000000000000)
79  ("P", 1000000000000000)
80  ("E", 1000000000000000000)
81  ;
82  if (qi::parse(s.begin(), s.end(), qi::ulong_long >> -unit >> qi::eoi, value, factor)) {
83  v = metric_quantity(value * factor);
84  } else {
85  throw boost::program_options::invalid_option_value(s);
86  }
87 }
88 
89 }
T parse(const std::string &n)
void validate(boost::any &v, const std::vector< std::string > &values, carl::settings::duration *, int)
Custom validator for duration that wraps some std::chrono::duration.
std::chrono::duration< std::size_t, std::milli > duration
The duration type used here.
Definition: Timing.h:12
Helper type to parse duration as std::chrono values with boost::program_options.
Helper type to parse quantities with binary SI-style suffixes.
Helper type to parse quantities with SI-style suffixes.