TypeInfo manipulation

Zhenya zheny at list.ru
Sat Oct 27 06:53:46 PDT 2012


I have double dispatcher:

template Dispatcher(R)
{
	R execute(Left,Right)(R delegate(Left,Right) f,Object 
left,Object right)
	{
		return f(cast(Left)left,cast(Right)right);
	}
	struct Dispatcher
	{
		private R 
delegate(Object,Object)[Tuple!(TypeInfo_Class,TypeInfo_Class)] 
m_callbacks;
		void add(Left,Right)(R delegate(Left,Right) f)
		{
			m_callbacks[tuple(Left.classinfo,Right.classinfo)] = (Object 
l,Object r){return execute!(Left,Right)(f,l,r);};
		}
		void remove(Left,Right)()
		{
			m_callbacks.remove(tuple(Left.classinfo,Right.classinfo));
		}
		R opCall(Object l,Object r)
		{
			R delegate(Object,Object)* callback = 
tuple(l.classinfo,r.classinfo) in m_callbacks;
			return (*callback)(l,r);
		}
	}
}

And I have some class ierarhy:

class GameObject
{
}

class Foo : GameObject
{
}

class Bar : GameObject
{
}

problem is to create some wrapper to this classes

struct GameHandle(Type)

and this code work


void main()
{
	Dispatcher!void d;
	d.add!(GameHandle!Foo,GameHandle!Bar)((GameHandle!Foo,GameHandle!Bar){writeln("haha");});
	GameHandle!GameObject a;a.m_hold = new Foo;
	GameHandle!GameObject b;b.m_hold = new Bar;
	d(a,c);
	readln();
}


More information about the Digitalmars-d-learn mailing list