Switchboard SDK
Toggle main menu visibility
Loading...
Searching...
No Matches
Result.hpp
1
//
2
// Result.hpp
3
// SwitchboardSDK
4
//
5
// Copyright © 2026 Synervoz. All rights reserved.
6
//
7
8
#pragma once
9
10
#include "export.h"
11
12
#include <switchboard/SBAny.hpp>
13
#include <optional>
14
#include <string>
15
#include <variant>
16
#include <stdexcept>
17
18
namespace
switchboard {
19
23
struct
SWITCHBOARDSDK_EXPORT
Error
{
25
std::string
message
;
26
32
Error
(std::string msg) :
message
(std::move(msg)) {}
33
};
34
38
template
<
typename
T>
39
class
SWITCHBOARDSDK_EXPORT Result {
40
private
:
41
using
ValueType = std::conditional_t<std::is_void_v<T>, std::monostate, T>;
42
std::variant<ValueType, Error> result;
43
44
public
:
45
explicit
Result(ValueType
value
) : result(std::move(
value
)) {}
46
explicit
Result(
Error
error
) : result(std::move(
error
)) {}
47
53
[[nodiscard]]
bool
isSuccess
()
const
{
54
return
std::holds_alternative<ValueType>(result);
55
}
56
62
[[nodiscard]]
bool
isError
()
const
{
63
return
std::holds_alternative<Error>(result);
64
}
65
71
[[nodiscard]] ValueType
value
()
const
{
72
if
(!
isSuccess
()) {
73
throw
std::runtime_error(
"Result does not contain a value, operation failed."
);
74
}
75
return
std::get<ValueType>(result);
76
}
77
83
[[nodiscard]]
Error
error
()
const
{
84
if
(!
isError
()) {
85
throw
std::runtime_error(
"Result does not contain an error, operation was successful."
);
86
}
87
return
std::get<Error>(result);
88
}
89
98
[[nodiscard]] Result<SBAny>
toAny
()
const
{
99
if
(
isSuccess
()) {
100
return
Result<SBAny>(
SBAny
(
value
()));
101
}
else
{
102
return
Result<SBAny>(
Error
(
error
().message));
103
}
104
}
105
};
106
114
template
<
typename
T>
115
Result<T> makeSuccess(T value) {
116
return
Result<T>(std::move(value));
117
}
118
124
inline
Result<void>
makeSuccess() {
125
return
Result<void>
(std::monostate {});
126
}
127
135
template
<
typename
T>
136
Result<T>
makeError(std::string errorMessage) {
137
return
Result<T>
(
Error
(std::move(errorMessage)));
138
}
139
140
}
switchboard::Result
Represents the result of an operation that can either succeed or fail.
Definition
Result.hpp:39
switchboard::Result::isSuccess
bool isSuccess() const
Returns true if the operation was successful.
Definition
Result.hpp:53
switchboard::Result::value
ValueType value() const
Returns the value of the operation if it was successful.
Definition
Result.hpp:71
switchboard::Result::toAny
Result< SBAny > toAny() const
Converts the result to a SBAny type.
Definition
Result.hpp:98
switchboard::Result::error
Error error() const
Returns the error of the operation if it failed.
Definition
Result.hpp:83
switchboard::Result::isError
bool isError() const
Returns true if the operation failed.
Definition
Result.hpp:62
switchboard::SBAny
A versatile container class that can hold values of various types.
Definition
SBAny.hpp:53
switchboard::Error
Represents an error that can occur during an operation.
Definition
Result.hpp:23
switchboard::Error::message
std::string message
Error message.
Definition
Result.hpp:25
switchboard::Error::Error
Error(std::string msg)
Error constructor.
Definition
Result.hpp:32
SwitchboardSDK
include
switchboard
Result.hpp
Generated by
1.17.0