RWFactory(3C++) RWFactory(3C++)
NameRWFactory - Rogue Wave library class
Synopsis
typedef unsigned short RWClassID;
typedef RWCollectable* (*RWuserCreator)();
#include <rw/factory.h>
RWFactory* theFactory;
Description
Class RWFactory can create an instance of an RWCollectable object, given
a class ID. It does this by maintaining a table of class IDs and
associated "creator functions." 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 which can be accessed using
getRWFactory. It is guaranteed to have creator functions in it for all
of the classes referenced by your program. See also the section in the
User's Guide about RWFactory.
Persistence
None
Example
#include <rw/factory.h>
#include <rw/rwbag.h>
#include <rw/colldate.h>
#include <rw/rstream.h>
main(){
// Create new RWBag off the heap, using Class ID __RWBAG.
RWBag* b = (RWBag*)getRWFactory ()->create(__RWBAG);
b->insert( new RWCollectableDate ); // Insert today's date
// ...
b->clearAndDestroy(); // Cleanup: first delete members,
delete b; // then the bag itself
}
Page 1
RWFactory(3C++) RWFactory(3C++)
Public ConstructorsRWFactory();
Construct an RWFactory.
Public Operator
RWBoolean
operator<=(const RWFactory& h);
Returns TRUE if self is a subset of h, that is, every element of self
has a counterpart in h which isEqual. This operator is included to fix
an inconsistency in the C++ language. It is not explicitly present
unless you are compiling with an implementation of the Standard C++
Library. It would normally be inherited from RWSet Note: If you
inherit from RWFactory in the presence of the Standard C++ Library, we
recommend that you override this operator and explicitly forward the
call. Overload resolution in C++ will choose the Standard Library
provided global operators over inherited class members. These global
definitions are not appropriate for set-like partial orderings.
Public Member Functions
void
addFunction(RWuserCreator uc, RWClassID id);
Adds to the RWFactory the global function pointed to by uc, which creates
an instance of an object with RWClassID id.
void
addFunction(RWuserCreator uc, RWClassID id, RWStringID sid);
Adds to the RWFactory the global function pointed to by uc, which creates
an instance of an object with RWClassID id and RWStringID sid.
RWCollectable*
create(RWClassID id) const;
Allocates a new instance of the class with RWClassID id off the heap and
returns a pointer to it. Returns nil if id does not exist. Because this
instance is allocated off the heap, you are responsible for deleting it
when done.
RWCollectable*
create(RWString sid) const;
Allocates a new instance of the class with RWStringID sid off the heap
and returns a pointer to it. Returns nil if sid does not exist. Because
Page 2
RWFactory(3C++) RWFactory(3C++)
this instance is allocated off the heap, you are responsible for deleting
it when done.
RWuserCreator
getFunction(RWClassID id) const;
Returns from the RWFactory a pointer to the global function associated
with RWClassID id. Returns nil if id does not exist.
RWuserCreator
getFunction(RWStringID sid) const;
Returns from the RWFactory a pointer to the global function associated
with RWStringID sid. Returns nil if sid does not exist.
void
removeFunction(RWClassID id);
Removes from the RWFactory the global function associated with RWClassID
id. If id does not exist in the factory, no action is taken.
void
removeFunction(RWStringID sid);
Removes from the RWFactory the global function associated with RWStringID
sid. If sid does not exist in the factory, no action is taken.
RWStringID
stringID(RWClassID id) const;
Looks up the RWStringID associated with id and returns it. If there is
no such association, returns RWStringID("NoID").
RWClassID
classID(RWStringID) const;
Looks up the RWClassID associated with sid and returns it. If there is
no such association, returns __RWUNKNOWN.
Page 3