|
Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members File Members
EkFactoryT Class Template ReferenceThe EkFactoryT class provides the generic functionality for an object factory.
More...
#include <EkFactoryT.h>
Inheritance diagram for EkFactoryT: [legend]List of all members.
Public Methods |
| EkFactoryT () |
| This is the default constructor. More...
|
| ~EkFactoryT () |
| This is the destructor. More...
|
FacType | create (const FacKeyType &facKey) |
| Return a copy of the prototype with the factory key facKey. More...
|
FacType | get (const FacKeyType &facKey) |
| Returns original prototype with the factory key facKey. More...
|
bool | addEntry (const FacKeyType &facKey, const FacType item) |
| adds a new prototype to the factory with factory key, facKey, by creating a copy of "item". More...
|
bool | removeEntry (const FacKeyType &facKey) |
| remove the prototype from the factory with factory key, facKey. More...
|
FacType | start () |
| Non factory key based access to the prototypes. More...
|
FacType | next () |
| This returns the next item in the factory. More...
|
void | clear () |
| This clears the entire factory. More...
|
read_only_iterator | getIterator () |
Protected Methods |
typedef | EK_DLL_MAP (FacKeyType, FacType) RegistryType |
Protected Attributes |
RegistryType | mRegistry |
EK_TYPENAME RegistryType::iterator | mCurr |
EK_DEFAULT_MUTEX | mRegistryLock |
Friends |
class | read_only_iterator |
Detailed Description
template<class FacKeyType, class FacType>
class EkFactoryT< FacKeyType, FacType >
The EkFactoryT class provides the generic functionality for an object factory.
In this case, the factory comprises object prototypes and their associated factory keys. The factory requires the clone() method to be present on the interface of the class whose specializations can be created through the factory. The EkFactoryT class is templated with two parameters: the factory key type, and the base class pointer type.
The EkFactoryT class can be used in one of two ways. First, create an instance of EkFactoryT in the base class and provide static methods on the base class interface that use the factory for adding, deleting, and creating derived classes, e.g.,
class Widget ;
typedef EkSmartPtr<Widget> WidgetPtr ;
typedef SPmtString WidgetKeyType ;
typedef EkFactoryT<WidgetKeyType, WidgetPtr> WidgetFactoryType;
class Widget : public EkRefCount<EK_DEFAULT_MUTEX>
{
public:
Widget(const WidgetKeyType& theKey, const WidgetKeyType& theFactoryKey )
:key(theKey), factoryKey(theFactoryKey)
{}
~Widget(void) {}
static WidgetPtr createFromFactory( const WidgetKeyType& widgetKey, const WidgetKeyType& factoryKey ) ;
// Provide access to addEntry and removeEntry methods
static WidgetFactoryType& getFactory( void ) { return factory ; }
virtual WidgetPtr clone( void ) = 0 ;
const WidgetKeyType& getKey( void ) const { return key ; }
void setKey( WidgetKeyType& theKey ) { key = theKey; return; }
const WidgetKeyType& getFactoryKey( void ) const { return factoryKey ; }
void setFactoryKey( WidgetKeyType& facKey ) { factoryKey = facKey; return; }
private:
static WidgetFactoryType factory ;
WidgetKeyType key ;
WidgetKeyType factoryKey ;
};
Usage:
WidgetPtr aWidget = Widget::createFromFactory( "myWidget", "WidgetA" ) ;
Second, derive a specialized factory by inheriting from
EkFactoryT, e.g.,
class WidgetFactory : public EkFactoryT<WidgetKeyType, WidgetPtr>
{
public:
WidgetFactory( void ) {}
~WidgetFactory( void ) {}
};
Usage:
WidgetFactory widgetFactory ;
WidgetPtr aWidget = widgetFactory.create( "WidgetA" ) ;
aWidget->setKey( "myWidget" ) ;
Prototype registration:
There are two methods by which prototypes can be added to the factory. First, add an initialization method to either the base class or specialized factory class that instantiates all the specializations and adds them to the factory. One draw back with this technique is that this method must be modified each time a new specialization is created. Another technique is to allow specializations to register themselves from their implementation file. The EK_PROVIDE macro provides this capability with the addition of a specialized macro in the base class header. For example, in the Widget base class header file, we define the following macro:
define WIDGET_TYPE(facKey, widgetType) Widget::getFactory().addEntry( facKey, new widgetType("", facKey) )
Then in the specialization's implementation file, in this case WidgetA, we add,
EK_PROVIDE(WidgetA, MMA_METADATA_TYPE("WidgetA", WidgetA))
to register the specialization, WidgetA, with the factory. Additional control over the availability of prototypes in the factory (and what specializations are linked into the application) is given to the application through the EK_REQUIRE macro. For example, an application may only need access to WidgetA, WidgetB, and WidgetC. In this case, the following would be added to the application code (just outside of main()):
EK_REQUIRE(WidgetA)
EK_REQUIRE(WidgetB)
EK_REQUIRE(WidgetC)
As a convienence to applications that require all instances, one may define an inplementation file that captures all instances, e.g.,
File AllWidget.cpp:
EK_REQUIRE(WidgetA)
EK_REQUIRE(WidgetB)
EK_REQUIRE(WidgetC)
EK_REQUIRE(WidgetD)
EK_REQUIRE(WidgetE)
EK_PROVIDE(AllWidgets, NULL )
The those applications requiring access to all widgets would just need:
EK_REQUIRE(AllWidgets)
Constructor & Destructor Documentation
template<class FacKeyType, class FacType> |
EkFactoryT< FacKeyType, FacType >::EkFactoryT |
( |
|
) |
[inline] |
|
|
This is the default constructor.
|
template<class FacKeyType, class FacType> |
EkFactoryT< FacKeyType, FacType >::~EkFactoryT |
( |
|
) |
[inline] |
|
Member Function Documentation
template<class FacKeyType, class FacType> |
bool EkFactoryT< FacKeyType, FacType >::addEntry |
( |
const FacKeyType & |
facKey, |
|
|
const FacType |
item |
|
) |
[inline] |
|
|
adds a new prototype to the factory with factory key, facKey, by creating a copy of "item".
Returns "true" if successful, "false" otherwise. |
template<class FacKeyType, class FacType> |
void EkFactoryT< FacKeyType, FacType >::clear |
( |
|
) |
[inline] |
|
|
This clears the entire factory.
|
template<class FacKeyType, class FacType> |
FacType EkFactoryT< FacKeyType, FacType >::create |
( |
const FacKeyType & |
facKey |
) |
[inline] |
|
|
Return a copy of the prototype with the factory key facKey.
|
template<class FacKeyType, class FacType> |
typedef EkFactoryT< FacKeyType, FacType >::EK_DLL_MAP |
( |
FacKeyType |
, |
|
|
FacType |
|
|
) |
[protected] |
|
template<class FacKeyType, class FacType> |
FacType EkFactoryT< FacKeyType, FacType >::get |
( |
const FacKeyType & |
facKey |
) |
[inline] |
|
|
Returns original prototype with the factory key facKey.
|
template<class FacKeyType, class FacType> |
EK_TYPENAME EkFactoryT< FacKeyType, FacType >::read_only_iterator EkFactoryT< FacKeyType, FacType >::getIterator |
( |
|
) |
[inline] |
|
template<class FacKeyType, class FacType> |
FacType EkFactoryT< FacKeyType, FacType >::next |
( |
|
) |
[inline] |
|
|
This returns the next item in the factory.
This function is deprecated. Use getIterator(). |
template<class FacKeyType, class FacType> |
bool EkFactoryT< FacKeyType, FacType >::removeEntry |
( |
const FacKeyType & |
facKey |
) |
[inline] |
|
|
remove the prototype from the factory with factory key, facKey.
returns "true" if successful, "false" otherwise. |
template<class FacKeyType, class FacType> |
FacType EkFactoryT< FacKeyType, FacType >::start |
( |
|
) |
[inline] |
|
|
Non factory key based access to the prototypes.
This function is deprecated. Use getIterator(). |
Friends And Related Function Documentation
template<class FacKeyType, class FacType> |
friend class read_only_iterator [friend]
|
|
Member Data Documentation
template<class FacKeyType, class FacType> |
EK_TYPENAME RegistryType::iterator EkFactoryT::mCurr [protected]
|
|
template<class FacKeyType, class FacType> |
RegistryType EkFactoryT::mRegistry [protected]
|
|
template<class FacKeyType, class FacType> |
EK_DEFAULT_MUTEX EkFactoryT::mRegistryLock [protected]
|
|
The documentation for this class was generated from the following file:
|