Panda3D
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  */
36 class EXPCL_PANDA_PUTIL FactoryBase {
37 public:
38  typedef TypedObject *BaseCreateFunc(const FactoryParams &params);
39 
40  // public interface
41 public:
42  FactoryBase() = default;
43  FactoryBase(const FactoryBase &copy) = delete;
44  ~FactoryBase() = default;
45 
46  FactoryBase &operator = (const FactoryBase &copy) = delete;
47 
48  TypedObject *make_instance(TypeHandle handle,
49  const FactoryParams &params);
50 
51  INLINE TypedObject *make_instance(const std::string &type_name,
52  const FactoryParams &params);
53 
54  TypedObject *make_instance_more_general(TypeHandle handle,
55  const FactoryParams &params);
56 
57  INLINE TypedObject *make_instance_more_general(const std::string &type_name,
58  const FactoryParams &params);
59 
60  TypeHandle find_registered_type(TypeHandle handle);
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 
74 private:
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 
80 private:
81  // internal mechanics and bookkeeping
82  struct Creator {
83  BaseCreateFunc *_func;
84  void *_user_data;
85  };
86 
88  Creators _creators;
89 
91  Preferred _preferred;
92 };
93 
94 #include "factoryBase.I"
95 
96 #endif /* FACTORY_H */
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A Factory can be used to create an instance of a particular subclass of some general base class.
Definition: factoryBase.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.