Panda3D
|
00001 // Filename: typeRegistry.h 00002 // Created by: drose (06Aug01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef TYPEREGISTRY_H 00016 #define TYPEREGISTRY_H 00017 00018 #include "dtoolbase.h" 00019 #include "mutexImpl.h" 00020 #include "memoryBase.h" 00021 00022 #include <set> 00023 #include <map> 00024 #include <vector> 00025 00026 class TypeHandle; 00027 class TypeRegistryNode; 00028 class TypedObject; 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Class : TypeRegistry 00032 // Description : The TypeRegistry class maintains all the assigned 00033 // TypeHandles in a given system. There should be only 00034 // one TypeRegistry class during the lifetime of the 00035 // application. It will be created on the local heap 00036 // initially, and it should be migrated to shared memory 00037 // as soon as shared memory becomes available. 00038 //////////////////////////////////////////////////////////////////// 00039 class EXPCL_DTOOL TypeRegistry : public MemoryBase { 00040 public: 00041 // User code shouldn't generally need to call 00042 // TypeRegistry::register_type() or record_derivation() directly; 00043 // instead, use the register_type convenience function, defined 00044 // below. 00045 bool register_type(TypeHandle &type_handle, const string &name); 00046 TypeHandle register_dynamic_type(const string &name); 00047 00048 void record_derivation(TypeHandle child, TypeHandle parent); 00049 void record_alternate_name(TypeHandle type, const string &name); 00050 00051 PUBLISHED: 00052 TypeHandle find_type(const string &name) const; 00053 TypeHandle find_type_by_id(int id) const; 00054 00055 string get_name(TypeHandle type, TypedObject *object) const; 00056 bool is_derived_from(TypeHandle child, TypeHandle base, 00057 TypedObject *child_object); 00058 00059 int get_num_typehandles(); 00060 TypeHandle get_typehandle(int n); 00061 00062 int get_num_root_classes(); 00063 TypeHandle get_root_class(int n); 00064 00065 int get_num_parent_classes(TypeHandle child, 00066 TypedObject *child_object) const; 00067 TypeHandle get_parent_class(TypeHandle child, int index) const; 00068 00069 int get_num_child_classes(TypeHandle child, 00070 TypedObject *child_object) const; 00071 TypeHandle get_child_class(TypeHandle child, int index) const; 00072 00073 TypeHandle get_parent_towards(TypeHandle child, TypeHandle base, 00074 TypedObject *child_object); 00075 00076 static void reregister_types(); 00077 00078 void write(ostream &out) const; 00079 00080 // ptr() returns the pointer to the global TypeRegistry object. 00081 static TypeRegistry *ptr(); 00082 00083 private: 00084 // The TypeRegistry class should never be constructed by user code. 00085 // There is only one in the universe, and it constructs itself! 00086 TypeRegistry(); 00087 00088 static void init_global_pointer(); 00089 TypeRegistryNode *look_up(TypeHandle type, TypedObject *object) const; 00090 00091 INLINE void freshen_derivations(); 00092 void rebuild_derivations(); 00093 00094 void do_write(ostream &out) const; 00095 void write_node(ostream &out, int indent_level, 00096 const TypeRegistryNode *node) const; 00097 00098 static INLINE void init_lock(); 00099 00100 typedef vector<TypeRegistryNode *> HandleRegistry; 00101 HandleRegistry _handle_registry; 00102 00103 typedef map<string, TypeRegistryNode *> NameRegistry; 00104 NameRegistry _name_registry; 00105 00106 typedef vector<TypeRegistryNode *> RootClasses; 00107 RootClasses _root_classes; 00108 00109 bool _derivations_fresh; 00110 00111 static MutexImpl *_lock; 00112 static TypeRegistry *_global_pointer; 00113 00114 friend class TypeHandle; 00115 }; 00116 00117 /////////////////////////////////////////// 00118 // Helper function to allow for "C" interaction into the type system 00119 extern "C" EXPCL_DTOOL int get_best_parent_from_Set(int id, const std::set<int> &this_set); 00120 00121 #include "typeRegistry.I" 00122 00123 #endif