90#include <unordered_set>
154 [[nodiscard]]
static std::shared_ptr<GraphTask>
create(
Callback&& callback,
int condition_number = 0);
164 [[nodiscard]]
static std::shared_ptr<GraphTask>
create(
const std::string& name,
Callback&& callback,
165 int condition_number = 0);
179 int condition_number = 0);
191 int condition_number = 0);
206 template <
class GraphEngineT>
207 void execute(GraphEngineT* graph_engine);
225 void precede(
const std::shared_ptr<GraphTask>& task);
235 void succeed(
const std::shared_ptr<GraphTask>& task);
411 int invoke(
bool once);
415 void notify(
int condition_number);
417 void update_status(
Status status);
419 bool detect_cycle(
const GraphTask* task, std::unordered_set<const GraphTask*>& visited,
420 std::unordered_set<const GraphTask*>& recursion_stack)
const;
422 static void clear_invalid_task(
const std::shared_ptr<GraphTask>& task);
424 std::unique_ptr<struct GraphTaskImpl> impl_;
435template <
class GraphEngineT>
437 auto self = shared_from_this();
440 constexpr bool kHaspriority =
VLINK_HAS_MEMBER(GraphEngineT, post_task_with_priority);
441 [[maybe_unused]]
constexpr uint8_t kPriorityType = 2;
443 auto task_func = [self, task]() {
444 if (task.get() != self.get()) {
448 int ret = task->invoke(
true);
455 if constexpr (kHaspriority) {
457 if (graph_engine->get_type() == kPriorityType) {
458 graph_engine->post_task_with_priority(std::move(task_func), task->get_priority());
460 graph_engine->post_task(std::move(task_func));
463 graph_engine->post_task(std::move(task_func));
466 graph_engine->post_task(std::move(task_func));
479 task->precede(target_task);
void remove_succeed_task(const std::shared_ptr< GraphTask > &task)
Removes a previously added successor dependency.
void precede(const std::shared_ptr< GraphTask > &task)
Declares that this task must complete before task starts.
void set_name(const std::string &name)
Sets the task name used in DOT export and status callbacks.
void cancel()
Cancels this task; sets its status to kStatusInActive.
void set_priority(uint16_t priority)
Sets the task dispatch priority (used by priority-aware engines).
void set_max_recursion_depth(uint32_t depth)
Sets the maximum recursion depth to guard against infinitely deep graphs.
void execute(GraphEngineT *graph_engine)
Submits this task (and all reachable successors) to a graph execution engine.
定义 graph_task.h:436
std::function< int()> ConditionCallback
Callback type for condition tasks; returns the branch index to activate.
定义 graph_task.h:136
std::function< void()> Callback
Callback type for regular (void-returning) tasks.
定义 graph_task.h:131
Status
Execution state of the task within a single execute() pass.
定义 graph_task.h:112
@ kStatusRunning
Currently executing
定义 graph_task.h:115
@ kStatusPending
Waiting for predecessors to complete
定义 graph_task.h:114
@ kStatusDone
Execution completed
定义 graph_task.h:116
@ kStatusInActive
Not yet submitted or cancelled
定义 graph_task.h:113
std::string get_name() const
Returns the task name.
std::string export_to_dot() const
Exports the reachable sub-graph as a Graphviz DOT string.
std::function< void(const std::string &, Status)> StatusCallback
Callback for status change notifications.
定义 graph_task.h:145
void process_and_traverse(const FindTaskCallback &callback)
void set_policy(Policy policy)
Sets the execution policy.
int get_condition_number() const
Returns the number of condition branches.
Status get_status() const
Returns the current execution status of this task.
std::function< void(const std::shared_ptr< GraphTask > &)> FindTaskCallback
定义 graph_task.h:396
static std::shared_ptr< GraphTask > create(const std::string &name, Callback &&callback, int condition_number=0)
Creates a named regular (void) task node.
void register_status_callback(StatusCallback &&callback)
Registers a callback invoked whenever this task's status changes.
static std::shared_ptr< GraphTask > create_condition(const std::string &name, ConditionCallback &&callback, int condition_number=0)
Creates a named condition task.
uint32_t get_max_recursion_depth() const
Returns the maximum recursion depth.
void set_condition_number(int condition_number)
Sets the number of condition branches this task can select.
void remove_precede_task(const std::shared_ptr< GraphTask > &task)
Removes a previously added predecessor dependency.
uint16_t get_priority() const
Returns the dispatch priority.
GraphTask(Callback &&callback, int condition_number)
std::vector< std::weak_ptr< GraphTask > > get_succeed_task_list() const
Returns the list of successor tasks (they depend on this task).
Policy get_policy() const
Returns the execution policy.
Policy
Execution policy controlling how many times and when the task runs.
定义 graph_task.h:122
@ kPolicyOnce
Run exactly once per execute() call (default)
定义 graph_task.h:123
@ kPolicyMultiple
Allow multiple invocations in a single pass
定义 graph_task.h:124
@ kPolicyWaitAll
Wait for ALL predecessors before running
定义 graph_task.h:125
std::string get_group_name() const
Returns the group name.
static std::shared_ptr< GraphTask > create(Callback &&callback, int condition_number=0)
Creates a regular (void) task node.
GraphTask(const std::string &name, ConditionCallback &&callback, int condition_number)
void set_group_name(const std::string &name)
Sets a group name for visual grouping in DOT export.
bool has_cycle() const
Detects whether the reachable sub-graph contains any cycles.
bool is_condition_task() const
Returns true if this task was created with create_condition().
GraphTask(const std::string &name, Callback &&callback, int condition_number)
void succeed(const std::shared_ptr< GraphTask > &task)
Declares that task must complete before this task starts.
static std::shared_ptr< GraphTask > create_condition(ConditionCallback &&callback, int condition_number=0)
Creates a condition task that returns a branch index.
GraphTask(ConditionCallback &&callback, int condition_number)
std::vector< std::weak_ptr< GraphTask > > get_precede_task_list() const
Returns the list of predecessor tasks (this task depends on them).
Platform-independent macro definitions for the VLink library.
#define VLINK_EXPORT
定义 macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
定义 macros.h:184
std::shared_ptr< GraphTask > GraphTaskPtr
定义 graph_task.h:429
Compile-time type-trait utilities used internally by VLink.
#define VLINK_HAS_MEMBER(T, member)
Macro Definitions
定义 traits.h:350