My favorites | Sign in
Project Home Source
Checkout   Browse   Changes    
 
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Squared.Util.Event {
public struct EventFilter {
public readonly WeakReference WeakSource;
public readonly object StrongSource;
public readonly int SourceHashCode;
public readonly string Type;
public readonly int TypeHashCode;

public EventFilter (object source, string type, bool weak) {
if (source == null)
throw new ArgumentNullException("source");
if (type == null)
throw new ArgumentNullException("type");

if (weak) {
WeakSource = new WeakReference(source);
StrongSource = null;
} else {
WeakSource = null;
StrongSource = source;
}
SourceHashCode = source.GetHashCode();
Type = type;
TypeHashCode = type.GetHashCode();
}

public object Source {
get {
if (StrongSource != null)
return StrongSource;
else
return WeakSource.Target;
}
}

public override int GetHashCode () {
return SourceHashCode ^ TypeHashCode;
}

public override bool Equals (object obj) {
if (obj is EventFilter) {
var ef = (EventFilter)obj;
return this.Equals(ef);
} else
return base.Equals(obj);
}

public bool Equals (EventFilter rhs) {
return (SourceHashCode == rhs.SourceHashCode) &&
(TypeHashCode == rhs.TypeHashCode) &&
(Type == rhs.Type) &&
(Source == rhs.Source);
}
}

public class EventFilterComparer : IEqualityComparer<EventFilter> {
public bool Equals (EventFilter x, EventFilter y) {
if ((x.SourceHashCode != y.SourceHashCode) || (x.TypeHashCode != y.TypeHashCode) || (x.Type != y.Type))
return false;

return (x.Source == y.Source);
}

public int GetHashCode (EventFilter obj) {
return obj.SourceHashCode ^ obj.TypeHashCode;
}
}

public class EventInfo {
public readonly EventBus Bus;
public readonly object Source;
public readonly EventCategoryToken Category;
public readonly string CategoryName;
public readonly string Type;
public readonly object Arguments;

private bool _IsConsumed;

public bool IsConsumed {
get {
return _IsConsumed;
}
}

public void Consume () {
_IsConsumed = true;
}

public EventInfo (EventBus bus, object source, EventCategoryToken categoryToken, string categoryName, string type, object arguments) {
Bus = bus;
Source = source;
Category = categoryToken;
CategoryName = categoryName;
Type = type;
Arguments = arguments;
_IsConsumed = false;
}
}

public delegate void EventSubscriber (EventInfo e);
public delegate void TypedEventSubscriber<in T> (EventInfo e, T arguments) where T : class;

public class EventSubscriberList : List<EventSubscriber> {
}

public struct EventSubscription : IDisposable {
public readonly EventBus EventBus;
public readonly EventSubscriber EventSubscriber;

private EventFilter _EventFilter;

public EventFilter EventFilter {
get {
return _EventFilter;
}
}

public EventSubscription (EventBus eventBus, ref EventFilter eventFilter, EventSubscriber subscriber) {
EventBus = eventBus;
_EventFilter = eventFilter;
EventSubscriber = subscriber;
}

public override int GetHashCode () {
return EventBus.GetHashCode() ^ EventSubscriber.GetHashCode() ^ _EventFilter.GetHashCode();
}

public override bool Equals (object obj) {
if (obj is EventSubscription) {
var es = (EventSubscription)obj;
return this.Equals(es);
} else
return base.Equals(obj);
}

public bool Equals (EventSubscription rhs) {
return (EventBus == rhs.EventBus) &&
(EventSubscriber == rhs.EventSubscriber) &&
(_EventFilter.Equals(rhs._EventFilter));
}

public void Dispose () {
if ((EventBus != null) && (EventSubscriber != null))
EventBus.Unsubscribe(ref _EventFilter, EventSubscriber);
}
}

public struct EventThunk {
public readonly EventBus EventBus;
public readonly object Source;
public readonly string Type;

public EventThunk (EventBus eventBus, object source, string type) {
EventBus = eventBus;
Source = source;
Type = type;
}

public void Broadcast (object arguments) {
EventBus.Broadcast(Source, Type, arguments);
}

public void Broadcast () {
EventBus.Broadcast(Source, Type, null);
}

public EventSubscription Subscribe (EventSubscriber subscriber) {
return EventBus.Subscribe(Source, Type, subscriber);
}

public EventSubscription Subscribe<T> (TypedEventSubscriber<T> subscriber)
where T : class {
return EventBus.Subscribe<T>(Source, Type, subscriber);
}
}

public interface IEventSource {
string CategoryName {
get;
}
}

public class EventCategoryToken {
public readonly string Name;

public EventCategoryToken (string name) {
Name = name;
}

public override int GetHashCode () {
return Name.GetHashCode();
}
}

public class EventBus : IDisposable {
public struct CategoryCollection {
public readonly EventBus EventBus;

public CategoryCollection (EventBus eventBus) {
EventBus = eventBus;
}

public EventCategoryToken this [string categoryName] {
get {
return EventBus.GetCategory(categoryName);
}
}
}

public static readonly object AnySource = "<Any Source>";
public static readonly string AnyType = "<Any Type>";

private Dictionary<string, EventCategoryToken> _Categories =
new Dictionary<string, EventCategoryToken>();

private Dictionary<EventFilter, EventSubscriberList> _Subscribers =
new Dictionary<EventFilter, EventSubscriberList>(new EventFilterComparer());

private static void CreateFilter (object source, string type, out EventFilter filter, bool weak) {
filter = new EventFilter(source ?? AnySource, type ?? AnyType, weak);
}

public EventSubscription Subscribe (object source, string type, EventSubscriber subscriber) {
EventFilter filter;
CreateFilter(source, type, out filter, true);

EventSubscriberList subscribers;
if (!_Subscribers.TryGetValue(filter, out subscribers)) {
subscribers = new EventSubscriberList();
_Subscribers[filter] = subscribers;
}

subscribers.Add(subscriber);

return new EventSubscription(this, ref filter, subscriber);
}

private EventCategoryToken GetCategory (string name) {
EventCategoryToken result;

if (!_Categories.TryGetValue(name, out result)) {
result = new EventCategoryToken(name);
_Categories[name] = result;
}

return result;
}

public CategoryCollection Categories {
get {
return new CategoryCollection(this);
}
}

public EventSubscription Subscribe<T> (object source, string type, TypedEventSubscriber<T> subscriber)
where T : class {
return Subscribe(source, type, (e) => {
var args = e.Arguments as T;
if (args != null)
subscriber(e, args);
});
}

public bool Unsubscribe (object source, string type, EventSubscriber subscriber) {
EventFilter filter;
CreateFilter(source, type, out filter, false);
return Unsubscribe(ref filter, subscriber);
}

public bool Unsubscribe (ref EventFilter filter, EventSubscriber subscriber) {
EventSubscriberList subscribers;
if (_Subscribers.TryGetValue(filter, out subscribers))
return subscribers.Remove(subscriber);

return false;
}

public void Broadcast (object source, string type, object arguments) {
if (source == null)
throw new ArgumentNullException("source");
if (type == null)
throw new ArgumentNullException("type");

EventInfo info = null;
EventSubscriberList subscribers;
EventFilter filter;
EventCategoryToken categoryToken = null;
string categoryName = null;

IEventSource iSource = source as IEventSource;
if (iSource != null) {
categoryName = iSource.CategoryName;
categoryToken = GetCategory(categoryName);
}

for (int i = 0; i < 6; i++) {
string typeFilter = (i & 1) == 1 ? type : AnyType;
object sourceFilter;

switch (i) {
case 0:
case 1:
sourceFilter = AnySource;
break;
case 2:
case 3:
sourceFilter = categoryToken;
break;
default:
sourceFilter = source;
break;
}

if ((sourceFilter == null) || (typeFilter == null))
continue;

CreateFilter(
sourceFilter,
typeFilter,
out filter,
false
);

if (!_Subscribers.TryGetValue(filter, out subscribers))
continue;

int count = subscribers.Count;
if (count <= 0)
continue;

if (info == null)
info = new EventInfo(this, source, categoryToken, categoryName, type, arguments);

using (var b = BufferPool<EventSubscriber>.Allocate(count)) {
var temp = b.Data;
subscribers.CopyTo(temp);

for (int j = count - 1; j >= 0; j--) {
temp[j](info);

if (info.IsConsumed)
return;
}
}
}
}

public int Compact () {
int result = 0;
var keys = new EventFilter[_Subscribers.Count];
_Subscribers.Keys.CopyTo(keys, 0);

foreach (var ef in keys) {
if (!(ef.WeakSource.IsAlive)) {
_Subscribers.Remove(ef);
result += 1;
}
}

return result;
}

public EventThunk GetThunk (object sender, string type) {
return new EventThunk(this, sender, type);
}

public void Dispose () {
_Subscribers.Clear();
}
}
}

Change log

r422 by kevin.gadd on Mar 18, 2011   Diff
More code cleanup
Go to: 
Project members, sign in to write a code review

Older revisions

r420 by kevin.gadd on Mar 18, 2011   Diff
Code cleanups and dead code removal
r388 by kevin.gadd on Nov 30, 2010   Diff
Expose ConnectionWrapper's
TaskScheduler
Add new utility Schedulable named
RunAsBackground along with a test (it
lets you wait for the completion of a
...
r300 by kevin.gadd on Aug 6, 2009   Diff
Don't create garbage when
unsubscribing from events or
broadcasting events
All revisions of this file

File info

Size: 12289 bytes, 376 lines
Powered by Google Project Hosting