Making the Basic Classes Serializable
The real estate application references the residential and rural_land objects as pointers to the real_property base class. As stated in Serialization Requirements to make these classes serializable as pointers you must:
Declare the streamContents() function and the global operators for streaming pointers by putting macros in the header file for each class.
Define the streamContents() function and the global operators for streaming pointers by putting macros in the source file for each class.
Header File Changes
Starting with the header files, add the following macros:
RW_DECLARE_VIRTUAL_STREAM_FNS(classname), which declares the streamContents() member function needed to serialize a class out to a virtual stream. Place this within the class declaration.
RW_DECLARE_STREAMABLE_POINTER(classname), which declares the global input and output operators needed for an object pointer to be written out and later restored. Because this macro declares global operators, you must place it outside the class declaration. The safest location for this macro declaration is immediately following the declaration of the class to which it applies.
RW_DECLARE_STREAMABLE_AS_SELF(classname), which declares the factory for your class and declares its registrar. This allows objects to be created on the fly from the stream.
RW_DECLARE_STREAMABLE_AS_BASE(classname), which declares the factory for your class and declares its registrar for serialization through base class pointers.
Here is the revised code for the header files:
// from real_property.h
class real_property
{
RW_DECLARE_VIRTUAL_STREAM_FNS(real_property)
public:
real_property() {}
real_property(const RWCString& address,
const RWCString& size)
: address_(address), size_(size)
{}
virtual ~real_property() {}
bool operator== (const real_property& prop) const {
return address_ == prop.address_;
}
private:
RWCString address_;
RWCString size_;
};
RW_DECLARE_STREAMABLE_POINTER(real_property)
RW_DECLARE_STREAMABLE_AS_SELF(real_property)
// from residential.h
class residential : public real_property
{
RW_DECLARE_VIRTUAL_STREAM_FNS(residential)
public:
residential()
{}
residential(const RWCString& address, const RWCString& size,
long footage)
: real_property(address, size),
footage_(footage)
{}
bool operator== (const residential& prop) const {
return real_property::operator==(prop);
}
private:
long footage_; // Square footage of the house
};
RW_DECLARE_STREAMABLE_AS_SELF(residential)
RW_DECLARE_STREAMABLE_AS_BASE(residential, real_property)
RW_DECLARE_STREAMABLE_POINTER(residential)
// from rural_land.h
class rural_land : public real_property
{
RW_DECLARE_VIRTUAL_STREAM_FNS(rural_land)
public:
rural_land()
{}
rural_land(const RWCString& address, const RWCString& size,
double irrigation = 0.0)
: real_property(address, size), irrigation_(irrigation)
{}
bool operator== (const rural_land& prop) const {
return real_property::operator==(prop);
}
private:
double irrigation_; // Irrigation acres for the property
};
RW_DECLARE_STREAMABLE_AS_SELF(rural_land)
RW_DECLARE_STREAMABLE_AS_BASE(rural_land, real_property)
RW_DECLARE_STREAMABLE_POINTER(rural_land)
Source File Changes
Moving on to the source files, you must now:
Define the streamContents() function using the following macro set:
RW_BEGIN_STREAM_CONTENTS(className)
RW_STREAM_PARENT(parentClass)
RW_STREAM_ATTR_MEMBER(variableLabel,variableName)
RW_END_STREAM_CONTENTS()
The definitions generated through these macros guarantee symmetry between the serialization output and input operations.
Define the global insertion and extraction operators that support the serialization of pointers to objects, using the following macro set:
RW_DEFINE_STREAMABLE_POINTER(className)
Apply this macro to all classes involved in the serialization.
RW_DEFINE_STREAMABLE_AS_SELF(className)
Apply this macro to a class if its instantiations will be serialized as pointers directly to the instantiations.
RW_DEFINE_STREAMABLE_AS_BASE(derivedClass,baseClass)
Apply this macro to a class derived from a base class if its instantiations will be serialized as pointers to the base class.
If you want to allow for the possibility that these objects might be serialized as pointers directly to themselves, you need to apply RW_DEFINE_STREAMABLE_AS_SELF as well. In the code that follows, both RW_DEFINE_STREAMABLE_AS_BASE and RW_DEFINE_STREAMABLE_AS_SELF are applied.
Here is the source code for your three classes, modified as needed with the macros described above:
// from 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 Begins the streamContents() function definition for the class real_property.
// 2 Generates the code for the streaming of the address_ variable. The first argument can be anything, but clearly something close to the variable name is preferable. The second argument must be the exact variable name.
// 3 Ends the streamContents() function definition.
// from residential.cpp
RW_BEGIN_STREAM_CONTENTS(residential)
{
RW_STREAM_PARENT(real_property) //1
RW_STREAM_ATTR_MEMBER(footage, footage_) //2
}
RW_END_STREAM_CONTENTS
// 1 Generates the code for the streaming of variables inherited from the parent class. This macro must appear immediately after the RW_BEGIN_STREAM_CONTENTS macro.
// 2 Generates the code for the streaming of the footage_ variable, which is an extension to the variables in the base class.
Note that the code below parallels the code for residential.
// from rural_land.cpp
RW_BEGIN_STREAM_CONTENTS(rural_land)
{
RW_STREAM_PARENT(real_property)
RW_STREAM_ATTR_MEMBER(irrigation, irrigation_)
}
RW_END_STREAM_CONTENTS