HyperspaceExplorer 0.7.1
|
Factory class creating Displayable objects given the name of their class. More...
#include <FunctionFactory.h>
Classes | |
struct | BadFunctionException |
Thrown by createFunction() when name does not map to a function. More... | |
Public Types | |
typedef Displayable *(* | CreateFunctionCallback )() |
callback function generating a Function and returning a Function* | |
Public Member Functions | |
bool | registerFunction (CreateFunctionCallback creator, const std::string &parent_category) |
Registers a function creating a Function under the Function's class name. | |
bool | unregisterFunction (const std::string &name) |
Remove a Function class from the factory. | |
Displayable * | createFunction (const std::string &name) |
Create an object derived from Function, given the name of its class. | |
std::vector< std::string > | listFunctions () |
Return the names of the registered Displayable classes as a std::vector<std::string> | |
Private Types | |
typedef std::map< std::string, CreateFunctionCallback > | CallbackMap |
stores a creator function with a string containing the class name | |
Private Member Functions | |
FunctionFactory () | |
disabled default constructor | |
FunctionFactory (const FunctionFactory &) | |
disabled copy constructor | |
FunctionFactory & | operator= (const FunctionFactory &) |
disabled assignment operator | |
~FunctionFactory () | |
disabled destructor | |
Private Attributes | |
CallbackMap | callbacks |
Stores the Function creators. | |
Friends | |
class | Loki::CreateUsingNew< FunctionFactory > |
Factory class creating Displayable objects given the name of their class.
The factory is instantiated as singleton in the variable (in fact, class) TheFunctionFactory.
Classes which should be creatable by the FunctionFactory must add the following (exemplary) code after their declaration in the header file:
namespace { Displayable *createHypercube() { return new Hypercube(); } const bool registered = TheFunctionFactory::Instance().registerFunction(createHypercube, "Hypercube"); }
Of course, the name of the class and the description passed to FunctionFactory::registerFunction() must be changed. The name of the variable registered
must be chosen to be unique in the anonymous namespace.
Objects are created with, e.g.:
Displayable *f = TheFunctionFactory::Instance().createFunction("Hypercube");
Displayable * FunctionFactory::createFunction | ( | const std::string & | name | ) |
Create an object derived from Function, given the name of its class.
name | The class name of the created Function object |
References callbacks, std::map< _Key, _Tp, _Compare, _Alloc >::end(), and std::map< _Key, _Tp, _Compare, _Alloc >::find().
bool FunctionFactory::registerFunction | ( | CreateFunctionCallback | creator, |
const std::string & | parent_category | ||
) |
Registers a function creating a Function under the Function's class name.
creator | Callback function creating an object of the desired class |
parent_category | Category the Function is under in the inheritance hierarchy and the menu structure |
References DisplayableClass::addDisplayable(), callbacks, DisplayableClass::findClass(), and std::map< _Key, _Tp, _Compare, _Alloc >::insert().
bool FunctionFactory::unregisterFunction | ( | const std::string & | name | ) |
Remove a Function class from the factory.
name | Name of the class which isn't available for creation any more |
References callbacks, and std::map< _Key, _Tp, _Compare, _Alloc >::erase().