28#if __has_include(<source_location>)
29 #include <source_location>
30 #define KCENON_MODULE_HAS_SOURCE_LOCATION 1
32 #define KCENON_MODULE_HAS_SOURCE_LOCATION 0
43#if KCENON_MODULE_HAS_SOURCE_LOCATION
44using source_location = std::source_location;
46struct source_location {
49 const char* file = __builtin_FILE(),
50 const char* function = __builtin_FUNCTION(),
51 int line = __builtin_LINE()
57 constexpr int line() const noexcept {
return line_; }
61 const char* file = __builtin_FILE(),
62 const char* function = __builtin_FUNCTION(),
63 int line = __builtin_LINE()
81template<
typename T>
class Result;
82template<
typename T>
class Optional;
110 std::optional<std::string>
details;
119 error_info(
int c,
const std::string& msg,
const std::string& mod =
"")
123 error_info(
int c,
const std::string& msg,
const std::string& mod,
124 const std::string& det)
130 template<
typename Enum,
131 typename std::enable_if_t<std::is_enum_v<Enum>,
int> = 0>
133 std::optional<std::string> det = std::nullopt)
134 :
code(static_cast<int>(c)),
141 module == other.module && details == other.details;
145 return !(*
this == other);
172 std::optional<error_info>
error_;
188 template<
typename U = T>
201 static Result<T> err(
int code,
const std::string& message,
const std::string& module =
"") {
210 return value_.has_value();
214 return error_.has_value();
217#if KCENON_MODULE_HAS_SOURCE_LOCATION
221 std::ostringstream oss;
222 oss <<
"Called unwrap on error: " <<
err.message <<
"\n"
223 <<
" Error code: " <<
err.code <<
"\n"
224 <<
" Module: " << (
err.module.empty() ?
"unknown" :
err.module) <<
"\n"
225 <<
" Location: " << loc.file_name() <<
":" << loc.line() <<
":" << loc.column() <<
"\n"
226 <<
" Function: " << loc.function_name();
227 if (
err.details.has_value()) {
228 oss <<
"\n Details: " <<
err.details.value();
230 throw std::runtime_error(oss.str());
238 std::ostringstream oss;
239 oss <<
"Called unwrap on error: " <<
err.message <<
"\n"
240 <<
" Error code: " <<
err.code <<
"\n"
241 <<
" Module: " << (
err.module.empty() ?
"unknown" :
err.module) <<
"\n"
242 <<
" Location: " << loc.file_name() <<
":" << loc.line() <<
":" << loc.column() <<
"\n"
243 <<
" Function: " << loc.function_name();
244 if (
err.details.has_value()) {
245 oss <<
"\n Details: " <<
err.details.value();
247 throw std::runtime_error(oss.str());
255 throw std::runtime_error(
"Called unwrap on error: " +
err.message);
263 throw std::runtime_error(
"Called unwrap on error: " +
err.message);
273 return default_value;
277 return unwrap_or(std::move(default_value));
294 using ReturnType =
decltype(func(std::declval<T>()));
306 auto and_then(F&& func)
const ->
decltype(func(std::declval<T>())) {
307 using ReturnType =
decltype(func(std::declval<T>()));
310 return func(
value_.value());
312 return ReturnType(
error_.value());
314 return ReturnType::uninitialized();
323 return func(
error_.value());
361#if KCENON_MODULE_HAS_SOURCE_LOCATION
364 std::ostringstream oss;
365 oss <<
"Called unwrap on None\n"
366 <<
" Location: " << loc.file_name() <<
":" << loc.line() <<
":" << loc.column() <<
"\n"
367 <<
" Function: " << loc.function_name();
368 throw std::runtime_error(oss.str());
375 throw std::runtime_error(
"Called unwrap on None");
382 return value_.value_or(default_value);
387 using ReturnType =
decltype(func(std::declval<T>()));
403inline Optional<T>
Some(T value) {
404 return Optional<T>(std::move(value));
411inline Optional<T>
None() {
412 return Optional<T>(std::nullopt);
443inline VoidResult err(
int code,
const std::string& message,
const std::string& module =
"") {
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="")
Result & operator=(Result &&)=default
static Result< T > uninitialized()
T value_or(T default_value) const
Result(Result &&)=default
auto map(F &&func) const -> Result< decltype(func(std::declval< T >()))>
T value_type
Type alias for the contained value type (for concept compatibility)
static Result< T > err(const error_info &error)
static Result< T > err(error_info &&error)
Result(error_info &&error)
static Result< T > ok(U &&value)
Result< T > or_else(F &&func) const
auto and_then(F &&func) const -> decltype(func(std::declval< T >()))
std::optional< error_info > error_
const T & value() const
Get value reference (const)
Result(const error_info &error)
std::optional< T > value_
Result & operator=(const Result &)=default
const T & unwrap() const
Get value from result (throws if error)
T unwrap_or(T default_value) const
Result< std::monostate > VoidResult
Specialized Result for void operations.
VoidResult err(const error_info &error)
Factory function to create error VoidResult.
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.
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.
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.
constexpr source_location(const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE()) noexcept
static constexpr source_location current(const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE()) noexcept
constexpr int line() const noexcept
constexpr int column() const noexcept
constexpr const char * file_name() const noexcept
constexpr const char * function_name() const noexcept