tenno 0.1.0
Loading...
Searching...
No Matches
expected.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
9#include <tenno/utility.hpp> // tenno::move
10
11namespace tenno
12{
13
19template <typename E> class unexpected
20{
21public:
22 const E unex;
23 constexpr unexpected() : unex(E())
24 {
25 }
26 constexpr unexpected(const E unex_in) : unex(unex_in)
27 {
28 }
29};
30
37template <typename T, typename E> class expected
38{
39public:
40 bool has_val;
41 const T val;
43
44 constexpr expected(const T val_in) : has_val(true), val(val_in), unex(E())
45 {
46 }
47
48 constexpr expected(const unexpected<E> unex_val)
49 : has_val(false), val(T()), unex(unex_val)
50 {
51 }
52
53 constexpr explicit operator bool() const noexcept
54 {
55 return has_val;
56 }
57
58 constexpr bool operator==(const expected<T, E> &other) const noexcept
59 {
60 if (has_val != other.has_val)
61 {
62 return false;
63 }
64 if (has_val)
65 {
66 return val == other.val;
67 }
68 return unex.unex == other.unex.unex;
69 }
70
71 constexpr bool operator!=(const expected<T, E> &other) const noexcept
72 {
73 return !(*this == other);
74 }
75
76 constexpr auto operator=(const expected<T, E> &other) noexcept
78 {
79 has_val = other.has_val;
80 val = other.val;
81 unex = other.unex;
82 return *this;
83 }
84
85 constexpr bool has_value() const noexcept
86 {
87 return has_val;
88 }
89
90 constexpr T value() const noexcept
91 {
92 return val;
93 }
94
95 constexpr E error() const noexcept
96 {
97 return unex.unex;
98 }
99
107 constexpr T value_or(const T default_val) const noexcept
108 {
109 if (has_val)
110 {
111 return val;
112 }
113 return default_val;
114 }
115
125 constexpr E error_or(const E default_err) const noexcept
126 {
127 if (!has_val)
128 {
129 return unex.unex;
130 }
131 return default_err;
132 }
133
134 // TODO:
135 // - and_then
136 // - transform
137 // - or_else
138 // - transform_error
139};
140
141} // namespace tenno
An expected value.
Definition expected.hpp:38
constexpr bool operator==(const expected< T, E > &other) const noexcept
Definition expected.hpp:58
constexpr E error_or(const E default_err) const noexcept
Returns the unexpected value of the expected object or a default value.
Definition expected.hpp:125
constexpr expected(const unexpected< E > unex_val)
Definition expected.hpp:48
constexpr E error() const noexcept
Definition expected.hpp:95
constexpr T value_or(const T default_val) const noexcept
Returns the value of the expected object or a default value.
Definition expected.hpp:107
constexpr expected(const T val_in)
Definition expected.hpp:44
constexpr bool has_value() const noexcept
Definition expected.hpp:85
constexpr bool operator!=(const expected< T, E > &other) const noexcept
Definition expected.hpp:71
constexpr T value() const noexcept
Definition expected.hpp:90
const unexpected< E > unex
Definition expected.hpp:42
constexpr auto operator=(const expected< T, E > &other) noexcept -> expected< T, E > &
Definition expected.hpp:76
An unexpected value.
Definition expected.hpp:20
constexpr unexpected()
Definition expected.hpp:23
constexpr unexpected(const E unex_in)
Definition expected.hpp:26