LiveKit C++ Client SDK v1.1.0
Real-time audio/video/data SDK for C++
Loading...
Searching...
No Matches
result.h
1/*
2 * Copyright 2026 LiveKit
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an “AS IS” BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <optional>
20#include <stdexcept>
21#include <type_traits>
22#include <utility>
23#include <variant>
24
25namespace livekit {
26
39template <typename T, typename E>
40class [[nodiscard]] Result {
41public:
43 template <typename U = T, typename = std::enable_if_t<std::is_constructible<T, U&&>::value>>
44 static Result success(U&& value) {
45 return Result(std::variant<T, E>(std::in_place_index<0>, std::forward<U>(value)));
46 }
47
49 template <typename F = E, typename = std::enable_if_t<std::is_constructible<E, F&&>::value>>
50 static Result failure(F&& error) {
51 return Result(std::variant<T, E>(std::in_place_index<1>, std::forward<F>(error)));
52 }
53
55 bool ok() const noexcept { return storage_.index() == 0; }
57 bool hasError() const noexcept { return !ok(); }
59 explicit operator bool() const noexcept { return ok(); }
60
64 T& value() & {
65 if (!ok()) {
66 throw std::logic_error("Result::value() called on an error result");
67 }
68 return std::get<0>(storage_);
69 }
70
74 const T& value() const& {
75 if (!ok()) {
76 throw std::logic_error("Result::value() called on an error result");
77 }
78 return std::get<0>(storage_);
79 }
80
84 T&& value() && {
85 if (!ok()) {
86 throw std::logic_error("Result::value() called on an error result");
87 }
88 return std::get<0>(std::move(storage_));
89 }
90
94 const T&& value() const&& {
95 if (!ok()) {
96 throw std::logic_error("Result::value() called on an error result");
97 }
98 return std::get<0>(std::move(storage_));
99 }
100
104 E& error() & {
105 if (!hasError()) {
106 throw std::logic_error("Result::error() called on a success result");
107 }
108 return std::get<1>(storage_);
109 }
110
114 const E& error() const& {
115 if (!hasError()) {
116 throw std::logic_error("Result::error() called on a success result");
117 }
118 return std::get<1>(storage_);
119 }
120
124 E&& error() && {
125 if (!hasError()) {
126 throw std::logic_error("Result::error() called on a success result");
127 }
128 return std::get<1>(std::move(storage_));
129 }
130
134 const E&& error() const&& {
135 if (!hasError()) {
136 throw std::logic_error("Result::error() called on a success result");
137 }
138 return std::get<1>(std::move(storage_));
139 }
140
141private:
142 explicit Result(std::variant<T, E> storage) : storage_(std::move(storage)) {}
143
144 std::variant<T, E> storage_;
145};
146
151template <typename E>
152class [[nodiscard]] Result<void, E> {
153public:
155 static Result success() { return Result(std::nullopt); }
156
158 template <typename F = E, typename = std::enable_if_t<std::is_constructible<E, F&&>::value>>
159 static Result failure(F&& error) {
160 return Result(std::optional<E>(std::forward<F>(error)));
161 }
162
164 bool ok() const noexcept { return !error_.has_value(); }
166 bool hasError() const noexcept { return error_.has_value(); }
168 explicit operator bool() const noexcept { return ok(); }
169
174 void value() const {
175 if (!ok()) {
176 throw std::logic_error("Result::value() called on an error result");
177 }
178 }
179
183 E& error() & {
184 if (!error_.has_value()) {
185 throw std::logic_error("Result::error() called on a success result");
186 }
187 return *error_;
188 }
189
193 const E& error() const& {
194 if (!error_.has_value()) {
195 throw std::logic_error("Result::error() called on a success result");
196 }
197 return *error_;
198 }
199
203 E&& error() && {
204 if (!error_.has_value()) {
205 throw std::logic_error("Result::error() called on a success result");
206 }
207 return std::move(*error_);
208 }
209
213 const E&& error() const&& {
214 if (!error_.has_value()) {
215 throw std::logic_error("Result::error() called on a success result");
216 }
217 return std::move(*error_);
218 }
219
220private:
221 explicit Result(std::optional<E> error) : error_(std::move(error)) {}
222
223 std::optional<E> error_;
224};
225
226} // namespace livekit
static Result failure(F &&error)
Construct a failed result containing an error.
Definition result.h:159
const E && error() const &&
Move the error value out.
Definition result.h:213
const E & error() const &
Access the error value.
Definition result.h:193
void value() const
Validates success.
Definition result.h:174
E && error() &&
Move the error value out.
Definition result.h:203
bool ok() const noexcept
True when the operation succeeded.
Definition result.h:164
static Result success()
Construct a successful result with no payload.
Definition result.h:155
bool hasError() const noexcept
True when the operation failed.
Definition result.h:166
E & error() &
Access the error value.
Definition result.h:183
Lightweight success-or-error return type for non-exceptional API failures.
Definition result.h:40
const T & value() const &
Access the success value.
Definition result.h:74
static Result failure(F &&error)
Construct a failed result containing an error.
Definition result.h:50
static Result success(U &&value)
Construct a successful result containing a value.
Definition result.h:44
const T && value() const &&
Move the success value out.
Definition result.h:94
bool ok() const noexcept
True when the result contains a success value.
Definition result.h:55
const E & error() const &
Access the error value.
Definition result.h:114
bool hasError() const noexcept
True when the result contains an error.
Definition result.h:57
T && value() &&
Move the success value out.
Definition result.h:84
T & value() &
Access the success value.
Definition result.h:64
const E && error() const &&
Move the error value out.
Definition result.h:134
E && error() &&
Move the error value out.
Definition result.h:124
E & error() &
Access the error value.
Definition result.h:104
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25