LibQuicR
expected.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) 2026 Cisco Systems
2 // SPDX-License-Identifier: BSD-2-Clause
3 
4 #pragma once
5 
6 #include <memory>
7 #include <optional>
8 #include <type_traits>
9 #include <utility>
10 #include <variant>
11 
12 namespace quicr {
17  template<typename E>
18  class Unexpected
19  {
20  public:
22  Unexpected(const Unexpected& other) = default;
23 
25  Unexpected(Unexpected&& other) = default;
26 
32  template<typename T>
33  Unexpected(T&& value)
34  : error_{ std::forward<T>(value) }
35  {
36  }
37 
43  template<typename... Args>
44  Unexpected(Args&&... args)
45  : error_{ std::forward<Args>(args)... }
46  {
47  }
48 
50  const E& error() const { return error_; }
51 
52  private:
53  E error_;
54  };
55 
63  template<typename T, typename E>
64  class Expected
65  {
66  public:
68  Expected(const Expected& other) = default;
69 
71  Expected(Expected&& other) = default;
72 
79  template<typename U>
80  requires std::is_convertible_v<U, T> || std::is_convertible_v<U, E>
82  : data_(std::forward<U>(value))
83  {
84  }
85 
91  template<typename U>
92  requires std::is_convertible_v<U, E>
94  : data_(unex.error())
95  {
96  }
97 
99  constexpr bool has_value() const noexcept { return data_.index() == 0; }
100 
102  constexpr explicit operator bool() const noexcept { return has_value(); }
103 
108  const T& value() const { return std::get<0>(data_); }
109 
114  const E& error() const { return std::get<1>(data_); }
115 
121  constexpr const T* operator->() const noexcept { return std::addressof(value()); }
122 
123  private:
124  std::variant<T, E> data_;
125  };
126 
131  template<typename E>
132  class Expected<void, E>
133  {
134  public:
136  Expected() = default;
137 
143  template<typename U>
145  : error_(std::forward<U>(value))
146  {
147  }
148 
154  template<typename U>
155  requires std::is_convertible_v<U, E>
157  : error_(unex.error())
158  {
159  }
160 
162  bool has_value() const noexcept { return !error_.has_value(); }
163 
165  explicit operator bool() const noexcept { return has_value(); }
166 
171  const E& error() const { return error_.value(); }
172 
173  private:
174  std::optional<E> error_;
175  };
176 }
requires std::is_convertible_v< U, E > Expected(Unexpected< U > &&unex)
Constructs an error result from an unexpected value.
Definition: expected.h:156
Expected(U &&value)
Constructs an error result.
Definition: expected.h:144
bool has_value() const noexcept
Definition: expected.h:162
Expected()=default
Constructs a successful result.
const E & error() const
Definition: expected.h:171
Holds either a success value or an error value.
Definition: expected.h:65
const E & error() const
Definition: expected.h:114
const T & value() const
Definition: expected.h:108
requires std::is_convertible_v< U, T > std::is_convertible_v< U, E > Expected(U &&value)
Constructs the variant alternative selected by std::variant<T, E>.
Definition: expected.h:81
Expected(const Expected &other)=default
Copies the contained alternative.
Expected(Expected &&other)=default
Moves the contained alternative.
constexpr bool has_value() const noexcept
Definition: expected.h:99
constexpr const T * operator->() const noexcept
Definition: expected.h:121
requires std::is_convertible_v< U, E > Expected(Unexpected< U > &&unex)
Constructs an error result from an unexpected value.
Definition: expected.h:93
Wrapper that explicitly marks a value as an error.
Definition: expected.h:19
const E & error() const
Definition: expected.h:50
Unexpected(const Unexpected &other)=default
Copies an error wrapper.
Unexpected(Args &&... args)
Constructs the error in place from arguments.
Definition: expected.h:44
Unexpected(Unexpected &&other)=default
Moves an error wrapper.
Unexpected(T &&value)
Constructs the error from a forwarded value.
Definition: expected.h:33
Definition: attributes.h:13