CVR-Lib last update 20 Sep 2009

cvr::factory< T > Class Template Reference

Factory of objects of base type T. More...

#include <cvrFactory.h>

List of all members.

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_typenewInstance (const char *name) const
base_typenewInstance (const std::string &name) const
void getRegisteredNames (std::map< std::string, std::string > &names) const

Static Public Member Functions

static factorygetFactory ()


Detailed Description

template<typename T>
class cvr::factory< T >

Factory of objects of base type T.

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.

Linkers and factory problems.

The chosen factory architecture in the CVR-Lib allows to register objects in the factory in a very flexible way. However, most linkers eliminate all references to classes that are not explicitly used, which affects directly the factories, as the code generated by the compiler, which should make the class registration in the factory is also removed. To solve this problem in a relatively standard form, there are two files cvrFactoryReferences.h and cvrFactoryReferences.cpp which ensure that whenever a factory is used, all registered objects in the factory are also referenced and therefore not eliminated.

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.


Member Typedef Documentation

template<typename T >
typedef T cvr::factory< T >::base_type

Base type of the instances in this factory.


Member Function Documentation

template<typename T >
static factory& cvr::factory< T >::getFactory (  )  [static]

template<typename T >
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.

template<typename T >
base_type* cvr::factory< T >::newInstance ( const std::string &  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.

Warning:
You have to take care of the memory management of the returned instance, i.e. you have the remove the object with delete.

template<typename T >
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.

Warning:
You have to take care of the memory management of the returned instance, i.e. you have the remove the object with delete.

Referenced by cvr::factory< T >::factory::registration< U >::registration().

template<typename T >
void cvr::factory< T >::registerObject ( const base_type obj  ) 

Registers an object under its class name (dynamically obtained).

template<typename T >
void cvr::factory< T >::registerObject ( const base_type obj,
const std::string &  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.

template<typename T >
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().


The documentation for this class was generated from the following file:

Generated on Sun Sep 20 22:08:39 2009 for CVR-Lib by Doxygen 1.5.8