Overview
Examples
Screenshots
Comparisons
Applications
Download
Manual
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site











SourceForge.net Logo



WithFactory

 

template <class T>

class WithFactory

T

Type of base class of polymorphic hyerarchy.

This template class adds some RTTI (Run Time Type Identification) to your classes, and implements a so called 'class factory', bringing the ability to register class hyerarchies, construct them by name, check their type at runtime, list all classes in hyerarchy and so on.

It's a base behaviour for most polymorphic class usage.

 

 

 

Basic usage

 

To implement the factory behaviour in your base class, just derive from WithFactory :

 

class MyBaseClass : public WithFactory<MyBaseClass>

{

        .........

};

Then you can derive your class hyerarchy from base, as usual :

 

class MyDerivedClass : public MyBaseClass

{

        .........

};

When you've your class definition, you must register them; that's done inserting in a .cpp file (or in separate ones if you like, but NOT inside include files, the following statement :

 

REGISTERCLASS(MyBaseClass [, "a class description" [, anIndex [, "an Iml image name"]]])

REGISTERCLASS(MyDerivedClass [, "a class description" [, anIndex [, "an Iml image name"]]])

 

where you can insert an optional class description string, an index and and icon in Iml format; their usage will be clarified later on.

Class creation can be done by following ways :

 

As a pointer by classic new operator:

        MyBaseClass *ptr = new MyBaseClass;

        MyBaseClass *ptr = new MyDerivedClass;

 

As a pointer, by ascii class name :

        MyBaseClass *ptr = MyBaseClass::CreatePtr("MyBaseClass");

        MyBaseClass *ptr = MyBaseClass::CreatePtr("MyDerivedClass");

 

As One<MyBaseClass> :

        One<MyBaseClass> ptr = MyBaseClass::CreateInstance("MyBaseClass");

        One<MyBaseClass> ptr = MyBaseClass::CreateInstance("MyDerivedClass");

 

You can inquire the type of an object at runtime with the IsA() member function ::

 

MyBaseClass *ptr = new MyDerivedClass;

String classType = ptr->IsA();    returns the string "MyDerivedClass"

 

You can also list at runtime your class hyerarchy by mean of static member Classes():

 

Vector<String> const &classList = MyBaseClass::Classes();

 

or get descriptions by class name with GetClassDescription() static member :

 

String classDesc = MyBaseClass::GetClassDescription("MyBaseClass");

String classDesc = MyBaseClass::GetClassDescription("MyDerivedClass");

 

or get their integer 'index' with GetClassIndex() static member :

 

int classIndex = MyBaseClass::GetClassIndex("MyBaseClass");

int classIndex = MyBaseClass::GetClassIndex("MyDerivedClass");

 

Class registering

 

REGISTERCLASS(type)

Registers a class by its type.

 


 

REGISTERCLASS(type, description)

Registers a class by its type giving an optional description which can be queried later on. Main purpose of description is the presentation of class lists on menus and or dialogs when creating classes at runtime.

 

REGISTERCLASS(type, description, index)

Registers a class by its type giving an optional description and an integer index which both can be queried later on. Main purpose of index is to give the ability to sort at runtime the class list by importance. Index can be any integer number


 

REGISTERCLASS(type, description, index, icon)

Registers a class by its type giving an optional description, an integer index and an icon which can be queried later on. Main purpose of index is to give the ability to sort at runtime the class list by importance or to have sort of class grouping. Index can be any integer number icon should be a String containing a full Iml icon name, as "MyIml::MyImage"

 

Detailed member list

 


 

static One<TCreateInstance(const String &className)

Creates a class derived from hyerarchy base T by its className.

Returns a smart pointer to the base class T

 


 

static T *CreatePtr(String const &className)

Creates a class derived from hyerarchy base T by its className.

Returns a traditional pointer to the base class T

 


 

static Vector<Stringconst &Classes(void)

Returns Vector of strings containing all registered class names in hyerarchy.

 


 

static String const &GetClassDescription(const String &className)

Returns an ascii description of a class identified by className.

If no description was given when registering the class, returns an empty string.

 


 

static int const &GetClassIndex(const String &className)

Return an integer index assigned to class type identified by className.

If no index was given when registering the class, returns 0.

 


 

static Image GetClassImage(const String &className)

Return the Image object assigned to class type identified by className.

If no image was given when registering the class, returns Null.

 


 

String const &IsA(void)

Returns a string containing the class name.

 


 

 

Last edit by micio on 03/18/2010. Do you want to contribute?. T++