Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
maybe.hpp
1#ifndef LIVE_CELLS_MAYBE_HPP
2#define LIVE_CELLS_MAYBE_HPP
3
4#include <concepts>
5#include <exception>
6#include <optional>
7
8#include "util.hpp"
9#include "exceptions.hpp"
10
11namespace live_cells {
16 template <typename T>
17 class maybe {
18 public:
24 try {
26 }
27 catch (...) {
28 error_ = std::current_exception();
29 }
30 }
31
37 maybe(const T &val) : value_(val) {}
38
45 maybe(std::exception_ptr error) : error_(error) {}
46
59 static maybe<T> error(const auto &error) {
60 try {
61 throw error;
62 }
63 catch (...) {
64 return maybe(std::current_exception());
65 }
66 }
67
81 static maybe<T> wrap(const std::invocable auto &f) {
82 try {
83 return maybe(f());
84 }
85 catch (...) {
86 return maybe(std::current_exception());
87 }
88 }
89
96 T unwrap() const {
97 if (value_) {
98 return value_.value();
99 }
100
101 std::rethrow_exception(error_);
102 }
103
112 std::optional<T> value() const {
113 return value_;
114 }
115
121 std::exception_ptr error() const {
122 return error_;
123 }
124
125 private:
129 std::optional<T> value_;
130
134 std::exception_ptr error_;
135 };
136
150 auto maybe_wrap(const std::invocable auto &f) {
151 return maybe<decltype(f())>::wrap(f);
152 }
153
154 template <typename A, typename B>
155 bool operator==(const maybe<A> &a, const maybe<B> &b) {
156 return a.value() == b.value() && a.error() == b.error();
157 }
158
159} // live_cells
160
161template<typename T>
162struct std::hash<live_cells::maybe<T>> {
163 std::size_t operator()(const live_cells::maybe<T> &m) {
164 return live_cells::internal::hash_combine(0, m.value(), m.error());
165 }
166};
167
168#endif /* LIVE_CELLS_MAYBE_HPP */
A computed cell which determines its argument cells at runtime.
Definition dynamic_compute_cell.hpp:153
Container holding a cell value or an exception that occurred while computing a value.
Definition maybe.hpp:17
maybe()
Create a container holding an uninitialized_cell_error exception.
Definition maybe.hpp:23
std::exception_ptr error() const
Get the exception held in this maybe.
Definition maybe.hpp:121
static maybe< T > wrap(const std::invocable auto &f)
Create a maybe holding the result of calling f.
Definition maybe.hpp:81
maybe(const T &val)
Create a container holding a value.
Definition maybe.hpp:37
maybe(std::exception_ptr error)
Create a container holding an exception that was thrown.
Definition maybe.hpp:45
std::optional< T > value() const
Get the value held in this.
Definition maybe.hpp:112
T unwrap() const
Get the value or throw the exception stored in this container.
Definition maybe.hpp:96
static maybe< T > error(const auto &error)
Create a container holding an exception that was thrown.
Definition maybe.hpp:59
Definition boolean.hpp:26
auto maybe_wrap(const std::invocable auto &f)
Create a maybe holding the result of calling f.
Definition maybe.hpp:150
auto operator==(const T &a, const U &b)
Create a Cell that compares two cells for equality by ==.
Definition equality.hpp:57
Exception indicating that the value of a cell was referenced before its initial value was computed.
Definition exceptions.hpp:43