PFactoryBase Class Reference

#include <pfactory.h>

Inheritance diagram for PFactoryBase:

PFactory< _Abstract_T, _Key_T > PDevicePluginFactory< _Abstract_T, _Key_T >

List of all members.

Public Member Functions

virtual ~PFactoryBase ()

Static Public Member Functions

static FactoryMapGetFactories ()
static PMutexGetFactoriesMutex ()

Public Attributes

PMutex mutex

Protected Member Functions

 PFactoryBase ()

Classes

class  FactoryMap


Detailed Description

These templates implement an Abstract Factory that allows creation of a class "factory" that can be used to create "concrete" instance that are descended from a abstract base class

Given an abstract class A with a descendant concrete class B, the concrete class is registered by instantiating the PFactory template as follows:

PFactory<A>::Worker<B> aFactory("B");

To instantiate an object of type B, use the following:

A * b = PFactory<A>::CreateInstance("B");

A vector containing the names of all of the concrete classes for an abstract type can be obtained as follows:

PFactory<A>::KeyList_T list = PFactory<A>::GetKeyList()

Note that these example assumes that the "key" type for the factory registration is of the default type PString. If a different key type is needed, then it is necessary to specify the key type:

PFactory<C, unsigned>::Worker<D> aFactory(42); C * d = PFactory<C, unsigned>::CreateInstance(42); PFactory<C, unsigned>::KeyList_T list = PFactory<C, unsigned>::GetKeyList()

The factory functions also allow the creation of "singleton" factories that return a single instance for all calls to CreateInstance. This can be done by passing a "true" as a second paramater to the factory registration as shown below, which will cause a single instance to be minted upon the first call to CreateInstance, and then returned for all subsequent calls.

PFactory<A>::Worker<E> eFactory("E", true);

It is also possible to manually set the instance in cases where the object needs to be created non-trivially.

The following types are defined as part of the PFactory template class:

KeyList_T a vector<> of the key type (usually std::string) Worker an abstract factory for a specified concrete type KeyMap_T a map<> that converts from the key type to the Worker instance for each concrete type registered for a specific abstract type

As a side issue, note that the factory lists are all thread safe for addition, creation, and obtaining the key lists. Base class for generic factories. This classes reason for existance and the FactoryMap contained within it is to resolve issues with static global construction order and Windows DLL multiple instances issues. THis mechanism guarantees that the one and one only global variable (inside the GetFactories() function) is initialised before any other factory related instances of classes.


Constructor & Destructor Documentation

PFactoryBase::PFactoryBase (  )  [inline, protected]

virtual PFactoryBase::~PFactoryBase (  )  [inline, virtual]


Member Function Documentation

static FactoryMap& PFactoryBase::GetFactories (  )  [static]

static PMutex& PFactoryBase::GetFactoriesMutex (  )  [static]


Member Data Documentation

PMutex PFactoryBase::mutex


The documentation for this class was generated from the following file:
Generated on Fri Sep 21 14:40:13 2007 for PWLib by  doxygen 1.5.3