TrinityCore
Loading...
Searching...
No Matches
EventProcessor.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __EVENTPROCESSOR_H
19#define __EVENTPROCESSOR_H
20
21#include "Define.h"
22#include "Duration.h"
23#include "Random.h"
24#include <concepts>
25#include <map>
26#include <type_traits>
27
28class EventProcessor;
29
30// Note. All times are in milliseconds here.
31
33{
34 friend class EventProcessor;
35
36 enum class AbortState : uint8
37 {
38 STATE_RUNNING,
39 STATE_ABORT_SCHEDULED,
40 STATE_ABORTED
41 };
42
43 public:
45 : m_abortState(AbortState::STATE_RUNNING), m_addTime(0), m_execTime(0) { }
46
47 virtual ~BasicEvent() { } // override destructor to perform some actions on event removal
48
49 // this method executes when the event is triggered
50 // return false if event does not want to be deleted
51 // e_time is execution time, p_time is update interval
52 virtual bool Execute(uint64 /*e_time*/, uint32 /*p_time*/) { return true; }
53
54 virtual bool IsDeletable() const { return true; } // this event can be safely deleted
55
56 virtual void Abort(uint64 /*e_time*/) { } // this method executes when the event is aborted
57
58 // Aborts the event at the next update tick
59 void ScheduleAbort();
60
61 private:
62 void SetAborted();
63 bool IsRunning() const { return (m_abortState == AbortState::STATE_RUNNING); }
64 bool IsAbortScheduled() const { return (m_abortState == AbortState::STATE_ABORT_SCHEDULED); }
65 bool IsAborted() const { return (m_abortState == AbortState::STATE_ABORTED); }
66
67 AbortState m_abortState; // set by externals when the event is aborted, aborted events don't execute
68
69 // these can be used for time offset control
70 uint64 m_addTime; // time when the event was added to queue, filled by event handler
71 uint64 m_execTime; // planned time of next execution, filled by event handler
72};
73
74template<typename T>
76{
77public:
78 explicit LambdaBasicEvent(T&& callback) : BasicEvent(), _callback(std::move(callback)) { }
79
80 bool Execute(uint64, uint32) override
81 {
82 _callback();
83 return true;
84 }
85
86private:
88};
89
91{
92 public:
93 EventProcessor() : m_time(0) { }
99
100 void Update(uint32 p_time);
101 void KillAllEvents(bool force);
102
103 void AddEvent(BasicEvent* event, Milliseconds e_time, bool set_addtime = true);
104 template<std::invocable<> T>
105 void AddEvent(T&& event, Milliseconds e_time, bool set_addtime = true) { AddEvent(new LambdaBasicEvent<T>(std::forward<T>(event)), e_time, set_addtime); }
106 void AddEventAtOffset(BasicEvent* event, Milliseconds offset) { AddEvent(event, CalculateTime(offset)); }
107 void AddEventAtOffset(BasicEvent* event, Milliseconds offset, Milliseconds offset2) { AddEvent(event, CalculateTime(randtime(offset, offset2))); }
108 template<std::invocable<> T>
109 void AddEventAtOffset(T&& event, Milliseconds offset) { AddEventAtOffset(new LambdaBasicEvent<T>(std::forward<T>(event)), offset); }
110 template<std::invocable<> T>
111 void AddEventAtOffset(T&& event, Milliseconds offset, Milliseconds offset2) { AddEventAtOffset(new LambdaBasicEvent<T>(std::forward<T>(event)), offset, offset2); }
112 void ModifyEventTime(BasicEvent* event, Milliseconds newTime);
113 Milliseconds CalculateTime(Milliseconds t_offset) const { return Milliseconds(m_time) + t_offset; }
114 std::multimap<uint64, BasicEvent*> const& GetEvents() const { return m_events; }
115
116 protected:
118 std::multimap<uint64, BasicEvent*> m_events;
119};
120
121#endif
uint8_t uint8
Definition Define.h:156
#define TC_COMMON_API
Definition Define.h:99
uint64_t uint64
Definition Define.h:153
uint32_t uint32
Definition Define.h:154
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition Duration.h:24
Milliseconds randtime(Milliseconds min, Milliseconds max)
Definition Random.cpp:62
bool IsAbortScheduled() const
bool IsAborted() const
virtual void Abort(uint64)
virtual ~BasicEvent()
virtual bool IsDeletable() const
virtual bool Execute(uint64, uint32)
uint64 m_addTime
bool IsRunning() const
uint64 m_execTime
AbortState m_abortState
std::multimap< uint64, BasicEvent * > m_events
EventProcessor & operator=(EventProcessor &&)=delete
EventProcessor(EventProcessor const &)=delete
void AddEventAtOffset(T &&event, Milliseconds offset)
void AddEvent(T &&event, Milliseconds e_time, bool set_addtime=true)
void AddEventAtOffset(T &&event, Milliseconds offset, Milliseconds offset2)
EventProcessor & operator=(EventProcessor const &)=delete
std::multimap< uint64, BasicEvent * > const & GetEvents() const
void AddEventAtOffset(BasicEvent *event, Milliseconds offset)
EventProcessor(EventProcessor &&)=delete
void AddEventAtOffset(BasicEvent *event, Milliseconds offset, Milliseconds offset2)
Milliseconds CalculateTime(Milliseconds t_offset) const
LambdaBasicEvent(T &&callback)
bool Execute(uint64, uint32) override
STL namespace.