Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
Either.hpp
Go to the documentation of this file.
1#pragma once
2#include <expected>
3#include <stdexcept>
4#include <functional>
5
7
8namespace fishnet{
9
10template<typename T, typename E>
11class Either: public std::expected<T,E>{
12public:
13 template<typename... Args>
14 Either(Args&&... args): std::expected<T,E>(std::forward<Args>(args)...){}
15
22 const T& value_or_throw() const& {
23 if(this->has_value())
24 return this->value();
25 if constexpr(std::convertible_to<E,std::string_view>){
26 throw std::runtime_error(this->error());
27 }else {
28 throw std::runtime_error("No value present!");
29 }
30 }
31
39 if(this->has_value())
40 return this->value();
41 if constexpr(std::convertible_to<E,std::string_view>){
42 throw std::runtime_error(this->error());
43 }else {
44 throw std::runtime_error("No value present!");
45 }
46 }
47
54 T&& value_or_throw() && {
55 if(this->has_value())
56 return std::move(this->value());
57 if constexpr(std::convertible_to<E,std::string_view>){
58 throw std::runtime_error(this->error());
59 }else {
60 throw std::runtime_error("No value present!");
61 }
62 }
63
71 const T& value_or_throw(const std::string & errorMsg) const& {
72 if(this->has_value())
73 return this->value();
74 else throw std::runtime_error(errorMsg);
75 }
76
84 T& value_or_throw(const std::string & errorMsg) & {
85 if(this->has_value())
86 return this->value();
87 else throw std::runtime_error(errorMsg);
88 }
89
97 T&& value_or_throw(const std::string & errorMsg) && {
98 if(this->has_value())
99 return std::move(this->value());
100 else throw std::runtime_error(errorMsg);
101 }
102
112 using U = typename std::remove_cvref_t<std::invoke_result_t<F, const T&>>::value_type;
113 if(this->has_value())
114 return std::invoke(std::forward<F>(f), this->value());
115 return Either<U,E>(std::unexpected(this->error()));
116 }
117
127 using U = typename std::remove_cvref_t<std::invoke_result_t<F, T>>::value_type;
128 if(this->has_value())
129 return std::invoke(std::forward<F>(f), std::move(this->value()));
130 return Either<U,E>(std::unexpected(this->error()));
131 }
132
142 using U = typename std::remove_cvref_t<std::invoke_result_t<F, const T&>>;
143 if(this->has_value())
144 return std::invoke(std::forward<F>(f), this->value());
145 return Either<U,E>(std::unexpected(this->error()));
146 }
147
157 using U = typename std::remove_cvref_t<std::invoke_result_t<F, T>>;
158 if(this->has_value())
159 return std::invoke(std::forward<F>(f), std::move(this->value()));
160 return Either<U,E>(std::unexpected(this->error()));
161 }
162
170 template<typename F> requires std::convertible_to<std::invoke_result_t<F>,Either<T,E>>
172 if(this->has_value())
173 return *this;
174 return Either<T,E>(std::invoke(std::forward<F>(f)));
175 }
176
184 template<typename F> requires std::convertible_to<std::invoke_result_t<F>,Either<T,E>>
186 if(this->has_value())
187 return std::move(*this);
188 return Either<T,E>(std::invoke(std::forward<F>(f)));
189 }
190
197 void if_value(fishnet::util::Consumer<T> auto const & consumer){
198 if(this->has_value())
199 consumer(std::forward<T>(this->value()));
200 }
201
209 void if_value_or_error(fishnet::util::Consumer<T> auto const & consumer, fishnet::util::Consumer<E> auto const & errorConsumer){
210 if(this->has_value())
211 consumer(std::forward<T>(this->value()));
212 else
213 errorConsumer(std::forward<E>(this->error()));
214 }
215};
216}
Definition Either.hpp:11
const T & value_or_throw(const std::string &errorMsg) const &
Get the value of the Either if it has a value, otherwise throw an exception with the provided message...
Definition Either.hpp:71
Either< T, E > or_else(F &&f) &&
If the Either has a value, return it. Otherwise, call the provided function and return its result.
Definition Either.hpp:185
Either< typename std::remove_cvref_t< std::invoke_result_t< F, T > >::value_type, E > and_then(F &&f) &&
flatMap the value of the Either if it has a value, otherwise return the error
Definition Either.hpp:126
Either< typename std::remove_cvref_t< std::invoke_result_t< F, T > >, E > transform(F &&f) &
Transform the value of the Either if it has a value, otherwise return the error.
Definition Either.hpp:141
Either(Args &&... args)
Definition Either.hpp:14
Either< T, E > or_else(F &&f) &
If the Either has a value, return it. Otherwise, call the provided function and return its result.
Definition Either.hpp:171
T & value_or_throw(const std::string &errorMsg) &
Get the value of the Either if it has a value, otherwise throw an exception with the provided message...
Definition Either.hpp:84
T & value_or_throw() &
Get the value of the Either if it has a value, otherwise throw an exception using the error value if ...
Definition Either.hpp:38
Either< typename std::remove_cvref_t< std::invoke_result_t< F, T > >::value_type, E > and_then(F &&f) &
flatMap the value of the Either if it has a value, otherwise return the error
Definition Either.hpp:111
const T & value_or_throw() const &
Get the value of the Either if it has a value, otherwise throw an exception using the error value if ...
Definition Either.hpp:22
T && value_or_throw(const std::string &errorMsg) &&
Get the value of the Either if it has a value, otherwise throw an exception with the provided message...
Definition Either.hpp:97
void if_value(fishnet::util::Consumer< T > auto const &consumer)
If the Either has a value, call the provided function with the value. Otherwise, do nothing.
Definition Either.hpp:197
Either< typename std::remove_cvref_t< std::invoke_result_t< F, T > >, E > transform(F &&f) &&
Transform the value of the Either if it has a value, otherwise return the error.
Definition Either.hpp:156
T && value_or_throw() &&
Get the value of the Either if it has a value, otherwise throw an exception using the error value if ...
Definition Either.hpp:54
void if_value_or_error(fishnet::util::Consumer< T > auto const &consumer, fishnet::util::Consumer< E > auto const &errorConsumer)
If the Either has a value, call the provided function with the value. Otherwise, call the provided fu...
Definition Either.hpp:209
Definition FunctionalConcepts.hpp:54
Definition FunctionalConcepts.hpp:9
Definition BFSClustering.hpp:5
STL namespace.