Making RWCollectable Objects That Are Also Serializable
To serialize RWCollectable-based objects to any object stream, it is necessary to do the full object stream conversion. Everything that you would do for any other class must now be done for the RWCollectable-based class. You can’t alter the RWCollectable class, so you must use the external technique for providing the streaming capability. For a full description, see External Serialization. Also, you must not include RWCollectableIO.h or use RW_DEFINE_COLLECTABLE_AS_STREAMABLE, since you are using the other macros to accomplish this. Instead, you must use RW_DECLARE_STREAMABLE_COLLECTABLE in a header file and RW_DEFINE_STREAMABLE_COLLECTABLE in a source file. This edition has taken this burden from the developer and has provided these in the library. Including the rw/serial/serial.h header gets the RW_DECLARE_STREAMABLE_COLLECTABLE which is all that is needed to get the serialization support for RWCollectable.
A simple example follows.
 
// examples\serial\advanced\tree2.h
 
#include <rw/serial/serial.h>
 
#define EXPORT // 1
 
class tree : public RWCollectable // 2
{
RW_DECLARE_COLLECTABLE_CLASS(EXPORT, tree); // 3
 
public:
tree() { }
tree(const RWCString& species)
: species_(species) { }
virtual void restoreGuts(RWvistream&);
virtual void saveGuts(RWvostream&) const;
virtual void restoreGuts(RWFile&) { }
virtual void saveGuts(RWFile&) const { }
RWCString getSpecies() const {
return species_;
}
RWCString setSpecies(const RWCString& species) {
RWCString tmp = species_;
species_ = species;
return tmp;
}
private:
RWCString species_;
};
 
RW_DECLARE_EXTERNAL_STREAMABLE_AS_SELF(tree)
RW_DECLARE_EXTERNAL_STREAMABLE_AS_BASE(tree, RWCollectable)
RW_DECLARE_STREAMABLE_POINTER(tree) // 4
RW_DECLARE_EXTERNAL_STREAM_FNS(tree) // 5
//1 Macro for the exporting of symbols. For more information on how to define and use the EXPORT macro, see the section “Exporting Symbols in a Windows Environment,” in the Essential Tools Module User’s Guide.
//2 New class derived from RWCollectable
//3 Existing macro from Rogue Wave persistence.
//4 Declare the new class as streamable by reference.
//5 Declare an external streaming function for the new class.
 
// examples\serial\advanced\tree2.cpp
 
RW_BEGIN_EXTERNAL_STREAM_CONTENTS(tree) // 1
{
RW_STREAM_ATTR_GET_SET(species, RWCString, getSpecies, setSpecies)
}
RW_END_EXTERNAL_STREAM_CONTENTS
 
RW_DEFINE_EXTERNAL_STREAMABLE_AS_SELF(tree)
RW_DEFINE_EXTERNAL_STREAMABLE_AS_BASE(tree, RWCollectable)
RW_DEFINE_EXTERNAL_STREAMABLE_POINTER(tree)
//1 Define the streamContents() function for the new class.
//2 Define our new class as streamable by reference.
 
// examples\serial\advanced\forest2.cpp
 
tree* fir = new tree("Noble Fir"); // 1
RWpostream postr(cout);
RWObjectOutputStream out = RWCompactObjectOutputStreamImp::
make(RWDataToVirtualOutputStreamImp::
make(postr)); // 2
 
out << fir; // 3
//1 Create a tree.
//2 Create a data stream using the standard file stream just opened and then create a compact object output stream from the data stream.
//3 Stream out the tree.