Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Result.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "Compiler.h" // SC_LANGUAGE_LIKELY
5namespace SC
6{
7struct SC_COMPILER_EXPORT Result;
10
12struct [[nodiscard]] Result
13{
15 const char* message;
18 explicit constexpr Result(bool result) : message(result ? nullptr : "Unspecified Error") {}
19
24 template <int numChars>
25 static constexpr Result Error(const char (&msg)[numChars])
26 {
27 return Result(msg);
28 }
29
34 static constexpr Result FromStableCharPointer(const char* msg) { return Result(msg); }
35
37 constexpr operator bool() const { return message == nullptr; }
38
39 private:
40 explicit constexpr Result(const char* message) : message(message) {}
41};
43} // namespace SC
44
47
49#define SC_TRY(expression) \
50 { \
51 if (auto _exprResConv = SC::Result(expression)) \
52 SC_LANGUAGE_LIKELY { (void)0; } \
53 else \
54 { \
55 return _exprResConv; \
56 } \
57 }
58
60#define SC_TRY_MSG(expression, failedMessage) \
61 if (not(expression)) \
62 SC_LANGUAGE_UNLIKELY { return SC::Result::Error(failedMessage); }
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
static constexpr Result Error(const char(&msg)[numChars])
Constructs an Error from a pointer to an ASCII string literal.
Definition Result.h:25
static constexpr Result FromStableCharPointer(const char *msg)
Constructs an Error from a pointer to an ascii string.
Definition Result.h:34
constexpr Result(bool result)
Build a Result object from a boolean.
Definition Result.h:18
const char * message
If == nullptr then Result is valid. If != nullptr it's the reason of the error.
Definition Result.h:15