Panda3D
Loading...
Searching...
No Matches
factoryBase.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file factoryBase.h
10 * @author cary
11 * @date 1999-10-06
12 */
13
14#ifndef FACTORYBASE_H
15#define FACTORYBASE_H
16
17#include "pandabase.h"
18
19#include "typedObject.h"
20#include "typedReferenceCount.h"
21#include "factoryParams.h"
22
23#include "pvector.h"
24#include "pmap.h"
25
26/**
27 * A Factory can be used to create an instance of a particular subclass of
28 * some general base class. Each subclass registers itself with the Factory,
29 * supplying a function that will construct an instance of that subclass; the
30 * Factory can later choose a suitable subclass and return a newly-constructed
31 * pointer to an object of that type on the user's demand. This is used, for
32 * instance, to manage the set of GraphicsPipes available to the user.
33 *
34 * FactoryBase is the main definition of the thin template class Factory.
35 */
36class EXPCL_PANDA_PUTIL FactoryBase {
37public:
38 typedef TypedObject *BaseCreateFunc(const FactoryParams &params);
39
40 // public interface
41public:
42 FactoryBase() = default;
43 FactoryBase(const FactoryBase &copy) = delete;
44 ~FactoryBase() = default;
45
46 FactoryBase &operator = (const FactoryBase &copy) = delete;
47
49 const FactoryParams &params);
50
51 INLINE TypedObject *make_instance(const std::string &type_name,
52 const FactoryParams &params);
53
55 const FactoryParams &params);
56
57 INLINE TypedObject *make_instance_more_general(const std::string &type_name,
58 const FactoryParams &params);
59
61
62 void register_factory(TypeHandle handle, BaseCreateFunc *func, void *user_data = nullptr);
63
64 size_t get_num_types() const;
65 TypeHandle get_type(size_t n) const;
66
67 void clear_preferred();
68 void add_preferred(TypeHandle handle);
69 size_t get_num_preferred() const;
70 TypeHandle get_preferred(size_t n) const;
71
72 void write_types(std::ostream &out, int indent_level = 0) const;
73
74private:
75 // internal utility functions
76 TypedObject *make_instance_exact(TypeHandle handle, FactoryParams params);
77 TypedObject *make_instance_more_specific(TypeHandle handle,
78 FactoryParams params);
79
80private:
81 // internal mechanics and bookkeeping
82 struct Creator {
83 BaseCreateFunc *_func;
84 void *_user_data;
85 };
86
87 typedef pmap<TypeHandle, Creator> Creators;
88 Creators _creators;
89
90 typedef pvector<TypeHandle> Preferred;
91 Preferred _preferred;
92};
93
94#include "factoryBase.I"
95
96#endif /* FACTORY_H */
void register_factory(TypeHandle handle, BaseCreateFunc *func, void *user_data=nullptr)
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,...
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,...
TypeHandle get_type(size_t n) const
Returns the nth type the Factory knows how to create.
TypedObject * make_instance(TypeHandle handle, const FactoryParams &params)
Attempts to create a new instance of some class of the indicated type, or some derivative if necessar...
size_t get_num_preferred() const
Returns the number of types added to the preferred-type list.
void add_preferred(TypeHandle handle)
Adds the indicated type to the end of the list of preferred types.
TypeHandle get_preferred(size_t n) const
Returns the nth type added to the preferred-type list.
void clear_preferred()
Empties the list of preferred types.
TypedObject * 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.
size_t get_num_types() const
Returns the number of different types the Factory knows how to create.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition typedObject.h:88
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.