Porytiles
Loading...
Searching...
No Matches
chainable_result.hpp File Reference
#include <expected>
#include <memory>
#include <string>
#include <type_traits>
#include "porytiles2/xcut/panic/panic.hpp"
#include "porytiles2/xcut/result/error.hpp"
Include dependency graph for chainable_result.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

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

Namespaces

namespace  porytiles2
 
namespace  porytiles2::detail
 

Macros

#define PT_TRY_ASSIGN_CHAIN_ERR(var, expr, msg, 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, msg, return_type, counter)
 
#define PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL(expr, msg, return_type, counter)    PT_DETAIL_TRY_CALL_CHAIN_ERR_EXPAND(expr, msg, return_type, counter)
 
#define PT_TRY_CALL_CHAIN_ERR(expr, msg, return_type)    PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL(expr, msg, return_type, __COUNTER__)
 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, __COUNTER__)
 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, __COUNTER__)
 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,
  msg,
  return_type,
  counter 
)
Value:
auto pt_try_call_result_##counter = (expr); \
if (!pt_try_call_result_##counter.has_value()) { \
return ChainableResult<return_type>{FormattableError{msg}, pt_try_call_result_##counter}; \
}

Definition at line 386 of file chainable_result.hpp.

◆ PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL

#define PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL (   expr,
  msg,
  return_type,
  counter 
)     PT_DETAIL_TRY_CALL_CHAIN_ERR_EXPAND(expr, msg, return_type, counter)

Definition at line 393 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 417 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 424 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 449 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 456 of file chainable_result.hpp.

◆ PT_TRY_ASSIGN_CHAIN_ERR

#define PT_TRY_ASSIGN_CHAIN_ERR (   var,
  expr,
  msg,
  return_type 
)
Value:
auto var##_result = (expr); \
if (!var##_result.has_value()) { \
return ChainableResult<return_type>{FormattableError{msg}, 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.

Parameters
varThe variable name to assign the unwrapped value to
exprThe expression returning a ChainableResult
msgThe error message to chain if the result contains an error
return_typeThe success type of the ChainableResult to return on error

Definition at line 331 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 356 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 378 of file chainable_result.hpp.

◆ PT_TRY_CALL_CHAIN_ERR

#define PT_TRY_CALL_CHAIN_ERR (   expr,
  msg,
  return_type 
)     PT_DETAIL_TRY_CALL_CHAIN_ERR_IMPL(expr, msg, return_type, __COUNTER__)

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<void> containing the original error chain plus the new error message provided.

Uses COUNTER internally to generate unique variable names and avoid naming collisions.

Parameters
exprThe expression returning a ChainableResult<void, E>
msgThe error message to chain if the result contains an error
return_typeThe success type of the ChainableResult to return on error

Definition at line 413 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, __COUNTER__)

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 COUNTER internally to generate unique variable names and avoid naming collisions.

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 446 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, __COUNTER__)

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 COUNTER internally to generate unique variable names and avoid naming collisions.

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

Definition at line 476 of file chainable_result.hpp.