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

Fluent builder for creating dag_job instances. More...

#include <dag_job_builder.h>

Collaboration diagram for kcenon::thread::dag_job_builder:
Collaboration graph

Public Member Functions

 dag_job_builder (const std::string &name="dag_job")
 Constructs a new builder with an optional job name.
 
 dag_job_builder (dag_job_builder &&) noexcept=default
 Move constructor.
 
auto operator= (dag_job_builder &&) noexcept -> dag_job_builder &=default
 Move assignment operator.
 
 dag_job_builder (const dag_job_builder &)=delete
 Copy constructor (deleted - use move semantics)
 
auto operator= (const dag_job_builder &) -> dag_job_builder &=delete
 Copy assignment operator (deleted - use move semantics)
 
 ~dag_job_builder ()=default
 Destructor.
 
auto work (std::function< common::VoidResult()> callable) -> dag_job_builder &
 Sets the work function to execute.
 
template<typename T >
auto work_with_result (std::function< common::Result< T >()> callable) -> dag_job_builder &
 Sets the work function with result.
 
template<typename T >
auto returns () -> dag_job_builder &
 Specifies the result type for the job.
 
auto depends_on (job_id dependency) -> dag_job_builder &
 Adds a single dependency.
 
auto depends_on (std::initializer_list< job_id > dependencies) -> dag_job_builder &
 Adds multiple dependencies from initializer list.
 
auto depends_on (const std::vector< job_id > &dependencies) -> dag_job_builder &
 Adds multiple dependencies from vector.
 
auto on_failure (std::function< common::VoidResult()> fallback) -> dag_job_builder &
 Sets the fallback function for failure recovery.
 
auto is_valid () const -> bool
 Validates the builder configuration.
 
auto get_validation_error () const -> std::optional< std::string >
 Gets the validation error message.
 
auto build () -> std::unique_ptr< dag_job >
 Builds and returns the configured dag_job.
 
auto reset () -> dag_job_builder &
 Resets the builder to its initial state.
 

Private Attributes

std::string name_
 
std::function< common::VoidResult()> work_func_
 
std::function< common::VoidResult(dag_job &)> work_with_result_func_
 
std::function< common::VoidResult()> fallback_func_
 
std::vector< job_iddependencies_
 
bool has_return_type_ {false}
 

Detailed Description

Fluent builder for creating dag_job instances.

The dag_job_builder provides a convenient way to construct dag_job objects with dependencies, work functions, and other properties using a fluent API.

Design Pattern

This class implements the Builder pattern with method chaining (fluent interface). Each setter method returns a reference to the builder, allowing calls to be chained.

Thread Safety

  • The builder itself is not thread-safe
  • The built dag_job follows dag_job's thread-safety guarantees

Usage Example

auto job = dag_job_builder("process_data")
.depends_on(fetch_job_id)
.work([]() -> common::VoidResult {
process_data();
return common::ok();
})
.on_failure([]() -> common::VoidResult {
log_failure();
return common::ok();
})
.build();
auto on_failure(std::function< common::VoidResult()> fallback) -> dag_job_builder &
Sets the fallback function for failure recovery.
auto build() -> std::unique_ptr< dag_job >
Builds and returns the configured dag_job.
dag_job_builder(const std::string &name="dag_job")
Constructs a new builder with an optional job name.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136

Definition at line 56 of file dag_job_builder.h.

Constructor & Destructor Documentation

◆ dag_job_builder() [1/3]

kcenon::thread::dag_job_builder::dag_job_builder ( const std::string & name = "dag_job")
explicit

Constructs a new builder with an optional job name.

Parameters
nameHuman-readable name for the job (default: "dag_job")

Definition at line 10 of file dag_job_builder.cpp.

11 : name_(name)
12{
13}

◆ dag_job_builder() [2/3]

kcenon::thread::dag_job_builder::dag_job_builder ( dag_job_builder && )
defaultnoexcept

Move constructor.

◆ dag_job_builder() [3/3]

kcenon::thread::dag_job_builder::dag_job_builder ( const dag_job_builder & )
delete

Copy constructor (deleted - use move semantics)

◆ ~dag_job_builder()

kcenon::thread::dag_job_builder::~dag_job_builder ( )
default

Destructor.

Member Function Documentation

◆ build()

auto kcenon::thread::dag_job_builder::build ( ) -> std::unique_ptr<dag_job>
nodiscard

Builds and returns the configured dag_job.

Returns
A unique_ptr to the built dag_job, or nullptr if validation fails

Validates the configuration before building. If validation fails, returns nullptr. Use is_valid() to check before building, or get_validation_error() to get the error message.

After calling build(), the builder is reset and can be reused.

Definition at line 74 of file dag_job_builder.cpp.

75{
76 // Validate configuration
77 if (!is_valid())
78 {
79 return nullptr;
80 }
81
82 auto job = std::make_unique<dag_job>(name_);
83
84 // Set dependencies
85 job->add_dependencies(dependencies_);
86
87 // Set work function
89 {
90 // Capture the work_with_result function and create a wrapper
91 auto result_func = std::move(work_with_result_func_);
92 job->set_work([j = job.get(), func = std::move(result_func)]() -> common::VoidResult {
93 return func(*j);
94 });
95 }
96 else if (work_func_)
97 {
98 job->set_work(std::move(work_func_));
99 }
100
101 // Set fallback function
102 if (fallback_func_)
103 {
104 job->set_fallback(std::move(fallback_func_));
105 }
106
107 // Reset builder for reuse (keep name)
108 reset();
109
110 return job;
111}
auto reset() -> dag_job_builder &
Resets the builder to its initial state.
std::function< common::VoidResult()> fallback_func_
std::vector< job_id > dependencies_
auto is_valid() const -> bool
Validates the builder configuration.
std::function< common::VoidResult(dag_job &)> work_with_result_func_
std::function< common::VoidResult()> work_func_

◆ depends_on() [1/3]

auto kcenon::thread::dag_job_builder::depends_on ( const std::vector< job_id > & dependencies) -> dag_job_builder&

Adds multiple dependencies from vector.

Parameters
dependenciesVector of job IDs to depend on
Returns
Reference to this builder for chaining

Definition at line 42 of file dag_job_builder.cpp.

43{
44 for (const auto& dep : dependencies)
45 {
46 if (dep != INVALID_JOB_ID)
47 {
48 dependencies_.push_back(dep);
49 }
50 }
51 return *this;
52}
constexpr job_id INVALID_JOB_ID
Invalid job ID constant.
Definition dag_job.h:38

References kcenon::thread::INVALID_JOB_ID.

◆ depends_on() [2/3]

auto kcenon::thread::dag_job_builder::depends_on ( job_id dependency) -> dag_job_builder&

Adds a single dependency.

Parameters
dependencyThe job ID to depend on
Returns
Reference to this builder for chaining

Definition at line 21 of file dag_job_builder.cpp.

22{
23 if (dependency != INVALID_JOB_ID)
24 {
25 dependencies_.push_back(dependency);
26 }
27 return *this;
28}

References kcenon::thread::INVALID_JOB_ID.

◆ depends_on() [3/3]

auto kcenon::thread::dag_job_builder::depends_on ( std::initializer_list< job_id > dependencies) -> dag_job_builder&

Adds multiple dependencies from initializer list.

Parameters
dependenciesList of job IDs to depend on
Returns
Reference to this builder for chaining

Definition at line 30 of file dag_job_builder.cpp.

31{
32 for (const auto& dep : dependencies)
33 {
34 if (dep != INVALID_JOB_ID)
35 {
36 dependencies_.push_back(dep);
37 }
38 }
39 return *this;
40}

References kcenon::thread::INVALID_JOB_ID.

◆ get_validation_error()

auto kcenon::thread::dag_job_builder::get_validation_error ( ) const -> std::optional<std::string>
nodiscard

Gets the validation error message.

Returns
Error message if invalid, std::nullopt if valid

Definition at line 65 of file dag_job_builder.cpp.

66{
68 {
69 return "No work function specified. Use work() or work_with_result() before build().";
70 }
71 return std::nullopt;
72}

References work_func_, and work_with_result_func_.

◆ is_valid()

auto kcenon::thread::dag_job_builder::is_valid ( ) const -> bool
nodiscard

Validates the builder configuration.

Returns
true if the configuration is valid

Checks that:

Definition at line 60 of file dag_job_builder.cpp.

61{
62 return work_func_ != nullptr || work_with_result_func_ != nullptr;
63}

References work_func_, and work_with_result_func_.

◆ on_failure()

auto kcenon::thread::dag_job_builder::on_failure ( std::function< common::VoidResult()> fallback) -> dag_job_builder&

Sets the fallback function for failure recovery.

Parameters
fallbackThe function to execute on failure
Returns
Reference to this builder for chaining

The fallback function is called when the main work function fails and the DAG scheduler is configured with dag_failure_policy::fallback.

Definition at line 54 of file dag_job_builder.cpp.

55{
56 fallback_func_ = std::move(fallback);
57 return *this;
58}
@ fallback
Execute fallback job if available.

References kcenon::thread::fallback.

◆ operator=() [1/2]

auto kcenon::thread::dag_job_builder::operator= ( const dag_job_builder & ) -> dag_job_builder &=delete
delete

Copy assignment operator (deleted - use move semantics)

◆ operator=() [2/2]

auto kcenon::thread::dag_job_builder::operator= ( dag_job_builder && ) -> dag_job_builder &=default
defaultnoexcept

Move assignment operator.

◆ reset()

auto kcenon::thread::dag_job_builder::reset ( ) -> dag_job_builder&

Resets the builder to its initial state.

Returns
Reference to this builder for chaining

Clears all configured settings (work function, dependencies, fallback) while preserving the job name. This allows the builder to be reused for creating multiple jobs.

Definition at line 113 of file dag_job_builder.cpp.

114{
115 work_func_ = nullptr;
116 work_with_result_func_ = nullptr;
117 fallback_func_ = nullptr;
118 dependencies_.clear();
119 has_return_type_ = false;
120 return *this;
121}

◆ returns()

template<typename T >
auto kcenon::thread::dag_job_builder::returns ( ) -> dag_job_builder&
inline

Specifies the result type for the job.

Template Parameters
TThe result type
Returns
Reference to this builder for chaining

This method is used in combination with work() when the work function will set the result manually via the job's set_result() method.

Note
This is a marker method that documents intent. The actual result type is determined at runtime when set_result() is called.

Usage Example

auto job = dag_job_builder("compute")
.returns<int>()
.work([&scheduler, dep_id]() -> common::VoidResult {
// Result type is specified by returns<int>()
return common::ok();
})
.build();
auto work(std::function< common::VoidResult()> callable) -> dag_job_builder &
Sets the work function to execute.

Definition at line 148 of file dag_job_builder.h.

149 {
150 has_return_type_ = true;
151 return *this;
152 }

References has_return_type_.

◆ work()

auto kcenon::thread::dag_job_builder::work ( std::function< common::VoidResult()> callable) -> dag_job_builder&

Sets the work function to execute.

Parameters
callableThe function to execute
Returns
Reference to this builder for chaining

The callable should return common::VoidResult:

  • common::ok() on success
  • common::VoidResult(error_info) on failure

Definition at line 15 of file dag_job_builder.cpp.

16{
17 work_func_ = std::move(callable);
18 return *this;
19}

◆ work_with_result()

template<typename T >
auto kcenon::thread::dag_job_builder::work_with_result ( std::function< common::Result< T >()> callable) -> dag_job_builder&
inline

Sets the work function with result.

Template Parameters
TThe result type
Parameters
callableThe function to execute
Returns
Reference to this builder for chaining

The result will be stored in the job and can be retrieved by dependent jobs.

Definition at line 111 of file dag_job_builder.h.

112 {
113 work_with_result_func_ = [func = std::move(callable)](dag_job& job) -> common::VoidResult {
114 auto result = func();
115 if (result.is_ok())
116 {
117 job.set_result(result.value());
118 return common::ok();
119 }
120 return common::VoidResult(result.error());
121 };
122 return *this;
123 }

References kcenon::thread::result< T >::is_ok(), kcenon::thread::result< T >::value(), and work_with_result_func_.

Here is the call graph for this function:

Member Data Documentation

◆ dependencies_

std::vector<job_id> kcenon::thread::dag_job_builder::dependencies_
private

Definition at line 228 of file dag_job_builder.h.

◆ fallback_func_

std::function<common::VoidResult()> kcenon::thread::dag_job_builder::fallback_func_
private

Definition at line 227 of file dag_job_builder.h.

◆ has_return_type_

bool kcenon::thread::dag_job_builder::has_return_type_ {false}
private

Definition at line 229 of file dag_job_builder.h.

229{false};

Referenced by returns().

◆ name_

std::string kcenon::thread::dag_job_builder::name_
private

Definition at line 224 of file dag_job_builder.h.

◆ work_func_

std::function<common::VoidResult()> kcenon::thread::dag_job_builder::work_func_
private

Definition at line 225 of file dag_job_builder.h.

Referenced by get_validation_error(), and is_valid().

◆ work_with_result_func_

std::function<common::VoidResult(dag_job&)> kcenon::thread::dag_job_builder::work_with_result_func_
private

Definition at line 226 of file dag_job_builder.h.

Referenced by get_validation_error(), is_valid(), and work_with_result().


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