last update 20 Sep 2009 |
#include <cvrFactory.h>
Classes | |
class | registration |
Represents the registration of an object of type U, constructed with the default parameterless constructor, and under its class name. More... | |
Public Types | |
typedef T | base_type |
Public Member Functions | |
void | registerObject (const base_type *obj, const char *name) |
void | registerObject (const base_type *obj, const std::string &name) |
void | registerObject (const base_type *obj) |
base_type * | newInstance (const char *name) const |
base_type * | newInstance (const std::string &name) const |
void | getRegisteredNames (std::map< std::string, std::string > &names) const |
Static Public Member Functions | |
static factory & | getFactory () |
There is a singleton instance of the factory for each base type T used, accessible through the getFactory() static method. Objects can be registered at compile time by the use of the _CVR_REGISTER_IN_FACTORY macro, or dynamically at runtime with the registerObject() method. An object is registered under a string name, usually the fully qualified name of the class (obtained dynamically from RTTI using the cvr::object method name(), or the cvr::className). However, registerObject() also allows the registration of an object under an arbitrary name.
If you use the macros _CVR_REGISTER_IN_FACTORY*, please be sure that you write the registration in one single line, as those lines are parsed automatically to force the necessary static references.
New objects are created using the newInstance() methods. It takes a name, and uses the object registered under this name as a template, calling its clone method (which must be defined).
Currently it is not allowed to register an object under an already registered name (the names have to be unique), nor to de-register an object.
Please note that you can register your own classes in the factories, but, for them to work, you have to ensure that the linker will not eliminate your classes. This can be easily accomplished creating a dummy function or class with references to your classes, which is exactly the task of cvrFactoryReferences.*.
The references in cvrFactoryReferences.cpp are created automatically by the Makefile that compiles the library.
typedef T cvr::factory< T >::base_type |
Base type of the instances in this factory.
static factory& cvr::factory< T >::getFactory | ( | ) | [static] |
Singleton factory access.
Referenced by cvr::factory< T >::factory::registration< U >::registration().
void cvr::factory< T >::getRegisteredNames | ( | std::map< std::string, std::string > & | names | ) | const |
Get a list with all registered names.
The map contains pairs of strings, where the first string contains the registered name and the second one the name of the class as reported by cvr::className.
base_type* cvr::factory< T >::newInstance | ( | const std::string & | name | ) | const |
base_type* cvr::factory< T >::newInstance | ( | const char * | name | ) | const |
Creates a new instance, which is a "clone" the object registered under the given name.
Returns a null pointer if no such object is registered.
Referenced by cvr::factory< T >::factory::registration< U >::registration().
void cvr::factory< T >::registerObject | ( | const base_type * | obj | ) |
Registers an object under its class name (dynamically obtained).
void cvr::factory< T >::registerObject | ( | const base_type * | obj, | |
const std::string & | name | |||
) |
void cvr::factory< T >::registerObject | ( | const base_type * | obj, | |
const char * | name | |||
) |
Registers an object under the given name.
Note that exactly the given instance will be kept. This means you should never ever delete the given object. This factory will take care of that at the proper time.
Referenced by cvr::factory< T >::factory::registration< U >::registration().