Panda3D
 All Classes Functions Variables Enumerations
typeRegistry.h
1 // Filename: typeRegistry.h
2 // Created by: drose (06Aug01)
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 TYPEREGISTRY_H
16 #define TYPEREGISTRY_H
17 
18 #include "dtoolbase.h"
19 #include "mutexImpl.h"
20 #include "memoryBase.h"
21 
22 #include <set>
23 #include <map>
24 #include <vector>
25 
26 class TypeHandle;
27 class TypeRegistryNode;
28 class TypedObject;
29 
30 ////////////////////////////////////////////////////////////////////
31 // Class : TypeRegistry
32 // Description : The TypeRegistry class maintains all the assigned
33 // TypeHandles in a given system. There should be only
34 // one TypeRegistry class during the lifetime of the
35 // application. It will be created on the local heap
36 // initially, and it should be migrated to shared memory
37 // as soon as shared memory becomes available.
38 ////////////////////////////////////////////////////////////////////
39 class EXPCL_DTOOL TypeRegistry : public MemoryBase {
40 public:
41  // User code shouldn't generally need to call
42  // TypeRegistry::register_type() or record_derivation() directly;
43  // instead, use the register_type convenience function, defined
44  // below.
45  bool register_type(TypeHandle &type_handle, const string &name);
46  TypeHandle register_dynamic_type(const string &name);
47 
48  void record_derivation(TypeHandle child, TypeHandle parent);
49  void record_alternate_name(TypeHandle type, const string &name);
50 
51 PUBLISHED:
52  TypeHandle find_type(const string &name) const;
53  TypeHandle find_type_by_id(int id) const;
54 
55  string get_name(TypeHandle type, TypedObject *object) const;
56  bool is_derived_from(TypeHandle child, TypeHandle base,
57  TypedObject *child_object);
58 
59  int get_num_typehandles();
60  TypeHandle get_typehandle(int n);
61  MAKE_SEQ(get_typehandles, get_num_typehandles, get_typehandle);
62 
63  int get_num_root_classes();
64  TypeHandle get_root_class(int n);
65  MAKE_SEQ(get_root_classes, get_num_root_classes, get_root_class);
66 
67  int get_num_parent_classes(TypeHandle child,
68  TypedObject *child_object) const;
69  TypeHandle get_parent_class(TypeHandle child, int index) const;
70 
71  int get_num_child_classes(TypeHandle child,
72  TypedObject *child_object) const;
73  TypeHandle get_child_class(TypeHandle child, int index) const;
74 
75  TypeHandle get_parent_towards(TypeHandle child, TypeHandle base,
76  TypedObject *child_object);
77 
78  static void reregister_types();
79 
80  void write(ostream &out) const;
81 
82  // ptr() returns the pointer to the global TypeRegistry object.
83  static TypeRegistry *ptr();
84 
85 private:
86  // The TypeRegistry class should never be constructed by user code.
87  // There is only one in the universe, and it constructs itself!
88  TypeRegistry();
89 
90  static void init_global_pointer();
91  INLINE TypeRegistryNode *look_up(TypeHandle type, TypedObject *object) const;
92  TypeRegistryNode *look_up_invalid(TypeHandle type, TypedObject *object) const;
93 
94  INLINE void freshen_derivations();
95  void rebuild_derivations();
96 
97  void do_write(ostream &out) const;
98  void write_node(ostream &out, int indent_level,
99  const TypeRegistryNode *node) const;
100 
101  static INLINE void init_lock();
102 
103  typedef vector<TypeRegistryNode *> HandleRegistry;
104  HandleRegistry _handle_registry;
105 
106  typedef map<string, TypeRegistryNode *> NameRegistry;
107  NameRegistry _name_registry;
108 
109  typedef vector<TypeRegistryNode *> RootClasses;
110  RootClasses _root_classes;
111 
112  bool _derivations_fresh;
113 
114  static MutexImpl *_lock;
115  static TypeRegistry *_global_pointer;
116 
117  friend class TypeHandle;
118 };
119 
120 ///////////////////////////////////////////
121 // Helper function to allow for "C" interaction into the type system
122 extern "C" EXPCL_DTOOL int get_best_parent_from_Set(int id, const std::set<int> &this_set);
123 
124 #include "typeHandle.h"
125 
126 #include "typeRegistry.I"
127 
128 #endif
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
This is a single entry in the TypeRegistry.
This class is intended to be the base class of all objects in Panda that might be allocated and delet...
Definition: memoryBase.h:73
int get_best_parent_from_Set(const std::set< int > &legal_vals) const
Return the Index of the BEst fit Classs from a set.
Definition: typeHandle.I:294
The TypeRegistry class maintains all the assigned TypeHandles in a given system.
Definition: typeRegistry.h:39
A fake mutex implementation for single-threaded applications that don&#39;t need any synchronization cont...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85