11#ifndef OCTANE_API_CLIENT_RESULT_H_
12#define OCTANE_API_CLIENT_RESULT_H_
37 template <
typename T_OK,
typename T_Error>
39 enum struct Tag { None, OK, Error };
58 new (&
ok) T_OK(result.
ok);
72 new (&
ok) T_OK(std::move(result.ok));
75 new (&
error) T_Error(std::move(result.error));
80 result.tag = Tag::None;
99 if (&result !=
this) {
100 switch (result.tag) {
108 *
this = result.error();
117 assert(
this != &result);
118 switch (result.tag) {
123 *
this = std::move(result.ok);
126 *
this = std::move(result.error);
131 result.tag = Tag::None;
136 Result(
const T_OK& ok, [[maybe_unused]]
int) : tag(Tag::OK),
ok(
ok) {}
137 Result(T_OK&& ok, [[maybe_unused]]
int) : tag(Tag::OK), ok(std::move(ok)) {}
139 Result(
const T_Error& error) : tag(Tag::Error),
error(
error) {}
140 Result(T_Error&& error) : tag(Tag::Error),
error(std::move(
error)) {}
149 operator bool() const noexcept {
150 assert(tag != Tag::None);
151 return tag == Tag::OK;
160 assert(tag != Tag::None);
161 return tag == Tag::Error;
172 assert(tag == Tag::OK);
184 assert(tag == Tag::OK);
195 const T_Error&
err()
const {
196 assert(tag == Tag::Error);
208 assert(tag == Tag::Error);
235 template <
typename T>
242 template <
typename T_OK,
typename T_Error>
260 template <
typename T_OK>
261 decltype(
auto)
ok(
const T_OK&
ok) {
277 template <
typename T_OK>
279 return ok_t(std::move(
ok));
293 template <
typename T_OK = _>
309 template <
typename T>
316 template <
typename T_OK,
typename T_Error>
349 template <
typename T_Error,
size_t N>
368 template <
typename T_Error>
387 template <
typename T_Error>
Wrapper class that bifurcates the values indicated by normal and quasi-normal states.
Definition: result.h:38
bool operator!() const noexcept
Judges whether the state is quasi-normal.
Definition: result.h:159
Result & operator=(const Result &result) &
Definition: result.h:98
T_Error error
Definition: result.h:43
T_OK ok
Definition: result.h:42
~Result()
Definition: result.h:82
Result(const Result &result)
Definition: result.h:53
Result & operator=(Result &&result) &
Definition: result.h:116
const T_OK & get() const
Get the value of normal state as a constant.
Definition: result.h:171
const T_Error & err() const
Get the value of quasi-normal state as a constant.
Definition: result.h:195
T_Error & err()
Get the value of quasi-normal state.
Definition: result.h:207
T_OK & get()
Get the value of normal state.
Definition: result.h:183
Result(Result &&result)
Definition: result.h:67
Utility class that can be used when we only want to specify quasi-normal state types.
Definition: result.h:310
error_t(T &&error)
Definition: result.h:315
error_t(const T &error)
Definition: result.h:314
Utility class that can be used when we only want to specify normal state types.
Definition: result.h:236
ok_t(const T &ok)
Definition: result.h:240
ok_t(T &&ok)
Definition: result.h:241
Definition: api_client.cpp:20
decltype(auto) error(const T_Error(&error)[N])
Utility function used to construct quasi-normal state values.
Definition: result.h:350
decltype(auto) err(T_Error &&error)
Utility function to construct quasi-normal state values.
Definition: result.h:388
decltype(auto) ok(const T_OK &ok)
Utility function used to construct normal state values.
Definition: result.h:261
ok_t< _ > ok()
Utitlity function used when the normal state type is _.
Definition: result.h:294
Structure representing that it returns nothing.
Definition: result.h:222