Panda3D

Loading...
Searching...
No Matches
typeRegistry.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 typeRegistry.h
10 * @author drose
11 * @date 2001-08-06
12 */
13
14#ifndef TYPEREGISTRY_H
15#define TYPEREGISTRY_H
16
17#include "dtoolbase.h"
18#include "mutexImpl.h"
19#include "memoryBase.h"
20
21#include <set>
22#include <map>
23#include <vector>
24
25 class TypeHandle;
26 class TypeRegistryNode;
27 class TypedObject;
28
29/**
30 * The TypeRegistry class maintains all the assigned TypeHandles in a given
31 * system. There should be only one TypeRegistry class during the lifetime of
32 * the application. It will be created on the local heap initially, and it
33 * should be migrated to shared memory as soon as shared memory becomes
34 * available.
35 */
36 class EXPCL_DTOOL_DTOOLBASE TypeRegistry : public MemoryBase {
37 public:
38 // User code shouldn't generally need to call TypeRegistry::register_type()
39 // or record_derivation() directly; instead, use the register_type
40 // convenience function, defined in register_type.h.
41 bool register_type(TypeHandle &type_handle, const std::string &name);
42
43PUBLISHED:
44 TypeHandle register_dynamic_type(const std::string &name);
45
46 void record_derivation(TypeHandle child, TypeHandle parent);
47 void record_alternate_name(TypeHandle type, const std::string &name);
48#ifdef HAVE_PYTHON
49 void record_python_type(TypeHandle type, PyObject *python_type);
50#endif
51
52 TypeHandle find_type(const std::string &name) const;
53 TypeHandle find_type_by_id(int id) const;
54
55 std::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(std::ostream &out) const;
81
82 // ptr() returns the pointer to the global TypeRegistry object.
83 static INLINE TypeRegistry *ptr();
84
85 MAKE_SEQ_PROPERTY(typehandles, get_num_typehandles, get_typehandle);
86 MAKE_SEQ_PROPERTY(root_classes, get_num_root_classes, get_root_class);
87
88 private:
89 // The TypeRegistry class should never be constructed by user code. There
90 // is only one in the universe, and it constructs itself!
91 TypeRegistry();
92
93 static void init_global_pointer();
94 INLINE TypeRegistryNode *look_up(TypeHandle type, TypedObject *object) const;
95 TypeRegistryNode *look_up_invalid(TypeHandle type, TypedObject *object) const;
96
97 INLINE void freshen_derivations();
98 void rebuild_derivations();
99
100 void do_write(std::ostream &out) const;
101 void write_node(std::ostream &out, int indent_level,
102 const TypeRegistryNode *node) const;
103
104 static INLINE void init_lock();
105
106 typedef std::vector<TypeRegistryNode *> HandleRegistry;
107 HandleRegistry _handle_registry;
108
109 typedef std::map<std::string, TypeRegistryNode *> NameRegistry;
110 NameRegistry _name_registry;
111
112 typedef std::vector<TypeRegistryNode *> RootClasses;
113 RootClasses _root_classes;
114
115 bool _derivations_fresh;
116
117 static MutexImpl *_lock;
118 static TypeRegistry *_global_pointer;
119
120 friend class TypeHandle;
121};
122
123// Helper function to allow for "C" interaction into the type system
124 extern "C" EXPCL_DTOOL_DTOOLBASE int get_best_parent_from_Set(int id, const std::set<int> &this_set);
125
126#include "typeHandle.h"
127
128#include "typeRegistry.I"
129
130#endif
MemoryBase
This class is intended to be the base class of all objects in Panda that might be allocated and delet...
Definition memoryBase.h:65
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
TypeHandle::get_best_parent_from_Set
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.cxx:175
TypeRegistryNode
This is a single entry in the TypeRegistry.
Definition typeRegistryNode.h:30
TypeRegistry::get_typehandle
get_typehandle
Returns the nth TypeHandle in the system.
Definition typeRegistry.h:61
TypeRegistry::get_num_typehandles
get_num_typehandles
Returns the total number of unique TypeHandles in the system.
Definition typeRegistry.h:61
TypeRegistry::record_alternate_name
void record_alternate_name(TypeHandle type, const std::string &name)
Indicates an alternate name for the same type.
Definition typeRegistry.cxx:189
TypeRegistry::find_type_by_id
TypeHandle find_type_by_id(int id) const
Looks for a previously-registered type with the given id number (as returned by TypeHandle::get_index...
Definition typeRegistry.cxx:253
TypeRegistry::get_parent_class
TypeHandle get_parent_class(TypeHandle child, int index) const
Returns the nth parent class of this type.
Definition typeRegistry.cxx:401
TypeRegistry::write
void write(std::ostream &out) const
Makes an attempt to format the entire TypeRegistry in a nice way that shows the derivation tree as in...
Definition typeRegistry.cxx:508
TypeRegistry::get_num_root_classes
get_num_root_classes
Returns the number of root classes–that is, classes that do not inherit from any other classes–known ...
Definition typeRegistry.h:65
TypeRegistry::record_derivation
void record_derivation(TypeHandle child, TypeHandle parent)
Records that the type referenced by child inherits directly from the type referenced by parent.
Definition typeRegistry.cxx:159
TypeRegistry::get_child_class
TypeHandle get_child_class(TypeHandle child, int index) const
Returns the nth child class of this type.
Definition typeRegistry.cxx:438
TypeRegistry::is_derived_from
bool is_derived_from(TypeHandle child, TypeHandle base, TypedObject *child_object)
Returns true if the first type is derived from the second type, false otherwise.
Definition typeRegistry.cxx:297
TypeRegistry::ptr
static TypeRegistry * ptr()
Returns the pointer to the global TypeRegistry object.
Definition typeRegistry.I:30
TypeRegistry::get_num_child_classes
int get_num_child_classes(TypeHandle child, TypedObject *child_object) const
Returns the number of child classes that the indicated type is known to have.
Definition typeRegistry.cxx:424
TypeRegistry::get_name
std::string get_name(TypeHandle type, TypedObject *object) const
Returns the name of the indicated type.
Definition typeRegistry.cxx:273
TypeRegistry::find_type
TypeHandle find_type(const std::string &name) const
Looks for a previously-registered type of the given name.
Definition typeRegistry.cxx:233
TypeRegistry::get_parent_towards
TypeHandle get_parent_towards(TypeHandle child, TypeHandle base, TypedObject *child_object)
Returns the parent of the indicated child class that is in a direct line of inheritance to the indica...
Definition typeRegistry.cxx:463
TypeRegistry::get_num_parent_classes
int get_num_parent_classes(TypeHandle child, TypedObject *child_object) const
Returns the number of parent classes that the indicated type is known to have.
Definition typeRegistry.cxx:387
TypeRegistry::reregister_types
static void reregister_types()
Walks through the TypeRegistry tree and makes sure that each type that was previously registered is *...
Definition typeRegistry.cxx:486
TypeRegistry::get_root_class
get_root_class
Returns the nth root class in the system.
Definition typeRegistry.h:65
TypedObject
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition typedObject.h:88
dtoolbase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
memoryBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
mutexImpl.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
register_type
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition register_type.I:22
register_dynamic_type
TypeHandle register_dynamic_type(const std::string &name)
This is essentially similar to register_type(), except that it doesn't store a reference to any TypeH...
Definition register_type.I:73
typeHandle.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
typeRegistry.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.