15 #include "typeRegistry.h" 16 #include "typeRegistryNode.h" 17 #include "typeHandle.h" 18 #include "typedObject.h" 20 #include "numeric_types.h" 46 if (&type_handle == &rnode->_ref) {
49 assert(rnode->_name == name);
54 NameRegistry::iterator ri;
55 ri = _name_registry.find(name);
57 if (ri == _name_registry.end()) {
62 new_handle._index = _handle_registry.size();
65 _handle_registry.push_back(rnode);
66 _name_registry[name] = rnode;
67 _derivations_fresh =
false;
69 type_handle = new_handle;
74 assert(rnode->_name == (*ri).first);
75 assert(rnode->_handle._index >= 0 &&
76 rnode->_handle._index < (
int)_handle_registry.size());
77 assert(_handle_registry[rnode->_handle._index] == rnode);
78 assert(rnode->_handle._index != 0);
81 if (&type_handle == &rnode->_ref) {
84 if (type_handle == rnode->_handle) {
94 cerr <<
"Reregistering " << name <<
"\n";
95 type_handle = rnode->_handle;
100 if (type_handle != rnode->_handle) {
103 <<
"Attempt to register type " << name <<
" more than once!\n";
110 type_handle = rnode->_handle;
129 NameRegistry::iterator ri;
130 ri = _name_registry.find(name);
132 if (ri == _name_registry.end()) {
140 new_handle->_index = _handle_registry.size();
143 _handle_registry.push_back(rnode);
144 _name_registry[name] = rnode;
145 _derivations_fresh =
false;
177 TypeRegistryNode::Classes::iterator ni;
178 ni = find(cnode->_parent_classes.begin(), cnode->_parent_classes.end(),
181 if (ni == cnode->_parent_classes.end()) {
182 cnode->_parent_classes.push_back(pnode);
183 pnode->_child_classes.push_back(cnode);
184 _derivations_fresh =
false;
205 NameRegistry::iterator ri =
206 _name_registry.insert(NameRegistry::value_type(name, rnode)).first;
208 if ((*ri).second != rnode) {
211 <<
"Name " << name <<
" already assigned to TypeHandle " 212 << rnode->_name <<
"; cannot reassign to " << type <<
"\n";
233 NameRegistry::const_iterator ri;
234 ri = _name_registry.find(name);
235 if (ri != _name_registry.end()) {
236 handle = (*ri).second->_handle;
253 if (id < 0 ||id >= (
int)_handle_registry.size()) {
255 <<
"Invalid TypeHandle index " <<
id 256 <<
"! Is memory corrupt?\n";
260 return _handle_registry[id]->_handle;
279 string name = rnode->_name;
314 freshen_derivations();
330 int num_types = (int)_handle_registry.size();
345 if (n >= 0 && n < (
int)_handle_registry.size()) {
346 rnode = _handle_registry[n];
351 return rnode->_handle;
367 freshen_derivations();
368 int num_roots = _root_classes.size();
382 freshen_derivations();
384 if (n >= 0 && n < (
int)_root_classes.size()) {
385 handle = _root_classes[n]->_handle;
413 int num_parents = rnode->_parent_classes.size();
431 if (index >= 0 && index < (
int)rnode->_parent_classes.size()) {
432 handle = rnode->_parent_classes[index]->_handle;
455 int num_children = rnode->_child_classes.size();
473 if (index >= 0 && index < (
int)rnode->_child_classes.size()) {
474 handle = rnode->_child_classes[index]->_handle;
503 freshen_derivations();
525 HandleRegistry::iterator ri;
527 for (ri = reg->_handle_registry.begin();
528 ri != reg->_handle_registry.end();
531 if (rnode != NULL && rnode->_handle != rnode->_ref) {
532 cerr <<
"Reregistering " << rnode->_name <<
"\n";
563 if (_global_pointer == NULL) {
564 init_global_pointer();
567 return _global_pointer;
580 _handle_registry.push_back(NULL);
582 _derivations_fresh =
false;
588 assert(
sizeof(PN_uint8) == 1 &&
sizeof(PN_int8) == 1);
589 assert(
sizeof(PN_uint16) == 2 &&
sizeof(PN_int16) == 2);
590 assert(
sizeof(PN_uint32) == 4 &&
sizeof(PN_int32) == 4);
591 assert(
sizeof(PN_uint64) == 8 &&
sizeof(PN_int64) == 8);
593 assert(
sizeof(PN_float32) == 4);
594 assert(
sizeof(PN_float64) == 8);
604 init_global_pointer() {
617 rebuild_derivations() {
620 _root_classes.clear();
622 HandleRegistry::iterator hi;
623 for (hi = _handle_registry.begin();
624 hi != _handle_registry.end();
634 for (hi = _handle_registry.begin();
635 hi != _handle_registry.end();
638 if (node != NULL && node->_parent_classes.empty()) {
639 _root_classes.push_back(node);
654 do_write(ostream &out)
const {
657 HandleRegistry::const_iterator hi;
658 for (hi = _handle_registry.begin();
659 hi != _handle_registry.end();
662 if (root != NULL && root->_parent_classes.empty()) {
663 write_node(out, 2, root);
675 write_node(ostream &out,
int indent_level,
const TypeRegistryNode *node)
const {
676 indent(out, indent_level) << node->_handle.
get_index() <<
" " << node->_name;
677 if (!node->_parent_classes.empty()) {
678 out <<
" : " << node->_parent_classes[0]->_name;
679 for (
int pi = 1; pi < (int)node->_parent_classes.size(); pi++) {
680 out <<
", " << node->_parent_classes[pi]->_name;
685 for (
int i = 0; i < (int)node->_child_classes.size(); i++) {
686 write_node(out, indent_level + 2, node->_child_classes[i]);
703 if (handle._index == 0) {
706 if (
object != NULL) {
711 handle =
object->force_init_type();
714 if (handle._index == 0) {
717 <<
"Unable to force_init_type() on unregistered TypeHandle.\n";
724 if (handle._index > 0 && handle._index < (
int)_handle_registry.size()) {
727 name << rnode->_name;
728 name <<
" (index " << handle._index <<
")";
730 name <<
"NULL (index " << handle._index <<
")";
733 name <<
"index " << handle._index;
736 if (handle == object->get_type()) {
739 <<
"Type " << name.str() <<
" was unregistered!\n";
745 <<
"Attempt to reference unregistered TypeHandle. Type is of some\n" 746 <<
"class derived from type " << name.str() <<
" that doesn't define\n" 747 <<
"a good force_init_type() method.\n";
755 <<
"Attempt to reference unregistered TypeHandle!\n" 756 <<
"Registered TypeHandles are:\n";
762 if (handle._index < 0 ||
763 handle._index >= (
int)_handle_registry.size()) {
765 <<
"Invalid TypeHandle index " << handle._index
766 <<
"! Is memory corrupt?\n";
771 return _handle_registry[handle._index];
779 get_best_parent_from_Set(
int id,
const std::set<int> &this_set) {
781 if (this_set.find(
id) != this_set.end()) {
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.
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
int get_best_parent_from_Set(const std::set< int > &legal_vals) const
Return the Index of the BEst fit Classs from a set.
TypeHandle find_type(const string &name) const
Looks for a previously-registered type of the given name.
void clear_subtree()
Removes any subtree definition previously set up via define_subtree(), in preparation for rebuilding ...
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...
static TypeHandle get_parent_towards(const TypeRegistryNode *child, const TypeRegistryNode *base)
Returns the first parent class of child that is a descendant of the indicated base class...
string get_name(TypeHandle type, TypedObject *object) const
Returns the name of the indicated type.
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.
TypeHandle get_typehandle(int n)
Returns the nth TypeHandle in the system.
int get_index() const
Returns the integer index associated with this TypeHandle.
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
This is a single entry in the TypeRegistry.
int get_num_typehandles()
Returns the total number of unique TypeHandles in the system.
TypeHandle register_dynamic_type(const string &name)
Registers a new type on-the-fly, presumably at runtime.
TypeHandle get_parent_class(TypeHandle child, int index) const
Returns the nth parent class of this type.
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...
static bool is_derived_from(const TypeRegistryNode *child, const TypeRegistryNode *base)
Returns true if the child RegistryNode represents a class that inherits directly or indirectly from t...
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.
int get_num_root_classes()
Returns the number of root classes–that is, classes that do not inherit from any other classes–know...
void write(ostream &out) const
Makes an attempt to format the entire TypeRegistry in a nice way that shows the derivation tree as in...
TypeHandle get_child_class(TypeHandle child, int index) const
Returns the nth child class of this type.
void record_alternate_name(TypeHandle type, const string &name)
Indicates an alternate name for the same type.
TypeHandle get_root_class(int n)
Returns the nth root class in the system.
static void reregister_types()
Walks through the TypeRegistry tree and makes sure that each type that was previously registered is s...
static TypeRegistry * ptr()
Returns the pointer to the global TypeRegistry object.
The TypeRegistry class maintains all the assigned TypeHandles in a given system.
void define_subtree()
Indicates that this TypeRegistryNode is the top of a subtree within the inheritance graph (typically...
A fake mutex implementation for single-threaded applications that don't need any synchronization cont...
TypeHandle is the identifier used to differentiate C++ class types.
bool register_type(TypeHandle &type_handle, const string &name)
Creates a new Type of the given name and assigns a unique value to the type_handle.
void record_derivation(TypeHandle child, TypeHandle parent)
Records that the type referenced by child inherits directly from the type referenced by parent...