soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SEventSet.cpp
1#include "souistd.h"
2#include <event/SEventSet.h>
3
4SNSBEGIN
5//////////////////////////////////////////////////////////////////////////
6// SEvent
7
8SEvent::SEvent(DWORD dwEventID, LPCWSTR pszEventName)
9 : m_dwEventID(dwEventID)
10 , m_strEventName(pszEventName)
11{
12}
13
15{
16 for (UINT i = 0; i < m_evtSlots.GetCount(); i++)
17 {
18 m_evtSlots[i]->Release();
19 }
20 m_evtSlots.RemoveAll();
21}
22
23BOOL SEvent::subscribe(const IEvtSlot *slot)
24{
25 if (findSlotFunctor(slot) != -1)
26 return false;
27 m_evtSlots.Add(slot->Clone());
28 return true;
29}
30
31BOOL SEvent::unsubscribe(const IEvtSlot *slot)
32{
33 int idx = findSlotFunctor(slot);
34 if (idx == -1)
35 return false;
36
37 m_evtSlots[idx]->Release();
38 m_evtSlots.RemoveAt(idx);
39 return true;
40}
41
42int SEvent::findSlotFunctor(const IEvtSlot *slot)
43{
44 for (UINT i = 0; i < m_evtSlots.GetCount(); i++)
45 {
46 if (m_evtSlots[i]->Equal(slot))
47 {
48 return i;
49 }
50 }
51 return -1;
52}
53
54void SEvent::fire(IEvtArgs *args)
55{
56 // execute all subscribers, updating the 'handled' state as we go
57 int slots = (int)m_evtSlots.GetCount();
58 if (slots == 0)
59 return;
60 SAutoRefPtr<IEvtSlot> *evtSlots = new SAutoRefPtr<IEvtSlot>[slots];
61 for (int i = 0; i < slots; i++)
62 {
63 evtSlots[i] = m_evtSlots[i];
64 }
65 for (int i = slots - 1; i >= 0; i--)
66 { // the latest event handler handles the event first.
67 BOOL bHandled = evtSlots[i]->Run(args);
68 if (bHandled)
69 {
70 args->IncreaseHandleCount();
71 if (!args->IsBubbleUp())
72 break;
73 }
74 }
75 delete[] evtSlots;
76}
77
78void SEvent::SetScriptHandler(const SStringA &strScriptHandler)
79{
80 m_strScriptHandler = strScriptHandler;
81}
82
87
89{
90 return m_strEventName;
91}
92
94{
95 return m_dwEventID;
96}
97
98//////////////////////////////////////////////////////////////////////////
99// SEventSet
101 : m_nMuted(0)
102{
103}
104
109
110void SEventSet::setMutedState(BOOL setting)
111{
112 if (setting)
113 m_nMuted++;
114 else
115 m_nMuted--;
116 SASSERT(m_nMuted >= 0);
117}
118
120{
121 for (UINT i = 0; i < m_evtArr.GetCount(); i++)
122 {
123 if (m_evtArr[i]->GetID() == dwEventID)
124 return m_evtArr[i];
125 }
126 return NULL;
127}
128
129void SEventSet::FireEvent(IEvtArgs *args)
130{
131 // find event object
132 SEvent *ev = GetEventObject(args->GetID());
133
134 // fire the event if present and set is not muted
135 if ((ev != 0) && m_nMuted == 0)
136 {
137 ev->fire(args);
138 }
139}
140
141BOOL SEventSet::addEvent(DWORD dwEventID, LPCWSTR pszEventHandlerName)
142{
143 if (isEventPresent(dwEventID))
144 return FALSE;
145 m_evtArr.Add(new SEvent(dwEventID, pszEventHandlerName));
146 return TRUE;
147}
148
149BOOL SEventSet::removeEvent(DWORD dwEventID)
150{
151 for (UINT i = 0; i < m_evtArr.GetCount(); i++)
152 {
153 if (m_evtArr[i]->GetID() == dwEventID)
154 {
155 delete m_evtArr[i];
156 m_evtArr.RemoveAt(i);
157 return TRUE;
158 }
159 }
160 return FALSE;
161}
162
163BOOL SEventSet::isEventPresent(DWORD dwEventID)
164{
165 return GetEventObject(dwEventID) != NULL;
166}
167
169{
170 for (UINT i = 0; i < m_evtArr.GetCount(); i++)
171 {
172 delete m_evtArr[i];
173 }
174 m_evtArr.RemoveAll();
175}
176
177BOOL SEventSet::subscribeEvent(DWORD dwEventID, const IEvtSlot *subscriber)
178{
179 if (!isEventPresent(dwEventID))
180 return false;
181 return GetEventObject(dwEventID)->subscribe(subscriber);
182}
183
184BOOL SEventSet::unsubscribeEvent(DWORD dwEventID, const IEvtSlot *subscriber)
185{
186 if (!isEventPresent(dwEventID))
187 return false;
188 return GetEventObject(dwEventID)->unsubscribe(subscriber);
189}
190
191#if _MSC_VER >= 1700 // VS2012
192BOOL SEventSet::subscribeEvent(DWORD dwEventID, const StdFunCallback &eventCallback)
193{
194 if (!isEventPresent(dwEventID))
195 return false;
196 StdFunctionSlot slot(eventCallback);
197 return GetEventObject(dwEventID)->subscribe(&slot);
198}
199#endif
200
201BOOL SEventSet::setEventScriptHandler(const SStringW &strEventName, const SStringA strScriptHandler)
202{
203 for (UINT i = 0; i < m_evtArr.GetCount(); i++)
204 {
205 if (m_evtArr[i]->GetName() == strEventName)
206 {
207 m_evtArr[i]->SetScriptHandler(strScriptHandler);
208 return true;
209 }
210 }
211 return false;
212}
213
215{
216 for (UINT i = 0; i < m_evtArr.GetCount(); i++)
217 {
218 if (m_evtArr[i]->GetName() == strEventName)
219 {
220 return m_evtArr[i]->GetScriptHandler();
221 }
222 }
223 return "";
224}
225SNSEND
Smart pointer class for managing COM-style reference-counted objects.
表示一个事件对象
Definition SEventSet.h:12
int findSlotFunctor(const IEvtSlot *slot)
查找事件槽对象
Definition SEventSet.cpp:42
SStringA m_strScriptHandler
脚本处理程序字符串
Definition SEventSet.h:80
BOOL unsubscribe(const IEvtSlot *slot)
取消订阅事件
Definition SEventSet.cpp:31
SStringW m_strEventName
事件名称
Definition SEventSet.h:79
BOOL subscribe(const IEvtSlot *slot)
订阅事件
Definition SEventSet.cpp:23
SEvent(DWORD dwEventID, LPCWSTR pszEventName)
构造函数
Definition SEventSet.cpp:8
DWORD GetID()
获取事件ID
Definition SEventSet.cpp:93
void fire(IEvtArgs *args)
触发事件
Definition SEventSet.cpp:54
SArray< IEvtSlot * > m_evtSlots
事件槽数组
Definition SEventSet.h:82
virtual ~SEvent()
析构函数
Definition SEventSet.cpp:14
SStringA GetScriptHandler() const
获取脚本处理程序
Definition SEventSet.cpp:83
SStringW GetName() const
获取事件名称
Definition SEventSet.cpp:88
void SetScriptHandler(const SStringA &strScriptHandler)
设置脚本处理程序
Definition SEventSet.cpp:78
DWORD m_dwEventID
事件ID
Definition SEventSet.h:78
SEvent * GetEventObject(const DWORD dwEventID)
获取事件对象
BOOL subscribeEvent(DWORD dwEventID, const IEvtSlot &subscriber)
订阅事件
Definition SEventSet.h:151
virtual ~SEventSet(void)
析构函数
void FireEvent(IEvtArgs *args)
触发事件
void removeAllEvents(void)
移除所有事件对象
SArray< SEvent * > m_evtArr
事件数组
Definition SEventSet.h:300
int m_nMuted
静音状态计数
Definition SEventSet.h:301
BOOL setEventScriptHandler(const SStringW &strEventName, const SStringA strScriptHandler)
设置事件的脚本处理程序
BOOL isEventPresent(DWORD dwEventID)
检查事件集是否包含指定ID的事件
BOOL unsubscribeEvent(DWORD dwEventID, const IEvtSlot *subscriber)
取消订阅事件
BOOL addEvent(DWORD dwEventID, LPCWSTR pszEventHandlerName)
添加一个新事件到事件集
BOOL removeEvent(DWORD dwEventID)
移除指定ID的事件
void setMutedState(BOOL setting)
设置事件集的静音状态
SStringA getEventScriptHandler(const SStringW &strEventName) const
获取事件的脚本处理程序
SEventSet(void)
构造函数
A class representing an ASCII string.
Definition sstringa.h:96
A class representing an ASCII string.
Definition sstringw.h:96
通过标准函数对象回调的槽函数类
Definition SEventSlot.h:102