-
Notifications
You must be signed in to change notification settings - Fork 7
/
Utils--CallbackList.html
325 lines (270 loc) · 13.9 KB
/
Utils--CallbackList.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Utils::CallbackList - Callback functions holder : OpenKore source code documentation</title>
<link rel="stylesheet" type="text/css" href="openkore.css">
<link rel="stylesheet" type="text/css" href="highlight.css">
<!-- Fix broken PNG transparency for IE/Win5-6+ -->
<!--[if gte IE 5.5000]>
<script type="text/javascript" src="pngfix.js"></script>
<![endif]-->
</head>
<body>
<div id="title">OpenKore source code documentation</div>
<div id="navigation">
<ul>
<li><a href="http://www.openkore.com/">Main website</a></li>
<li><a href="index.html">Table of contents</a></li>
<li><b>Utils::CallbackList</b></li>
</ul>
</div>
<div id="main">
<h1>Utils::CallbackList - Callback functions holder</h1>
<h3>What is an event?</h3>
An event in is a way for a class to provide notifications to clients of
that class when some interesting thing happens to an object. The most
familiar use for events is in graphical user interfaces; typically, the
classes that represent controls in the interface have events that are
notified when the user does something to the control (for example, click
a button).
<p>
An event is the outcome of an action. There are two important
terms with respect to events. The <i>event source</i> and the <i>event
receiver</i>. The object that raises the event is called event source and
the object or callback function that responds to the event is called event receiver.
The communication channel between an event source and an event receiver is
this class, CallbackList.<br>
<small>(Explanation taken from
<a href="http://www.c-sharpcorner.com/UploadFile/sksaha/EventsinNet11152005043514AM/EventsinNet.aspx">CSharpCorner</a>
and slightly modified.</small>
<p>
A CallbackList is used to implement OpenKore's event model.
<p>
<h3>Example 1: How it works</h3>
Here's a simple example, which demonstrates how CallbackList actually works.
<pre class="example">
<span class="hl kwc">my</span> <span class="hl kwb">$cl</span> <span class="hl sym">=</span> new <span class="hl kwd">CallbackList</span><span class="hl sym">(</span><span class="hl str">"my l33t event name"</span><span class="hl sym">);</span>
<span class="hl kwb">$cl</span><span class="hl sym">-></span><span class="hl kwd">add</span><span class="hl sym">(</span>undef<span class="hl sym">,</span> \<span class="hl sym">&</span>myCallback<span class="hl sym">);</span>
<span class="hl kwb">$cl</span><span class="hl sym">-></span><span class="hl kwd">call</span><span class="hl sym">();</span> <span class="hl slc"># myCallback() will now be called.</span>
<span class="hl kwa">sub</span> myCallback <span class="hl sym">{</span>
<span class="hl kwc">print</span> <span class="hl str">"I have been called!</span><span class="hl esc">\n</span><span class="hl str">"</span><span class="hl sym">;</span>
<span class="hl sym">}</span>
</pre>
<p>
<h3>Example 2: Simple usage - adding functions as callbacks</h3>
The <a href="ActorList.html"><code>ActorList</code></a> class is a well-known class which supports events.
Whenever you add an Actor to an ActorList, an <b>onAdd</b> event is triggered.
This example shows you how to respond to an onAdd event.
<p>
This also demonstrates how <a href="Utils--CallbackList.html#$CallbackList->add"><code>$CallbackList->add()</code></a> and <a href="Utils--CallbackList.html#$CallbackList->remove"><code>$CallbackList->remove()</code></a>
work.
<pre class="example">
<span class="hl kwc">my</span> <span class="hl kwb">$monstersList</span> <span class="hl sym">=</span> new <span class="hl kwd">ActorList</span><span class="hl sym">(</span><span class="hl str">"Actor::Monster"</span><span class="hl sym">);</span>
<span class="hl kwc">my</span> <span class="hl kwb">$callbackID</span> <span class="hl sym">=</span> <span class="hl kwb">$monstersList</span><span class="hl sym">-></span>onAdd<span class="hl sym">-></span><span class="hl kwd">add</span><span class="hl sym">(</span>undef<span class="hl sym">,</span> \<span class="hl sym">&</span>monsterAdded<span class="hl sym">);</span>
<span class="hl slc"># monsterAdded() will now be called.</span>
<span class="hl kwb">$monstersList</span><span class="hl sym">-></span><span class="hl kwd">add</span><span class="hl sym">(</span>new Actor<span class="hl sym">::</span><span class="hl kwd">Monster</span><span class="hl sym">());</span>
<span class="hl slc"># Unregister the previously registred callback.</span>
<span class="hl kwb">$monstersList</span><span class="hl sym">-></span>onAdd<span class="hl sym">-></span><span class="hl kwd">remove</span><span class="hl sym">(</span><span class="hl kwb">$callbackID</span><span class="hl sym">);</span>
<span class="hl slc"># monsterAdded() will NOT be called.</span>
<span class="hl kwb">$monstersList</span><span class="hl sym">-></span><span class="hl kwd">add</span><span class="hl sym">(</span>new Actor<span class="hl sym">::</span><span class="hl kwd">Monster</span><span class="hl sym">());</span>
<span class="hl kwa">sub</span> monsterAdded <span class="hl sym">{</span>
<span class="hl kwc">print</span> <span class="hl str">"A monster has been added to the monsters list!</span><span class="hl esc">\n</span><span class="hl str">"</span><span class="hl sym">;</span>
<span class="hl sym">}</span>
</pre>
<p><table class="functionIndex">
<tr><th colspan="3">Class CallbackList</th></tr><tr onclick="location.href='#CallbackList->new';">
<td class="return-type"><a href="Utils--CallbackList.html">CallbackList</a></td>
<td class="func"><a href="#CallbackList->new">CallbackList->new</a></td>
<td class="decl">()</td>
</tr><tr onclick="location.href='#$CallbackList->add';">
<td class="return-type">Scalar</td>
<td class="func"><a href="#$CallbackList->add">$CallbackList->add</a></td>
<td class="decl">(<span class="type">Object</span> object, function, userData)</td>
</tr><tr onclick="location.href='#$CallbackList->call';">
<td class="return-type">void</td>
<td class="func"><a href="#$CallbackList->call">$CallbackList->call</a></td>
<td class="decl">(<span class="type">Scalar</span> source, [argument])</td>
</tr><tr onclick="location.href='#$CallbackList->checkValidity';">
<td class="return-type">void</td>
<td class="func"><a href="#$CallbackList->checkValidity">$CallbackList->checkValidity</a></td>
<td class="decl">()</td>
</tr><tr onclick="location.href='#$CallbackList->deepCopy';">
<td class="return-type"><a href="Utils--CallbackList.html">CallbackList</a></td>
<td class="func"><a href="#$CallbackList->deepCopy">$CallbackList->deepCopy</a></td>
<td class="decl">()</td>
</tr><tr onclick="location.href='#$CallbackList->empty';">
<td class="return-type">boolean</td>
<td class="func"><a href="#$CallbackList->empty">$CallbackList->empty</a></td>
<td class="decl">()</td>
</tr><tr onclick="location.href='#$CallbackList->remove';">
<td class="return-type">void</td>
<td class="func"><a href="#$CallbackList->remove">$CallbackList->remove</a></td>
<td class="decl">(ID)</td>
</tr><tr onclick="location.href='#$CallbackList->size';">
<td class="return-type">int</td>
<td class="func"><a href="#$CallbackList->size">$CallbackList->size</a></td>
<td class="decl">()</td>
</tr>
</table>
<p><hr class="details_sep">
<h2>Details</h2>
<div class="details">
<p>
<div class="function"><a name="$CallbackList->add"></a>
<h3>$CallbackList->add</h3>
<dl>
<dt class="decl">
<span class="return-type"> Scalar</span> <strong>$CallbackList->add</strong>(<span class="type">Object</span> object, function, userData)
</dt>
<dd>
<dl class="params_and_returns">
<dt class="params"><strong>Parameters:</strong></dt>
<dd class="param"><code>object</code> : An object to pass to the callback function, as the first argument. May be undef.</dd>
<dd class="param"><code>function</code> : A callback function.</dd>
<dd class="param"><code>userData</code> : A user data argument to pass to the callback function when it's called. May be undef.</dd>
<dt class="requires"><strong>Requires:</strong></dt>
<dd class="requires">defined($function)</dd>
<dt class="ensures"><strong>Ensures:</strong></dt>
<dd class="ensures">defined(result)</dd>
<dt class="returns"><strong>Returns:</strong></dt>
<dd class="returns">An ID, which can be used by remove() to remove this callback from the list.</dd>
</dl><p>
<div class="desc">Add a new callback function to this CallbackList. See <a href="Utils--CallbackList.html#$CallbackList->call"><code>$CallbackList->call()</code></a>
for information about how <code>$function</code> will be called.
<p>
CallbackList will internally hold a weak reference to <code>$object</code>,
so there will be no garbage collection problems if this <code>$CallbackList</code> is
a member of <code>$object</code>.</div>
</dd>
</dl>
</div>
<p><hr class="function_sep"><p>
<div class="function"><a name="$CallbackList->call"></a>
<h3>$CallbackList->call</h3>
<dl>
<dt class="decl">
<span class="return-type"> void</span> <strong>$CallbackList->call</strong>(<span class="type">Scalar</span> source, [argument])
</dt>
<dd>
<dl class="params_and_returns">
<dt class="params"><strong>Parameters:</strong></dt>
<dd class="param"><code>source</code> : The object which emitted this event.</dd>
</dl><p>
<div class="desc">Call all callback functions in this CallbackList. Each function
<code>$function</code> will be called as follows:
<pre>
<span class="hl kwb">$function</span><span class="hl sym">->(</span><span class="hl kwb">$object</span><span class="hl sym">,</span> <span class="hl kwb">$source</span><span class="hl sym">,</span> <span class="hl kwb">$argument</span><span class="hl sym">,</span> <span class="hl kwb">$userData</span><span class="hl sym">);</span>
</pre>
<ul>
<li>$object and <code>$userData</code> are the arguments passed to <a href="Utils--CallbackList.html#$CallbackList->add"><code>$CallbackList->add()</code></a></li>
<li>$list is this CallbackList.</li>
<li>$source and <code>$argument</code> are the parameters passed to this method.
</li>
</ul></div>
</dd>
</dl>
</div>
<p><hr class="function_sep"><p>
<div class="function"><a name="$CallbackList->checkValidity"></a>
<h3>$CallbackList->checkValidity</h3>
<dl>
<dt class="decl">
<span class="return-type"> void</span> <strong>$CallbackList->checkValidity</strong>()
</dt>
<dd>
<div class="desc">Check whether all internal invariants are true.</div>
</dd>
</dl>
</div>
<p><hr class="function_sep"><p>
<div class="function"><a name="$CallbackList->deepCopy"></a>
<h3>$CallbackList->deepCopy</h3>
<dl>
<dt class="decl">
<span class="return-type"> <a href="Utils--CallbackList.html">CallbackList</a></span> <strong>$CallbackList->deepCopy</strong>()
</dt>
<dd>
<dl class="params_and_returns">
<dt class="ensures"><strong>Ensures:</strong></dt>
<dd class="ensures">defined(result)</dd>
</dl><p>
<div class="desc">Create a deep copy of this CallbackList.</div>
</dd>
</dl>
</div>
<p><hr class="function_sep"><p>
<div class="function"><a name="$CallbackList->empty"></a>
<h3>$CallbackList->empty</h3>
<dl>
<dt class="decl">
<span class="return-type"> boolean</span> <strong>$CallbackList->empty</strong>()
</dt>
<dd>
<div class="desc">Check whether there are any callbacks in this CallbackList.</div>
</dd>
</dl>
</div>
<p><hr class="function_sep"><p>
<div class="function"><a name="$CallbackList->remove"></a>
<h3>$CallbackList->remove</h3>
<dl>
<dt class="decl">
<span class="return-type"> void</span> <strong>$CallbackList->remove</strong>(ID)
</dt>
<dd>
<dl class="params_and_returns">
<dt class="params"><strong>Parameters:</strong></dt>
<dd class="param"><code>ID</code> : An ID, as returned by <a href="Utils--CallbackList.html#$CallbackList->add"><code>$CallbackList->add()</code></a></dd>
<dt class="requires"><strong>Requires:</strong></dt>
<dd class="requires">defined($ID)</dd>
</dl><p>
<div class="desc">Removes a callback from this CallbackList.</div>
</dd>
</dl>
</div>
<p><hr class="function_sep"><p>
<div class="function"><a name="$CallbackList->size"></a>
<h3>$CallbackList->size</h3>
<dl>
<dt class="decl">
<span class="return-type"> int</span> <strong>$CallbackList->size</strong>()
</dt>
<dd>
<dl class="params_and_returns">
<dt class="ensures"><strong>Ensures:</strong></dt>
<dd class="ensures">result >= 0</dd>
</dl><p>
<div class="desc">Returns the number of registered callbacks in this list.</div>
</dd>
</dl>
</div>
<p><hr class="function_sep"><p>
<div class="function"><a name="CallbackList->new"></a>
<h3>CallbackList->new</h3>
<dl>
<dt class="decl">
<span class="return-type"> <a href="Utils--CallbackList.html">CallbackList</a></span> <strong>CallbackList->new</strong>()
</dt>
<dd>
<dl class="params_and_returns">
<dt class="ensures"><strong>Ensures:</strong></dt>
<dd class="ensures"> <code>$self->size()</code> == 0</dd>
</dl><p>
<div class="desc">Create a new CallbackList object.</div>
</dd>
</dl>
</div>
</div>
<p><hr><p>
<div id="footer">
<ul>
<li><a href="http://validator.w3.org/check?uri=referer" title="Valid HTML 4.01!"><img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" height="31" width="88"></a></li>
<li><a href="http://www.mozilla.com/" title="Get Firefox - Take Back the Web"><img width="104" height="32" src="http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png" alt="Get Firefox - Take Back the Web"></a></li>
<li><a href="http://www.mozilla.com/" title="If you were looking at this page in any browser but Microsoft Internet Explorer, it would look and run better and faster"><img width="45" height="45" src="http://linuxart.com/img/noIE-small.png" alt="If you were looking at this page in any browser but Microsoft Internet Explorer, it would look and run better and faster"></a></li>
</ul>
Last modified: Fri Nov 16 10:05:11 2012
</div>
</div>
</body>
</html>