40 _now = clock_t::now();
47 _now = clock_t::now();
54 _now += std::chrono::milliseconds(milliseconds);
61 _now += std::chrono::milliseconds(milliseconds);
97 return task->IsInGroup(group);
106 return std::ranges::any_of(groups, [&](
group_t group) {
return task->IsInGroup(group); });
115 task->_end += duration;
125 if (task->IsInGroup(group))
127 task->_end += duration;
152 if (task->IsInGroup(group))
177 return InsertTask(std::make_shared<Task>(end + time, time, std::move(task)));
182 static constexpr repeated_t DEFAULT_REPEATED = 0;
183 return InsertTask(std::make_shared<Task>(end + time, time, group, DEFAULT_REPEATED, std::move(task)));
221 container.insert(std::move(node));
226 return container.extract(container.begin());
231 return *container.begin();
241 for (
auto itr = container.begin(); itr != container.end();)
243 itr = container.erase(itr);
251 for (
auto itr = container.begin(); itr != container.end();)
253 cache.insert(container.extract(itr++));
257 container.merge(cache);
262 return container.empty();
266 : _task(std::move(task)), _owner(std::move(owner))
271 : _task(std::move(right._task)), _owner(std::move(right._owner))
274 right._task = GetTaskContainer();
281 _task = std::move(right._task);
282 _owner = std::move(right._owner);
285 right._task = GetTaskContainer();
294 return _owner.expired();
299 return GetTask()->IsInGroup(group);
304 GetTask()->_group = group;
310 GetTask()->_group = std::nullopt;
316 return GetTask()->_repeated;
322 ASSERT(std::holds_alternative<TaskScheduler::TaskQueue::Container::node_type>(_task),
"Bad task logic, task context was consumed already!");
325 TaskScheduler::TaskQueue::Container::node_type& taskNode = std::get<TaskScheduler::TaskQueue::Container::node_type>(_task);
327 task->_duration = duration;
328 task->_end += duration;
329 task->_repeated += 1;
330 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
331 scheduler->InsertTask(std::move(taskNode));
334 _task = std::move(task);
341 return Repeat(GetTask()->_duration);
346 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
347 scheduler->
Async(std::move(callable));
354 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
355 scheduler->ScheduleAt(GetTask()->_end, time, std::move(task));
362 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
363 scheduler->ScheduleAt(GetTask()->_end, time, group, std::move(task));
370 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
378 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
386 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
394 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
402 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
410 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
418 if (std::shared_ptr<TaskScheduler> scheduler = _owner.lock())
426 GetTask()->
_task(*
this);
431 static_assert(std::variant_size_v<
decltype(_task)> == 2);
432 switch (_task.index())
434 case 0:
return std::get<0>(_task).value();
435 case 1:
return std::get<1>(_task);
443 static_assert(std::variant_size_v<
decltype(_task)> == 2);
444 switch (_task.index())
446 case 0:
return std::get<0>(_task).value().get();
447 case 1:
return std::get<1>(_task).get();
static bool EmptyValidator()
TaskContext & Async(std::function< void()> callable)
TaskContext & CancelGroupsOf(std::span< TaskScheduler::group_t > groups)
TaskContext & DelayGroup(TaskScheduler::group_t group, TaskScheduler::duration_t duration)
Delays all tasks of a group with the given duration from within the context.
void Invoke()
Invokes the associated hook of the task.
TaskContext & CancelAll()
Cancels all tasks from within the context.
std::variant< TaskScheduler::TaskQueue::Container::node_type, TaskScheduler::TaskContainer > _task
Associated task.
TaskContext & RescheduleAll(TaskScheduler::duration_t duration)
Reschedule all tasks with the given duration.
TaskContext & Schedule(TaskScheduler::duration_t time, TaskScheduler::task_handler_t task)
TaskContext & SetGroup(TaskScheduler::group_t group)
Sets the event in the given group.
TaskContext & operator=(TaskContext const &right)=delete
TaskScheduler::repeated_t GetRepeatCounter() const
Returns the repeat counter which increases every time the task is repeated.
TaskContext & DelayAll(TaskScheduler::duration_t duration)
Delays all tasks with the given duration from within the context.
TaskContext & ClearGroup()
Removes the group from the event.
TaskScheduler::Task * GetTask() const noexcept
TaskScheduler::TaskContainer & GetTaskContainer() noexcept
bool IsInGroup(TaskScheduler::group_t group) const
Returns true if the event is in the given group.
TaskContext & CancelGroup(TaskScheduler::group_t group)
Cancel all tasks of a single group from within the context.
bool IsExpired() const
Returns true if the owner was deallocated and this context has expired.
TaskContext & RescheduleGroup(TaskScheduler::group_t group, TaskScheduler::duration_t duration)
Reschedule all tasks of a group with the given duration.
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
TaskContainer const & First() const
void RemoveIf(std::function< bool(TaskContainer const &)> const &filter)
Container::node_type Pop()
Pops the task out of the container.
std::multiset< TaskContainer, Compare > Container
void Push(TaskContainer &&task)
TaskScheduler & CancelGroup(group_t group)
TaskScheduler & DelayGroup(group_t group, duration_t duration)
Delays all tasks of a group with the given duration.
TaskScheduler & CancelAll()
TaskScheduler & RescheduleAll(duration_t duration)
Reschedule all tasks with a given duration.
std::shared_ptr< Task > TaskContainer
clock_t::time_point timepoint_t
void Dispatch(success_t const &callback)
Dispatch remaining tasks.
std::function< void()> success_t
std::shared_ptr< TaskScheduler > self_reference
Contains a self reference to track if this object was deleted or not.
TaskScheduler & InsertTask(TaskContainer &&task)
Insert a new task to the enqueued tasks.
TaskScheduler & RescheduleGroup(group_t group, duration_t duration)
Reschedule all tasks of a group with the given duration.
clock_t::duration duration_t
TaskScheduler & Schedule(duration_t time, task_handler_t task)
static success_t const EmptySuccessCallback
TaskScheduler & ClearValidator()
Clears the validator which is asked if tasks are allowed to be executed.
timepoint_t _now
The current time point (now)
TaskScheduler & DelayAll(duration_t duration)
Delays all tasks with the given duration.
TaskScheduler & Async(std::function< void()> callable)
TaskScheduler & CancelGroupsOf(std::span< group_t > groups)
static bool EmptyValidator()
TaskScheduler & ScheduleAt(timepoint_t end, duration_t time, task_handler_t task)
TaskQueue _task_holder
The Task Queue which contains all task objects.
TaskScheduler & Update()
Update the scheduler to the current time.