Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
result_helpers.h File Reference

Type-safe helper functions replacing macros for Result<T> pattern. More...

#include "result.h"
#include <utility>
#include <type_traits>
Include dependency graph for result_helpers.h:

Go to the source code of this file.

Namespaces

namespace  kcenon
 
namespace  kcenon::common
 Core interfaces.
 
namespace  kcenon::common::helpers
 

Functions

template<typename T >
auto kcenon::common::helpers::return_if_error (const Result< T > &result) -> std::optional< error_info >
 Return error if Result is in error state (type-safe alternative to RETURN_IF_ERROR)
 
template<typename T >
auto kcenon::common::helpers::try_extract (Result< T > &&result) -> std::conditional_t< std::is_void_v< T >, Result< std::monostate >, Result< T > >
 Safely extract value from Result or return error.
 
auto kcenon::common::helpers::error_if (bool condition, const error_info &error) -> std::optional< error_info >
 Check condition and return error if false (type-safe alternative to RETURN_ERROR_IF)
 
auto kcenon::common::helpers::make_error (int code, const std::string &message, const std::string &module="") noexcept -> error_info
 Create error_info with code, message, and module (type-safe)
 
auto kcenon::common::helpers::make_error_with_details (int code, const std::string &message, const std::string &module, const std::string &details) noexcept -> error_info
 Create error_info with code, message, module, and details.
 
template<typename... Funcs>
auto kcenon::common::helpers::chain (Funcs &&... funcs)
 Chain multiple Result-returning operations.
 
template<typename Func >
auto kcenon::common::helpers::safe_execute (Func &&func, const std::string &module="") -> Result< std::invoke_result_t< Func > >
 Execute function and convert exceptions to Result.
 
template<typename T , typename ErrorHandler >
auto kcenon::common::helpers::unwrap_or_handle (Result< T > &&result, ErrorHandler &&error_handler) -> T
 Unwrap Result with custom error handler.
 
template<typename... Results>
auto kcenon::common::helpers::combine_results (Results &&... results) -> Result< std::tuple< typename std::decay_t< Results >::value_type... > >
 Combine multiple Results into a single Result containing a tuple.
 

Detailed Description

Type-safe helper functions replacing macros for Result<T> pattern.

This header provides template-based utilities that replace the macro-based helpers in result.h, offering better type safety, debuggability, and compile-time error checking.

Improvements over macros:

  • Full type safety with template deduction
  • Better error messages from compiler
  • Debugger-friendly (no macro expansion)
  • No name collision risks
  • Works with IDEs and code analysis tools

Definition in file result_helpers.h.