xorp

ElementFactory Class Reference

A factory for creating elements based on their type. More...

#include <element_factory.hh>

List of all members.

Classes

class  UnknownElement
 Exception thrown if an Unknown element is being created. More...

Public Types

typedef Element *(* Callback )(const char *)
typedef map< string, Callback > Map

Public Member Functions

void add (const string &key, Callback cb)
 Register a callback with the factory.
Elementcreate (const string &key, const char *arg)
 Create an element.

Static Public Member Functions

static bool can_create (const string &key)
 Checks whether a type exists.

Static Private Attributes

static Map _map
 There is only one factory map.
static RegisterElements _regelems
 A class which registers defined callbacks upon creation.

Detailed Description

A factory for creating elements based on their type.

Elements are created via their string represented type. They are initialized via a c-style string. If this string is null, then a default value is assigned to the element.

Functions that perform the creation are registered with the factory at run-time.

An exception is throw on element creationg failure.

Similar to Dispatcher.


Member Function Documentation

void ElementFactory::add ( const string &  key,
Callback  cb 
)

Register a callback with the factory.

Parameters:
keythe element id/type. Must be unique.
cbfunction to be called when the element must be created.
bool ElementFactory::can_create ( const string &  key) [static]

Checks whether a type exists.

Parameters:
keythe element to check for.
Returns:
true if the element can be create via the factory.
Element * ElementFactory::create ( const string &  key,
const char *  arg 
)

Create an element.

Returns:
the requested element. Caller is responsible for deletion.
Parameters:
keythe type of element that needs to be created.
arginitial value of element. If null, a default is assigned.

Member Data Documentation

ElementFactory::Map ElementFactory::_map [static, private]

There is only one factory map.

Creating additional factory objects is therefore safe. No need to pass a global factory around in the various classes.

A class which registers defined callbacks upon creation.

Callbacks are thus registered once [static] and before the factory is actually used.


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