52template<
typename T>
class Result;
53template<
typename T>
class Optional;
93 error_info(
int c,
const std::string& msg,
const std::string& mod =
"")
97 error_info(
int c,
const std::string& msg,
const std::string& mod,
98 const std::string& det)
107 template<
typename Enum,
108 typename std::enable_if_t<std::is_enum_v<Enum>,
int> = 0>
110 std::optional<std::string> det = std::nullopt)
111 :
code(static_cast<int>(c)),
118 module == other.module && details == other.details;
122 return !(*
this == other);
137 module(std::string(ec.category_name())) {}
222 template<
typename U = T>
252 static Result<T> err(
int code,
const std::string& message,
const std::string& module =
"") {
291 return value_.has_value();
298 return error_.has_value();
306#if KCENON_HAS_SOURCE_LOCATION
312 std::ostringstream oss;
313 oss <<
"Called unwrap on error: " <<
err.message <<
"\n"
314 <<
" Error code: " <<
err.code <<
"\n"
315 <<
" Module: " << (
err.module.empty() ?
"unknown" :
err.module) <<
"\n"
316 <<
" Location: " << loc.file_name() <<
":" << loc.line() <<
":" << loc.column() <<
"\n"
317 <<
" Function: " << loc.function_name();
318 if (
err.details.has_value()) {
319 oss <<
"\n Details: " <<
err.details.value();
321 throw std::runtime_error(oss.str());
329 throw std::runtime_error(
"Called unwrap on error: " +
err.message);
340#if KCENON_HAS_SOURCE_LOCATION
346 std::ostringstream oss;
347 oss <<
"Called unwrap on error: " <<
err.message <<
"\n"
348 <<
" Error code: " <<
err.code <<
"\n"
349 <<
" Module: " << (
err.module.empty() ?
"unknown" :
err.module) <<
"\n"
350 <<
" Location: " << loc.file_name() <<
":" << loc.line() <<
":" << loc.column() <<
"\n"
351 <<
" Function: " << loc.function_name();
352 if (
err.details.has_value()) {
353 oss <<
"\n Details: " <<
err.details.value();
355 throw std::runtime_error(oss.str());
363 throw std::runtime_error(
"Called unwrap on error: " +
err.message);
376 return default_value;
385 return unwrap_or(std::move(default_value));
414 using ReturnType =
decltype(func(std::declval<T>()));
430 auto and_then(F&& func)
const ->
decltype(func(std::declval<T>())) {
431 using ReturnType =
decltype(func(std::declval<T>()));
434 return func(
value_.value());
436 return ReturnType(
error_.value());
439 return ReturnType::uninitialized();
451 return func(
error_.value());
494#if KCENON_HAS_SOURCE_LOCATION
499 std::ostringstream oss;
500 oss <<
"Called unwrap on None\n"
501 <<
" Location: " << loc.file_name() <<
":" << loc.line() <<
":" << loc.column() <<
"\n"
502 <<
" Function: " << loc.function_name();
503 throw std::runtime_error(oss.str());
510 throw std::runtime_error(
"Called unwrap on None");
517 return value_.value_or(default_value);
522 using ReturnType =
decltype(func(std::declval<T>()));
Optional type similar to std::optional with Rust-like API.
T value_type
Type alias for the contained value type (for concept compatibility)
std::optional< T > value_
const T & unwrap() const
Get value from optional (throws if None)
T unwrap_or(T default_value) const
auto map(F &&func) const -> Optional< decltype(func(std::declval< T >()))>
Result type for error handling with member function support.
const error_info & error() const
Get error reference.
Result(const Result &)=default
static Result< T > err(int code, const std::string &message, const std::string &module="")
Create an error result with code and message (static factory)
Result & operator=(Result &&)=default
bool is_err() const
Check if result contains an error.
Result()=delete
Default constructor is deleted to enforce explicit initialization.
T & unwrap()
Get mutable value from result (throws if error)
Result(const typed_error_code &ec)
Construct from category-based typed_error_code.
static Result< T > err(const typed_error_code &ec)
Create an error result from category-based typed_error_code (static factory)
static Result< T > uninitialized()
Create an explicitly uninitialized result (use with caution)
T value_or(T default_value) const
Get value or return default (C++23 std::expected compatible)
Result(Result &&)=default
auto map(F &&func) const -> Result< decltype(func(std::declval< T >()))>
Map a function over a successful result.
T value_type
Type alias for the contained value type (for concept compatibility)
static Result< T > err(const error_info &error)
Create an error result from error_info (static factory)
static Result< T > err(error_info &&error)
Create an error result from error_info (static factory, move)
Result(error_info &&error)
static Result< T > ok(U &&value)
Create a successful result with value (static factory)
Result< T > or_else(F &&func) const
Provide alternative value if error.
auto and_then(F &&func) const -> decltype(func(std::declval< T >()))
Map a function that returns a Result (flatMap/bind)
std::optional< error_info > error_
const T & value() const
Get value reference (const)
Result(const error_info &error)
std::optional< T > value_
bool is_ok() const
Check if result contains a successful value.
Result & operator=(const Result &)=default
const T & unwrap() const
Get value from result (throws if error)
T unwrap_or(T default_value) const
Get value or return default.
T & value()
Get value reference (mutable)
A type-safe error code that carries its category.
Decentralized error category system for improved system isolation.
VoidResult ok()
Create a successful void result.
result_state
Result state enum for tracking initialization.
Optional< T > None()
Create an empty Optional.
Optional< T > Some(T value)
Create an Optional with value.
C++17-compatible source_location implementation.
Standard error information used by Result<T>.
error_info(int c, const std::string &msg, const std::string &mod, const std::string &det)
Construct with code, message, module and details.
std::optional< std::string > details
error_info(Enum c, std::string msg, std::string mod="", std::optional< std::string > det=std::nullopt)
Construct from strongly-typed enum error codes.
bool operator==(const error_info &other) const
error_info(int c, const std::string &msg, const std::string &mod="")
Construct with code, message and optional module.
error_info(const typed_error_code &ec)
Construct from the new category-based typed_error_code type.
bool operator!=(const error_info &other) const
error_info(const std::string &msg)
Construct with message only.
C++17-compatible source_location implementation using compiler builtins.
static constexpr source_location current(const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE()) noexcept