diff options
Diffstat (limited to 'src/common/Utilities')
-rw-r--r-- | src/common/Utilities/EventMap.cpp | 41 | ||||
-rw-r--r-- | src/common/Utilities/EventMap.h | 13 | ||||
-rw-r--r-- | src/common/Utilities/TaskScheduler.cpp | 144 | ||||
-rw-r--r-- | src/common/Utilities/TaskScheduler.h | 261 | ||||
-rw-r--r-- | src/common/Utilities/Util.h | 4 |
5 files changed, 228 insertions, 235 deletions
diff --git a/src/common/Utilities/EventMap.cpp b/src/common/Utilities/EventMap.cpp index dcfe4686f6b..c3997634370 100644 --- a/src/common/Utilities/EventMap.cpp +++ b/src/common/Utilities/EventMap.cpp @@ -18,6 +18,10 @@ #include "EventMap.h" #include "Random.h" +EventMap::EventMap(EventMap const& other) = default; +EventMap& EventMap::operator=(EventMap const& other) = default; +EventMap::~EventMap() = default; + void EventMap::Reset() { _eventMap.clear(); @@ -41,7 +45,7 @@ void EventMap::ScheduleEvent(uint32 eventId, Milliseconds time, uint32 group /*= if (phase && phase <= 8) eventId |= (1 << (phase + 23)); - _eventMap.insert(EventStore::value_type(_time + time, eventId)); + _eventMap.insert({ _time + time, eventId }); } void EventMap::ScheduleEvent(uint32 eventId, Milliseconds minTime, Milliseconds maxTime, uint32 group /*= 0*/, uint8 phase /*= 0*/) @@ -102,7 +106,7 @@ void EventMap::DelayEvents(Milliseconds delay) for (EventStore::iterator itr = delayed.begin(); itr != delayed.end();) { EventStore::node_type node = delayed.extract(itr++); - node.key() = node.key() + delay; + node.key() += delay; _eventMap.insert(_eventMap.end(), std::move(node)); } } @@ -113,19 +117,19 @@ void EventMap::DelayEvents(Milliseconds delay, uint32 group) return; EventStore delayed; - for (EventStore::iterator itr = _eventMap.begin(); itr != _eventMap.end();) { if (itr->second & (1 << (group + 15))) { - delayed.insert(EventStore::value_type(itr->first + delay, itr->second)); - _eventMap.erase(itr++); + EventStore::node_type node = _eventMap.extract(itr++); + node.key() += delay; + delayed.insert(delayed.end(), std::move(node)); } else ++itr; } - _eventMap.insert(delayed.begin(), delayed.end()); + _eventMap.merge(delayed); } void EventMap::CancelEvent(uint32 eventId) @@ -187,33 +191,32 @@ void EventMap::ScheduleNextFromSeries(uint32 eventData) if (itr == _timerSeries.end()) return; - if (itr->first != eventData) - return; + ScheduleEvent(eventData, itr->second.front()); - if (itr->second.size() == 0) - return; - - Milliseconds time = itr->second.front(); - itr->second.pop(); - - ScheduleEvent(eventData, time); + if (itr->second.size() > 1) + itr->second.erase(itr->second.begin()); + else + _timerSeries.erase(itr); } -void EventMap::ScheduleEventSeries(uint32 eventId, uint8 group, uint8 phase, std::initializer_list<Milliseconds> const& timeSeries) +void EventMap::ScheduleEventSeries(uint32 eventId, uint8 group, uint8 phase, std::initializer_list<Milliseconds> timeSeries) { + if (!timeSeries.size()) + return; + if (group && group <= 8) eventId |= (1 << (group + 15)); if (phase && phase <= 8) eventId |= (1 << (phase + 23)); - for (Milliseconds const& time : timeSeries) - _timerSeries[eventId].push(time); + std::vector<Milliseconds>& series = _timerSeries[eventId]; + series.insert(series.end(), timeSeries.begin(), timeSeries.end()); ScheduleNextFromSeries(eventId); } -void EventMap::ScheduleEventSeries(uint32 eventId, std::initializer_list<Milliseconds> const& timeSeries) +void EventMap::ScheduleEventSeries(uint32 eventId, std::initializer_list<Milliseconds> timeSeries) { ScheduleEventSeries(eventId, 0, 0, timeSeries); } diff --git a/src/common/Utilities/EventMap.h b/src/common/Utilities/EventMap.h index 82c466408cd..7d8c006e3bf 100644 --- a/src/common/Utilities/EventMap.h +++ b/src/common/Utilities/EventMap.h @@ -21,7 +21,7 @@ #include "Define.h" #include "Duration.h" #include <map> -#include <queue> +#include <vector> class TC_COMMON_API EventMap { @@ -37,10 +37,15 @@ class TC_COMMON_API EventMap * - Pattern: 0xPPGGEEEE */ typedef std::multimap<TimePoint, uint32> EventStore; - typedef std::map<uint32 /*event data*/, std::queue<Milliseconds>> EventSeriesStore; + typedef std::map<uint32 /*event data*/, std::vector<Milliseconds>> EventSeriesStore; public: EventMap() : _time(TimePoint::min()), _phase(0), _lastEvent(0) { } + EventMap(EventMap const& other); + EventMap(EventMap&& other) noexcept = default; + EventMap& operator=(EventMap const& other); + EventMap& operator=(EventMap&& other) noexcept = default; + ~EventMap(); /** * @name Reset @@ -242,7 +247,7 @@ public: * @param phase of the event. * @param timeSeries specifying the times the event should be automatically scheduled after each trigger (first value is initial schedule) */ - void ScheduleEventSeries(uint32 eventId, uint8 group, uint8 phase, std::initializer_list<Milliseconds> const& timeSeries); + void ScheduleEventSeries(uint32 eventId, uint8 group, uint8 phase, std::initializer_list<Milliseconds> timeSeries); /** * @name ScheduleEventSeries @@ -250,7 +255,7 @@ public: * @param eventId of the event. * @param timeSeries specifying the times the event should be automatically scheduled after each trigger (first value is initial schedule) */ - void ScheduleEventSeries(uint32 eventId, std::initializer_list<Milliseconds> const& series); + void ScheduleEventSeries(uint32 eventId, std::initializer_list<Milliseconds> series); private: /** diff --git a/src/common/Utilities/TaskScheduler.cpp b/src/common/Utilities/TaskScheduler.cpp index 60eb9284713..4ee9451b846 100644 --- a/src/common/Utilities/TaskScheduler.cpp +++ b/src/common/Utilities/TaskScheduler.cpp @@ -18,6 +18,15 @@ #include "TaskScheduler.h" #include "Errors.h" +TaskScheduler::TaskScheduler() + : self_reference(this, [](TaskScheduler const*) { }), + _now(clock_t::now()), + _predicate(EmptyValidator) +{ +} + +TaskScheduler::~TaskScheduler() = default; + TaskScheduler& TaskScheduler::ClearValidator() { _predicate = EmptyValidator; @@ -36,6 +45,13 @@ TaskScheduler& TaskScheduler::Update(size_t milliseconds, success_t const& callb return Update(std::chrono::milliseconds(milliseconds), callback); } +TaskScheduler& TaskScheduler::Update(duration_t difftime, success_t const& callback/* = nullptr*/) +{ + _now += difftime; + Dispatch(callback); + return *this; +} + TaskScheduler& TaskScheduler::Async(std::function<void()> callable) { _asyncHolder.emplace(std::move(callable)); @@ -67,12 +83,75 @@ TaskScheduler& TaskScheduler::CancelGroupsOf(std::vector<group_t> const& groups) return *this; } +TaskScheduler& TaskScheduler::DelayAll(duration_t duration) +{ + _task_holder.ModifyIf([&duration](TaskContainer const& task) -> bool + { + task->_end += duration; + return true; + }); + return *this; +} + +TaskScheduler& TaskScheduler::DelayGroup(group_t const group, duration_t duration) +{ + _task_holder.ModifyIf([&duration, group](TaskContainer const& task) -> bool + { + if (task->IsInGroup(group)) + { + task->_end += duration; + return true; + } + else + return false; + }); + return *this; +} + +TaskScheduler& TaskScheduler::RescheduleAll(duration_t duration) +{ + auto const end = _now + duration; + _task_holder.ModifyIf([end](TaskContainer const& task) -> bool + { + task->_end = end; + return true; + }); + return *this; +} + +TaskScheduler& TaskScheduler::RescheduleGroup(group_t const group, duration_t duration) +{ + auto const end = _now + duration; + _task_holder.ModifyIf([end, group](TaskContainer const& task) -> bool + { + if (task->IsInGroup(group)) + { + task->_end = end; + return true; + } + else + return false; + }); + return *this; +} + TaskScheduler& TaskScheduler::InsertTask(TaskContainer task) { _task_holder.Push(std::move(task)); return *this; } +TaskScheduler& TaskScheduler::ScheduleAt(timepoint_t end, duration_t time, task_handler_t task) +{ + return InsertTask(TaskContainer(new Task(end + time, time, std::move(task)))); +} + +TaskScheduler& TaskScheduler::ScheduleAt(timepoint_t end, duration_t time, group_t const group, task_handler_t task) +{ + static constexpr repeated_t DEFAULT_REPEATED = 0; + return InsertTask(TaskContainer(new Task(end + time, time, group, DEFAULT_REPEATED, std::move(task)))); +} + void TaskScheduler::Dispatch(success_t const& callback/* = nullptr*/) { // If the validation failed abort the dispatching here. @@ -198,6 +277,21 @@ TaskScheduler::repeated_t TaskContext::GetRepeatCounter() const return _task->_repeated; } +TaskContext& TaskContext::Repeat(TaskScheduler::duration_t duration) +{ + AssertOnConsumed(); + + // Set new duration, in-context timing and increment repeat counter + _task->_duration = duration; + _task->_end += duration; + _task->_repeated += 1; + (*_consumed) = true; + return this->Dispatch([this](TaskScheduler& scheduler) -> TaskScheduler& + { + return scheduler.InsertTask(_task); + }); +} + TaskContext& TaskContext::Async(std::function<void()> const& callable) { return Dispatch([&](TaskScheduler& scheduler) -> TaskScheduler& @@ -206,6 +300,24 @@ TaskContext& TaskContext::Async(std::function<void()> const& callable) }); } +TaskContext& TaskContext::Schedule(TaskScheduler::duration_t time, TaskScheduler::task_handler_t task) +{ + auto const end = _task->_end; + return this->Dispatch([end, time, task = std::move(task)](TaskScheduler& scheduler) mutable -> TaskScheduler& + { + return scheduler.ScheduleAt(end, time, std::move(task)); + }); +} + +TaskContext& TaskContext::Schedule(TaskScheduler::duration_t time, TaskScheduler::group_t const group, TaskScheduler::task_handler_t task) +{ + auto const end = _task->_end; + return this->Dispatch([end, time, group, task = std::move(task)](TaskScheduler& scheduler) mutable -> TaskScheduler& + { + return scheduler.ScheduleAt(end, time, group, std::move(task)); + }); +} + TaskContext& TaskContext::CancelAll() { return Dispatch(&TaskScheduler::CancelAll); @@ -227,6 +339,38 @@ TaskContext& TaskContext::CancelGroupsOf(std::vector<TaskScheduler::group_t> con }); } +TaskContext& TaskContext::DelayAll(TaskScheduler::duration_t duration) +{ + return this->Dispatch([=](TaskScheduler& scheduler) -> TaskScheduler& + { + return scheduler.DelayAll(duration); + }); +} + +TaskContext& TaskContext::DelayGroup(TaskScheduler::group_t const group, TaskScheduler::duration_t duration) +{ + return this->Dispatch([=](TaskScheduler& scheduler) -> TaskScheduler& + { + return scheduler.DelayGroup(group, duration); + }); +} + +TaskContext& TaskContext::RescheduleAll(TaskScheduler::duration_t duration) +{ + return this->Dispatch([=](TaskScheduler& scheduler) -> TaskScheduler& + { + return scheduler.RescheduleAll(duration); + }); +} + +TaskContext& TaskContext::RescheduleGroup(TaskScheduler::group_t const group, TaskScheduler::duration_t duration) +{ + return this->Dispatch([=](TaskScheduler& scheduler) -> TaskScheduler& + { + return scheduler.RescheduleGroup(group, duration); + }); +} + void TaskContext::AssertOnConsumed() const { // This was adapted to TC to prevent static analysis tools from complaining. diff --git a/src/common/Utilities/TaskScheduler.h b/src/common/Utilities/TaskScheduler.h index 2ebd3e57b37..c9029a002db 100644 --- a/src/common/Utilities/TaskScheduler.h +++ b/src/common/Utilities/TaskScheduler.h @@ -171,11 +171,10 @@ class TC_COMMON_API TaskScheduler } public: - TaskScheduler() - : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(EmptyValidator) { } + TaskScheduler(); template<typename P> - TaskScheduler(P&& predicate) + explicit TaskScheduler(P&& predicate) : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(std::forward<P>(predicate)) { } TaskScheduler(TaskScheduler const&) = delete; @@ -183,7 +182,7 @@ public: TaskScheduler& operator= (TaskScheduler const&) = delete; TaskScheduler& operator= (TaskScheduler&&) = delete; - ~TaskScheduler() = default; + ~TaskScheduler(); /// Sets a validator which is asked if tasks are allowed to be executed. template<typename P> @@ -206,14 +205,7 @@ public: /// Update the scheduler with a difftime. /// Calls the optional callback on successfully finish. - template<class Rep, class Period> - TaskScheduler& Update(std::chrono::duration<Rep, Period> difftime, - success_t const& callback = nullptr) - { - _now += difftime; - Dispatch(callback); - return *this; - } + TaskScheduler& Update(duration_t difftime, success_t const& callback = nullptr); /// Schedule an callable function that is executed at the next update tick. /// Its safe to modify the TaskScheduler from within the callable. @@ -221,8 +213,7 @@ public: /// Schedule an event with a fixed rate. /// Never call this from within a task context! Use TaskContext::Schedule instead! - template<class Rep, class Period> - TaskScheduler& Schedule(std::chrono::duration<Rep, Period> time, + TaskScheduler& Schedule(duration_t time, task_handler_t task) { return this->ScheduleAt(_now, time, std::move(task)); @@ -230,8 +221,7 @@ public: /// Schedule an event with a fixed rate. /// Never call this from within a task context! Use TaskContext::Schedule instead! - template<class Rep, class Period> - TaskScheduler& Schedule(std::chrono::duration<Rep, Period> time, + TaskScheduler& Schedule(duration_t time, group_t const group, task_handler_t task) { return this->ScheduleAt(_now, time, group, std::move(task)); @@ -239,18 +229,16 @@ public: /// Schedule an event with a randomized rate between min and max rate. /// Never call this from within a task context! Use TaskContext::Schedule instead! - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskScheduler& Schedule(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max, task_handler_t task) + TaskScheduler& Schedule(std::chrono::milliseconds min, + std::chrono::milliseconds max, task_handler_t task) { return this->Schedule(::randtime(min, max), std::move(task)); } /// Schedule an event with a fixed rate. /// Never call this from within a task context! Use TaskContext::Schedule instead! - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskScheduler& Schedule(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max, group_t const group, + TaskScheduler& Schedule(std::chrono::milliseconds min, + std::chrono::milliseconds max, group_t const group, task_handler_t task) { return this->Schedule(::randtime(min, max), group, std::move(task)); @@ -269,95 +257,42 @@ public: TaskScheduler& CancelGroupsOf(std::vector<group_t> const& groups); /// Delays all tasks with the given duration. - template<class Rep, class Period> - TaskScheduler& DelayAll(std::chrono::duration<Rep, Period> duration) - { - _task_holder.ModifyIf([&duration](TaskContainer const& task) -> bool - { - task->_end += duration; - return true; - }); - return *this; - } + TaskScheduler& DelayAll(duration_t duration); /// Delays all tasks with a random duration between min and max. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskScheduler& DelayAll(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + TaskScheduler& DelayAll(std::chrono::milliseconds min, + std::chrono::milliseconds max) { return this->DelayAll(::randtime(min, max)); } /// Delays all tasks of a group with the given duration. - template<class Rep, class Period> - TaskScheduler& DelayGroup(group_t const group, std::chrono::duration<Rep, Period> duration) - { - _task_holder.ModifyIf([&duration, group](TaskContainer const& task) -> bool - { - if (task->IsInGroup(group)) - { - task->_end += duration; - return true; - } - else - return false; - }); - return *this; - } + TaskScheduler& DelayGroup(group_t const group, duration_t duration); /// Delays all tasks of a group with a random duration between min and max. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> TaskScheduler& DelayGroup(group_t const group, - std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + std::chrono::milliseconds min, + std::chrono::milliseconds max) { return this->DelayGroup(group, ::randtime(min, max)); } /// Reschedule all tasks with a given duration. - template<class Rep, class Period> - TaskScheduler& RescheduleAll(std::chrono::duration<Rep, Period> duration) - { - auto const end = _now + duration; - _task_holder.ModifyIf([end](TaskContainer const& task) -> bool - { - task->_end = end; - return true; - }); - return *this; - } + TaskScheduler& RescheduleAll(duration_t duration); /// Reschedule all tasks with a random duration between min and max. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskScheduler& RescheduleAll(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + TaskScheduler& RescheduleAll(std::chrono::milliseconds min, std::chrono::milliseconds max) { return this->RescheduleAll(::randtime(min, max)); } /// Reschedule all tasks of a group with the given duration. - template<class Rep, class Period> - TaskScheduler& RescheduleGroup(group_t const group, std::chrono::duration<Rep, Period> duration) - { - auto const end = _now + duration; - _task_holder.ModifyIf([end, group](TaskContainer const& task) -> bool - { - if (task->IsInGroup(group)) - { - task->_end = end; - return true; - } - else - return false; - }); - return *this; - } + TaskScheduler& RescheduleGroup(group_t const group, duration_t duration); /// Reschedule all tasks of a group with a random duration between min and max. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> TaskScheduler& RescheduleGroup(group_t const group, - std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + std::chrono::milliseconds min, + std::chrono::milliseconds max) { return this->RescheduleGroup(group, ::randtime(min, max)); } @@ -366,23 +301,14 @@ private: /// Insert a new task to the enqueued tasks. TaskScheduler& InsertTask(TaskContainer task); - template<class Rep, class Period> TaskScheduler& ScheduleAt(timepoint_t end, - std::chrono::duration<Rep, Period> time, task_handler_t task) - { - return InsertTask(TaskContainer(new Task(end + time, time, std::move(task)))); - } + duration_t time, task_handler_t task); /// Schedule an event with a fixed rate. /// Never call this from within a task context! Use TaskContext::schedule instead! - template<class Rep, class Period> TaskScheduler& ScheduleAt(timepoint_t end, - std::chrono::duration<Rep, Period> time, - group_t const group, task_handler_t task) - { - static constexpr repeated_t DEFAULT_REPEATED = 0; - return InsertTask(TaskContainer(new Task(end + time, time, group, DEFAULT_REPEATED, std::move(task)))); - } + duration_t time, + group_t const group, task_handler_t task); /// Dispatch remaining tasks void Dispatch(success_t const& callback); @@ -414,36 +340,16 @@ public: : _task(std::move(task)), _owner(std::move(owner)), _consumed(std::make_shared<bool>(false)) { } // Copy construct - TaskContext(TaskContext const& right) - : _task(right._task), _owner(right._owner), _consumed(right._consumed) { } + TaskContext(TaskContext const& right) = default; // Move construct - TaskContext(TaskContext&& right) noexcept - : _task(std::move(right._task)), _owner(std::move(right._owner)), _consumed(std::move(right._consumed)) { } + TaskContext(TaskContext&& right) noexcept = default; // Copy assign - TaskContext& operator=(TaskContext const& right) - { - if (this != &right) - { - _task = right._task; - _owner = right._owner; - _consumed = right._consumed; - } - return *this; - } + TaskContext& operator=(TaskContext const& right) = default; // Move assign - TaskContext& operator=(TaskContext&& right) noexcept - { - if (this != &right) - { - _task = std::move(right._task); - _owner = std::move(right._owner); - _consumed = std::move(right._consumed); - } - return *this; - } + TaskContext& operator=(TaskContext&& right) noexcept = default; ~TaskContext() = default; @@ -466,21 +372,7 @@ public: /// std::chrono::seconds(5) for example. /// This will consume the task context, its not possible to repeat the task again /// from the same task context! - template<class Rep, class Period> - TaskContext& Repeat(std::chrono::duration<Rep, Period> duration) - { - AssertOnConsumed(); - - // Set new duration, in-context timing and increment repeat counter - _task->_duration = duration; - _task->_end += duration; - _task->_repeated += 1; - (*_consumed) = true; - return this->Dispatch([this](TaskScheduler& scheduler) -> TaskScheduler& - { - return scheduler.InsertTask(_task); - }); - } + TaskContext& Repeat(TaskScheduler::duration_t duration); /// Repeats the event with the same duration. /// This will consume the task context, its not possible to repeat the task again @@ -494,9 +386,8 @@ public: /// std::chrono::seconds(5) for example. /// This will consume the task context, its not possible to repeat the task again /// from the same task context! - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskContext& Repeat(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + TaskContext& Repeat(std::chrono::milliseconds min, + std::chrono::milliseconds max) { return this->Repeat(::randtime(min, max)); } @@ -509,39 +400,22 @@ public: /// Its possible that the new event is executed immediately! /// Use TaskScheduler::Async to create a task /// which will be called at the next update tick. - template<class Rep, class Period> - TaskContext& Schedule(std::chrono::duration<Rep, Period> time, - TaskScheduler::task_handler_t task) - { - auto const end = _task->_end; - return this->Dispatch([end, time, task = std::move(task)](TaskScheduler& scheduler) -> TaskScheduler& - { - return scheduler.ScheduleAt<Rep, Period>(end, time, std::move(task)); - }); - } + TaskContext& Schedule(TaskScheduler::duration_t time, + TaskScheduler::task_handler_t task); /// Schedule an event with a fixed rate from within the context. /// Its possible that the new event is executed immediately! /// Use TaskScheduler::Async to create a task /// which will be called at the next update tick. - template<class Rep, class Period> - TaskContext& Schedule(std::chrono::duration<Rep, Period> time, - TaskScheduler::group_t const group, TaskScheduler::task_handler_t task) - { - auto const end = _task->_end; - return this->Dispatch([end, time, group, task = std::move(task)](TaskScheduler& scheduler) -> TaskScheduler& - { - return scheduler.ScheduleAt<Rep, Period>(end, time, group, std::move(task)); - }); - } + TaskContext& Schedule(TaskScheduler::duration_t time, + TaskScheduler::group_t const group, TaskScheduler::task_handler_t task); /// Schedule an event with a randomized rate between min and max rate from within the context. /// Its possible that the new event is executed immediately! /// Use TaskScheduler::Async to create a task /// which will be called at the next update tick. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskContext& Schedule(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max, TaskScheduler::task_handler_t task) + TaskContext& Schedule(std::chrono::milliseconds min, + std::chrono::milliseconds max, TaskScheduler::task_handler_t task) { return this->Schedule(::randtime(min, max), std::move(task)); } @@ -550,9 +424,8 @@ public: /// Its possible that the new event is executed immediately! /// Use TaskScheduler::Async to create a task /// which will be called at the next update tick. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskContext& Schedule(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max, TaskScheduler::group_t const group, + TaskContext& Schedule(std::chrono::milliseconds min, + std::chrono::milliseconds max, TaskScheduler::group_t const group, TaskScheduler::task_handler_t task) { return this->Schedule(::randtime(min, max), group, std::move(task)); @@ -569,75 +442,43 @@ public: TaskContext& CancelGroupsOf(std::vector<TaskScheduler::group_t> const& groups); /// Delays all tasks with the given duration from within the context. - template<class Rep, class Period> - TaskContext& DelayAll(std::chrono::duration<Rep, Period> duration) - { - return this->Dispatch([=](TaskScheduler& scheduler) -> TaskScheduler& - { - return scheduler.DelayAll(duration); - }); - } + TaskContext& DelayAll(TaskScheduler::duration_t duration); /// Delays all tasks with a random duration between min and max from within the context. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskContext& DelayAll(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + TaskContext& DelayAll(std::chrono::milliseconds min, + std::chrono::milliseconds max) { return this->DelayAll(::randtime(min, max)); } /// Delays all tasks of a group with the given duration from within the context. - template<class Rep, class Period> - TaskContext& DelayGroup(TaskScheduler::group_t const group, std::chrono::duration<Rep, Period> duration) - { - return this->Dispatch([=](TaskScheduler& scheduler) -> TaskScheduler& - { - return scheduler.DelayGroup(group, duration); - }); - } + TaskContext& DelayGroup(TaskScheduler::group_t const group, TaskScheduler::duration_t duration); /// Delays all tasks of a group with a random duration between min and max from within the context. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> TaskContext& DelayGroup(TaskScheduler::group_t const group, - std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + std::chrono::milliseconds min, + std::chrono::milliseconds max) { return this->DelayGroup(group, ::randtime(min, max)); } /// Reschedule all tasks with the given duration. - template<class Rep, class Period> - TaskContext& RescheduleAll(std::chrono::duration<Rep, Period> duration) - { - return this->Dispatch([=](TaskScheduler& scheduler) -> TaskScheduler& - { - return scheduler.RescheduleAll(duration); - }); - } + TaskContext& RescheduleAll(TaskScheduler::duration_t duration); /// Reschedule all tasks with a random duration between min and max. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> - TaskContext& RescheduleAll(std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + TaskContext& RescheduleAll(std::chrono::milliseconds min, + std::chrono::milliseconds max) { return this->RescheduleAll(::randtime(min, max)); } /// Reschedule all tasks of a group with the given duration. - template<class Rep, class Period> - TaskContext& RescheduleGroup(TaskScheduler::group_t const group, std::chrono::duration<Rep, Period> duration) - { - return this->Dispatch([=](TaskScheduler& scheduler) -> TaskScheduler& - { - return scheduler.RescheduleGroup(group, duration); - }); - } + TaskContext& RescheduleGroup(TaskScheduler::group_t const group, TaskScheduler::duration_t duration); /// Reschedule all tasks of a group with a random duration between min and max. - template<class RepLeft, class PeriodLeft, class RepRight, class PeriodRight> TaskContext& RescheduleGroup(TaskScheduler::group_t const group, - std::chrono::duration<RepLeft, PeriodLeft> min, - std::chrono::duration<RepRight, PeriodRight> max) + std::chrono::milliseconds min, + std::chrono::milliseconds max) { return this->RescheduleGroup(group, ::randtime(min, max)); } diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index a16e6ba0e89..dd233a9266d 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -482,7 +482,7 @@ TC_COMMON_API void StringReplaceAll(std::string* str, std::string_view text, std // simple class for not-modifyable list template <typename T> -class HookList final +class HookList { private: typedef std::vector<T> ContainerType; @@ -495,7 +495,7 @@ class HookList final HookList<T>& operator+=(T&& t) { - _container.push_back(std::move(t)); + _container.emplace_back(std::move(t)); return *this; } |