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
// 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)
// 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