Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
Option.hpp
Go to the documentation of this file.
1#pragma once
3#include <concepts>
4#include <optional>
5#include <string>
6#include <functional>
7#include <stdexcept>
8#include <type_traits>
9
10namespace fishnet {
11
12template<typename T>
13class Option : public std::optional<T> {
14public:
15 template<typename... Args>
16 Option(Args&&... args) : std::optional<T>(std::forward<Args>(args)...) {}
17
25 const T & value_or_throw(const std::string& message="No value present!") const& {
26 if (this->has_value())
27 return this->value();
28 throw std::runtime_error(message);
29 }
30
38 T&& value_or_throw(const std::string& message="No value present!") && {
39 if (this->has_value())
40 return std::move(this->value());
41 throw std::runtime_error(message);
42 }
43
53 using U = typename std::remove_cvref_t<std::invoke_result_t<F, const T&>>::value_type;
54 if (this->has_value())
55 return std::invoke(std::forward<F>(f), this->value());
56 return Option<U>{};
57 }
58
68 using U = typename std::remove_cvref_t<std::invoke_result_t<F, T>>::value_type;
69 if (this->has_value())
70 return std::invoke(std::forward<F>(f), std::move(this->value()));
71 return Option<U>{};
72 }
73
83 using U = std::remove_cvref_t<std::invoke_result_t<F, const T&>>;
84 if (this->has_value())
85 return Option<U>(std::invoke(std::forward<F>(f), this->value()));
86 return Option<U>{};
87 }
88
98 using U = std::remove_cvref_t<std::invoke_result_t<F, T>>;
99 if (this->has_value())
100 return Option<U>(std::invoke(std::forward<F>(f), std::move(this->value())));
101 return Option<U>{};
102 }
103
111 template<typename F> requires std::convertible_to<std::invoke_result_t<F>,T>
112 Option<T> or_else(F&& f) const& {
113 if (this->has_value()) return *this;
114 return std::invoke(std::forward<F>(f));
115 }
116
124 template<typename F> requires std::convertible_to<std::invoke_result_t<F>,Option<T>>
125 Option<T> or_else(F&& f) && {
126 if (this->has_value()) return std::move(*this);
127 return std::invoke(std::forward<F>(f));
128 }
129
137 if (this->has_value() && predicate(this->value()))
138 return *this;
139 return Option<T>{};
140 }
141
148 void if_value(fishnet::util::Consumer<T> auto const & consumer) {
149 if (this->has_value())
150 consumer(std::forward<T>(this->value()));
151 }
152
160 void if_value_or_else(fishnet::util::Consumer<T> auto const & consumer, fishnet::util::Runnable auto const & runnable) {
161 if (this->has_value())
162 consumer(std::forward<T>(this->value()));
163 else
164 runnable();
165 }
166};
167
168template<typename T>
169Option(std::optional<T> opt) -> Option<T>;
170
171template<typename T>
172Option(T value) -> Option<T>;
173
174} // namespace fishnet
Definition Option.hpp:13
void if_value_or_else(fishnet::util::Consumer< T > auto const &consumer, fishnet::util::Runnable auto const &runnable)
If the Option has a value, call the provided function with the value. Otherwise, call the provided fu...
Definition Option.hpp:160
Option< typename std::remove_cvref_t< std::invoke_result_t< F, T > >::value_type > and_then(F &&f) const &
FlatMap the value of the Option if it has a value, otherwise return an empty Option.
Definition Option.hpp:52
Option< T > or_else(F &&f) const &
If the Option has a value, return it. Otherwise, call the provided function and return its result.
Definition Option.hpp:112
Option< T > or_else(F &&f) &&
If the Option has a value, return it. Otherwise, call the provided function and return its result.
Definition Option.hpp:125
Option(Args &&... args)
Definition Option.hpp:16
void if_value(fishnet::util::Consumer< T > auto const &consumer)
If the Option has a value, call the provided function with the value. Otherwise, do nothing.
Definition Option.hpp:148
Option< typename std::remove_cvref_t< std::invoke_result_t< F, T > > > transform(F &&f) const &
Transform the value of the Option if it has a value, otherwise return an empty Option.
Definition Option.hpp:82
T && value_or_throw(const std::string &message="No value present!") &&
Get the value of the Option if it has a value, otherwise throw an exception with the provided message...
Definition Option.hpp:38
Option< T > filter(fishnet::util::Predicate< T > auto const &predicate)
If the Option has a value and the predicate returns true, return the Option. Otherwise,...
Definition Option.hpp:136
Option< typename std::remove_cvref_t< std::invoke_result_t< F, T > >::value_type > and_then(F &&f) &&
FlatMap the value of the Option if it has a value, otherwise return an empty Option.
Definition Option.hpp:67
Option< typename std::remove_cvref_t< std::invoke_result_t< F, T > > > transform(F &&f) &&
Transform the value of the Option if it has a value, otherwise return an empty Option.
Definition Option.hpp:97
const T & value_or_throw(const std::string &message="No value present!") const &
Get the value of the Option if it has a value, otherwise throw an exception with the provided message...
Definition Option.hpp:25
Definition FunctionalConcepts.hpp:54
Definition FunctionalConcepts.hpp:36
Definition FunctionalConcepts.hpp:60
Definition FunctionalConcepts.hpp:9
Definition BFSClustering.hpp:5
STL namespace.