Marshaling in C++ using Perl

A better, expanded version of this article can be found on my new home page.

This package provides simple generation of headers and source for marshaling binary data between C++ structs and byte streams. The nice thing about it is that you can easily extend the system to support generating property sheets, or going to/from XML, or one of a number of other things you want to do to data structures. You do so by just adding new visitor classes, without having to change the data structure code.

As supplied, this code requires "perl" in your path (either cygwin, or ActivePerl). You can add the custom build rules file to Visual Studio and set it to build for .pl files; then you can describe your data structures in a .pl file (see Packets.pl for an example) and it will generate the .h/.cpp files for your structs. The file Msg.codes is used to keep marshaling compatible between generations; the same structure will retain the same integer identifier across versions.

You can also use the MsgBase.h and MsgBase.cpp files with your own structures, by writing your own visit functions for the structs -- the perl code is just supplied as a convenience.

Download the source (5.5 kB).

Back to index.