Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::result< void > Class Reference

Specialization of result for void. More...

#include <error_handling.h>

Collaboration diagram for kcenon::thread::result< void >:
Collaboration graph

Public Types

using value_type = void
 
using error_type = error
 

Public Member Functions

 result ()
 Constructs a successful void result.
 
 result (const result_void &r)
 Constructs a void result from a result_void.
 
 result (error err)
 Constructs a result with an error.
 
 operator bool () const noexcept
 Checks if the result is successful.
 
bool has_value () const noexcept
 Checks if the result is successful.
 
bool is_ok () const noexcept
 Checks if the result is successful.
 
bool is_error () const noexcept
 Checks if the result contains an error.
 
errorget_error () &
 Gets the error.
 
const errorget_error () const &
 Gets the error.
 
error && get_error () &&
 Gets the error.
 
void value_or_throw () const
 Throws an exception if the result contains an error.
 
template<typename Fn >
auto map (Fn &&fn) const -> result< std::invoke_result_t< Fn > >
 Maps the result to another type using a function.
 
template<typename Fn >
auto and_then (Fn &&fn) const -> std::invoke_result_t< Fn >
 Maps the result to another type using a function that returns a result.
 

Private Attributes

bool success_ = false
 
error error_ {error_code::success, ""}
 

Detailed Description

Specialization of result for void.

Definition at line 466 of file error_handling.h.

Member Typedef Documentation

◆ error_type

using kcenon::thread::result< void >::error_type = error

Definition at line 469 of file error_handling.h.

◆ value_type

using kcenon::thread::result< void >::value_type = void

Definition at line 468 of file error_handling.h.

Constructor & Destructor Documentation

◆ result() [1/3]

kcenon::thread::result< void >::result ( )
inline

Constructs a successful void result.

Definition at line 474 of file error_handling.h.

◆ result() [2/3]

kcenon::thread::result< void >::result ( const result_void & r)
inline

Constructs a void result from a result_void.

Definition at line 479 of file error_handling.h.

479: success_(!r.has_error()), error_(r.has_error() ? r.get_error() : error{error_code::success, ""}) {}
@ error
Error events that might still allow continuation.

◆ result() [3/3]

kcenon::thread::result< void >::result ( error err)
inline

Constructs a result with an error.

Parameters
errThe error to store

Definition at line 485 of file error_handling.h.

485: success_(false), error_(std::move(err)) {}

Member Function Documentation

◆ and_then()

template<typename Fn >
auto kcenon::thread::result< void >::and_then ( Fn && fn) const -> std::invoke_result_t<Fn>
inlinenodiscard

Maps the result to another type using a function that returns a result.

Parameters
fnThe function to apply if successful
Returns
A new result with the mapped value or the original error

Definition at line 588 of file error_handling.h.

588 {
589 using ResultType = std::invoke_result_t<Fn>;
590 static_assert(std::is_same_v<typename ResultType::error_type, error>, "Function must return a result with the same error type");
591
592 if (success_) {
593 return std::invoke(std::forward<Fn>(fn));
594 }
595 return ResultType(error_);
596 }

References kcenon::thread::result< T >::error_.

◆ get_error() [1/3]

error & kcenon::thread::result< void >::get_error ( ) &
inlinenodiscard

Gets the error.

Returns
A reference to the contained error
Exceptions
std::runtime_errorif the result is successful

Definition at line 526 of file error_handling.h.

526 {
527 if (success_) {
528 throw std::runtime_error("Cannot get error from successful result");
529 }
530 return error_;
531 }

References kcenon::thread::result< T >::error_.

◆ get_error() [2/3]

error && kcenon::thread::result< void >::get_error ( ) &&
inlinenodiscard

Gets the error.

Returns
An rvalue reference to the contained error
Exceptions
std::runtime_errorif the result is successful

Definition at line 550 of file error_handling.h.

550 {
551 if (success_) {
552 throw std::runtime_error("Cannot get error from successful result");
553 }
554 return std::move(error_);
555 }

References kcenon::thread::result< T >::error_.

◆ get_error() [3/3]

const error & kcenon::thread::result< void >::get_error ( ) const &
inlinenodiscard

Gets the error.

Returns
A const reference to the contained error
Exceptions
std::runtime_errorif the result is successful

Definition at line 538 of file error_handling.h.

538 {
539 if (success_) {
540 throw std::runtime_error("Cannot get error from successful result");
541 }
542 return error_;
543 }

References kcenon::thread::result< T >::error_.

◆ has_value()

bool kcenon::thread::result< void >::has_value ( ) const
inlinenodiscardnoexcept

Checks if the result is successful.

Returns
true if the result is successful, false if it contains an error

Definition at line 499 of file error_handling.h.

499 {
500 return success_;
501 }

◆ is_error()

bool kcenon::thread::result< void >::is_error ( ) const
inlinenodiscardnoexcept

Checks if the result contains an error.

Returns
true if the result contains an error, false if successful
Note
Added for API compatibility with common::Result

Definition at line 517 of file error_handling.h.

517 {
518 return !success_;
519 }

◆ is_ok()

bool kcenon::thread::result< void >::is_ok ( ) const
inlinenodiscardnoexcept

Checks if the result is successful.

Returns
true if the result is successful, false if it contains an error
Note
Added for API compatibility with common::Result

Definition at line 508 of file error_handling.h.

508 {
509 return success_;
510 }

◆ map()

template<typename Fn >
auto kcenon::thread::result< void >::map ( Fn && fn) const -> result<std::invoke_result_t<Fn>>
inlinenodiscard

Maps the result to another type using a function.

Parameters
fnThe function to apply if successful
Returns
A new result with the mapped value or the original error

Definition at line 573 of file error_handling.h.

573 {
574 using ResultType = result<std::invoke_result_t<Fn>>;
575
576 if (success_) {
577 return ResultType(std::invoke(std::forward<Fn>(fn)));
578 }
579 return ResultType(error_);
580 }
result()
Constructs a successful void result.

References kcenon::thread::result< T >::error_.

◆ operator bool()

kcenon::thread::result< void >::operator bool ( ) const
inlineexplicitnodiscardnoexcept

Checks if the result is successful.

Returns
true if the result is successful, false if it contains an error

Definition at line 491 of file error_handling.h.

491 {
492 return success_;
493 }

◆ value_or_throw()

void kcenon::thread::result< void >::value_or_throw ( ) const
inline

Throws an exception if the result contains an error.

Exceptions
std::runtime_errorwith the error message if the result contains an error

Definition at line 561 of file error_handling.h.

561 {
562 if (!success_) {
563 throw std::runtime_error(error_.to_string());
564 }
565 }
std::string to_string() const
Converts the error to a string representation.

References kcenon::thread::result< T >::error_, and kcenon::thread::error::to_string().

Here is the call graph for this function:

Member Data Documentation

◆ error_

error kcenon::thread::result< void >::error_ {error_code::success, ""}
private

Definition at line 600 of file error_handling.h.

◆ success_

bool kcenon::thread::result< void >::success_ = false
private

Definition at line 599 of file error_handling.h.


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