libzypp: zypp-core/parser/json/JsonNumber.h Source File

libzypp 17.38.14

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
12#include <zypp-core/ng/pipelines/mtry.h>
13#include <zypp-core/ng/pipelines/expected.h>
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
zypp::json::Int
Definition JsonNumber.h:66
zypp::json::Int::asJSON
std::string asJSON() const
JSON representation.
Definition JsonNumber.h:94
zypp::json::Int::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonNumber.h:98
zypp::json::Int::operator=
Int & operator=(Int &&)=default
zypp::json::Int::fromString
static zyppng::expected< Int > fromString(const std::string &str)
Definition JsonNumber.h:86
zypp::json::Int::Int
Int(Int &&)=default
zypp::json::Int::Int
Int(const Int &)=default
zypp::json::Int::operator=
Int & operator=(const Int &)=default
zypp::json::Int::Int
Int(std::int64_t v)
Definition JsonNumber.h:70
zypp::json::Int::~Int
~Int()=default
zypp::json::Int::value
std::int64_t value() const
Definition JsonNumber.h:82
zypp::json::Int::_value
std::int64_t _value
Definition JsonNumber.h:102
zypp::json::Int::Int
Int()=default
zypp::json::Number
Definition JsonNumber.h:19
zypp::json::Number::_value
double _value
Definition JsonNumber.h:56
zypp::json::Number::value
double value() const
Definition JsonNumber.h:43
zypp::json::Number::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonNumber.h:52
zypp::json::Number::Number
Number(Number &&)=default
zypp::json::Number::asJSON
std::string asJSON() const
JSON representation.
Definition JsonNumber.h:48
zypp::json::Number::~Number
~Number()=default
zypp::json::Number::Number
Number()=default
zypp::json::Number::Number
Number(double v)
Definition JsonNumber.h:23
zypp::json::Number::Number
Number(float v)
Definition JsonNumber.h:24
zypp::json::Number::operator=
Number & operator=(const Number &)=default
zypp::json::Number::fromString
static zyppng::expected< Number > fromString(const std::string &str)
Definition JsonNumber.h:32
zypp::json::Number::operator=
Number & operator=(Number &&)=default
zypp::json::Number::Number
Number(const Number &)=default
zypp::json::UInt
Definition JsonNumber.h:111
zypp::json::UInt::UInt
UInt(const UInt &)=default
zypp::json::UInt::~UInt
~UInt()=default
zypp::json::UInt::asJSON
std::string asJSON() const
JSON representation.
Definition JsonNumber.h:139
zypp::json::UInt::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonNumber.h:143
zypp::json::UInt::UInt
UInt()=default
zypp::json::UInt::_value
std::uint64_t _value
Definition JsonNumber.h:147
zypp::json::UInt::fromString
static zyppng::expected< UInt > fromString(const std::string &str)
Definition JsonNumber.h:123
zypp::json::UInt::operator=
UInt & operator=(UInt &&)=default
zypp::json::UInt::UInt
UInt(std::uint64_t v)
Definition JsonNumber.h:115
zypp::json::UInt::UInt
UInt(UInt &&)=default
zypp::json::UInt::operator=
UInt & operator=(const UInt &)=default
zypp::json::UInt::value
std::uint64_t value() const
Definition JsonNumber.h:134
zyppng::expected
Definition expected.h:56
expected.h
mtry.h
str
String related utilities and Regular expression matching.
zypp::json
Definition TargetImpl.cc:153
zypp::json::operator<<
std::ostream & operator<<(std::ostream &str, const Bool &obj)
relates: Bool Stream output
Definition JsonBool.h:56
zyppng::operators
Definition expected.h:699
zyppng::operators::and_then
auto and_then(Fun &&function)
Definition expected.h:708
zyppng::make_expected_success
static expected< std::decay_t< Type >, Err > make_expected_success(Type &&t)
Definition expected.h:470
zyppng::mtry
auto mtry(F &&f, Args &&...args)
Definition mtry.h:50