Mercurial > projects > dynamin
comparison dynamin/core/event.d @ 79:e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 06 Feb 2010 15:45:53 -0600 |
parents | 651082a9b364 |
children | 73060bc3f004 |
comparison
equal
deleted
inserted
replaced
78:651082a9b364 | 79:e7595d58f8a3 |
---|---|
68 public alias void delegate(ArgsType e) Handler; | 68 public alias void delegate(ArgsType e) Handler; |
69 /// void delegate(ArgsType e) | 69 /// void delegate(ArgsType e) |
70 public alias void delegate(ArgsType e) Dispatcher; | 70 public alias void delegate(ArgsType e) Dispatcher; |
71 | 71 |
72 Handler[] handlers; | 72 Handler[] handlers; |
73 /// | 73 private void* ptr; |
74 private Handler mainHandler; | 74 private void function(ArgsType e) mainHandler; |
75 /// | 75 private void function(ArgsType e) dispatcher; |
76 private Dispatcher dispatcher; | 76 |
77 void setUp(Handler mainHandler, Dispatcher dispatcher = null) { | 77 void setUp(Handler mainHandler, Dispatcher dispatcher = null) { |
78 this.mainHandler = mainHandler; | 78 if(mainHandler.ptr != dispatcher.ptr && dispatcher.ptr != null) |
79 this.dispatcher = dispatcher; | 79 throw new Exception("mainHandler.ptr must equal dispatcher.ptr"); |
80 } | 80 ptr = mainHandler.ptr; |
81 this.mainHandler = mainHandler.funcptr; | |
82 this.dispatcher = dispatcher.funcptr; | |
83 } | |
84 | |
81 void defaultDispatch(ArgsType e) { | 85 void defaultDispatch(ArgsType e) { |
82 callHandlers(e); | 86 callHandlers(e); |
83 callMainHandler(e); | 87 callMainHandler(e); |
84 } | 88 } |
85 | 89 |
87 * Calls all the handlers added to this event, passing e to them. | 91 * Calls all the handlers added to this event, passing e to them. |
88 */ | 92 */ |
89 void opCall(ArgsType e) { | 93 void opCall(ArgsType e) { |
90 if(e is null) | 94 if(e is null) |
91 Stdout("Warning: EventArgs null").newline; | 95 Stdout("Warning: EventArgs null").newline; |
92 if(!dispatcher.funcptr) | 96 if(!dispatcher) { |
93 defaultDispatch(e); | 97 defaultDispatch(e); |
94 else | 98 return; |
95 dispatcher(e); | 99 } |
100 Dispatcher dg; | |
101 dg.ptr = ptr; | |
102 dg.funcptr = dispatcher; | |
103 dg(e); | |
96 } | 104 } |
97 /** | 105 /** |
98 * Adds the specified handler to this event. The handler will be called | 106 * Adds the specified handler to this event. The handler will be called |
99 * when the event is fired. | 107 * when the event is fired. |
100 */ | 108 */ |
135 * Only use this method from a method that does custom dispatching. | 143 * Only use this method from a method that does custom dispatching. |
136 */ | 144 */ |
137 void callMainHandler(ArgsType e) { | 145 void callMainHandler(ArgsType e) { |
138 auto stopEventArgs = cast(StopEventArgs)e; | 146 auto stopEventArgs = cast(StopEventArgs)e; |
139 // if e is an instance of StopEventArgs, then check if it is stopped | 147 // if e is an instance of StopEventArgs, then check if it is stopped |
140 if(stopEventArgs is null || !stopEventArgs.stopped) | 148 if(stopEventArgs is null || !stopEventArgs.stopped) { |
141 mainHandler(e); | 149 Handler dg; |
150 dg.ptr = ptr; | |
151 dg.funcptr = mainHandler; | |
152 dg(e); | |
153 } | |
142 } | 154 } |
143 } | 155 } |
144 | 156 |
145 // usage: mixin Event!(whenMoved) moved; | 157 // usage: mixin Event!(whenMoved) moved; |
146 // mixin Event!(whenMoved, dispatchMoved) moved; | 158 // mixin Event!(whenMoved, dispatchMoved) moved; |