Porytiles
Loading...
Searching...
No Matches
chainable_result.hpp File Reference
#include <expected>
#include <memory>
#include <string>
#include <type_traits>
#include "porytiles/utilities/panic/panic.hpp"
#include "porytiles/utilities/result/error.hpp"
Include dependency graph for chainable_result.hpp:

Go to the source code of this file.

Classes

class  porytiles::ChainableResult< T, E >
 A result type that maintains a chainable sequence of errors for debugging and error reporting. More...
 
class  porytiles::ChainableResult< void, E >
 Template specialization of ChainableResult for void success type. More...
 

Namespaces

namespace  porytiles
 
namespace  porytiles::detail
 

Macros

#define PT_TRY_ASSIGN_CHAIN_ERR(var, expr, return_type, ...)
 Unwraps a ChainableResult, chaining a new error message on failure.
 
#define PT_TRY_ASSIGN_PASS_ERR(var, expr, return_type)
 Unwraps a ChainableResult, passing through the error chain with an empty FormattableError when types differ.
 
#define PT_TRY_ASSIGN_PASS_SAME_ERR(var, expr)
 Unwraps a ChainableResult, passing through the error unchanged when types match.
 
#define PT_DETAIL_TRY_CALL_CHAIN_ERR_EXPAND(expr, return_type, counter, ...)
 
#define PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL(expr, return_type, counter, ...)    PT_DETAIL_TRY_CALL_CHAIN_ERR_EXPAND(expr, return_type, counter, __VA_ARGS__)
 
#define PT_TRY_CALL_CHAIN_ERR(expr, return_type, ...)    PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL(expr, return_type, __LINE__, __VA_ARGS__)
 Unwraps a void ChainableResult, chaining a new error message on failure.
 
#define PT_DETAIL_TRY_CALL_PASS_ERR_EXPAND(expr, return_type, counter)
 
#define PT_DETAIL_TRY_CALL_PASS_ERR_IMPL(expr, return_type, counter)    PT_DETAIL_TRY_CALL_PASS_ERR_EXPAND(expr, return_type, counter)
 
#define PT_TRY_CALL_PASS_ERR(expr, return_type)   PT_DETAIL_TRY_CALL_PASS_ERR_IMPL(expr, return_type, __LINE__)
 Unwraps a void ChainableResult, passing through the error chain with an empty FormattableError when types differ.
 
#define PT_DETAIL_TRY_CALL_PASS_SAME_ERR_EXPAND(expr, counter)
 
#define PT_DETAIL_TRY_CALL_PASS_SAME_ERR_IMPL(expr, counter)   PT_DETAIL_TRY_CALL_PASS_SAME_ERR_EXPAND(expr, counter)
 
#define PT_TRY_CALL_PASS_SAME_ERR(expr)   PT_DETAIL_TRY_CALL_PASS_SAME_ERR_IMPL(expr, __LINE__)
 Unwraps a void ChainableResult, passing through the error unchanged when types match.
 

Macro Definition Documentation

◆ PT_DETAIL_TRY_CALL_CHAIN_ERR_EXPAND

#define PT_DETAIL_TRY_CALL_CHAIN_ERR_EXPAND (   expr,
  return_type,
  counter,
  ... 
)
Value:
auto pt_try_call_result_##counter = (expr); \
if (!pt_try_call_result_##counter.has_value()) { \
return ChainableResult<return_type>{FormattableError{__VA_ARGS__}, pt_try_call_result_##counter}; \
}

Definition at line 425 of file chainable_result.hpp.

◆ PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL

#define PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL (   expr,
  return_type,
  counter,
  ... 
)     PT_DETAIL_TRY_CALL_CHAIN_ERR_EXPAND(expr, return_type, counter, __VA_ARGS__)

Definition at line 432 of file chainable_result.hpp.

◆ PT_DETAIL_TRY_CALL_PASS_ERR_EXPAND

#define PT_DETAIL_TRY_CALL_PASS_ERR_EXPAND (   expr,
  return_type,
  counter 
)
Value:
auto pt_try_call_result_##counter = (expr); \
if (!pt_try_call_result_##counter.has_value()) { \
return ChainableResult<return_type>{FormattableError{}, pt_try_call_result_##counter}; \
}

Definition at line 463 of file chainable_result.hpp.

◆ PT_DETAIL_TRY_CALL_PASS_ERR_IMPL

#define PT_DETAIL_TRY_CALL_PASS_ERR_IMPL (   expr,
  return_type,
  counter 
)     PT_DETAIL_TRY_CALL_PASS_ERR_EXPAND(expr, return_type, counter)

Definition at line 470 of file chainable_result.hpp.

◆ PT_DETAIL_TRY_CALL_PASS_SAME_ERR_EXPAND

#define PT_DETAIL_TRY_CALL_PASS_SAME_ERR_EXPAND (   expr,
  counter 
)
Value:
auto pt_try_call_result_##counter = (expr); \
if (!pt_try_call_result_##counter.has_value()) { \
return pt_try_call_result_##counter; \
}

Definition at line 497 of file chainable_result.hpp.

◆ PT_DETAIL_TRY_CALL_PASS_SAME_ERR_IMPL

#define PT_DETAIL_TRY_CALL_PASS_SAME_ERR_IMPL (   expr,
  counter 
)    PT_DETAIL_TRY_CALL_PASS_SAME_ERR_EXPAND(expr, counter)

Definition at line 504 of file chainable_result.hpp.

◆ PT_TRY_ASSIGN_CHAIN_ERR

#define PT_TRY_ASSIGN_CHAIN_ERR (   var,
  expr,
  return_type,
  ... 
)
Value:
auto var##_result = (expr); \
if (!var##_result.has_value()) { \
return ChainableResult<return_type>{FormattableError{__VA_ARGS__}, var##_result}; \
} \
auto var = std::move(var##_result).value();

Unwraps a ChainableResult, chaining a new error message on failure.

This macro provides a succinct way to handle ChainableResult unwrapping with error propagation. It evaluates the expression, checks if it contains a value, and either assigns the value to the variable or returns early with a new error chained to the existing error chain. This reduces the common 6-line error handling pattern to a single line.

If the result contains an error, the macro returns from the current function with a ChainableResult containing the original error chain plus the new error message provided.

The variadic args are forwarded directly to the FormattableError constructor, so you can pass a plain string or a format string with FormatParam arguments. When passing FormatParam inside the macro call, use parentheses instead of braces to avoid preprocessor comma-splitting: FormatParam(name, Style::bold) not FormatParam{name, Style::bold}.

Parameters
varThe variable name to assign the unwrapped value to
exprThe expression returning a ChainableResult
return_typeThe success type of the ChainableResult to return on error
...Arguments forwarded to the FormattableError constructor (format string, optional FormatParams)

Definition at line 370 of file chainable_result.hpp.

◆ PT_TRY_ASSIGN_PASS_ERR

#define PT_TRY_ASSIGN_PASS_ERR (   var,
  expr,
  return_type 
)
Value:
auto var##_result = (expr); \
if (!var##_result.has_value()) { \
return ChainableResult<return_type>{FormattableError{}, var##_result}; \
} \
auto var = std::move(var##_result).value();

Unwraps a ChainableResult, passing through the error chain with an empty FormattableError when types differ.

This macro provides a succinct way to handle ChainableResult unwrapping with error passthrough when the inner result's success type differs from the outer function's return type. It evaluates the expression, checks if it contains a value, and either assigns the value to the variable or returns early with an empty FormattableError chained to the existing error chain. This is useful when the current layer doesn't need to add additional error context but the result types don't match (e.g., inner function returns ChainableResult<Foo, E> but outer returns ChainableResult<Bar, E>).

If the result contains an error, the macro returns from the current function with a new ChainableResult<return_type> containing an empty FormattableError chained to the original error chain.

Parameters
varThe variable name to assign the unwrapped value to
exprThe expression returning a ChainableResult
return_typeThe success type of the ChainableResult to return on error (differs from expr's success type)

Definition at line 395 of file chainable_result.hpp.

◆ PT_TRY_ASSIGN_PASS_SAME_ERR

#define PT_TRY_ASSIGN_PASS_SAME_ERR (   var,
  expr 
)
Value:
auto var##_result = (expr); \
if (!var##_result.has_value()) { \
return var##_result; \
} \
auto var = std::move(var##_result).value();

Unwraps a ChainableResult, passing through the error unchanged when types match.

This macro provides a succinct way to handle ChainableResult unwrapping with error passthrough when the inner result's type matches the outer function's return type. It evaluates the expression, checks if it contains a value, and either assigns the value to the variable or returns early with the same error result unchanged. This is useful when the current layer doesn't need to add additional error context and the result types match exactly.

If the result contains an error, the macro returns from the current function with the same error result unchanged, preserving the existing error chain without any modifications.

Parameters
varThe variable name to assign the unwrapped value to
exprThe expression returning a ChainableResult with the same type as the outer function's return type

Definition at line 417 of file chainable_result.hpp.

◆ PT_TRY_CALL_CHAIN_ERR

#define PT_TRY_CALL_CHAIN_ERR (   expr,
  return_type,
  ... 
)     PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL(expr, return_type, __LINE__, __VA_ARGS__)

Unwraps a void ChainableResult, chaining a new error message on failure.

This macro provides a succinct way to handle void-returning ChainableResult unwrapping with error propagation. It evaluates the expression, checks if it contains a success value, and either continues execution or returns early with a new error chained to the existing error chain. This is the void equivalent of PT_TRY_ASSIGN_CHAIN_ERR.

If the result contains an error, the macro returns from the current function with a ChainableResult containing the original error chain plus the new error message provided.

The variadic args are forwarded directly to the FormattableError constructor, so you can pass a plain string or a format string with FormatParam arguments. When passing FormatParam inside the macro call, use parentheses instead of braces to avoid preprocessor comma-splitting: FormatParam(name, Style::bold) not FormatParam{name, Style::bold}.

Uses __LINE__ internally to generate unique variable names and avoid naming collisions between multiple expansions within the same scope. Because the uniqueness token is the source line number, this macro must not be invoked more than once on the same physical line — doing so would produce colliding local variable names.

Parameters
exprThe expression returning a ChainableResult<void, E>
return_typeThe success type of the ChainableResult to return on error
...Arguments forwarded to the FormattableError constructor (format string, optional FormatParams)

Definition at line 459 of file chainable_result.hpp.

◆ PT_TRY_CALL_PASS_ERR

#define PT_TRY_CALL_PASS_ERR (   expr,
  return_type 
)    PT_DETAIL_TRY_CALL_PASS_ERR_IMPL(expr, return_type, __LINE__)

Unwraps a void ChainableResult, passing through the error chain with an empty FormattableError when types differ.

This macro provides a succinct way to handle void-returning ChainableResult unwrapping with error passthrough when the inner result's success type differs from the outer function's return type. It evaluates the expression, checks if it contains a success value, and either continues execution or returns early with an empty FormattableError chained to the existing error chain. This is the void equivalent of PT_TRY_ASSIGN_PASS_ERR and is useful when the current layer doesn't need to add additional error context but the result types don't match.

If the result contains an error, the macro returns from the current function with a new ChainableResult<return_type> containing an empty FormattableError chained to the original error chain.

Uses __LINE__ internally to generate unique variable names and avoid naming collisions between multiple expansions within the same scope. Because the uniqueness token is the source line number, this macro must not be invoked more than once on the same physical line — doing so would produce colliding local variable names.

Parameters
exprThe expression returning a ChainableResult<void, E>
return_typeThe success type of the ChainableResult to return on error (differs from expr's success type)

Definition at line 494 of file chainable_result.hpp.

◆ PT_TRY_CALL_PASS_SAME_ERR

#define PT_TRY_CALL_PASS_SAME_ERR (   expr)    PT_DETAIL_TRY_CALL_PASS_SAME_ERR_IMPL(expr, __LINE__)

Unwraps a void ChainableResult, passing through the error unchanged when types match.

This macro provides a succinct way to handle void-returning ChainableResult unwrapping with error passthrough when the inner result's type matches the outer function's return type. It evaluates the expression, checks if it contains a success value, and either continues execution or returns early with the same error result unchanged. This is the void equivalent of PT_TRY_ASSIGN_PASS_SAME_ERR and is useful when the current layer doesn't need to add additional error context and the result types match exactly.

If the result contains an error, the macro returns from the current function with the same error result unchanged, preserving the existing error chain without any modifications.

Uses __LINE__ internally to generate unique variable names and avoid naming collisions between multiple expansions within the same scope. Because the uniqueness token is the source line number, this macro must not be invoked more than once on the same physical line — doing so would produce colliding local variable names.

Parameters
exprThe expression returning a ChainableResult<void, E> with the same type as the outer function's return type

Definition at line 526 of file chainable_result.hpp.