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
 
//1 Include these files to get serialization macros and the inserters and extractors for RWCollectable.
//2 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.
//3 New class derived from RWCollectable.
//4 Existing macro from Rogue Wave persistence.
//5 Make your collectable base class streamable.
 
// examples\serial\advanced\forest.cpp
 
tree fir("Noble Fir"); // 1
 
RWpostream postr(cout);
RWObjectOutputStream out = RWCompactObjectOutputStreamImp:: // 2
make(RWDataToVirtualOutputStreamImp::make(postr));
 
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.
If you want to convert RWCollectable-based objects to stream properly in any object stream format, see Making RWCollectable Objects That Are Also Serializable.