Cheetah Serialization

When sending messages in C++, it is very convenient to be able to say "send this object across the wire" without worrying about the internal structure of the object. The problem is, not all C++ objects lie in one contiguous chunk of memory, and so you can't just blindly copy bytes to do that send. In general, you have to do an operation specific to a given class to pack it into a buffer and unpack it on the other side. The Cheetah serialization interface provides an extensible mechanism to pack and unpack objects.

There are four basic operations you need to be able to do for serialization, and we illustrate them with the simplest case. For the details of the syntax, see the API Reference.

These four operations will work fine for objects that can use a bitwise copy constructor. These are the same fundamental operations that you would use if you were coding this up explicitly in C, but by using this interface we can transparently use objects for which a bitwise copy doesn't do the job.

For a more complex object you can supply different methods by template specializing the four functions. Consider a simple array of doubles, stored as in integer length and a pointer to an array of doubles on the heap. We want to store the integer length and then the doubles in the buffer. The four operations would then be:

This strategy is optimized to reduce the number of memory copies: the array is not copied out of the message buffer until it is actually used. An alternative way to handle the not calling delete on the data in the message buffer would be to use an array class in which you can set a flag to tell it that it does not "own" the memory it is pointing to, and so it should not call delete on it. We allow the serialization user to supply their own function though so that you can use arrays (or other data structures) that are not designed with that in mind.

Another form of flexibility built into the serialization API is the ability to have multiple serialization strategies for any given class. All of the serialization routines are static members of a Serializer class, and that class has a policy template parameter which can be used to build different specializations for different serialization strategies. See the API Reference for details.