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.
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.
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.
|Try, 30 Days Free!|
Mac OS X Universal