Using Object Streams with RWCollectable-Derived Objects
Because the format used by
RWCompactObjectInputStreamImp for storing data is the same as that used by objects derived from
RWCollectable, it is possible to interleave the two. The header file
\rw\serial\RWCollectableIO.h contains definitions of insertion and extraction operators for
RWCollectable. Aside from including this header, the only other requirement is that the macro
RW_DEFINE_COLLECTABLE_AS_STREAMABLE be defined for each
RWCollectable-based object. This will also allow you to stream out these
RWCollectable-based objects to other kinds of object streams, but the effect will be to embed compact object stream formatted data into the stream. The following example demonstrates how to get the compact format working without adding all the additional code to your existing
RWCollectable-based classes. (If you want to convert
RWCollectable-based objects to stream properly in any object stream format, go to
Making RWCollectable Objects That Are Also Serializable).
// examples\serial\advanced\tree.h)
#include <rw/serial/RWObjectStreamMacros.h>
#include <rw/serial/RWCollectableIO.h> // 1
#define EXPORT // 2
class tree : public RWCollectable // 3
{
RW_DECLARE_COLLECTABLE_CLASS(EXPORT, tree); // 4
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 species_;
};
RW_DEFINE_COLLECTABLE_AS_STREAMABLE(tree) // 5
// examples\serial\advanced\forest.cpp
tree fir("Noble Fir"); // 1
RWpostream postr(cout);
RWObjectOutputStream out = RWCompactObjectOutputStreamImp:: // 2
make(RWDataToVirtualOutputStreamImp::make(postr));
out << &fir; // 3
If you want to convert
RWCollectable-based objects to stream properly in any object stream format, see
Making RWCollectable Objects That Are Also Serializable.