Rerun C++ SDK
Loading...
Searching...
No Matches
result.hpp
1#pragma once
2
3#include <type_traits>
4#include <utility>
5
6#include "error.hpp"
7
8namespace rerun {
9 /// A class for representing either a usable value, or an error.
10 ///
11 /// In essence a simplified version of rust's Result or arrow's arrow::Result, always using
12 /// rerun::Status. For simplicity, the wrapped type must be default constructible.
13 template <typename T>
14 class [[nodiscard]] Result {
15 static_assert(
16 std::is_default_constructible<T>::value,
17 "Result can only wrap default constructible types."
18 );
19
20 public:
21 /// Don't allow uninitialized results.
22 Result() = delete;
23
24 /// Construct a result from a value, setting error to ok.
25 Result(T _value) : value(std::move(_value)), error() {}
26
27 /// Construct a result from an error, default constructing the value.
28 Result(rerun::Error _error) : value(), error(std::move(_error)) {}
29
30 /// Construct a result from an arrow status, default constructing the value.
31 Result(const arrow::Status& status) : value(), error(status) {}
32
33 /// Construct a result from an arrow status, default constructing the value.
34 Result(arrow::Status&& status) : value(), error(std::move(status)) {}
35
36 /// Returns true if error is set to rerun::ErrorCode::Ok, implying that a value is
37 /// contained, false otherwise.
38 bool is_ok() const {
39 return error.is_ok();
40 }
41
42 /// Returns true if error is not set to rerun::ErrorCode::Ok, implying that no value is
43 /// contained, false otherwise.
44 bool is_err() const {
45 return error.is_err();
46 }
47
48#ifdef __cpp_exceptions
49 /// Returns the value if status is ok, throws otherwise.
50 const T& value_or_throw() const& {
51 error.throw_on_failure();
52 return value;
53 }
54
55 /// Returns the value if status is ok, throws otherwise.
56 T value_or_throw() && {
57 error.throw_on_failure();
58 return std::move(value);
59 }
60#endif
61
62 public:
63 T value;
64 rerun::Error error;
65 };
66} // namespace rerun
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:95
A class for representing either a usable value, or an error.
Definition result.hpp:14
Result(const arrow::Status &status)
Construct a result from an arrow status, default constructing the value.
Definition result.hpp:31
Result()=delete
Don't allow uninitialized results.
bool is_ok() const
Returns true if error is set to rerun::ErrorCode::Ok, implying that a value is contained,...
Definition result.hpp:38
bool is_err() const
Returns true if error is not set to rerun::ErrorCode::Ok, implying that no value is contained,...
Definition result.hpp:44
Result(rerun::Error _error)
Construct a result from an error, default constructing the value.
Definition result.hpp:28
Result(arrow::Status &&status)
Construct a result from an arrow status, default constructing the value.
Definition result.hpp:34
Result(T _value)
Construct a result from a value, setting error to ok.
Definition result.hpp:25
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:22