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

A template class representing either a value or an error. More...

#include <error_handling.h>

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

Public Types

using value_type = T
 
using error_type = error
 

Public Member Functions

 result (T value)
 Constructs a result with a value.
 
 result (error err)
 Constructs a result with an error.
 
 operator bool () const noexcept
 Checks if the result contains a value.
 
bool has_value () const noexcept
 Checks if the result contains a value.
 
bool is_ok () const noexcept
 Checks if the result is successful.
 
bool is_error () const noexcept
 Checks if the result contains an error.
 
T & value () &
 Gets the value.
 
const T & value () const &
 Gets the value.
 
T && value () &&
 Gets the value.
 
errorget_error () &
 Gets the error.
 
const errorget_error () const &
 Gets the error.
 
error && get_error () &&
 Gets the error.
 
template<typename U >
value_or (U &&default_value) const &
 Gets the value or a default.
 
template<typename U >
value_or (U &&default_value) &&
 Gets the value or a default.
 
value_or_throw () const &
 Gets the value or throws an exception.
 
value_or_throw () &&
 Gets the value or throws an exception.
 
template<typename Fn >
auto map (Fn &&fn) const -> result< std::invoke_result_t< Fn, const T & > >
 Maps the result to another type using a function.
 
template<typename Fn >
auto and_then (Fn &&fn) const -> std::invoke_result_t< Fn, const T & >
 Maps the result to another type using a function that returns a result.
 

Private Attributes

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

Detailed Description

template<typename T>
class kcenon::thread::result< T >

A template class representing either a value or an error.

This class is similar to std::expected from C++23, but can be used with C++20. It holds either a value of type T or an error.

Examples
adaptive_queue_sample.cpp, composition_example.cpp, crash_protection/main.cpp, hazard_pointer_sample.cpp, minimal_thread_pool.cpp, mpmc_queue_sample.cpp, multi_process_monitoring_integration.cpp, queue_factory_sample.cpp, thread_pool_sample.cpp, and typed_job_queue_sample.cpp.

Definition at line 252 of file error_handling.h.

Member Typedef Documentation

◆ error_type

template<typename T >
using kcenon::thread::result< T >::error_type = error

Definition at line 255 of file error_handling.h.

◆ value_type

template<typename T >
using kcenon::thread::result< T >::value_type = T

Definition at line 254 of file error_handling.h.

Constructor & Destructor Documentation

◆ result() [1/2]

template<typename T >
kcenon::thread::result< T >::result ( T value)
inline

Constructs a result with a value.

Parameters
valueThe value to store

Definition at line 261 of file error_handling.h.

◆ result() [2/2]

template<typename T >
kcenon::thread::result< T >::result ( error err)
inline

Constructs a result with an error.

Parameters
errThe error to store

Definition at line 267 of file error_handling.h.

267: has_value_(false), error_(std::move(err)) {}

Member Function Documentation

◆ and_then()

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

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

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

Definition at line 446 of file error_handling.h.

446 {
447 using ResultType = std::invoke_result_t<Fn, const T&>;
448 static_assert(std::is_same_v<typename ResultType::error_type, error>, "Function must return a result with the same error type");
449
450 if (has_value_) {
451 return std::invoke(std::forward<Fn>(fn), value_);
452 }
453 return ResultType(error_);
454 }

References kcenon::thread::result< T >::error_, kcenon::thread::result< T >::has_value_, and kcenon::thread::result< T >::value_.

◆ get_error() [1/3]

template<typename T >
error & kcenon::thread::result< T >::get_error ( ) &
inlinenodiscard

Gets the error.

Returns
A reference to the contained error
Exceptions
std::runtime_errorif the result contains a value

Definition at line 344 of file error_handling.h.

344 {
345 if (has_value_) {
346 throw std::runtime_error("Cannot get error from successful result");
347 }
348 return error_;
349 }

References kcenon::thread::result< T >::error_, and kcenon::thread::result< T >::has_value_.

Referenced by kcenon::thread::typed_thread_pool_builder< job_type >::build(), kcenon::thread::result_to_optional_error(), and kcenon::thread::result_to_pair().

Here is the caller graph for this function:

◆ get_error() [2/3]

template<typename T >
error && kcenon::thread::result< T >::get_error ( ) &&
inlinenodiscard

Gets the error.

Returns
An rvalue reference to the contained error
Exceptions
std::runtime_errorif the result contains a value

Definition at line 368 of file error_handling.h.

368 {
369 if (has_value_) {
370 throw std::runtime_error("Cannot get error from successful result");
371 }
372 return std::move(error_);
373 }

References kcenon::thread::result< T >::error_, and kcenon::thread::result< T >::has_value_.

◆ get_error() [3/3]

template<typename T >
const error & kcenon::thread::result< T >::get_error ( ) const &
inlinenodiscard

Gets the error.

Returns
A const reference to the contained error
Exceptions
std::runtime_errorif the result contains a value

Definition at line 356 of file error_handling.h.

356 {
357 if (has_value_) {
358 throw std::runtime_error("Cannot get error from successful result");
359 }
360 return error_;
361 }

References kcenon::thread::result< T >::error_, and kcenon::thread::result< T >::has_value_.

◆ has_value()

template<typename T >
bool kcenon::thread::result< T >::has_value ( ) const
inlinenodiscardnoexcept

Checks if the result contains a value.

Returns
true if the result contains a value, false if it contains an error
Examples
mpmc_queue_sample.cpp.

Definition at line 281 of file error_handling.h.

281 {
282 return has_value_;
283 }

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

Referenced by basic_spsc_example(), kcenon::thread::aging_typed_job_queue_t< job_type >::dequeue(), kcenon::thread::aging_typed_job_queue_t< job_type >::dequeue(), mpmc_example(), performance_example(), and kcenon::thread::numa_work_stealer::steal_for().

Here is the caller graph for this function:

◆ is_error()

template<typename T >
bool kcenon::thread::result< T >::is_error ( ) const
inlinenodiscardnoexcept

Checks if the result contains an error.

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

Definition at line 299 of file error_handling.h.

299 {
300 return !has_value_;
301 }

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

◆ is_ok()

template<typename T >
bool kcenon::thread::result< T >::is_ok ( ) const
inlinenodiscardnoexcept

Checks if the result is successful.

Returns
true if the result contains a value, false if it contains an error
Note
Added for API compatibility with common::Result
Examples
adaptive_queue_sample.cpp, mpmc_queue_sample.cpp, queue_factory_sample.cpp, and typed_job_queue_sample.cpp.

Definition at line 290 of file error_handling.h.

290 {
291 return has_value_;
292 }

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

Referenced by adaptive_behavior_example(), kcenon::thread::backpressure_job_queue::apply_backpressure(), batch_operations_example(), kcenon::thread::adaptive_job_queue::dequeue(), kcenon::thread::aging_typed_job_queue_t< job_type >::dequeue(), different_policies_example(), kcenon::thread::protected_job::do_work(), kcenon::thread::adaptive_job_queue::enqueue(), kcenon::thread::backpressure_job_queue::enqueue_batch(), kcenon::thread::dag_scheduler::execute_job(), kcenon::thread::autoscaler::execute_scaling(), kcenon::thread::backpressure_job_queue::handle_adaptive_policy(), kcenon::thread::backpressure_job_queue::handle_block_policy(), kcenon::thread::backpressure_job_queue::handle_drop_oldest_policy(), mpmc_typed_queue_example(), kcenon::thread::dag_scheduler::on_job_failed(), optimal_selection(), performance_monitoring_example(), policy_comparison_example(), practical_use_cases(), kcenon::thread::dag_job::set_work_with_result(), kcenon::thread::adapters::thread_pool_executor_adapter::submit(), task_scheduling_example(), kcenon::thread::adaptive_job_queue::try_dequeue(), web_server_simulation(), and kcenon::thread::dag_job_builder::work_with_result().

Here is the caller graph for this function:

◆ map()

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

Maps the result to another type using a function.

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

Definition at line 431 of file error_handling.h.

431 {
433
434 if (has_value_) {
435 return ResultType(std::invoke(std::forward<Fn>(fn), value_));
436 }
437 return ResultType(error_);
438 }
result(T value)
Constructs a result with a value.

References kcenon::thread::result< T >::error_, kcenon::thread::result< T >::has_value_, and kcenon::thread::result< T >::value_.

◆ operator bool()

template<typename T >
kcenon::thread::result< T >::operator bool ( ) const
inlineexplicitnodiscardnoexcept

Checks if the result contains a value.

Returns
true if the result contains a value, false if it contains an error

Definition at line 273 of file error_handling.h.

273 {
274 return has_value_;
275 }

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

◆ value() [1/3]

◆ value() [2/3]

template<typename T >
T && kcenon::thread::result< T >::value ( ) &&
inlinenodiscard

Gets the value.

Returns
An rvalue reference to the contained value
Exceptions
std::runtime_errorif the result contains an error

Definition at line 332 of file error_handling.h.

332 {
333 if (!has_value_) {
334 throw std::runtime_error("Cannot access value of an error result");
335 }
336 return std::move(value_);
337 }

References kcenon::thread::result< T >::has_value_, and kcenon::thread::result< T >::value_.

◆ value() [3/3]

template<typename T >
const T & kcenon::thread::result< T >::value ( ) const &
inlinenodiscard

Gets the value.

Returns
A const reference to the contained value
Exceptions
std::runtime_errorif the result contains an error

Definition at line 320 of file error_handling.h.

320 {
321 if (!has_value_) {
322 throw std::runtime_error("Cannot access value of an error result");
323 }
324 return value_;
325 }

References kcenon::thread::result< T >::has_value_, and kcenon::thread::result< T >::value_.

◆ value_or() [1/2]

template<typename T >
template<typename U >
T kcenon::thread::result< T >::value_or ( U && default_value) &&
inlinenodiscard

Gets the value or a default.

Parameters
default_valueThe value to return if the result contains an error
Returns
The contained value or the default

Definition at line 394 of file error_handling.h.

394 {
395 if (has_value_) {
396 return std::move(value_);
397 }
398 return static_cast<T>(std::forward<U>(default_value));
399 }

References kcenon::thread::result< T >::has_value_, and kcenon::thread::result< T >::value_.

◆ value_or() [2/2]

template<typename T >
template<typename U >
T kcenon::thread::result< T >::value_or ( U && default_value) const &
inlinenodiscard

Gets the value or a default.

Parameters
default_valueThe value to return if the result contains an error
Returns
The contained value or the default

Definition at line 381 of file error_handling.h.

381 {
382 if (has_value_) {
383 return value_;
384 }
385 return static_cast<T>(std::forward<U>(default_value));
386 }

References kcenon::thread::result< T >::has_value_, and kcenon::thread::result< T >::value_.

◆ value_or_throw() [1/2]

template<typename T >
T kcenon::thread::result< T >::value_or_throw ( ) &&
inlinenodiscard

Gets the value or throws an exception.

Returns
The contained value
Exceptions
std::runtime_errorwith the error message if the result contains an error

Definition at line 418 of file error_handling.h.

418 {
419 if (has_value_) {
420 return std::move(value_);
421 }
422 throw std::runtime_error(error_.to_string());
423 }
std::string to_string() const
Converts the error to a string representation.

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

Here is the call graph for this function:

◆ value_or_throw() [2/2]

template<typename T >
T kcenon::thread::result< T >::value_or_throw ( ) const &
inlinenodiscard

Gets the value or throws an exception.

Returns
The contained value
Exceptions
std::runtime_errorwith the error message if the result contains an error

Definition at line 406 of file error_handling.h.

406 {
407 if (has_value_) {
408 return value_;
409 }
410 throw std::runtime_error(error_.to_string());
411 }

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

Here is the call graph for this function:

Member Data Documentation

◆ error_

◆ has_value_

◆ value_


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