Repository navigation
Expand file tree
/
Copy pathresult.hpp
More file actions
168 lines (134 loc) · 6.89 KB
/
Copy pathresult.hpp
File metadata and controls
168 lines (134 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* File: /lib/tl/result.hpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <nikita.zuev@gmx.com>
*/
#ifndef CPPLIB__lib__tl__result__hpp
#define CPPLIB__lib__tl__result__hpp
#include <compare>
#include <utility>
#include <variant>
#include <cpp/lib_concepts>
#include <cpp/lib_debug>
#include <cpp/lib_system_error>
namespace lib::tl {
// DECLARATION lib::tl::result_t<>
template< class T >
class result_t {
public:
using value_type = T;
using error_type = ::cpp::error_condition;
constexpr result_t() noexcept = default;
constexpr result_t( result_t && other ) noexcept
: result {::std::move( other.result )} {}
constexpr result_t( const result_t & other ) noexcept
: result {other.result} {}
constexpr result_t( value_type && value ) noexcept
: result {::std::move( value )} {}
constexpr result_t( const value_type & value ) noexcept
: result {value} {}
constexpr explicit result_t( const error_type & error ) noexcept
: result {error} {}
constexpr result_t & operator = ( result_t && other ) noexcept
{ result = ::std::move( other.result ); return *this; }
constexpr result_t & operator = ( const result_t & other ) noexcept
{ result = other.result; return *this; }
constexpr result_t & operator = ( value_type && value ) noexcept
{ result = ::std::move( value ); return *this; }
constexpr result_t & operator = ( const value_type & value ) noexcept
{ result = value; return *this; }
constexpr bool operator == ( const result_t & other ) const noexcept
{ return result == other.result; }
constexpr value_type & value() noexcept
{ CPP_ASSERT( success() ); return ::std::get<value_type>( result ); }
constexpr const value_type & value() const noexcept
{ CPP_ASSERT( success() ); return ::std::get<value_type>( result ); }
constexpr const error_type & error() const noexcept
{ CPP_ASSERT( failed() ); return ::std::get<error_type>( result ); }
constexpr bool success() const noexcept
{ return ::std::holds_alternative<value_type>( result ); }
constexpr bool failed() const noexcept
{ return ::std::holds_alternative<error_type>( result ); }
protected:
::std::variant<value_type, error_type> result;
};
// DECLARATION lib::tl::result_numeric_t<>
template< ::cpp::Numeric T >
class result_numeric_t
: public result_t<T>
{
using Super = result_t<T>;
public:
using value_type = typename Super::value_type;
constexpr result_numeric_t( auto&&...args ) noexcept : Super {args...} {}
constexpr explicit operator value_type () const noexcept { return Super::value(); }
constexpr bool operator == ( const result_numeric_t & other ) const noexcept
{ return Super::operator == ( other ); }
template< ::cpp::Numeric V >
constexpr bool operator == ( const V & other ) const noexcept
{ return Super::success() ? Super::value() == other : false; }
template< ::cpp::Numeric V >
constexpr auto operator <=> ( const V & other ) const noexcept
{ return Super::value() <=> other; }
constexpr result_numeric_t & operator ++ () noexcept
{ if ( Super::success() ) ++Super::value(); return *this; }
constexpr result_numeric_t & operator -- () noexcept
{ if ( Super::success() ) --Super::value(); return *this; }
constexpr result_numeric_t operator ++ ( int ) noexcept
{ auto r = *this; if ( r.success() ) ++Super::value(); return r; }
constexpr result_numeric_t operator -- ( int ) noexcept
{ auto r = *this; if ( r.success() ) --Super::value(); return r; }
constexpr result_numeric_t operator + ( const result_numeric_t & other ) const noexcept
{ auto r = *this; if ( other.success() ) r += other.value(); return r; }
constexpr result_numeric_t operator - ( const result_numeric_t & other ) const noexcept
{ auto r = *this; if ( other.success() ) r -= other.value(); return r; }
constexpr result_numeric_t operator * ( const result_numeric_t & other ) const noexcept
{ auto r = *this; if ( other.success() ) r *= other.value(); return r; }
constexpr result_numeric_t operator / ( const result_numeric_t & other ) const noexcept
{ auto r = *this; if ( other.success() ) r /= other.value(); return r; }
constexpr result_numeric_t operator + ( const value_type & other ) const noexcept
{ return Super::success() ? result_numeric_t {Super::value() + other} : *this; }
constexpr result_numeric_t operator - ( const value_type & other ) const noexcept
{ return Super::success() ? result_numeric_t {Super::value() - other} : *this; }
constexpr result_numeric_t operator * ( const value_type & other ) const noexcept
{ return Super::success() ? result_numeric_t {Super::value() * other} : *this; }
constexpr result_numeric_t operator / ( const value_type & other ) const noexcept
{ return Super::success() ? result_numeric_t {Super::value() / other} : *this; }
constexpr result_numeric_t & operator += ( const value_type & other ) noexcept
{ if ( Super::success() ) Super::value() += other; return *this; }
constexpr result_numeric_t & operator -= ( const value_type & other ) noexcept
{ if ( Super::success() ) Super::value() -= other; return *this; }
constexpr result_numeric_t & operator *= ( const value_type & other ) noexcept
{ if ( Super::success() ) Super::value() *= other; return *this; }
constexpr result_numeric_t & operator /= ( const value_type & other ) noexcept
{ if ( Super::success() ) Super::value() /= other; return *this; }
};
// DECLARATION lib::tl::result_numeric_t<> operator + - * /
template< ::cpp::Numeric T >
inline constexpr result_numeric_t<T> operator + ( const T & lhv, const result_numeric_t<T> & rhv ) noexcept
{ return rhv.success() ? result_numeric_t<T> {lhv + rhv.value()} : rhv; }
template< ::cpp::Numeric T >
inline constexpr result_numeric_t<T> operator - ( const T & lhv, const result_numeric_t<T> & rhv ) noexcept
{ return rhv.success() ? result_numeric_t<T> {lhv - rhv.value()} : rhv; }
template< ::cpp::Numeric T >
inline constexpr result_numeric_t<T> operator * ( const T & lhv, const result_numeric_t<T> & rhv ) noexcept
{ return rhv.success() ? result_numeric_t<T> {lhv * rhv.value()} : rhv; }
template< ::cpp::Numeric T >
inline constexpr result_numeric_t<T> operator / ( const T & lhv, const result_numeric_t<T> & rhv ) noexcept
{ return rhv.success() ? result_numeric_t<T> {lhv / rhv.value()} : rhv; }
template< ::cpp::Numeric T >
inline constexpr T & operator += ( T & lhv, const result_numeric_t<T> & rhv ) noexcept
{ return lhv += rhv.value(); }
template< ::cpp::Numeric T >
inline constexpr T & operator -= ( T & lhv, const result_numeric_t<T> & rhv ) noexcept
{ return lhv -= rhv.value(); }
template< ::cpp::Numeric T >
inline constexpr T & operator *= ( T & lhv, const result_numeric_t<T> & rhv ) noexcept
{ return lhv *= rhv.value(); }
template< ::cpp::Numeric T >
inline constexpr T & operator /= ( T & lhv, const result_numeric_t<T> & rhv ) noexcept
{ return lhv /= rhv.value(); }
} // namespace lib::tl
#endif // CPPLIB__lib__tl__result__hpp