00001 #include "cl/Register.h" 00002 #include <string.h> 00003 00004 00005 BaseTable *Register::table = 0; 00006 00007 Void PrintClassID(ostream &s, ClassID &id) 00008 { 00009 char *t = (char*) &id; 00010 s << '<' << t[0] << t[1] << t[2] << t[3] << '>'; 00011 } 00012 00013 00014 // --- Register methods ---------------------------------- 00015 00016 00017 Void Register::RegisterClass( 00018 ClassID baseID, 00019 ClassID id, 00020 ObjectCreator creator, 00021 const Char *name, 00022 const Char *description 00023 ) 00024 { 00025 Int i = 0; 00026 ClassTable *ctable; 00027 00028 if (!table) 00029 table = new BaseTable; 00030 else 00031 while (i < table->NumItems()) 00032 { 00033 if ((*table)[i].baseID == baseID) 00034 break; 00035 else 00036 i++; 00037 } 00038 00039 if (i == table->NumItems()) 00040 { 00041 table->Add(1); 00042 table->Last().baseID = baseID; 00043 } 00044 00045 ctable = &((*table)[i].table); 00046 ctable->Add(1); 00047 ctable->Last().id = id; 00048 ctable->Last().creator = creator; 00049 ctable->Last().name = name; 00050 ctable->Last().description = description; 00051 } 00052 00053 ClassTable *Register::FindClassTable(ClassID baseID) 00054 { 00055 Int i = 0; 00056 00057 if (!table) 00058 table = new BaseTable; 00059 else 00060 while (i < table->NumItems()) 00061 { 00062 if ((*table)[i].baseID == baseID) 00063 break; 00064 else 00065 i++; 00066 } 00067 00068 if (i < table->NumItems()) 00069 return(&((*table)[i].table)); 00070 else 00071 return(0); 00072 } 00073 00074 Void Register::ListObjects(ClassID baseID) 00075 { 00076 Int i; 00077 ClassTable *ctable = FindClassTable(baseID); 00078 00079 if (!ctable) 00080 return; 00081 00082 for (i = 0; i < ctable->NumItems(); i++) 00083 { 00084 cout << " "; 00085 PrintClassID(cout, (*ctable)[i].id); 00086 cout << " " << (*ctable)[i].name; 00087 if ((*ctable)[i].description) 00088 cout << ", " << (*ctable)[i].description; 00089 cout << endl; 00090 } 00091 } 00092 00093 Void Register::ListObjects() 00094 { 00095 Int i, j; 00096 ClassTable *ctable; 00097 00098 if (!table) return; 00099 00100 for (j = 0; j < table->NumItems(); j++) 00101 { 00102 PrintClassID(cout, (*table)[j].baseID); 00103 cout << endl; 00104 ctable = &((*table)[j].table); 00105 for (i = 0; i < ctable->NumItems(); i++) 00106 { 00107 cout << " "; 00108 PrintClassID(cout, (*ctable)[i].id); 00109 cout << " " << (*ctable)[i].name; 00110 if ((*ctable)[i].description) 00111 cout << ", " << (*ctable)[i].description; 00112 cout << endl; 00113 } 00114 } 00115 } 00116 00117 Void *Register::MakeObject(ClassID id) 00118 { 00119 return(MakeObject('none', id)); 00120 } 00121 00122 Void *Register::MakeObject(ClassID baseID, ClassID id) 00123 { 00124 ClassTable *ctable; 00125 Int i = 0; 00126 00127 if (!(ctable = FindClassTable(baseID))) 00128 return(0); 00129 00130 while (i < ctable->NumItems()) 00131 { 00132 if ((*ctable)[i].id == id) 00133 break; 00134 else 00135 i++; 00136 } 00137 00138 if (i == ctable->NumItems()) 00139 { 00140 Assert(false, "(Register::MakeObject) Can't find class ID"); 00141 return(0); 00142 } 00143 return((*(*ctable)[i].creator)()); 00144 } 00145 00146 Void *Register::MakeObject(const Char *name) 00147 { 00148 return(MakeObject('none', name)); 00149 } 00150 00151 Void *Register::MakeObject(ClassID baseID, const Char *name) 00152 { 00153 ClassTable *ctable; 00154 Int i = 0; 00155 00156 if (!(ctable = FindClassTable(baseID))) 00157 return(0); 00158 00159 while (i < ctable->NumItems()) 00160 { 00161 if (strcmp((*ctable)[i].name, name) == 0) 00162 break; 00163 else 00164 i++; 00165 } 00166 00167 if (i == ctable->NumItems()) 00168 { 00169 Assert(false, "(Register::MakeObject) Can't find name"); 00170 return(0); 00171 } 00172 return((*(*ctable)[i].creator)()); 00173 } 00174 00175 #include "cl/CLConfig.h" 00176 00177 #ifdef CL_TMPL_INST 00178 template class Array<RegistryEntry>; 00179 template class Array<BaseEntry>; 00180 #endif 00181 00182 #ifdef CL_SGI_INST 00183 #pragma instantiate Array<RegistryEntry> 00184 #pragma instantiate Array<BaseEntry> 00185 #endif 00186 00187 #ifdef TEST 00188 Int main(Int argc, Char *argv[]) 00189 { 00190 00191 return(0); 00192 } 00193 #endif