changeset 7:1311fae1ca9b

Change template mixin (still unused) to take a dispatcher and clean up code a little.
author Jordan Miner <jminer7@gmail.com>
date Wed, 15 Jul 2009 13:59:16 -0500
parents c41eb8d907b2
children b621b528823d
files dynamin/core/event.d
diffstat 1 files changed, 18 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/dynamin/core/event.d	Sun Jun 28 15:04:38 2009 -0500
+++ b/dynamin/core/event.d	Wed Jul 15 13:59:16 2009 -0500
@@ -28,10 +28,7 @@
 import tango.io.Stdout;
 import dynamin.core.global;
 import tango.core.Exception;
-import tango.core.Traits;
-
-// TODO: mixin Event!(WhenMoved) Moved;
-// TODO: mixin Event!(WhenMoved, DispatchMoved) Moved;
+public import tango.core.Traits;
 
 /**
  * A class used to notify handlers of an event. It is similar to .NET's events.
@@ -111,7 +108,7 @@
 		handlers.length = handlers.length + 1;
 		handlers[length-1] = handler;
 		// TODO: use a list?
-		//handlers.Add(handler);
+		//handlers.add(handler);
 	}
 	/// ditto
 	void opAddAssign(void delegate() handler) {
@@ -157,10 +154,10 @@
 	}
 }
 
-
-public import tango.core.Traits;
-template Event2(alias mainHandler) {
-protected:
+// usage: mixin Event!(whenMoved) moved;
+//        mixin Event!(whenMoved, dispatchMoved) moved;
+template Event2(alias mainHandler, alias dispatcher) {
+private:
 	alias ParameterTupleOf!(mainHandler)[0] ArgsType;
 	/// void delegate(ArgsType e)
 	public alias void delegate(ArgsType e) Handler;
@@ -177,8 +174,7 @@
 	void opCall(ArgsType e) {
 		if(e is null)
 			Stdout("Warning: EventArgs null").newline;
-		callHandlers(e);
-		callMainHandler(e);
+		dispatcher(e);
 	}
 	/**
 	 * Adds the specified handler to this event. The handler will be called
@@ -189,7 +185,7 @@
 		handlers.length = handlers.length + 1;
 		handlers[length-1] = handler;
 		// TODO: use a list?
-		//handlers.Add(handler);
+		//handlers.add(handler);
 	}
 	/// ditto
 	void opAddAssign(void delegate() handler) {
@@ -216,7 +212,7 @@
 			handler(e);
 	}
 	/**
-	 * Calls the main handler unless the StopEventArgs.Stopped has been
+	 * Calls the main handler unless the StopEventArgs.stopped has been
 	 * set to true.
 	 * Only use this method from a method that does custom dispatching.
 	 */
@@ -228,6 +224,15 @@
 	}
 }
 
+template Event2(alias mainHandler) {
+	alias ParameterTupleOf!(mainHandler)[0] ArgsType;
+	void defaultDispatch(ArgsType e) {
+		callHandlers(e);
+		callMainHandler(e);
+	}
+	mixin Event2!(mainHandler, defaultDispatch);
+}
+
 /// The base class for passing arguments to event handlers.
 // TODO: shorter name?
 class EventArgs {