Hello everybody,
Yesterday I was chatting with Dimitar Blagoev and he was telling me about some of his work and ideas about code generation tools and how most programmers nowadays are like copying machines (just implementing trivial things). I shared with him my wish for a more powerful C++ preprocessor. I wished I could parameterize function names and not only types… a bit later, while having a shower, I came to the idea that maybe I could write a template for a packet class header in… php! Indeed, what I wanted was a combination of static and dynamic content. The only difference is the content is not HTML, it’s about C++! I’m right now experimenting with this really great idea! I think we can have over 80% of the source code for the packet classes generated!
The proof of concept php script is attached to this message. That’s a script that defines a few helper functions and describes the packet in a few variables (things like class name, base class name, fields). The rest of the file is a template for the header file. If you run the script you’ll see the result is (almost) identical to the contents of Protocols/BitTorrent/Packets/Have.h.
I’m thinking about to ways of going further: either keep up using php and use the scripts as a tool the generate the files just once; or create a small console qt-based tool that would be invoked by the makefile to generate the sources for the packet classes. The former would be easier but the latter would be more powerful (I guess).
I would like to use XML for the description of the packets. There is definitely either builtin support for XML or a library for PHP and Qt also has good support for XML parsing, so using XML shouldn’t be an issue.
It should be optional whether the functions readPayload() and writePayload() be generated automatically or not because not all packets have a straight forward binary structure.
I’m wondering if unit-tests could be generated as well or that is not a good idea…
Maybe someone is interested in helping me for this one?
Regards,
Peter
