libzypp 17.37.17
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
17namespace 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::string asJSON() const
JSON representation.
Definition JsonNumber.h:94
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonNumber.h:98
Int & operator=(Int &&)=default
static zyppng::expected< Int > fromString(const std::string &str)
Definition JsonNumber.h:86
std::ostream & operator<<(std::ostream &str, const Int &obj)
Stream output.
Definition JsonNumber.h:106
Int(Int &&)=default
Int(const Int &)=default
Int & operator=(const Int &)=default
Int(std::int64_t v)
Definition JsonNumber.h:70
~Int()=default
std::int64_t value() const
Definition JsonNumber.h:82
std::int64_t _value
Definition JsonNumber.h:102
double value() const
Definition JsonNumber.h:43
std::ostream & operator<<(std::ostream &str, const Number &obj)
Stream output.
Definition JsonNumber.h:61
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonNumber.h:52
Number(Number &&)=default
std::string asJSON() const
JSON representation.
Definition JsonNumber.h:48
Number & operator=(const Number &)=default
static zyppng::expected< Number > fromString(const std::string &str)
Definition JsonNumber.h:32
Number & operator=(Number &&)=default
Number(const Number &)=default
UInt(const UInt &)=default
std::string asJSON() const
JSON representation.
Definition JsonNumber.h:139
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonNumber.h:143
std::uint64_t _value
Definition JsonNumber.h:147
static zyppng::expected< UInt > fromString(const std::string &str)
Definition JsonNumber.h:123
UInt & operator=(UInt &&)=default
UInt(std::uint64_t v)
Definition JsonNumber.h:115
UInt(UInt &&)=default
UInt & operator=(const UInt &)=default
std::uint64_t value() const
Definition JsonNumber.h:134
std::ostream & operator<<(std::ostream &str, const UInt &obj)
Stream output.
Definition JsonNumber.h:151
String related utilities and Regular expression matching.
auto and_then(Fun &&function)
Definition expected.h:623
Exp mtry(F &&f, Args &&...args)
Definition mtry.h:28
static expected< std::decay_t< Type >, Err > make_expected_success(Type &&t)
Definition expected.h:397