Bus Example Code
The code below shows how we might declare the classes described in the previous section. Later we will use the macro RW_DECLARE_COLLECTABLE_CLASS and discuss our function choices. You will find the complete code from which this example is taken at the end of this chapter; it is also given as the bus example in the buildspace\examples\tools directory.
 
#define EXPORT
 
class Bus : public RWCollectable {
RW_DECLARE_COLLECTABLE_CLASS(EXPORT, Bus)
public:
Bus();
Bus(int busno, const RWCString& driver);
~Bus();
// Inherited from class "RWCollectable":
Rwspace binaryStoreSize() const;
int compareTo(const RWCollectable*) const;
bool isEqual(const RWCollectable*) const;
unsigned hash() const;
void restoreGuts(RWFile&);
void restoreGuts(RWvistream&);
void saveGuts(RWFile&) const;
void saveGuts(RWvostream&) const;
void addPassenger(const char* name);
void addCustomer(const char* name);
size_t customers() const;
size_t passengers() const;
RWCString driver() const {return driver_;}
int number() const {return busNumber_;}
private:
RWSet customers_;
RWSet* passengers_;
int busNumber_;
RWCString driver_;
};
class Client : public RWCollectable {
RW_DECLARE_COLLECTABLE_CLASS(USER_MODULE, Client)
Client(const char* name) : name_(name) {}
private:
RWCString name_;
// ignore other client information for this example
};
Note how both classes inherit from RWCollectable. We have chosen to implement the set of customers by using class RWSet, which does not allow duplicate entries. This will guarantee that the same person is not entered into the customer list more than once. For the same reason, we have also chosen to implement the set of passengers using class RWSet. However, we have chosen to have this set live on the heap. This will help illustrate some points in the coming discussion.