FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
expected.hpp File Reference

Enhanced error handling framework built on std::expected. More...

#include <expected>
#include <string>
#include <array>
#include <type_traits>
#include "string.hpp"
#include "../lib/log.hpp"
Include dependency graph for expected.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Value< T, E >
 Enhanced expected type with integrated logging capabilities. More...
 

Macros

#define NOPT(expr, ...)
 
#define NOPT_IDENTITY(...)
 
#define NOPT_EAT(...)
 
#define Pop(expr, ...)
 
#define discard(fmt, ...)
 
#define forward(fmt, ...)
 
#define Try(expr, ...)
 
#define Catch(expr, ...)
 
#define Error(fmt, ...)
 

Functions

template<typename Fn>
requires (not ns_concept::IsInstanceOf<std::invoke_result_t<Fn>, Value>) and (not ns_concept::IsInstanceOf<std::invoke_result_t<Fn>, std::expected>)
auto __except_impl (Fn &&f) -> Value< std::invoke_result_t< Fn > >
 Convert exceptions to Value errors.
 

Variables

constexpr auto __expected_fn = [](auto&& e) { return e; }
 Lambda helper for Pop macro error returns.
 

Detailed Description

Enhanced error handling framework built on std::expected.

Author
Ruan Formigoni

This module provides a sophisticated error handling system that extends C++23's std::expected with integrated logging capabilities, convenient macros for error propagation, and utilities for exception handling.

Key Components:

  • Value<T,E>: Enhanced std::expected with logging
  • Pop macro: Unwrap-or-return with logging
  • Try/Catch macros: Exception-to-expected conversion
  • Error macro: Create unexpected values with logging

Definition in file expected.hpp.

Macro Definition Documentation

◆ Catch

#define Catch ( expr,
... )
Value:
(__except_impl([&]{ return (expr); })__VA_OPT__(.template forward(__VA_ARGS__)))
auto __except_impl(Fn &&f) -> Value< std::invoke_result_t< Fn > >
Convert exceptions to Value errors.
Definition expected.hpp:254

Definition at line 311 of file expected.hpp.

◆ discard

#define discard ( fmt,
... )
Value:
discard_impl<fmt>(ns_log::Location() __VA_OPT__(,) __VA_ARGS__)
Source code location information for log messages.
Definition log.hpp:403

Definition at line 224 of file expected.hpp.

◆ Error

#define Error ( fmt,
... )
Value:
({ \
logger(fmt __VA_OPT__(,) __VA_ARGS__); \
[&](auto&&... __fmt_args) \
{ \
if constexpr (sizeof...(__fmt_args) > 0) \
{ \
return std::unexpected( \
std::format(std::string_view(fmt).substr(3) \
, ns_string::to_string(__fmt_args)...) \
); \
} \
else \
{ \
return std::unexpected( \
std::format(std::string_view(fmt).substr(3)) \
); \
} \
}(__VA_ARGS__); \
})
std::string to_string(T &&t) noexcept
Converts a type to a string.
Definition string.hpp:97

Definition at line 335 of file expected.hpp.

◆ forward

#define forward ( fmt,
... )
Value:
forward_impl<fmt>(ns_log::Location() __VA_OPT__(,) __VA_ARGS__)

Definition at line 238 of file expected.hpp.

◆ NOPT

#define NOPT ( expr,
... )
Value:
NOPT_IDENTITY(__VA_OPT__(NOPT_EAT) (expr))

Definition at line 154 of file expected.hpp.

◆ NOPT_EAT

#define NOPT_EAT ( ...)

Definition at line 156 of file expected.hpp.

◆ NOPT_IDENTITY

#define NOPT_IDENTITY ( ...)
Value:
__VA_ARGS__

Definition at line 155 of file expected.hpp.

◆ Pop

#define Pop ( expr,
... )
Value:
({ \
auto __expected_ret = (expr); \
if (!__expected_ret) \
{ \
std::string error = std::move(__expected_ret).error(); \
/* Log expected error at DEBUG level */ \
logger("D::{}", error); \
/* If a custom error was provided, log and propagate it instead */ \
__VA_OPT__( \
logger(__VA_ARGS__); \
error = [&](auto &&fmt, auto &&...args) \
{ \
if constexpr (sizeof...(args) > 0) \
{ \
return ns_log::vformat(std::string_view(fmt).substr(3), \
ns_string::to_string(args)...); \
} \
else \
{ \
return std::string_view(fmt).substr(3); \
} \
}(__VA_ARGS__); \
) \
return __expected_fn(std::unexpected(error)); \
} \
std::move(__expected_ret).value(); \
})
constexpr auto __expected_fn
Lambda helper for Pop macro error returns.
Definition expected.hpp:147
#define logger(fmt,...)
Compile-time log level dispatch macro with automatic location capture.
Definition log.hpp:682
std::string vformat(std::string_view fmt, Ts &&... ts)
Workaround make_format_args only taking references.
Definition log.hpp:440

Definition at line 184 of file expected.hpp.

◆ Try

#define Try ( expr,
... )
Value:
Pop(__except_impl([&]{ return (expr); }), __VA_ARGS__)

Definition at line 295 of file expected.hpp.

Function Documentation

◆ __except_impl()

template<typename Fn>
requires (not ns_concept::IsInstanceOf<std::invoke_result_t<Fn>, Value>) and (not ns_concept::IsInstanceOf<std::invoke_result_t<Fn>, std::expected>)
auto __except_impl ( Fn && f) -> Value<std::invoke_result_t<Fn>>

Convert exceptions to Value errors.

Executes a callable and catches any exceptions, converting them to Value errors with appropriate logging.

Template Parameters
FnCallable type
Parameters
fFunction to execute with exception handling
Returns
Value containing result or error message
Examples
/flatimage/src/std/expected.hpp.

Definition at line 254 of file expected.hpp.

Variable Documentation

◆ __expected_fn

auto __expected_fn = [](auto&& e) { return e; }
constexpr

Lambda helper for Pop macro error returns.

Definition at line 147 of file expected.hpp.