libzypp  17.37.5
JsonNumber.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 #ifndef ZYPP_CORE_PARSER_JSON_JSON_NUMBER_DEFINED
10 #define ZYPP_CORE_PARSER_JSON_JSON_NUMBER_DEFINED
11 
14 #include <string>
15 #include <cstdint>
16 
17 namespace zypp::json {
18 
19  class Number {
20 
21  public:
22  Number() = default;
23  explicit Number( double v ) : _value(v) {}
24  explicit Number( float v ) : _value(v) {}
25  ~Number() = default;
26 
27  Number( const Number & ) = default;
28  Number( Number && ) = default;
29  Number &operator=(const Number &) = default;
30  Number &operator=(Number &&) = default;
31 
32  static zyppng::expected<Number> fromString ( const std::string &str ) {
33  using namespace zyppng::operators;
34  using zyppng::operators::operator|;
35  return zyppng::mtry([&](){ return std::stod( str, nullptr ); })
36  | and_then([](double res) -> zyppng::expected<Number> { return zyppng::make_expected_success( Number(res)); } );
37  }
38 
39  operator double() const {
40  return _value;
41  }
42 
43  double value() const {
44  return _value;
45  }
46 
48  std::string asJSON() const
49  { return std::to_string (_value); }
50 
52  std::ostream & dumpOn( std::ostream & str ) const
53  { return str << asJSON(); }
54 
55  private:
56  double _value = 0;
57 
58  };
59 
61  inline std::ostream & operator<<( std::ostream & str, const Number & obj )
62  {
63  return obj.dumpOn( str );
64  }
65 
66  class Int {
67 
68  public:
69  Int() = default;
70  Int( std::int64_t v ) : _value(v) {}
71  ~Int() = default;
72 
73  Int( const Int & ) = default;
74  Int( Int && ) = default;
75  Int &operator=(const Int &) = default;
76  Int &operator=(Int &&) = default;
77 
78  operator std::int64_t() const {
79  return _value;
80  }
81 
82  std::int64_t value() const {
83  return _value;
84  }
85 
86  static zyppng::expected<Int> fromString ( const std::string &str ) {
87  using namespace zyppng::operators;
88  using zyppng::operators::operator|;
89  return zyppng::mtry([&](){ return std::stoll( str, nullptr ); })
90  | and_then([](std::int64_t res) { return zyppng::make_expected_success( Int(res)); } );
91  }
92 
94  std::string asJSON() const
95  { return std::to_string (_value); }
96 
98  std::ostream & dumpOn( std::ostream & str ) const
99  { return str << asJSON(); }
100 
101  private:
102  std::int64_t _value = 0;
103  };
104 
106  inline std::ostream & operator<<( std::ostream & str, const Int & obj )
107  {
108  return obj.dumpOn( str );
109  }
110 
111  class UInt {
112 
113  public:
114  UInt() = default;
115  UInt( std::uint64_t v ) : _value(v) {}
116  ~UInt() = default;
117 
118  UInt( const UInt & ) = default;
119  UInt( UInt && ) = default;
120  UInt &operator=(const UInt &) = default;
121  UInt &operator=(UInt &&) = default;
122 
123  static zyppng::expected<UInt> fromString ( const std::string &str ){
124  using namespace zyppng::operators;
125  using zyppng::operators::operator|;
126  return zyppng::mtry([&](){ return std::stoull( str, nullptr ); })
127  | and_then([](unsigned long long res) { return zyppng::make_expected_success( UInt(res)); } );
128  }
129 
130  operator std::uint64_t() const {
131  return _value;
132  }
133 
134  std::uint64_t value() const {
135  return _value;
136  }
137 
139  std::string asJSON() const
140  { return std::to_string (_value); }
141 
143  std::ostream & dumpOn( std::ostream & str ) const
144  { return str << asJSON(); }
145 
146  private:
147  std::uint64_t _value = 0;
148  };
149 
151  inline std::ostream & operator<<( std::ostream & str, const UInt & obj )
152  {
153  return obj.dumpOn( str );
154  }
155 
156 }
157 
158 #endif
std::uint64_t value() const
Definition: JsonNumber.h:134
std::string asJSON() const
JSON representation.
Definition: JsonNumber.h:94
Int(std::int64_t v)
Definition: JsonNumber.h:70
UInt(std::uint64_t v)
Definition: JsonNumber.h:115
std::string asJSON() const
JSON representation.
Definition: JsonNumber.h:48
String related utilities and Regular expression matching.
static expected< std::decay_t< Type >, Err > make_expected_success(Type &&t)
Definition: expected.h:397
std::ostream & operator<<(std::ostream &str, const Number &obj)
Definition: JsonNumber.h:61
static zyppng::expected< UInt > fromString(const std::string &str)
Definition: JsonNumber.h:123
std::ostream & operator<<(std::ostream &str, const UInt &obj)
Definition: JsonNumber.h:151
std::ostream & operator<<(std::ostream &str, const Int &obj)
Definition: JsonNumber.h:106
Exp mtry(F &&f, Args &&...args)
Definition: mtry.h:28
std::string asJSON() const
JSON representation.
Definition: JsonNumber.h:139
std::int64_t value() const
Definition: JsonNumber.h:82
static zyppng::expected< Int > fromString(const std::string &str)
Definition: JsonNumber.h:86
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition: JsonNumber.h:98
static zyppng::expected< Number > fromString(const std::string &str)
Definition: JsonNumber.h:32
Number(double v)
Definition: JsonNumber.h:23
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition: JsonNumber.h:143
Number & operator=(const Number &)=default
ResultType and_then(const expected< T, E > &exp, Function &&f)
Definition: expected.h:423
double value() const
Definition: JsonNumber.h:43
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition: JsonNumber.h:52