Dear all,
Almost a year ago Atul suggested that we use the Visitor Patter to double dispatch Packet objects to the correct handler in Gnutella::PacketProcessor. The idea was really great - the only drawback was the extra overhead of visitor class hierarchies. Then I read about Visitor implementations using reflection in languages that support it and then I came up with the idea and a proof of concept to use the Qt metaobject system to implement a “Dynamic Visitor”.
Earlier today I pushed revno 247 of my dev branch that presents an initial version of PacketDispatcher - a helper class that dispatches objects to static/non-member or member handler functions based on the object’s typeid. I was amazed how simple that solution turned out to be! Just a little bit of RTTI and templates - no need for reflection or Qt’s metaobject system at all! And in addition, it’s totally type-safe!
If you look at the tests you’ll see how the class can be used but still, here is a tiny example:
class PacketProcessor
{
...
PacketProcessor()
{
dispatcher.registerHandler (this, &PacketProcessor::handleQuery);
dispatcher.registerHandler (this, &PacketProcessor::handleQueryHits);
}
void handlePacket (const PacketBase &packet)
{
dispatcher.dispatch (packet);
}
void handleQuery (const Query &query)
{
...
}
void handleQueryHits (const QueryHits &queryHits)
{
...
}
private:
PacketDispatcher dispatcher;
}
};
Pretty awesome - no switching on type codes, little to type, type-safe, multiple handlers supported - all nicely object-oriented! :-)
I think the core functionality is already there, now I’ll be thinking about generalizing the class further and maybe making a template class out if that could dispatch objects of any polymorphic type!
Any comments or suggestions are of course welcome!
Regards,
Peter
