LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
Result.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <expected>
4#include <functional>
5#include <optional>
6#include <stdexcept>
7#include <utility>
8#include <variant>
9
10namespace util {
11
14template<typename T, typename E>
15class Result {
16public:
17 [[nodiscard]] static Result Ok(T value)
18 {
19 return Result{std::expected<T,E>{std::in_place, std::move(value)}};
20 }
21
22 [[nodiscard]] static Result Err(E error)
23 {
24 return Result{std::expected<T,E>{std::unexpect, std::move(error)}};
25 }
26
27 [[nodiscard]] bool isOk() const noexcept { return m_data.has_value(); }
28 [[nodiscard]] bool isErr() const noexcept { return !m_data.has_value(); }
29 explicit operator bool() const noexcept { return m_data.has_value(); }
30
31 [[nodiscard]] T unwrap()
32 {
33 if (!m_data.has_value())
34 throw std::logic_error("Called unwrap() on an Err Result");
35 return std::move(*m_data);
36 }
37
38 [[nodiscard]] T unwrapOr(T defaultValue) const
39 {
40 return m_data.value_or(std::move(defaultValue));
41 }
42
43 template<typename F>
44 [[nodiscard]] T unwrapOrElse(F&& fn)
45 {
46 return m_data.has_value() ? std::move(*m_data)
47 : std::invoke(std::forward<F>(fn));
48 }
49
50 [[nodiscard]] const E& error() const
51 {
52 if (m_data.has_value())
53 throw std::logic_error("Called error() on an Ok Result");
54 return m_data.error();
55 }
56
57 template<typename F>
58 [[nodiscard]] auto map(F&& fn)
59 {
60 using U = std::invoke_result_t<F, T>;
61 if (m_data.has_value())
62 return Result<U,E>::Ok(std::invoke(std::forward<F>(fn), std::move(*m_data)));
63 return Result<U,E>::Err(m_data.error());
64 }
65
66 template<typename F>
67 [[nodiscard]] auto mapErr(F&& fn)
68 {
69 using E2 = std::invoke_result_t<F, E>;
70 if (m_data.has_value())
71 return Result<T,E2>::Ok(std::move(*m_data));
72 return Result<T,E2>::Err(std::invoke(std::forward<F>(fn), m_data.error()));
73 }
74
75 template<typename F>
76 [[nodiscard]] auto andThen(F&& fn) -> std::invoke_result_t<F, T>
77 {
78 using Ret = std::invoke_result_t<F, T>;
79 if (m_data.has_value())
80 return std::invoke(std::forward<F>(fn), std::move(*m_data));
81 return Ret::Err(m_data.error());
82 }
83
84 template<typename F>
85 [[nodiscard]] Result orElse(F&& fn)
86 {
87 if (!m_data.has_value())
88 return std::invoke(std::forward<F>(fn), m_data.error());
89 return Result::Ok(std::move(*m_data));
90 }
91
92 [[nodiscard]] std::optional<T> ok() const
93 {
94 if (m_data.has_value()) return *m_data;
95 return std::nullopt;
96 }
97
98 [[nodiscard]] std::optional<E> err() const
99 {
100 if (!m_data.has_value()) return m_data.error();
101 return std::nullopt;
102 }
103
104private:
105 explicit Result(std::expected<T,E> data) : m_data(std::move(data)) {}
106 std::expected<T,E> m_data;
107};
108
109// ---------------------------------------------------------------------------
110// Void specialisation — uses std::expected<void,E> directly (C++23).
111// Callers construct with Result<void,E>::Ok({}) or just ::Ok().
112// ---------------------------------------------------------------------------
113template<typename E>
114class Result<void, E> {
115public:
116 [[nodiscard]] static Result Ok(std::monostate = {})
117 {
118 return Result{std::expected<void,E>{}};
119 }
120
121 [[nodiscard]] static Result Err(E error)
122 {
123 return Result{std::expected<void,E>{std::unexpect, std::move(error)}};
124 }
125
126 [[nodiscard]] bool isOk() const noexcept { return m_data.has_value(); }
127 [[nodiscard]] bool isErr() const noexcept { return !m_data.has_value(); }
128 explicit operator bool() const noexcept { return m_data.has_value(); }
129
130 [[nodiscard]] const E& error() const
131 {
132 if (m_data.has_value())
133 throw std::logic_error("Called error() on an Ok Result<void,E>");
134 return m_data.error();
135 }
136
137private:
138 explicit Result(std::expected<void,E> data) : m_data(std::move(data)) {}
139 std::expected<void,E> m_data;
140};
141
142} // namespace util
bool isErr() const noexcept
Definition Result.hpp:127
bool isOk() const noexcept
Definition Result.hpp:126
static Result Err(E error)
Definition Result.hpp:121
static Result Ok(std::monostate={})
Definition Result.hpp:116
std::expected< void, E > m_data
Definition Result.hpp:139
const E & error() const
Definition Result.hpp:130
Result(std::expected< void, E > data)
Definition Result.hpp:138
Definition Result.hpp:15
static Result Ok(T value)
Definition Result.hpp:17
std::optional< T > ok() const
Definition Result.hpp:92
Result orElse(F &&fn)
Definition Result.hpp:85
Result(std::expected< T, E > data)
Definition Result.hpp:105
auto andThen(F &&fn) -> std::invoke_result_t< F, T >
Definition Result.hpp:76
auto mapErr(F &&fn)
Definition Result.hpp:67
bool isErr() const noexcept
Definition Result.hpp:28
static Result Err(E error)
Definition Result.hpp:22
bool isOk() const noexcept
Definition Result.hpp:27
auto map(F &&fn)
Definition Result.hpp:58
T unwrap()
Definition Result.hpp:31
std::expected< T, E > m_data
Definition Result.hpp:106
T unwrapOr(T defaultValue) const
Definition Result.hpp:38
const E & error() const
Definition Result.hpp:50
std::optional< E > err() const
Definition Result.hpp:98
T unwrapOrElse(F &&fn)
Definition Result.hpp:44
Definition Error.hpp:17
Utility components and helper types for the LogViewer application.