PetaVision  Alpha
PV::Factory Class Reference

#include <Factory.hpp>

Public Member Functions

BaseObjectcreateByKeyword (char const *keyword, char const *name, HyPerCol *hc) const
 
int registerKeyword (char const *keyword, ObjectCreateFn creator)
 

Static Public Member Functions

template<typename T >
static BaseObjectcreate (char const *name, HyPerCol *hc)
 
static Factoryinstance ()
 

Private Member Functions

 Factory ()
 
virtual ~Factory ()
 
int clearKeywordHandlerList ()
 
int copyKeywordHandlerList (std::vector< KeywordHandler * > const &orig)
 
KeywordHandler const * getKeywordHandler (char const *keyword) const
 
int registerCoreKeywords ()
 

Private Attributes

std::vector< KeywordHandler * > keywordHandlerList
 

Detailed Description

The class to generate layers, connections, etc. for adding to a HyPerCol. The function build() in buildandrun.cpp (which underlies all the functions in buildandrun.cpp) uses Factory to build the HyPerCol. Factory is a singleton which is retrieved using the static method Factory::instance().

The standard Factory constructor already registers the objects in the PetaVision core. For example, calling Factory::instance()->create() method with keyword "ANNLayer" returns a new ANNLayer.

If you have a custom class, you need to register it with the Factory object by associating its keyword with a function instantiates a new object of that class, and returns it as a pointer of type BaseObject In most cases, the template Factory::create is sufficient as the instantiator function.

For example:

class CustomLayerType : public HyPerLayer { ... }; ... PV_Init pv_init(&argc, &argv, false); HyPerCol * hc = new HyPerCol("column", &pv_init); pv_init.registerKeyword("CustomLayerType", Factory::create<CustomLayerType>); Factory::instance()->create("customLayerType", hc); ... Note that buildandrun() automates the task of calling the create() method; in practice, you only need to specify the instantiator function, and call the registerKeyword method calling before one of the buildandrun functions.

It is possible to use a custom instantiator function instead of create. The function must take two arguments, the name as a C-style constant string and a pointer to a HyPerCol.

For example: ... BaseObject * createCustomLayerType(char const * name, HyPerCol * hc) { return new CustomLayerType(name, hc); } ... pv_init.registerKeyword("CustomLayerType", createCustomLayerType); ...

Definition at line 66 of file Factory.hpp.

Constructor & Destructor Documentation

PV::Factory::Factory ( )
private

The constructor for Factory. It initializes the list of known keywords to the core PetaVision keywords.

Definition at line 125 of file Factory.cpp.

PV::Factory::~Factory ( )
privatevirtual

The destructor for Factory

Definition at line 302 of file Factory.cpp.

Member Function Documentation

int PV::Factory::clearKeywordHandlerList ( )
private

A method used internally by the copy assignment operator and destructor, to deallocate and clear the keyword handler list.

Definition at line 294 of file Factory.cpp.

int PV::Factory::copyKeywordHandlerList ( std::vector< KeywordHandler * > const &  orig)
private

A method used internally by the copy assignment operator and copy constructor, to copy a keyword handler list into the Factory.

Definition at line 254 of file Factory.cpp.

template<typename T >
static BaseObject* PV::Factory::create ( char const *  name,
HyPerCol hc 
)
inlinestatic

A function template that can be used to register most subclasses of BaseObject in the factory using the registerKeyword function. The requirements on the BaseObject subclass is that it have a constructor with two arguments, the name and a pointer to the HyPerCol.

Definition at line 80 of file Factory.hpp.

BaseObject * PV::Factory::createByKeyword ( char const *  keyword,
char const *  name,
HyPerCol hc 
) const

The method to create an object of the type specified by keyword, with the given name and parent HyPerCol. It calls the function associated with the keyword by the registerKeyword pointer.

Definition at line 271 of file Factory.cpp.

KeywordHandler const * PV::Factory::getKeywordHandler ( char const *  keyword) const
private

A method used internally to retrieve the keyword handler corresponding to a given keyword.

Definition at line 284 of file Factory.cpp.

int PV::Factory::registerCoreKeywords ( )
private

The function called by the default constructor, to add the core PetaVision keywords.

Definition at line 127 of file Factory.cpp.

int PV::Factory::registerKeyword ( char const *  keyword,
ObjectCreateFn  creator 
)

The method to add a new object type to the Factory. keyword is the string that labels the object type, matching the keyword used in params files. creator is a pointer to a function that takes a name and a HyPerCol pointer, and creates an object of the corresponding keyword, with the given name and parent HyPerCol. The function should return a pointer of type BaseObject, created with the new operator.

Definition at line 261 of file Factory.cpp.


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