A Factory can be used to create an instance of a particular subclass of some general base class. More...
#include "factoryBase.h"
Public Types | |
typedef TypedObject * | BaseCreateFunc(const FactoryParams ¶ms) |
Public Member Functions | |
FactoryBase (const FactoryBase ©)=delete | |
void | add_preferred (TypeHandle handle) |
Adds the indicated type to the end of the list of preferred types. More... | |
void | clear_preferred () |
Empties the list of preferred types. More... | |
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. More... | |
size_t | get_num_preferred () const |
Returns the number of types added to the preferred-type list. More... | |
size_t | get_num_types () const |
Returns the number of different types the Factory knows how to create. More... | |
TypeHandle | get_preferred (size_t n) const |
Returns the nth type added to the preferred-type list. More... | |
TypeHandle | get_type (size_t n) const |
Returns the nth type the Factory knows how to create. More... | |
TypedObject * | make_instance (TypeHandle handle, const FactoryParams ¶ms) |
Attempts to create a new instance of some class of the indicated type, or some derivative if necessary. More... | |
TypedObject * | make_instance (const std::string &type_name, const FactoryParams ¶ms) |
Attempts to create a new instance of some class of the indicated type, or some derivative if necessary. More... | |
TypedObject * | make_instance_more_general (TypeHandle handle, const FactoryParams ¶ms) |
Attempts to create an instance of the type requested, or some base type of the type requested. More... | |
TypedObject * | make_instance_more_general (const std::string &type_name, const FactoryParams ¶ms) |
Attempts to create an instance of the type requested, or some base type of the type requested. More... | |
FactoryBase & | operator= (const FactoryBase ©)=delete |
void | register_factory (TypeHandle handle, BaseCreateFunc *func, void *user_data=nullptr) |
Registers a new kind of thing the Factory will be able to create. More... | |
void | write_types (std::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. More... | |
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.
FactoryBase is the main definition of the thin template class Factory.
Definition at line 36 of file factoryBase.h.
void FactoryBase::add_preferred | ( | TypeHandle | handle | ) |
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 174 of file factoryBase.cxx.
void FactoryBase::clear_preferred | ( | ) |
Empties the list of preferred types.
Definition at line 164 of file factoryBase.cxx.
TypeHandle FactoryBase::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.
If no ancestor of the indicated type is registered, returns TypeHandle::none().
Definition at line 90 of file factoryBase.cxx.
References TypeHandle::get_num_parent_classes.
size_t FactoryBase::get_num_preferred | ( | ) | const |
Returns the number of types added to the preferred-type list.
Definition at line 183 of file factoryBase.cxx.
size_t FactoryBase::get_num_types | ( | ) | const |
Returns the number of different types the Factory knows how to create.
Definition at line 135 of file factoryBase.cxx.
TypeHandle FactoryBase::get_preferred | ( | size_t | n | ) | const |
Returns the nth type added to the preferred-type list.
Definition at line 191 of file factoryBase.cxx.
TypeHandle FactoryBase::get_type | ( | size_t | n | ) | const |
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 145 of file factoryBase.cxx.
TypedObject * FactoryBase::make_instance | ( | TypeHandle | handle, |
const FactoryParams & | params | ||
) |
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.
Definition at line 25 of file factoryBase.cxx.
Referenced by Factory< Type >::make_instance().
|
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 priorities 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.
Definition at line 24 of file factoryBase.I.
References TypeRegistry::find_type(), and TypeRegistry::ptr().
TypedObject * FactoryBase::make_instance_more_general | ( | TypeHandle | handle, |
const FactoryParams & | params | ||
) |
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.
Definition at line 53 of file factoryBase.cxx.
References TypeHandle::get_num_parent_classes.
Referenced by Factory< Type >::make_instance_more_general().
|
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.
Definition at line 42 of file factoryBase.I.
References TypeRegistry::find_type(), and TypeRegistry::ptr().
void FactoryBase::register_factory | ( | TypeHandle | handle, |
BaseCreateFunc * | func, | ||
void * | user_data = nullptr |
||
) |
Registers a new kind of thing the Factory will be able to create.
user_data | an optional pointer to be passed along to the function. |
Definition at line 121 of file factoryBase.cxx.
Referenced by Factory< Type >::register_factory().
void FactoryBase::write_types | ( | std::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.
Definition at line 201 of file factoryBase.cxx.
References indent().