Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
callback_job.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
6
24namespace kcenon::thread
25{
47 callback_job::callback_job(const std::function<std::optional<std::string>(void)>& callback,
48 const std::string& name)
49 : job(name), callback_([callback](void) -> common::VoidResult {
50 // Call the original callback and convert error format
51 auto result = callback();
52 if (result.has_value()) {
53 // Error occurred: convert string to error object
54 return common::error_info{static_cast<int>(error_code::job_execution_failed), result.value(), "thread_system"};
55 }
56 // Success: return empty result
57 return common::ok();
58 }), data_callback_(nullptr)
59 {
60 }
61
79 callback_job::callback_job(
80 const std::function<common::VoidResult(void)>& callback,
81 const std::string& name)
82 : job(name), callback_(callback), data_callback_(nullptr)
83 {
84 }
85
109 const std::function<std::optional<std::string>(const std::vector<uint8_t>&)>& data_callback,
110 const std::vector<uint8_t>& data, const std::string& name)
111 : job(data, name), callback_(nullptr), data_callback_([data_callback](const std::vector<uint8_t>& callback_data) -> common::VoidResult {
112 // Call the original data callback with provided data
113 auto result = data_callback(callback_data);
114 if (result.has_value()) {
115 // Error occurred: convert string to error object
116 return common::error_info{static_cast<int>(error_code::job_execution_failed), result.value(), "thread_system"};
117 }
118 // Success: return empty result
119 return common::ok();
120 })
121 {
122 }
123
143 const std::function<common::VoidResult(const std::vector<uint8_t>&)>& data_callback,
144 const std::vector<uint8_t>& data, const std::string& name)
145 : job(data, name), callback_(nullptr), data_callback_(data_callback)
146 {
147 }
148
158
185 auto callback_job::do_work(void) -> common::VoidResult
186 {
187 // Priority 1: Data callback with stored binary data
188 if (data_callback_)
189 {
190 // Execute data-processing callback with job's stored data
191 return data_callback_(data_);
192 }
193 // Priority 2: Standard parameterless callback
194 else if (callback_)
195 {
196 // Execute standard callback function
197 return callback_();
198 }
199 // Priority 3: Fallback to base class implementation
200 else
201 {
202 // No callbacks set - delegate to base job class
203 // This typically returns a "not implemented" error
204 return job::do_work();
205 }
206 }
207} // namespace kcenon::thread
Specialized job class that encapsulates user-defined callbacks.
callback_job(const std::function< std::optional< std::string >(void)> &callback, const std::string &name="callback_job")
Constructs a new callback_job instance with a parameterless callback.
~callback_job(void) override
Virtual destructor for proper cleanup in derived classes.
auto do_work(void) -> common::VoidResult override
Executes the appropriate callback function to perform the job's work.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
virtual auto do_work(void) -> common::VoidResult
The core task execution method to be overridden by derived classes.
Definition job.cpp:135
A template class representing either a value or an error.
bool has_value() const noexcept
Checks if the result contains a value.
@ callback
Call user callback for custom decision.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.