15 #include "factoryBase.h"
17 #include "config_util.h"
50 instance = make_instance_exact(handle, params);
53 instance = make_instance_more_specific(handle, params);
56 if (util_cat.is_debug()) {
58 <<
"make_instance(" << handle <<
", params) returns "
62 <<
", of type " << instance->get_type();
64 util_cat.debug(
false) <<
"\n";
79 TypedObject *
object = make_instance_exact(handle, params);
89 for (
int i = 0; i < num_parents &&
object == (
TypedObject *)NULL; i++) {
94 if (util_cat.is_debug()) {
96 <<
"make_instance(" << handle <<
", params) returns "
100 <<
", of type " <<
object->get_type();
102 util_cat.debug(
false) <<
"\n";
120 Creators::const_iterator ci = _creators.find(handle);
121 if (ci != _creators.end()) {
133 for (
int i = 0; i < num_parents; i++) {
153 nassertv(func != (BaseCreateFunc *)NULL);
154 _creators[handle] = func;
165 return _creators.size();
180 Creators::const_iterator ci;
181 for (ci = _creators.begin(); ci != _creators.end(); ++ci) {
215 _preferred.push_back(handle);
226 return _preferred.size();
238 return _preferred[n];
249 Creators::const_iterator ci;
250 for (ci = _creators.begin(); ci != _creators.end(); ++ci) {
251 indent(out, indent_level) << (*ci).first <<
"\n";
284 Creators::const_iterator ci = _creators.find(handle);
285 if (ci == _creators.end()) {
289 BaseCreateFunc *func = (BaseCreateFunc *)((*ci).second);
290 nassertr(func != (BaseCreateFunc *)NULL, NULL);
291 return (*func)(params);
307 Preferred::const_iterator pi;
308 for (pi = _preferred.begin(); pi != _preferred.end(); ++pi) {
311 TypedObject *
object = make_instance_exact(ptype, params);
321 Creators::const_iterator ci;
322 for (ci = _creators.begin(); ci != _creators.end(); ++ci) {
325 BaseCreateFunc *func = (BaseCreateFunc *)((*ci).second);
326 nassertr(func != (BaseCreateFunc *)NULL, NULL);
bool is_derived_from(TypeHandle parent, TypedObject *object=(TypedObject *) NULL) const
Returns true if this type is derived from the indicated type, false otherwise.
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 necessar...
void register_factory(TypeHandle handle, BaseCreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
TypeHandle get_parent_class(int index) const
Returns the nth parent class of this type.
int get_num_preferred() const
Returns the number of types added to the preferred-type list.
int get_num_types() const
Returns the number of different types the Factory knows how to create.
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
void clear_preferred()
Empties the list of preferred types.
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.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
TypeHandle get_type(int n) const
Returns the nth type the Factory knows how to create.
A Factory can be used to create an instance of a particular subclass of some general base class...
TypeHandle is the identifier used to differentiate C++ class types.
void add_preferred(TypeHandle handle)
Adds the indicated type to the end of the list of preferred types.
int get_num_parent_classes(TypedObject *object=(TypedObject *) NULL) const
Returns the number of parent classes that this type is known to have.
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.
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...