Code generator for packet classes!

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

Attached Files:

Would you like to post a relpy?


This post starts a thread.
Follow-ups:
XML Experts? (was: Code generator for packet classes!)
Hello everybody, This is a call for XML experts to shed some light on the powers of XML in the context of packet generation. I myself don't have a very detailed (more...)
Re: Code generator for packet classes!
Hi, I am not sure my reply is relevant.. Velocity template engine is the one i have been experimenting with recently for code generation.. it is very good.. Regards Vijay
Re: Code generator for packet classes!
Dear mate You can as well take a look at GNU m4... I think it does a lot of these things.... The info pages say it is very addicting ;-) -- cheerio atul