CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
Classes | Public Member Functions | Private Types | Private Attributes
claw::pattern::factory< BaseClass, IdentifierType > Class Template Reference

The design pattern of the factory allow to dynamically instanciate classes of various types given an identifier of this type. More...

#include <factory.hpp>

Inheritance diagram for claw::pattern::factory< BaseClass, IdentifierType >:
claw::pattern::basic_singleton< factory< BaseClass, IdentifierType > > claw::pattern::non_copyable

List of all members.

Classes

class  class_creator
 A class creator is an object that allocate a given type. More...
class  class_creator_base
 A class creator is an object that allocate a given type. This is the base class for all class creators. More...

Public Member Functions

 ~factory ()
 Destructor.
template<typename T >
bool register_type (const identifier_type &id)
 Register a type in the factory.
base_classcreate (const identifier_type &id) const
 Create a new instance.
bool is_known_type (const identifier_type &id) const
 Tell in a type with a given id is known by the factory.

Private Types

typedef IdentifierType identifier_type
 The type of the data used to identify a class.
typedef BaseClass base_class
 A base class for all instances created by this factory.
typedef std::map
< identifier_type,
class_creator_base * > 
class_map
 The association identifier <-> type.

Private Attributes

class_map m_classes
 The association identifier <-> type.

Detailed Description

template<typename BaseClass, typename IdentifierType>
class claw::pattern::factory< BaseClass, IdentifierType >

The design pattern of the factory allow to dynamically instanciate classes of various types given an identifier of this type.

Template parameters:

Type requirements:

Definition at line 70 of file factory.hpp.


Member Typedef Documentation

template<typename BaseClass, typename IdentifierType>
typedef BaseClass claw::pattern::factory< BaseClass, IdentifierType >::base_class [private]

A base class for all instances created by this factory.

Definition at line 108 of file factory.hpp.

template<typename BaseClass, typename IdentifierType>
typedef std::map<identifier_type, class_creator_base*> claw::pattern::factory< BaseClass, IdentifierType >::class_map [private]

The association identifier <-> type.

Definition at line 111 of file factory.hpp.

template<typename BaseClass, typename IdentifierType>
typedef IdentifierType claw::pattern::factory< BaseClass, IdentifierType >::identifier_type [private]

The type of the data used to identify a class.

Definition at line 105 of file factory.hpp.


Constructor & Destructor Documentation

template<typename BaseClass , typename IdentifierType >
claw::pattern::factory< BaseClass, IdentifierType >::~factory ( )

Destructor.

Definition at line 65 of file factory.tpp.

{
  typename class_map::const_iterator it;

  for (it=m_classes.begin(); it!=m_classes.end(); ++it)
    delete it->second;
} // factory::~factory()

Member Function Documentation

template<typename BaseClass , typename IdentifierType >
claw::pattern::factory< BaseClass, IdentifierType >::base_class * claw::pattern::factory< BaseClass, IdentifierType >::create ( const identifier_type id) const

Create a new instance.

Parameters:
idThe identifier of the type to instanciate.
Returns:
A pointer to the newly created instance.

Definition at line 108 of file factory.tpp.

{
  typename class_map::const_iterator it = m_classes.find(id);

  if ( it==m_classes.end() )
    throw bad_type_identifier();
  else
    return it->second->create();
} // factory::create()
template<typename BaseClass , typename IdentifierType >
bool claw::pattern::factory< BaseClass, IdentifierType >::is_known_type ( const identifier_type id) const

Tell in a type with a given id is known by the factory.

Parameters:
idThe identifier of the type to check.

Definition at line 125 of file factory.tpp.

{
  return m_classes.find(id) != m_classes.end();
} // factory::is_known_type()
template<typename BaseClass , typename IdentifierType >
template<typename T >
bool claw::pattern::factory< BaseClass, IdentifierType >::register_type ( const identifier_type id)

Register a type in the factory.

Parameters:
idThe identifier to which the type is associated.
Returns:
true if successful.

Template parameters:

  • T The type to register.

Definition at line 86 of file factory.tpp.

{
  typename class_map::iterator it = m_classes.find(id);

  if ( it == m_classes.end() )
    {
      m_classes[id] = new class_creator<T>;
      return true;
    }
  else
    return false;
} // factory::register_type()

Member Data Documentation

template<typename BaseClass, typename IdentifierType>
class_map claw::pattern::factory< BaseClass, IdentifierType >::m_classes [private]

The association identifier <-> type.

Definition at line 125 of file factory.hpp.


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