Isle
Loading...
Searching...
No Matches
mxnotificationmanager.cpp
Go to the documentation of this file.
2
3#include "compat.h"
4#include "decomp.h"
5#include "mxautolock.h"
6#include "mxmisc.h"
8#include "mxparam.h"
9#include "mxticklemanager.h"
10#include "mxtypes.h"
11
14
15// FUNCTION: LEGO1 0x100ac220
17{
18 m_target = p_target;
19 m_param = p_param.Clone();
20}
21
22// FUNCTION: LEGO1 0x100ac240
24{
25 delete m_param;
26}
27
28// FUNCTION: LEGO1 0x100ac250
29// FUNCTION: BETA10 0x10125805
31{
32 m_unk0x2c = 0;
33 m_queue = NULL;
34 m_active = TRUE;
35 m_sendList = NULL;
36}
37
38// FUNCTION: LEGO1 0x100ac450
40{
41 AUTOLOCK(m_lock);
42 Tickle();
43 delete m_queue;
44 m_queue = NULL;
45
47}
48
49// FUNCTION: LEGO1 0x100ac600
50MxResult MxNotificationManager::Create(MxU32 p_frequencyMS, MxBool p_createThread)
51{
52 MxResult result = SUCCESS;
53 m_queue = new MxNotificationPtrList();
54
55 if (m_queue == NULL) {
56 result = FAILURE;
57 }
58 else {
59 TickleManager()->RegisterClient(this, 10);
60 }
61
62 return result;
63}
64
65// FUNCTION: LEGO1 0x100ac6c0
66// FUNCTION: BETA10 0x10125b57
68{
69 AUTOLOCK(m_lock);
70
71 if (!m_active) {
72 return FAILURE;
73 }
74
75 MxIdList::iterator it = find(m_listenerIds.begin(), m_listenerIds.end(), p_listener->GetId());
76 if (it == m_listenerIds.end()) {
77 return FAILURE;
78 }
79
80 MxNotification* notif = new MxNotification(p_listener, p_param);
81 if (notif != NULL) {
82 m_queue->push_back(notif);
83 return SUCCESS;
84 }
85
86 return FAILURE;
87}
88
89// FUNCTION: LEGO1 0x100ac800
91{
92 m_sendList = new MxNotificationPtrList();
93 if (m_sendList == NULL) {
94 return FAILURE;
95 }
96 else {
97 {
98 AUTOLOCK(m_lock);
99 MxNotificationPtrList* temp1 = m_queue;
100 MxNotificationPtrList* temp2 = m_sendList;
101 m_queue = temp2;
102 m_sendList = temp1;
103 }
104
105 while (m_sendList->size() != 0) {
106 MxNotification* notif = m_sendList->front();
107 m_sendList->pop_front();
108 notif->GetTarget()->Notify(*notif->GetParam());
109 delete notif;
110 }
111
112 delete m_sendList;
113 m_sendList = NULL;
114 return SUCCESS;
115 }
116}
117
118// FUNCTION: LEGO1 0x100ac990
119void MxNotificationManager::FlushPending(MxCore* p_listener)
120{
121 MxNotificationPtrList pending;
122 MxNotification* notif;
123
124 {
125 AUTOLOCK(m_lock);
126
127 // Find all notifications from, and addressed to, p_listener.
128 if (m_sendList != NULL) {
129 MxNotificationPtrList::iterator it = m_sendList->begin();
130 while (it != m_sendList->end()) {
131 notif = *it;
132 if (notif->GetTarget()->GetId() == p_listener->GetId() ||
133 (notif->GetParam()->GetSender() && notif->GetParam()->GetSender()->GetId() == p_listener->GetId()
134 )) {
135 m_sendList->erase(it++);
136 pending.push_back(notif);
137 }
138 else {
139 it++;
140 }
141 }
142 }
143
144 MxNotificationPtrList::iterator it = m_queue->begin();
145 while (it != m_queue->end()) {
146 notif = *it;
147 if (notif->GetTarget()->GetId() == p_listener->GetId() ||
148 (notif->GetParam()->GetSender() && notif->GetParam()->GetSender()->GetId() == p_listener->GetId())) {
149 m_queue->erase(it++);
150 pending.push_back(notif);
151 }
152 else {
153 it++;
154 }
155 }
156 }
157
158 // Deliver those notifications.
159 while (pending.size() != 0) {
160 notif = pending.front();
161 pending.pop_front();
162 notif->GetTarget()->Notify(*notif->GetParam());
163 delete notif;
164 }
165}
166
167// FUNCTION: LEGO1 0x100acd20
169{
170 AUTOLOCK(m_lock);
171
172 MxIdList::iterator it = find(m_listenerIds.begin(), m_listenerIds.end(), p_listener->GetId());
173 if (it != m_listenerIds.end()) {
174 return;
175 }
176
177 m_listenerIds.push_back(p_listener->GetId());
178}
179
180// FUNCTION: LEGO1 0x100acdf0
181// FUNCTION: BETA10 0x10126785
183{
184 AUTOLOCK(m_lock);
185
186 MxIdList::iterator it = find(m_listenerIds.begin(), m_listenerIds.end(), p_listener->GetId());
187
188 if (it != m_listenerIds.end()) {
189 m_listenerIds.erase(it);
190 FlushPending(p_listener);
191 }
192}
[AI] Base virtual class for all Mindscape engine (Mx) objects.
Definition: mxcore.h:15
virtual MxLong Notify(MxParam &p_param)
[AI] Virtual callback notification mechanism.
Definition: mxcore.cpp:25
MxU32 GetId()
[AI] Gets the unique (per-process) id assigned to this object instance.
Definition: mxcore.h:52
[AI] Central registry and dispatcher of asynchronous notifications between MxCore objects.
MxNotificationManager()
[AI] Constructs a new NotificationManager, initializing all state to defaults.
MxResult Tickle() override
[AI] Processes and dispatches all queued notifications to their targets.
~MxNotificationManager() override
[AI] Destroys the NotificationManager.
void Unregister(MxCore *p_listener)
[AI] Removes a previously registered listener and flushes any pending notifications for it.
void Register(MxCore *p_listener)
[AI] Registers a listener object to receive notifications.
MxResult Send(MxCore *p_listener, const MxNotificationParam &p_param)
[AI] Queues a notification to be sent to a specific registered listener.
virtual MxResult Create(MxU32 p_frequencyMS, MxBool p_createThread)
[AI] Initializes the notification manager, setting up queues and optionally registering with the tick...
[AI] Parameter object representing a single notification or event, carrying an identifier and sender ...
MxCore * GetSender() const
[AI] Retrieves the sender pointer associated with this notification.
virtual MxNotificationParam * Clone() const
[AI] Creates a copy of this notification parameter object on the heap.
[AI] List of notification pointers used to queue notifications for delivery.
[AI] Carries a notification to be sent to a target MxCore object.
MxCore * GetTarget()
[AI] Returns the target of this notification.
MxNotification(MxCore *p_target, const MxNotificationParam &p_param)
[AI] Constructor for MxNotification.
~MxNotification()
[AI] Destructor for MxNotification.
MxNotificationParam * GetParam()
[AI] Returns the notification parameter (cloned by the notification).
virtual void UnregisterClient(MxCore *p_client)
[AI] Unregisters (marks for destruction) a previously registered client.
virtual void RegisterClient(MxCore *p_client, MxTime p_interval)
[AI] Registers an MxCore object to receive periodic tickles.
#define TRUE
Definition: d3drmdef.h:28
#define DECOMP_SIZE_ASSERT(T, S)
Definition: decomp.h:19
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
#define FAILURE
[AI] Used to indicate a failed operation in result codes.
Definition: legotypes.h:34
#define SUCCESS
[AI] Used to indicate a successful operation in result codes.
Definition: legotypes.h:30
#define AUTOLOCK(CS)
[AI] Macro for automatic locking using the MxAutoLock class. This macro instantiates an MxAutoLock ob...
Definition: mxautolock.h:5
MxTickleManager * TickleManager()
[AI] Provides access to the global tickle manager.
Definition: mxmisc.cpp:25
MxU8 MxBool
[AI]
Definition: mxtypes.h:124
MxLong MxResult
[AI]
Definition: mxtypes.h:106
unsigned int MxU32
[AI]
Definition: mxtypes.h:32