Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::Result< T > Class Template Referenceexport

Result type for error handling with member function support. More...

#include <core.h>

Collaboration diagram for kcenon::common::Result< T >:
Collaboration graph

Public Types

using value_type = T
 Type alias for the contained value type (for concept compatibility)
 
using error_type = error_info
 Type alias for the error type.
 

Public Member Functions

 Result (const T &value)
 
 Result (T &&value)
 
 Result (const error_info &error)
 
 Result (error_info &&error)
 
 Result (const typed_error_code &ec)
 Construct from category-based typed_error_code.
 
 Result ()=delete
 Default constructor is deleted to enforce explicit initialization.
 
 Result (const Result &)=default
 
 Result (Result &&)=default
 
Resultoperator= (const Result &)=default
 
Resultoperator= (Result &&)=default
 
bool is_ok () const
 Check if result contains a successful value.
 
bool is_err () const
 Check if result contains an error.
 
const T & unwrap () const
 Get value from result (throws if error)
 
T & unwrap ()
 Get mutable value from result (throws if error)
 
unwrap_or (T default_value) const
 Get value or return default.
 
value_or (T default_value) const
 Get value or return default (C++23 std::expected compatible)
 
const T & value () const
 Get value reference (const)
 
T & value ()
 Get value reference (mutable)
 
const error_infoerror () const
 Get error reference.
 
template<typename F >
auto map (F &&func) const -> Result< decltype(func(std::declval< T >()))>
 Map a function over a successful result.
 
template<typename F >
auto and_then (F &&func) const -> decltype(func(std::declval< T >()))
 Map a function that returns a Result (flatMap/bind)
 
template<typename F >
Result< T > or_else (F &&func) const
 Provide alternative value if error.
 
 Result (const T &value)
 
 Result (T &&value)
 
 Result (const error_info &error)
 
 Result (error_info &&error)
 
 Result ()=delete
 
 Result (const Result &)=default
 
 Result (Result &&)=default
 
Resultoperator= (const Result &)=default
 
Resultoperator= (Result &&)=default
 
bool is_ok () const
 
bool is_err () const
 
const T & unwrap () const
 
T & unwrap ()
 
unwrap_or (T default_value) const
 
value_or (T default_value) const
 
const T & value () const
 
T & value ()
 
const error_infoerror () const
 
template<typename F >
auto map (F &&func) const -> Result< decltype(func(std::declval< T >()))>
 
template<typename F >
auto and_then (F &&func) const -> decltype(func(std::declval< T >()))
 
template<typename F >
Result< T > or_else (F &&func) const
 

Static Public Member Functions

template<typename U = T>
static Result< T > ok (U &&value)
 Create a successful result with value (static factory)
 
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)
 
static Result< T > err (int code, const std::string &message, const std::string &module="")
 Create an error result with code and message (static factory)
 
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)
 
template<typename U = T>
static Result< T > ok (U &&value)
 
static Result< T > err (const error_info &error)
 
static Result< T > err (error_info &&error)
 
static Result< T > err (int code, const std::string &message, const std::string &module="")
 
static Result< T > uninitialized ()
 

Private Attributes

std::optional< T > value_
 
std::optional< error_infoerror_
 

Detailed Description

template<typename T>
class kcenon::common::Result< T >

Result type for error handling with member function support.

A Result<T> can be in one of two states:

  1. Ok - Contains a valid value of type T
  2. Error - Contains an error_info describing the failure

This provides a type-safe way to handle errors without exceptions.

IMPORTANT: Default-constructed Results are deleted to enforce explicit initialization. For explicit construction, use factory methods Result<T>::ok() and Result<T>::err().

Thread Safety Note:

  • Based on std::optional, which is NOT thread-safe for concurrent access.
  • Safe to pass by value or const reference across threads.
  • Concurrent reads of the same Result are safe if guaranteed no writes.
  • For shared mutable access, wrap in std::mutex or similar.

A Result<T> can be in one of two states:

  1. Ok - Contains a valid value of type T
  2. Error - Contains an error_info describing the failure
Template Parameters
TThe type of the successful value
Examples
concepts_showcase_example.cpp, executor_example.cpp, multi_system_app/main.cpp, result_example.cpp, and unwrap_demo.cpp.

Definition at line 165 of file core.cppm.

Member Typedef Documentation

◆ error_type

template<typename T >
typedef error_info kcenon::common::Result< T >::error_type = error_info
export

Type alias for the error type.

Definition at line 172 of file core.h.

◆ value_type

template<typename T >
typedef T kcenon::common::Result< T >::value_type = T
export

Type alias for the contained value type (for concept compatibility)

Definition at line 169 of file core.h.

Constructor & Destructor Documentation

◆ Result() [1/15]

template<typename T >
kcenon::common::Result< T >::Result ( const T & value)
inline

Definition at line 182 of file core.h.

182: value_(value), error_(std::nullopt) {}
std::optional< error_info > error_
Definition core.h:178
const T & value() const
Get value reference (const)
Definition core.h:391
std::optional< T > value_
Definition core.h:177

◆ Result() [2/15]

template<typename T >
kcenon::common::Result< T >::Result ( T && value)
inline

Definition at line 183 of file core.h.

183: value_(std::move(value)), error_(std::nullopt) {}

◆ Result() [3/15]

template<typename T >
kcenon::common::Result< T >::Result ( const error_info & error)
inline

Definition at line 184 of file core.h.

184: value_(std::nullopt), error_(error) {}
const error_info & error() const
Get error reference.
Definition core.h:405

◆ Result() [4/15]

template<typename T >
kcenon::common::Result< T >::Result ( error_info && error)
inline

Definition at line 185 of file core.h.

185: value_(std::nullopt), error_(std::move(error)) {}

◆ Result() [5/15]

template<typename T >
kcenon::common::Result< T >::Result ( const typed_error_code & ec)
inline

Construct from category-based typed_error_code.

Enables seamless integration with the decentralized error category system. The typed_error_code is converted to error_info internally.

Parameters
ecCategory-based typed error code

Definition at line 195 of file core.h.

195: value_(std::nullopt), error_(error_info(ec)) {}

◆ Result() [6/15]

template<typename T >
kcenon::common::Result< T >::Result ( )
delete

Default constructor is deleted to enforce explicit initialization.

This forces developers to explicitly use factory methods.

Migration: Use Result<T>::ok() for successful results or Result<T>::err() for error results. For cases requiring a default error state, use Result<T>::uninitialized() factory method.

Referenced by kcenon::common::Result< T >::err(), kcenon::common::Result< T >::err(), kcenon::common::Result< T >::err(), kcenon::common::Result< T >::err(), kcenon::common::Result< T >::map(), kcenon::common::Result< T >::ok(), and kcenon::common::Result< T >::uninitialized().

Here is the caller graph for this function:

◆ Result() [7/15]

template<typename T >
kcenon::common::Result< T >::Result ( const Result< T > & )
default

◆ Result() [8/15]

template<typename T >
kcenon::common::Result< T >::Result ( Result< T > && )
default

◆ Result() [9/15]

template<typename T >
kcenon::common::Result< T >::Result ( const T & value)
inlineexport

Definition at line 175 of file core.cppm.

175: value_(value), error_(std::nullopt) {}

◆ Result() [10/15]

template<typename T >
kcenon::common::Result< T >::Result ( T && value)
inlineexport

Definition at line 176 of file core.cppm.

176: value_(std::move(value)), error_(std::nullopt) {}

◆ Result() [11/15]

template<typename T >
kcenon::common::Result< T >::Result ( const error_info & error)
inlineexport

Definition at line 177 of file core.cppm.

177: value_(std::nullopt), error_(error) {}

◆ Result() [12/15]

template<typename T >
kcenon::common::Result< T >::Result ( error_info && error)
inlineexport

Definition at line 178 of file core.cppm.

178: value_(std::nullopt), error_(std::move(error)) {}

◆ Result() [13/15]

template<typename T >
kcenon::common::Result< T >::Result ( )
exportdelete

◆ Result() [14/15]

template<typename T >
kcenon::common::Result< T >::Result ( const Result< T > & )
exportdefault

◆ Result() [15/15]

template<typename T >
kcenon::common::Result< T >::Result ( Result< T > && )
exportdefault

Member Function Documentation

◆ and_then() [1/2]

template<typename T >
template<typename F >
auto kcenon::common::Result< T >::and_then ( F && func) const -> decltype(func(std::declval<T>()))
inline

Map a function that returns a Result (flatMap/bind)

Definition at line 430 of file core.h.

430 {
431 using ReturnType = decltype(func(std::declval<T>()));
432
433 if (is_ok()) {
434 return func(value_.value());
435 } else if (is_err()) {
436 return ReturnType(error_.value());
437 } else {
438 // Uninitialized state - use uninitialized factory
439 return ReturnType::uninitialized();
440 }
441 }
bool is_err() const
Check if result contains an error.
Definition core.h:297
bool is_ok() const
Check if result contains a successful value.
Definition core.h:290

References kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), kcenon::common::Result< T >::is_ok(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ and_then() [2/2]

template<typename T >
template<typename F >
auto kcenon::common::Result< T >::and_then ( F && func) const -> decltype(func(std::declval<T>()))
inlineexport

Definition at line 306 of file core.cppm.

306 {
307 using ReturnType = decltype(func(std::declval<T>()));
308
309 if (is_ok()) {
310 return func(value_.value());
311 } else if (is_err()) {
312 return ReturnType(error_.value());
313 } else {
314 return ReturnType::uninitialized();
315 }
316 }

References kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), kcenon::common::Result< T >::is_ok(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ err() [1/7]

template<typename T >
static Result< T > kcenon::common::Result< T >::err ( const error_info & error)
inlinestatic

Create an error result from error_info (static factory)

Parameters
errorThe error information
Returns
Result<T> containing the error

Definition at line 232 of file core.h.

232 {
233 return Result<T>(error);
234 }
Result()=delete
Default constructor is deleted to enforce explicit initialization.

References kcenon::common::Result< T >::error(), and kcenon::common::Result< T >::Result().

Referenced by kcenon::common::enum_from_string(), kcenon::common::Result< T >::unwrap(), and kcenon::common::Result< T >::unwrap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ err() [2/7]

template<typename T >
static Result< T > kcenon::common::Result< T >::err ( const error_info & error)
inlinestaticexport

Definition at line 193 of file core.cppm.

193 {
194 return Result<T>(error);
195 }

References kcenon::common::Result< T >::error(), and kcenon::common::Result< T >::Result().

Here is the call graph for this function:

◆ err() [3/7]

template<typename T >
static Result< T > kcenon::common::Result< T >::err ( const typed_error_code & ec)
inlinestatic

Create an error result from category-based typed_error_code (static factory)

Enables seamless integration with the decentralized error category system.

Parameters
ecCategory-based typed error code
Returns
Result<T> containing the error

Example usage:

);
Result type for error handling with member function support.
Definition core.cppm:165
static Result< T > err(const error_info &error)
Create an error result from error_info (static factory)
Definition core.h:232
typed_error_code make_typed_error_code(common_error_category::codes code) noexcept
Creates a typed_error_code from common_error_category::codes enum.

Definition at line 271 of file core.h.

271 {
272 return Result<T>(ec);
273 }

References kcenon::common::Result< T >::Result().

Here is the call graph for this function:

◆ err() [4/7]

template<typename T >
static Result< T > kcenon::common::Result< T >::err ( error_info && error)
inlinestatic

Create an error result from error_info (static factory, move)

Parameters
errorThe error information
Returns
Result<T> containing the error

Definition at line 241 of file core.h.

241 {
242 return Result<T>(std::move(error));
243 }

References kcenon::common::Result< T >::error(), and kcenon::common::Result< T >::Result().

Here is the call graph for this function:

◆ err() [5/7]

template<typename T >
static Result< T > kcenon::common::Result< T >::err ( error_info && error)
inlinestaticexport

Definition at line 197 of file core.cppm.

197 {
198 return Result<T>(std::move(error));
199 }

References kcenon::common::Result< T >::error(), and kcenon::common::Result< T >::Result().

Here is the call graph for this function:

◆ err() [6/7]

template<typename T >
static Result< T > kcenon::common::Result< T >::err ( int code,
const std::string & message,
const std::string & module = "" )
inlinestatic

Create an error result with code and message (static factory)

Parameters
codeError code
messageError message
moduleOptional module name
Returns
Result<T> containing the error

Definition at line 252 of file core.h.

252 {
253 return Result<T>(error_info{code, message, module});
254 }

References kcenon::common::Result< T >::Result().

Here is the call graph for this function:

◆ err() [7/7]

template<typename T >
static Result< T > kcenon::common::Result< T >::err ( int code,
const std::string & message,
const std::string & module = "" )
inlinestaticexport

Definition at line 201 of file core.cppm.

201 {
202 return Result<T>(error_info{code, message, module});
203 }

References kcenon::common::Result< T >::Result().

Here is the call graph for this function:

◆ error() [1/2]

template<typename T >
const error_info & kcenon::common::Result< T >::error ( ) const
inline

Get error reference.

Examples
executor_example.cpp, and result_example.cpp.

Definition at line 405 of file core.h.

405 {
406 return error_.value();
407 }

References kcenon::common::Result< T >::error_.

Referenced by kcenon::common::Result< T >::err(), kcenon::common::Result< T >::err(), mock_executor::execute(), mock_executor::execute_delayed(), and main().

Here is the caller graph for this function:

◆ error() [2/2]

template<typename T >
const error_info & kcenon::common::Result< T >::error ( ) const
inlineexport

Definition at line 288 of file core.cppm.

288 {
289 return error_.value();
290 }

References kcenon::common::Result< T >::error_.

◆ is_err() [1/2]

template<typename T >
bool kcenon::common::Result< T >::is_err ( ) const
inline

Check if result contains an error.

Definition at line 297 of file core.h.

297 {
298 return error_.has_value();
299 }

References kcenon::common::Result< T >::error_.

Referenced by kcenon::common::Result< T >::and_then(), kcenon::common::Result< T >::map(), kcenon::common::Result< T >::or_else(), kcenon::common::Result< T >::unwrap(), and kcenon::common::Result< T >::unwrap().

Here is the caller graph for this function:

◆ is_err() [2/2]

template<typename T >
bool kcenon::common::Result< T >::is_err ( ) const
inlineexport

Definition at line 213 of file core.cppm.

213 {
214 return error_.has_value();
215 }

References kcenon::common::Result< T >::error_.

◆ is_ok() [1/2]

template<typename T >
bool kcenon::common::Result< T >::is_ok ( ) const
inline

Check if result contains a successful value.

Definition at line 290 of file core.h.

290 {
291 return value_.has_value();
292 }

References kcenon::common::Result< T >::value_.

Referenced by kcenon::common::Result< T >::and_then(), kcenon::common::Result< T >::map(), kcenon::common::Result< T >::or_else(), and kcenon::common::Result< T >::unwrap_or().

Here is the caller graph for this function:

◆ is_ok() [2/2]

template<typename T >
bool kcenon::common::Result< T >::is_ok ( ) const
inlineexport

Definition at line 209 of file core.cppm.

209 {
210 return value_.has_value();
211 }

References kcenon::common::Result< T >::value_.

◆ map() [1/2]

template<typename T >
template<typename F >
auto kcenon::common::Result< T >::map ( F && func) const -> Result<decltype(func(std::declval<T>()))>
inline

Map a function over a successful result.

Definition at line 413 of file core.h.

413 {
414 using ReturnType = decltype(func(std::declval<T>()));
415
416 if (is_ok()) {
417 return Result<ReturnType>(func(value_.value()));
418 } else if (is_err()) {
419 return Result<ReturnType>(error_.value());
420 } else {
421 // Uninitialized state - use uninitialized factory
423 }
424 }
static Result< T > uninitialized()
Create an explicitly uninitialized result (use with caution)
Definition core.h:283

References kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), kcenon::common::Result< T >::is_ok(), kcenon::common::Result< T >::Result(), kcenon::common::Result< T >::uninitialized(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ map() [2/2]

template<typename T >
template<typename F >
auto kcenon::common::Result< T >::map ( F && func) const -> Result<decltype(func(std::declval<T>()))>
inlineexport

Definition at line 293 of file core.cppm.

293 {
294 using ReturnType = decltype(func(std::declval<T>()));
295
296 if (is_ok()) {
297 return Result<ReturnType>(func(value_.value()));
298 } else if (is_err()) {
299 return Result<ReturnType>(error_.value());
300 } else {
302 }
303 }

References kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), kcenon::common::Result< T >::is_ok(), kcenon::common::Result< T >::Result(), kcenon::common::Result< T >::uninitialized(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ ok() [1/2]

template<typename T >
template<typename U = T>
static Result< T > kcenon::common::Result< T >::ok ( U && value)
inlinestatic

Create a successful result with value (static factory)

Parameters
valueThe value to wrap in Result (forwarded)
Returns
Result<T> containing the value

Uses perfect forwarding to avoid ambiguity and support both lvalues and rvalues efficiently.

Definition at line 223 of file core.h.

223 {
224 return Result<T>(std::forward<U>(value));
225 }

References kcenon::common::Result< T >::Result(), and kcenon::common::Result< T >::value().

Referenced by kcenon::common::config::cli_config_parser::apply_override(), kcenon::common::di::service_container::check_already_registered(), kcenon::common::di::service_container::check_frozen_for_registration(), kcenon::common::di::service_container::clear(), kcenon::common::di::service_scope::clear(), kcenon::common::config::config_watcher::do_reload(), kcenon::common::enum_from_string(), kcenon::common::interfaces::NullLogger::flush(), kcenon::common::bootstrap::SystemBootstrapper::initialize(), kcenon::common::di::unified_bootstrapper::initialize(), kcenon::common::config::config_loader::load_from_env(), kcenon::common::config::config_loader::load_from_string(), kcenon::common::config::cli_config_parser::load_with_cli_overrides(), kcenon::common::interfaces::NullLogger::log(), kcenon::common::interfaces::NullLogger::log(), kcenon::common::interfaces::NullLogger::log(), kcenon::common::logging::log(), kcenon::common::logging::log(), kcenon::common::logging::log(), kcenon::common::config::config_loader::merge_env_overrides(), kcenon::common::config::cli_config_parser::parse(), kcenon::common::di::unified_bootstrapper::register_core_services(), kcenon::common::interfaces::GlobalLoggerRegistry::register_factory(), kcenon::common::di::service_container::register_factory_internal(), kcenon::common::di::service_container::register_instance_internal(), kcenon::common::interfaces::GlobalLoggerRegistry::register_logger(), kcenon::common::bootstrap::SystemBootstrapper::register_loggers(), kcenon::common::di::unified_bootstrapper::register_module(), kcenon::common::di::unified_bootstrapper::register_optional_services(), kcenon::common::di::unified_bootstrapper::register_shutdown_hook(), kcenon::common::config::config_watcher::rollback(), kcenon::common::interfaces::GlobalLoggerRegistry::set_default_factory(), kcenon::common::interfaces::GlobalLoggerRegistry::set_default_logger(), kcenon::common::interfaces::NullLogger::set_level(), kcenon::common::di::unified_bootstrapper::shutdown(), kcenon::common::config::config_watcher::start(), kcenon::common::di::service_container::unregister_internal(), kcenon::common::interfaces::GlobalLoggerRegistry::unregister_logger(), kcenon::common::di::unified_bootstrapper::unregister_module(), kcenon::common::di::unified_bootstrapper::unregister_shutdown_hook(), and kcenon::common::config::config_loader::validate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ok() [2/2]

template<typename T >
template<typename U = T>
static Result< T > kcenon::common::Result< T >::ok ( U && value)
inlinestaticexport

Definition at line 189 of file core.cppm.

189 {
190 return Result<T>(std::forward<U>(value));
191 }

References kcenon::common::Result< T >::Result(), and kcenon::common::Result< T >::value().

Here is the call graph for this function:

◆ operator=() [1/4]

template<typename T >
Result & kcenon::common::Result< T >::operator= ( const Result< T > & )
default

◆ operator=() [2/4]

template<typename T >
Result & kcenon::common::Result< T >::operator= ( const Result< T > & )
exportdefault

◆ operator=() [3/4]

template<typename T >
Result & kcenon::common::Result< T >::operator= ( Result< T > && )
default

◆ operator=() [4/4]

template<typename T >
Result & kcenon::common::Result< T >::operator= ( Result< T > && )
exportdefault

◆ or_else() [1/2]

template<typename T >
template<typename F >
Result< T > kcenon::common::Result< T >::or_else ( F && func) const
inline

Provide alternative value if error.

Definition at line 447 of file core.h.

447 {
448 if (is_ok()) {
449 return *this;
450 } else if (is_err()) {
451 return func(error_.value());
452 } else {
453 // Uninitialized state
454 return *this;
455 }
456 }

References kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), and kcenon::common::Result< T >::is_ok().

Here is the call graph for this function:

◆ or_else() [2/2]

template<typename T >
template<typename F >
Result< T > kcenon::common::Result< T >::or_else ( F && func) const
inlineexport

Definition at line 319 of file core.cppm.

319 {
320 if (is_ok()) {
321 return *this;
322 } else if (is_err()) {
323 return func(error_.value());
324 } else {
325 return *this;
326 }
327 }

References kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), and kcenon::common::Result< T >::is_ok().

Here is the call graph for this function:

◆ uninitialized() [1/2]

template<typename T >
static Result< T > kcenon::common::Result< T >::uninitialized ( )
inlinestatic

Create an explicitly uninitialized result (use with caution)

Returns
Result<T> containing an error state indicating uninitialized

This factory method is provided for cases where an uninitialized state is explicitly required (e.g., delayed initialization, placeholder values). Use sparingly and prefer explicit initialization with ok() or err().

Definition at line 283 of file core.h.

283 {
284 return Result<T>(error_info{-6, "Result not initialized", "common::Result"});
285 }

References kcenon::common::Result< T >::Result().

Referenced by kcenon::common::Result< T >::map().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uninitialized() [2/2]

template<typename T >
static Result< T > kcenon::common::Result< T >::uninitialized ( )
inlinestaticexport

Definition at line 205 of file core.cppm.

205 {
206 return Result<T>(error_info{-6, "Result not initialized", "common::Result"});
207 }

References kcenon::common::Result< T >::Result().

Here is the call graph for this function:

◆ unwrap() [1/4]

template<typename T >
T & kcenon::common::Result< T >::unwrap ( )
inline

Get mutable value from result (throws if error)

Exceptions
std::runtime_errorif result contains error
Note
When source_location is available, error messages include file/line info

Definition at line 360 of file core.h.

360 {
361 if (is_err()) {
362 const auto& err = error_.value();
363 throw std::runtime_error("Called unwrap on error: " + err.message);
364 }
365 return value_.value();
366 }

References kcenon::common::Result< T >::err(), kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ unwrap() [2/4]

template<typename T >
T & kcenon::common::Result< T >::unwrap ( )
inlineexport

Definition at line 260 of file core.cppm.

260 {
261 if (is_err()) {
262 const auto& err = error_.value();
263 throw std::runtime_error("Called unwrap on error: " + err.message);
264 }
265 return value_.value();
266 }

References kcenon::common::Result< T >::err(), kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ unwrap() [3/4]

template<typename T >
const T & kcenon::common::Result< T >::unwrap ( ) const
inline

Get value from result (throws if error)

Exceptions
std::runtime_errorif result contains error
Note
When source_location is available, error messages include file/line info

Definition at line 326 of file core.h.

326 {
327 if (is_err()) {
328 const auto& err = error_.value();
329 throw std::runtime_error("Called unwrap on error: " + err.message);
330 }
331 return value_.value();
332 }

References kcenon::common::Result< T >::err(), kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ unwrap() [4/4]

template<typename T >
const T & kcenon::common::Result< T >::unwrap ( ) const
inlineexport

Definition at line 252 of file core.cppm.

252 {
253 if (is_err()) {
254 const auto& err = error_.value();
255 throw std::runtime_error("Called unwrap on error: " + err.message);
256 }
257 return value_.value();
258 }

References kcenon::common::Result< T >::err(), kcenon::common::Result< T >::error_, kcenon::common::Result< T >::is_err(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ unwrap_or() [1/2]

template<typename T >
T kcenon::common::Result< T >::unwrap_or ( T default_value) const
inline

Get value or return default.

Examples
concepts_showcase_example.cpp.

Definition at line 372 of file core.h.

372 {
373 if (is_ok()) {
374 return value_.value();
375 }
376 return default_value;
377 }

References kcenon::common::Result< T >::is_ok(), and kcenon::common::Result< T >::value_.

Referenced by safe_unwrap(), and kcenon::common::Result< T >::value_or().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unwrap_or() [2/2]

template<typename T >
T kcenon::common::Result< T >::unwrap_or ( T default_value) const
inlineexport

Definition at line 269 of file core.cppm.

269 {
270 if (is_ok()) {
271 return value_.value();
272 }
273 return default_value;
274 }

References kcenon::common::Result< T >::is_ok(), and kcenon::common::Result< T >::value_.

Here is the call graph for this function:

◆ value() [1/4]

template<typename T >
T & kcenon::common::Result< T >::value ( )
inline

Get value reference (mutable)

Definition at line 398 of file core.h.

398 {
399 return value_.value();
400 }

References kcenon::common::Result< T >::value_.

◆ value() [2/4]

template<typename T >
T & kcenon::common::Result< T >::value ( )
inlineexport

Definition at line 284 of file core.cppm.

284 {
285 return value_.value();
286 }

References kcenon::common::Result< T >::value_.

◆ value() [3/4]

template<typename T >
const T & kcenon::common::Result< T >::value ( ) const
inline

Get value reference (const)

Definition at line 391 of file core.h.

391 {
392 return value_.value();
393 }

References kcenon::common::Result< T >::value_.

Referenced by kcenon::common::Result< T >::ok().

Here is the caller graph for this function:

◆ value() [4/4]

template<typename T >
const T & kcenon::common::Result< T >::value ( ) const
inlineexport

Definition at line 280 of file core.cppm.

280 {
281 return value_.value();
282 }

References kcenon::common::Result< T >::value_.

◆ value_or() [1/2]

template<typename T >
T kcenon::common::Result< T >::value_or ( T default_value) const
inline

Get value or return default (C++23 std::expected compatible)

Alias for unwrap_or() that matches std::expected::value_or() API.

Definition at line 384 of file core.h.

384 {
385 return unwrap_or(std::move(default_value));
386 }
T unwrap_or(T default_value) const
Get value or return default.
Definition core.h:372

References kcenon::common::Result< T >::unwrap_or().

Referenced by kcenon::common::interfaces::health_monitor::~health_monitor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ value_or() [2/2]

template<typename T >
T kcenon::common::Result< T >::value_or ( T default_value) const
inlineexport

Definition at line 276 of file core.cppm.

276 {
277 return unwrap_or(std::move(default_value));
278 }

References kcenon::common::Result< T >::unwrap_or().

Here is the call graph for this function:

Member Data Documentation

◆ error_

◆ value_


The documentation for this class was generated from the following files: