Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
executor_interface.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
14#pragma once
15
16#include <chrono>
17#include <functional>
18#include <future>
19#include <memory>
20#include <string>
21#include "../patterns/result.h"
22
23namespace kcenon::common {
24namespace interfaces {
25
32class IJob {
33public:
34 virtual ~IJob() = default;
35
40 virtual VoidResult execute() = 0;
41
46 virtual std::string get_name() const { return "unnamed_job"; }
47
52 virtual int get_priority() const { return 0; }
53};
54
75class IExecutor {
76public:
77 virtual ~IExecutor() = default;
78
79 // ===== Job-based execution =====
80
88 virtual Result<std::future<void>> execute(std::unique_ptr<IJob>&& job) = 0;
89
97 std::unique_ptr<IJob>&& job,
98 std::chrono::milliseconds delay) = 0;
99
100 // ===== Status and control =====
101
106 virtual size_t worker_count() const = 0;
107
112 virtual bool is_running() const = 0;
113
118 virtual size_t pending_tasks() const = 0;
119
124 virtual void shutdown(bool wait_for_completion = true) = 0;
125};
126
130using ExecutorFactory = std::function<std::shared_ptr<IExecutor>()>;
131
136class IExecutorProvider {
137public:
138 virtual ~IExecutorProvider() = default;
139
144 virtual std::shared_ptr<IExecutor> get_executor() = 0;
145
151 virtual std::shared_ptr<IExecutor> create_executor(size_t worker_count) = 0;
152};
153
154} // namespace interfaces
155} // namespace kcenon::common
Result type for error handling with member function support.
Definition core.cppm:165
Interface for modules that provide executor implementations.
virtual std::shared_ptr< IExecutor > create_executor(size_t worker_count)=0
Create a new executor with specific configuration.
virtual std::shared_ptr< IExecutor > get_executor()=0
Get the default executor instance.
virtual void shutdown(bool wait_for_completion=true)=0
Shutdown the executor gracefully.
virtual size_t worker_count() const =0
Get the number of worker threads.
virtual Result< std::future< void > > execute_delayed(std::unique_ptr< IJob > &&job, std::chrono::milliseconds delay)=0
Execute a job with delay.
virtual bool is_running() const =0
Check if the executor is running.
virtual Result< std::future< void > > execute(std::unique_ptr< IJob > &&job)=0
Execute a job with Result-based error handling.
virtual size_t pending_tasks() const =0
Get the number of pending tasks.
virtual VoidResult execute()=0
Execute the job.
virtual int get_priority() const
Get the priority of the job (higher = more important)
virtual std::string get_name() const
Get the name of the job (for logging/debugging)
std::function< std::shared_ptr< IExecutor >()> ExecutorFactory
Factory function type for creating executor instances.
Core interfaces.
Definition adapter.h:21
Umbrella header for Result<T> type and related utilities.