Tools.h++ Manual

21-108 104011 Tandem Computers Incorporated
21
RWFactory
Synopsis
typedef unsigned short RWClassID;
typedef RWCollectable* (*sRWuserCreator)();
#include <rw/factory.h>
RWFactory* theFactory
;
Description Class
RWFactory
can create an instance of a
RWCollectable
object, given a
class ID. It does this by maintaining a table of class ID's and associated
"creator function". A creator function has prototype:
RWCollectable*
aCreatorFunction
();
This function should create an instance of a particular class. For a given
RWClassID
tag, the appropriate function is selected, invoked and the resultant
pointer returned. Because any object created this way is created off the heap,
you are responsible for deleting it when done.
There is a one-of-a-kind global
RWFactory
pointed to by the pointer
theFactory
. It is guaranteed to have creator functions in it for all of the
classes referenced by your program. See “An aside: the RWFactory” on page 6
in Chapter 17, “Designing an RWCollectable Class,” for more information.
Example
#include <rw/factory.h>
#include <rw/rstream.h>
main()
{
// Create a new RWBag off the heap, using the Class ID __RWBAG.
// "theFactory" points to the predefined global factory:
RWBag* b = (RWBag*)theFactory-create(__RWBAG);
b->insert( new (CollectableDate );// Insert today’s date
// ...
b->clearAndDestroy(); // Cleanup: first delete members,