HyperspaceExplorer 0.7.1
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends
FunctionFactory Class Reference

Factory class creating Displayable objects given the name of their class. More...

#include <FunctionFactory.h>

Collaboration diagram for FunctionFactory:
Collaboration graph
[legend]

List of all members.

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.
DisplayablecreateFunction (const std::string &name)
 Create an object derived from Function, given the name of its class.
std::vector< std::stringlistFunctions ()
 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
FunctionFactoryoperator= (const FunctionFactory &)
 disabled assignment operator
 ~FunctionFactory ()
 disabled destructor

Private Attributes

CallbackMap callbacks
 Stores the Function creators.

Friends

class Loki::CreateUsingNew< FunctionFactory >

Detailed Description

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");
Author:
Helge Preuss <lene.preuss@gmail.com>

Member Function Documentation

Displayable * FunctionFactory::createFunction ( const std::string name)

Create an object derived from Function, given the name of its class.

Parameters:
nameThe class name of the created Function object
Returns:
a newly created object of class "name"

References callbacks, std::map< _Key, _Tp, _Compare, _Alloc >::end(), and std::map< _Key, _Tp, _Compare, _Alloc >::find().

Here is the call graph for this function:

bool FunctionFactory::registerFunction ( CreateFunctionCallback  creator,
const std::string parent_category 
)

Registers a function creating a Function under the Function's class name.

Parameters:
creatorCallback function creating an object of the desired class
parent_categoryCategory the Function is under in the inheritance hierarchy and the menu structure
Returns:
true if registration was successful

References DisplayableClass::addDisplayable(), callbacks, DisplayableClass::findClass(), and std::map< _Key, _Tp, _Compare, _Alloc >::insert().

Here is the call graph for this function:

bool FunctionFactory::unregisterFunction ( const std::string name)

Remove a Function class from the factory.

Parameters:
nameName of the class which isn't available for creation any more
Returns:
true if unregistration was successful

References callbacks, and std::map< _Key, _Tp, _Compare, _Alloc >::erase().

Here is the call graph for this function:


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends

Generated on Mon Apr 9 2012 20:25:18 for HyperspaceExplorer 0.7.1 by doxygen 1.7.4  -  Hosted bySourceForge.net Logo