Panda3D
 All Classes Functions Variables Enumerations
factoryBase.h
1 // Filename: factoryBase.h
2 // Created by: cary (06Oct99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef FACTORYBASE_H
16 #define FACTORYBASE_H
17 
18 #include "pandabase.h"
19 
20 #include "typedObject.h"
21 #include "typedReferenceCount.h"
22 #include "factoryParams.h"
23 
24 #include "pvector.h"
25 #include "pmap.h"
26 
27 ////////////////////////////////////////////////////////////////////
28 // Class : FactoryBase
29 // Description : A Factory can be used to create an instance of a
30 // particular subclass of some general base class. Each
31 // subclass registers itself with the Factory, supplying
32 // a function that will construct an instance of that
33 // subclass; the Factory can later choose a suitable
34 // subclass and return a newly-constructed pointer to an
35 // object of that type on the user's demand. This is
36 // used, for instance, to manage the set of
37 // GraphicsPipes available to the user.
38 //
39 // FactoryBase is the main definition of the thin
40 // template class Factory.
41 ////////////////////////////////////////////////////////////////////
42 class EXPCL_PANDA_PUTIL FactoryBase {
43 public:
44  typedef TypedObject *BaseCreateFunc(const FactoryParams &params);
45 
46  // public interface
47 public:
48  FactoryBase();
49  ~FactoryBase();
50 
51  TypedObject *make_instance(TypeHandle handle,
52  const FactoryParams &params);
53 
54  INLINE TypedObject *make_instance(const string &type_name,
55  const FactoryParams &params);
56 
57  TypedObject *make_instance_more_general(TypeHandle handle,
58  const FactoryParams &params);
59 
60  INLINE TypedObject *make_instance_more_general(const string &type_name,
61  const FactoryParams &params);
62 
63  TypeHandle find_registered_type(TypeHandle handle);
64 
65  void register_factory(TypeHandle handle, BaseCreateFunc *func);
66 
67  int get_num_types() const;
68  TypeHandle get_type(int n) const;
69 
70  void clear_preferred();
71  void add_preferred(TypeHandle handle);
72  int get_num_preferred() const;
73  TypeHandle get_preferred(int n) const;
74 
75  void write_types(ostream &out, int indent_level = 0) const;
76 
77 private:
78  // These are private; we shouldn't be copy-constructing Factories.
79  FactoryBase(const FactoryBase &copy);
80  void operator = (const FactoryBase &copy);
81 
82  // internal utility functions
83  TypedObject *make_instance_exact(TypeHandle handle,
84  const FactoryParams &params);
85  TypedObject *make_instance_more_specific(TypeHandle handle,
86  const FactoryParams &params);
87 
88 private:
89  // internal mechanics and bookkeeping
90 
91 #if (defined(WIN32_VC) || defined(WIN64_VC)) && !defined(__ICL) //__ICL is Intel C++
92  // Visual C++ seems to have a problem with building a map based on
93  // BaseCreateFunc. We'll have to typecast it on the way out.
95 #else
97 #endif
98 
99  Creators _creators;
100 
102  Preferred _preferred;
103 };
104 
105 #include "factoryBase.I"
106 
107 #endif /* FACTORY_H */
This is our own Panda specialization on the default STL map.
Definition: pmap.h:52
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
A Factory can be used to create an instance of a particular subclass of some general base class...
Definition: factoryBase.h:42
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85