std.pattern.. templated publisher subscriber pattern, adding events to collections

BLS windevguy at hotmail.de
Tue Jul 6 15:01:57 PDT 2010


Hi,
Andrei brings in the idea of std.pattern. Seems that this module is 
"stalled";  Unfortunately !
However I would like to enhance collection classes (likewise 
dcollections)  with a Publisher - Subscriber pattern (signal - slot, or 
observer pattern) , if you prefer)
Hope the idea of enhancing collections with events become clear with the 
following snippet.
---I am able to spend just a few hours a month with D programming.. in 
other words, please don't kill me ;)

Untested Draft code which requires a lot of help and ,more important, 
feedback from you.

struct publisherMsg(T) {
	T data;
	Action a;
}
mixin template Publisher(T) {
private :

	enum Action = {INSERT, UPDATE, DELETE, READONLY};	
	//alias typeof(this) PT;

	struct receiver {
		Object o;
		callback cb;	
		// opCall() or this() ?
	}

	receiver[] subscriber;
		
	publisherMsg!(T) msg;
	
	alias void delegate(ref msg) callback;		
	
	void addSubscriber(object o, callback cb) {
		//subscriber ~= o;
	}	
	
	void publish(T t, Action act) {
		foreach  (object o ; subscriber)
		{
		// create and send message

		}
	}	
}

mixin template Subscriber() {
	// see UndoList
}

final class Stack(T, bool observable = false ) /+ should use alias +/ {
	T[] data;	
	static if (observable)	mixin Observable!T;

	void push(T t) {
		publish(t, Action.INSERT)
	}
	T pop() {

	}	
	bool empty() {
		
	}
	T top() {
		
	}
	size_t size() {

	}
}


/// Undo list will receive every pushed or popped item (T and Action)
class UndoList(T) {
	private:
	T[] data;
	
	// should be part of the subscriber mixin template.
	publisherMsg!T stackMessage;

	void delegate(ref stackMessage) dg;
	
	alias Stack!(int) IntegerStack;
	IntegerStack intstack = new IntegerStack;
	
	private this() {
		dg = &this.feedback;
		
	}
	public void feedback(ref stackMessage msg ) {
		writefln("push or pop action");
	}
		
}
-Bjoern


More information about the Digitalmars-d-learn mailing list