Serialization using Boost.Serialization

Boost.Serialization allows you to very simply specify what all in an object needs to be stored / retrieved. Boost.Serialization itself allows you to save / load a serializable object into / from a text file, binary file, or XML file. MCSerializedServer and MCSerializedClient classes allow you to send / recieve a serializable object via the multicast protocol.

Boost.Serialization has documentation availible online.

An example for sending / receiving a custom serializable object via the multiclass protocol follows:

#include <Utility/MulticastSerialized.h>
#include <boost/serialization/vector.hpp>
#include <Utility/BThread.h>
#include "UtilityTests.h"

#include <iostream>
using namespace std;
using namespace ame;
using namespace boost::serialization;
using namespace boost;

// an example of a custom struct/class that can be made serializable via Boost.Serialize
struct CustomSerializable
{
        // this structure has three member objects
        float f;
        vector<int> v;
        string name;

        CustomSerializable() : f(0) {};
        // this functions sets the objects to specific values
        void SetDefault()
        {
                f = -0.3f;
                v.push_back(9);
                v.push_back(0);
                v.push_back(2);
                v.push_back(1);
                v.push_back(0);
                name = "default";
        }
        // this function checks whether the values of the objects match those in SetDefault
        void CheckDefault()
        {
                BOOST_CHECK(f == -0.3f);
                BOOST_CHECK(v.size() == 5);
                if (v.size() == 5)
                {
                        BOOST_CHECK(v[0] == 9);
                        BOOST_CHECK(v[1] == 0);
                        BOOST_CHECK(v[2] == 2);
                        BOOST_CHECK(v[3] == 1);
                        BOOST_CHECK(v[4] == 0);
                }
                BOOST_CHECK(name == "default");
        }
        // it is best to declare the serialization code private, since it should not be called directly
private:
        // this will allow the serialization code to access it
        friend class boost::serialization::access;
        // the following code specifies both how the object is stored and retreived
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                // basically, list all of the data objects in the order they should be stored
                ar & f;
                ar & v;
                ar & name;
        }
};

// this class is in charge of receiving a CustomSerializable object
class Receiver : public BThread
{
        MCSerializedClient client;
        volatile bool completed;
public:
        Receiver() : completed(false) {Start();}
        // the PThread class will execute ThreadFunction in its own thread
        void ThreadFunction()
        {
                // lock the class mutex (not necessary in this case, but a good practice)
                mutex::scoped_lock lock(mutex_);

                // open the client and read the first packet
                client.open("234.0.1.9", 1501);
                for (int i=0; i<2; i++)
                {
                        client.read();

                        // instantiate a new CustomSerializable object and unpack the received packet into it
                        CustomSerializable custom;
                        client.unpack(custom);
                        
                        // make sure that the received values match the sent ones
                        custom.CheckDefault();
                }
                completed = true;
        }
        bool Completed() {return completed;}
};

// this is the main function of the test
void custom_test()
{
        // instantiate and start the server
        MCSerializedServer server;
        server.open("234.0.1.9", 1501);

        // instantiate a receiver
        Receiver receiver;

        // instantiate a new CustomSerializable object and set its values
        CustomSerializable custom;
        custom.SetDefault();

        // keep sending until the receiver thread starts up and gets a packet
        while (!receiver.Completed())
        {
                server.pack(custom);
                server.send();
        }
        receiver.Join();
} // end void custom_test()

Generated on Tue Mar 6 17:57:08 2007 for AME Repository by  doxygen 1.5.1-p1