The following code shows a simple class and how it is externalized with the serializer class srl_xml. Here the member function osl_init() is defined to register the class' members. We will later see that there are alternative ways of doing this.

#include "osl/rfx.h"
#include <string>

using namespace std;

class AddressCard
    wstring Name;
    wstring Street;
    int     Zip;
    wstring Country;
    time_t  DateOfBirth;

    AddressCard() : Zip(0) {}

    static void osl_init(osl::cls<AddressCard>& c) {
        c.member(&AddressCard::Name, "Name");
        c.member(&AddressCard::Street, "Street");
        c.member(&AddressCard::Zip, "Zip");
        c.member(&AddressCard::Country, "Country");
        c.member(&AddressCard::DateOfBirth, "DateOfBirth");
osl::cls<AddressCard> cAddressCard("AddressCard");

In this example, the member fields could have been private, but just to simplify the code here all are kept public.

A call to cls_reg::init() must be made before the serialization mechanism can be used. In the next code listing the call is placed as the first operation in main().

The classes ofstream_xml and ifstream_xml provides an easy to use interface to the XML serializer. These classes inherits from the std::fstream classes and encapsulates OSL's srl_xml which does the XML encoding. The operator << is doing the actual work of writing the object to the stream. To read an object, the corresponding operator >> would be used.

#include "osl/objstream.h"

int main()

        AddressCard ac;
        ac.Name = L"Tomas";
        ac.Street = L"Pacific Highway";
        ac.Zip = 2077;
        ac.Country = L"Australia";

        ofstream_xml out("addresscard.xml");

        out << &ac;

        AddressCard *ac;

        ifstream_xml in("addresscard.xml");

        in >> ac;

        delete ac;

    return 0;

The above example first writes the object to the file "addresscard.xml" and just after that reads it from the same file. When reading, the ac pointer is assigned the address to the read object. The object must be destroyed after use just as any other object created with new().


Before continuing, some terminology should be established. The general process of storing/loading class instances to/from streams is called serialization, thus the name of this library. The process of converting a run time object into an external representation is called externalization, and the process of converting an external representation into the run-time object is called internalization.

Follow @ClearObjects