Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Factory< Type > Class Template Reference

A Factory can be used to create an instance of a particular subclass of some general base class. More...

#include <factory.h>

Inheritance diagram for Factory< Type >:

FactoryBase List of all members.

Public Types

typedef Type * CreateFunc (const FactoryParams &params)
typedef TypedObjectBaseCreateFunc (const FactoryParams &params)

Public Member Functions

Type * make_instance (TypeHandle handle, const FactoryParams &params=FactoryParams())
 Attempts to create a new instance of some class of the indicated type, or some derivative if necessary.

Type * make_instance (const string &type_name, const FactoryParams &params=FactoryParams())
 Attempts to create a new instance of some class of the indicated type, or some derivative if necessary.

Type * make_instance_more_general (TypeHandle handle, const FactoryParams &params=FactoryParams())
 Attempts to create an instance of the type requested, or some base type of the type requested.

Type * make_instance_more_general (const string &type_name, const FactoryParams &params=FactoryParams())
 Attempts to create an instance of the type requested, or some base type of the type requested.

void register_factory (TypeHandle handle, CreateFunc *func)
 Registers a new kind of thing the Factory will be able to create.

TypeHandle find_registered_type (TypeHandle handle)
 Returns the TypeHandle given, if it is a registered type, or if it is not registered, searches for the nearest ancestor of the indicated type that is registered and returns it.

void register_factory (TypeHandle handle, BaseCreateFunc *func)
 Registers a new kind of thing the Factory will be able to create.

int get_num_types () const
 Returns the number of different types the Factory knows how to create.

TypeHandle get_type (int n) const
 Returns the nth type the Factory knows how to create.

void clear_preferred ()
 Empties the list of preferred types.

void add_preferred (TypeHandle handle)
 Adds the indicated type to the end of the list of preferred types.

int get_num_preferred () const
 Returns the number of types added to the preferred-type list.

TypeHandle get_preferred (int n) const
 Returns the nth type added to the preferred-type list.

void write_types (ostream &out, int indent_level=0) const
 Writes a list of all known types the Factory can create to the indicated output stream, one per line.


Detailed Description

template<class Type>
class Factory< Type >

A Factory can be used to create an instance of a particular subclass of some general base class.

Each subclass registers itself with the Factory, supplying a function that will construct an instance of that subclass; the Factory can later choose a suitable subclass and return a newly-constructed pointer to an object of that type on the user's demand. This is used, for instance, to manage the set of GraphicsPipes available to the user.

This is a thin template wrapper around FactoryBase. All it does is ensure the types are correctly cast. All of its methods are inline, and it has no data members, so it is not necessary to export the class from the DLL.

Definition at line 65 of file factory.h.


Member Typedef Documentation

typedef TypedObject* FactoryBase::BaseCreateFunc(const FactoryParams &params) [inherited]
 

Definition at line 64 of file factoryBase.h.

template<class Type>
typedef Type* Factory< Type >::CreateFunc(const FactoryParams &params)
 

Definition at line 67 of file factory.h.


Member Function Documentation

void FactoryBase::add_preferred TypeHandle  handle  )  [inherited]
 

Adds the indicated type to the end of the list of preferred types.

On the next call to make_instance(), if the exact type requested cannot be created, the preferred types are first tried in the order specified.

Definition at line 270 of file factoryBase.cxx.

void FactoryBase::clear_preferred  )  [inherited]
 

Empties the list of preferred types.

Definition at line 249 of file factoryBase.cxx.

References FactoryBase::_creators, and indent().

TypeHandle FactoryBase::find_registered_type TypeHandle  handle  )  [inherited]
 

Returns the TypeHandle given, if it is a registered type, or if it is not registered, searches for the nearest ancestor of the indicated type that is registered and returns it.

If no ancestor of the indicated type is registered, returns TypeHandle::none().

Definition at line 151 of file factoryBase.cxx.

References FactoryBase::_creators, nassertv, TypeHandle::none(), and NULL.

int FactoryBase::get_num_preferred  )  const [inherited]
 

Returns the number of types added to the preferred-type list.

Definition at line 286 of file factoryBase.cxx.

References FactoryBase::_creators, nassertr, and NULL.

int FactoryBase::get_num_types  )  const [inherited]
 

Returns the number of different types the Factory knows how to create.

Definition at line 204 of file factoryBase.cxx.

References FactoryBase::_preferred.

TypeHandle FactoryBase::get_preferred int  n  )  const [inherited]
 

Returns the nth type added to the preferred-type list.

Definition at line 301 of file factoryBase.cxx.

References FactoryBase::_preferred, TypeHandle::is_derived_from(), FactoryBase::make_instance_exact(), and NULL.

TypeHandle FactoryBase::get_type int  n  )  const [inherited]
 

Returns the nth type the Factory knows how to create.

This is not a terribly efficient function; it's included primarily for debugging output. Normally you wouldn't need to traverse the list of the Factory's types.

Definition at line 225 of file factoryBase.cxx.

References FactoryBase::_preferred.

template<class Type>
Type * Factory< Type >::make_instance const string &  type_name,
const FactoryParams params = FactoryParams()
[inline]
 

Attempts to create a new instance of some class of the indicated type, or some derivative if necessary.

If an instance of the exact type cannot be created, the specified preferred will specify which derived class will be preferred.

This flavor of make_instance() accepts a string name that indicates the desired type. It must be the name of some already-registered type.

Reimplemented from FactoryBase.

Definition at line 70 of file factory.I.

References INLINE, and FactoryBase::make_instance_more_general().

template<class Type>
Type * Factory< Type >::make_instance TypeHandle  handle,
const FactoryParams params = FactoryParams()
[inline]
 

Attempts to create a new instance of some class of the indicated type, or some derivative if necessary.

If an instance of the exact type cannot be created, the specified preferred will specify which derived class will be preferred.

Reimplemented from FactoryBase.

Definition at line 40 of file factory.I.

References INLINE, and FactoryBase::make_instance().

template<class Type>
Type * Factory< Type >::make_instance_more_general const string &  type_name,
const FactoryParams params = FactoryParams()
[inline]
 

Attempts to create an instance of the type requested, or some base type of the type requested.

Returns the new instance created, or NULL if the instance could not be created.

This flavor of make_instance_more_general() accepts a string name that indicates the desired type. It must be the name of some already-registered type.

Reimplemented from FactoryBase.

Definition at line 118 of file factory.I.

template<class Type>
Type * Factory< Type >::make_instance_more_general TypeHandle  handle,
const FactoryParams params = FactoryParams()
[inline]
 

Attempts to create an instance of the type requested, or some base type of the type requested.

Returns the new instance created, or NULL if the instance could not be created.

Reimplemented from FactoryBase.

Definition at line 90 of file factory.I.

References INLINE, and FactoryBase::register_factory().

void FactoryBase::register_factory TypeHandle  handle,
BaseCreateFunc func
[inherited]
 

Registers a new kind of thing the Factory will be able to create.

Definition at line 187 of file factoryBase.cxx.

Referenced by Factory< Type >::make_instance_more_general().

template<class Type>
void Factory< Type >::register_factory TypeHandle  handle,
CreateFunc func
[inline]
 

Registers a new kind of thing the Factory will be able to create.

Definition at line 135 of file factory.I.

void FactoryBase::write_types ostream &  out,
int  indent_level = 0
const [inherited]
 

Writes a list of all known types the Factory can create to the indicated output stream, one per line.

Definition at line 317 of file factoryBase.cxx.


The documentation for this class was generated from the following files:
Generated on Fri May 2 00:50:35 2003 for Panda by doxygen1.3