ReXGlue SDK
Loading...
Searching...
No Matches
result.h File Reference
#include <expected>
#include <string>
#include <utility>
#include <rex/types.h>

Go to the source code of this file.

Classes

struct  rex::Error

Namespaces

namespace  rex

Macros

#define REX_TRY_PASTE(a, b)
#define REX_TRY_EXPAND_PASTE(a, b)
#define REX_TRY(decl, expr)
#define REX_TRY_VOID(expr)
#define REX_ERROR_IF(cond, error)

Typedefs

template<typename T>
using rex::Result = std::expected<T, Error>

Enumerations

enum class  rex::ErrorCategory : u8 {
  kNoError , kIO , rex::kMemory , rex::kFormat ,
  rex::kCrypto , kCompression , rex::kRuntime , kPlatform ,
  kConfig , rex::kValidation , kNotFound , kNotImplemented ,
  rex::kModule
}

Functions

template<typename T>
Result< T > rex::Ok (T &&value)
Result< void > rex::Ok ()
template<typename T = void>
auto rex::Err (Error error)
template<typename T = void>
auto rex::Err (ErrorCategory category, std::string message, int code=0)

Detailed Description

License
BSD 3-Clause License See LICENSE file in the project root for full license text.

Macro Definition Documentation

◆ REX_ERROR_IF

#define REX_ERROR_IF ( cond,
error )
Value:
do { \
if (cond) { \
return std::unexpected(error); \
} \
} while (0)

Returns an error from the enclosing function when a condition holds.

Parameters
errorThe rex::Error to return when the condition is true.
@ kValidation
Definition result.h:34
#define REX_ERROR_IF(cond, error)
Definition result.h:209
Definition result.h:43

◆ REX_TRY

#define REX_TRY ( decl,
expr )
Value:
auto REX_TRY_EXPAND_PASTE(_rex_try_, __LINE__) = (expr); \
if (!REX_TRY_EXPAND_PASTE(_rex_try_, __LINE__)) { \
return std::unexpected(std::move(REX_TRY_EXPAND_PASTE(_rex_try_, __LINE__)).error()); \
} \
decl = *std::move(REX_TRY_EXPAND_PASTE(_rex_try_, __LINE__))
#define REX_TRY_EXPAND_PASTE(a, b)
Definition result.h:160

Unwraps a rex::Result into a declaration, or returns its error from the enclosing function.

It is a statement, not an expression.

Parameters
declThe declaration that receives the value.
exprAn expression of type rex::Result<T>.
REX_TRY(auto file, Open(path));
#define REX_TRY(decl, expr)
Definition result.h:175

◆ REX_TRY_EXPAND_PASTE

#define REX_TRY_EXPAND_PASTE ( a,
b )
Value:
#define REX_TRY_PASTE(a, b)
Definition result.h:151

Pastes two tokens after expanding them, so __LINE__ becomes the line number.

auto REX_TRY_EXPAND_PASTE(_rex_try_, __LINE__) = Open(path);

◆ REX_TRY_PASTE

#define REX_TRY_PASTE ( a,
b )
Value:
a##b

Pastes two tokens without expanding them first. Helper of REX_TRY_EXPAND_PASTE.

The example yields the token _rex_try_12.

REX_TRY_PASTE(_rex_try_, 12)

◆ REX_TRY_VOID

#define REX_TRY_VOID ( expr)
Value:
do { \
auto&& _rex_try_status = (expr); \
if (!_rex_try_status) { \
return std::unexpected(std::move(_rex_try_status).error()); \
} \
} while (0)

Returns the error of a rex::Result expression from the enclosing function.

Parameters
exprAn expression of type rex::Result<T>, usually rex::Result<void>.
REX_TRY_VOID(module->Init());
#define REX_TRY_VOID(expr)
Definition result.h:192