libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
result.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2#pragma once
3
4#include <sshpp/error.hpp>
5
6#include <optional>
7#include <type_traits>
8#include <utility>
9
10namespace sshpp {
11
15template <class T>
16class [[nodiscard]] Result {
17public:
18 using value_type = T;
20
21 Result(T value) : value_(std::move(value)) {}
22 Result(ErrorInfo error) : error_(std::move(error)) {}
23
24 Result(const Result&) = default;
25 Result(Result&&) noexcept = default;
26 Result& operator=(const Result&) = default;
27 Result& operator=(Result&&) noexcept = default;
28 ~Result() = default;
29
30 [[nodiscard]] bool has_value() const noexcept { return value_.has_value(); }
31 explicit operator bool() const noexcept { return value_.has_value(); }
32
33 T& value() & {
34 if (!value_) throw_error(error_);
35 return *value_;
36 }
37 const T& value() const& {
38 if (!value_) throw_error(error_);
39 return *value_;
40 }
41 T&& value() && {
42 if (!value_) throw_error(error_);
43 return std::move(*value_);
44 }
45
46 template <class U>
47 T value_or(U&& fallback) const& {
48 return value_ ? *value_ : static_cast<T>(std::forward<U>(fallback));
49 }
50
51 T* operator->() noexcept { return &*value_; }
52 const T* operator->() const noexcept { return &*value_; }
53 T& operator*() & noexcept { return *value_; }
54 const T& operator*() const& noexcept { return *value_; }
55
56 const ErrorInfo& error() const& noexcept { return error_; }
57 std::error_code code() const noexcept { return value_ ? std::error_code{} : error_.code; }
58
59 void throw_if_error() const {
60 if (!value_) throw_error(error_);
61 }
62
63 template <class F>
64 auto and_then(F&& f) && {
65 using R = std::invoke_result_t<F, T&&>;
66 if (value_) return std::forward<F>(f)(std::move(*value_));
67 return R(error_);
68 }
69
70 template <class F>
71 auto transform(F&& f) && {
72 using U = std::invoke_result_t<F, T&&>;
73 if (value_) return Result<U>(std::forward<F>(f)(std::move(*value_)));
74 return Result<U>(error_);
75 }
76
77 template <class F>
78 Result or_else(F&& f) && {
79 if (value_) return std::move(*this);
80 return std::forward<F>(f)(error_);
81 }
82
83private:
84 std::optional<T> value_;
85 ErrorInfo error_{};
86};
87
88template <>
89class [[nodiscard]] Result<void> {
90public:
91 using value_type = void;
93
94 Result() : has_value_(true) {}
95 Result(ErrorInfo error) : has_value_(false), error_(std::move(error)) {}
96
97 [[nodiscard]] bool has_value() const noexcept { return has_value_; }
98 explicit operator bool() const noexcept { return has_value_; }
99
100 void value() const {
101 if (!has_value_) throw_error(error_);
102 }
103
104 const ErrorInfo& error() const& noexcept { return error_; }
105 std::error_code code() const noexcept { return has_value_ ? std::error_code{} : error_.code; }
106
107 void throw_if_error() const {
108 if (!has_value_) throw_error(error_);
109 }
110
111 template <class F>
112 auto and_then(F&& f) && {
113 using R = std::invoke_result_t<F>;
114 if (has_value_) return std::forward<F>(f)();
115 return R(error_);
116 }
117
118private:
119 bool has_value_;
120 ErrorInfo error_{};
121};
122
123} // namespace sshpp
bool has_value() const noexcept
Definition result.hpp:97
void value() const
Definition result.hpp:100
void throw_if_error() const
Definition result.hpp:107
auto and_then(F &&f) &&
Definition result.hpp:112
void value_type
Definition result.hpp:91
const ErrorInfo & error() const &noexcept
Definition result.hpp:104
Result(ErrorInfo error)
Definition result.hpp:95
Result()
Definition result.hpp:94
std::error_code code() const noexcept
Definition result.hpp:105
Definition result.hpp:16
T value_type
Definition result.hpp:18
std::error_code code() const noexcept
Definition result.hpp:57
void throw_if_error() const
Definition result.hpp:59
Result(Result &&) noexcept=default
Result(T value)
Definition result.hpp:21
const T & value() const &
Definition result.hpp:37
T && value() &&
Definition result.hpp:41
Result(ErrorInfo error)
Definition result.hpp:22
T value_or(U &&fallback) const &
Definition result.hpp:47
auto transform(F &&f) &&
Definition result.hpp:71
const ErrorInfo & error() const &noexcept
Definition result.hpp:56
bool has_value() const noexcept
Definition result.hpp:30
Result(const Result &)=default
T * operator->() noexcept
Definition result.hpp:51
T & value() &
Definition result.hpp:33
auto and_then(F &&f) &&
Definition result.hpp:64
T & operator*() &noexcept
Definition result.hpp:53
const T & operator*() const &noexcept
Definition result.hpp:54
Result or_else(F &&f) &&
Definition result.hpp:78
const T * operator->() const noexcept
Definition result.hpp:52
Definition auth.hpp:18
SSHPP_INLINE void throw_error(ErrorInfo info)
Definition error.ipp:152
Definition error.hpp:110
Definition error.hpp:119