Second Change
Define the streamContents() function for this class. Some of the macros that do this are:
*RW_BEGIN_STREAM_CONTENTS()
*RW_STREAM_PARENT()
*RW_STREAM_ATTR_GET_SET()
*RW_STREAM_ATTR_MEMBER()
*RW_END_STREAM_CONTENTS()
Using these macros provides a built-in symmetry between the input and the output operations on the class. The macros defining the streamContents() function can be placed in any module, but are normally put in the same source file as the other definitions for that class. The following example from shows what it looks like for the real_property class.
 
// examples\serial\simple\real_property.cpp
 
RW_BEGIN_STREAM_CONTENTS(real_property) // 1
{
RW_STREAM_ATTR_MEMBER(address, address_) // 2
RW_STREAM_ATTR_MEMBER(size, size_)
}
RW_END_STREAM_CONTENTS // 3
//1 This macro begins the definition of the streamContents() function.
//2 This macro defines the code for inserting and extracting data to and from the address_ data member of the real_property class. The first parameter to the macro is an attribute label and need not match any name in the class, but the second parameter must be the actual name of the member that you want to stream. The macro is used again on the next line to accomplish the same thing for the size_ member.
//3 The streamContents() function is closed out with this macro.
Now that these macros have been added to real_property, this class can be written to and read from object streams using the operators operator<<(RWObjectOutputStream&, const real_property&) and operator>>(RWObjectInputStream&, real_property&), respectively. The following code shows how an instance of class real_property can be written to a file and read back.
 
// examples\serial\simple\real_estate.cpp
 
real_property real1("1980 Main St. Corvallis, Oregon","50x100"); // 1
real_property real2;
 
{ // 2
ofstream fout("real_estate.dat"); // 3
 
RWpostream posstr(fout);
RWObjectOutputStream out = RWCompactObjectOutputStreamImp::make(
RWDataToVirtualOutputStreamImp::make(
postr)); // 4
out << real1; // 5
}
 
ifstream fin("real_estate.dat"); // 6
RWpistream pistr(fin);
RWObjectInputStream in = RWCompactObjectInputStreamImp::
make(RWDataFromVirtualInputStreamImp::
make(pistr)); // 7
in >> real2; // 8
//1 Create a real_property object.
//2 Create a local scope so the output file closes automatically when it’s no longer needed.
//3 Open a file for output.
//4 Create a data stream using the standard file stream just opened and then create a compact object output stream from the data stream. The second parameter indicates the kind of object that will serve as the root element. It can be one of: objectTag, sequenceTag, or mapTag.
//5 Stream out the contents of real1 to the file in compact format.
//6 Now open the file for input.
//7 Create a data stream and then create a compact object input stream from the data stream.
//8 Read the data from the first variable (real1) into the second (real2).