00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041
00042 #include "OSGConfig.h"
00043
00044 #include <iostream>
00045
00046 #include "OSGTypeFactory.h"
00047
00048 #include <OSGLog.h>
00049 #include <OSGTypeBase.h>
00050
00051 OSG_USING_NAMESPACE
00052
00056 TypeFactory *TypeFactory::_the = NULL;
00057
00058
00059
00060
00061 TypeFactory *TypeFactory::the(void)
00062 {
00063 if(_the == NULL)
00064 _the = new TypeFactory;
00065
00066 return _the;
00067 }
00068
00069
00070
00071
00072 UInt32 TypeFactory::registerType(TypeBase *pType)
00073 {
00074 UInt32 returnValue = 0;
00075
00076 if(pType == NULL)
00077 {
00078 SWARNING << "no data store given" << std::endl;
00079
00080 return returnValue;
00081 }
00082
00083 if(pType->getName().isEmpty() == true)
00084 {
00085 SWARNING << "DataElementType without name" << std::endl;
00086
00087 return returnValue;
00088 }
00089
00090 UInt32 uiTypeId = findTypeId(pType->getCName (),
00091 pType->getNameSpace());
00092
00093 if(uiTypeId != 0)
00094 {
00095 if(pType != findType(uiTypeId))
00096 {
00097 SWARNING << "ERROR: Can't add a second "
00098 << "type with the name " << pType->getCName()
00099 << "(" << pType << ")"
00100 << std::endl;
00101 }
00102 else
00103 {
00104 SWARNING << "Do not run ctr twice "
00105 << "type with the name " << pType->getCName()
00106 << "(" << pType << ")"
00107 << std::endl;
00108
00109
00111
00112 returnValue = uiTypeId;
00113 }
00114
00115 return returnValue;
00116 }
00117
00118 returnValue = _vTypeStore.size();
00119
00120 _vTypeStore.push_back(pType);
00121
00122 while(_vTypeNameMaps.size() <= pType->getNameSpace())
00123 {
00124 _vTypeNameMaps.push_back(new TypeNameMap);
00125
00126 PINFO << "Added namespace : " << _vTypeNameMaps.size() << std::endl;
00127 }
00128
00129 (*(_vTypeNameMaps[pType->getNameSpace()]))
00130 [IDStringLink(pType->getCName())] = returnValue;
00131
00132 FDEBUG(("Registered type %s | %d (%d)\n", pType->getCName(), returnValue,
00133 pType));
00134
00135 return returnValue;
00136 }
00137
00138
00139 UInt32 TypeFactory::findTypeId(const Char8 *szName,
00140 const UInt32 uiNameSpace)
00141 {
00142 TypeNameMapConstIt typeIt;
00143 UInt32 uiTypeId = 0;
00144
00145 if(szName == NULL)
00146 return uiTypeId;
00147
00148 if(_vTypeNameMaps.size() <= uiNameSpace)
00149 return uiTypeId;
00150
00151 TypeNameMap *pMap = _vTypeNameMaps[uiNameSpace];
00152
00153 typeIt = pMap->find(IDStringLink(szName));
00154
00155 uiTypeId = (typeIt == pMap->end()) ? 0 : (*typeIt).second;
00156
00157 return uiTypeId;
00158 }
00159
00160
00161 TypeBase *TypeFactory::findType(UInt32 uiTypeId)
00162 {
00163 if(uiTypeId < _vTypeStore.size())
00164 {
00165 return _vTypeStore[uiTypeId];
00166 }
00167 else
00168 {
00169 return NULL;
00170 }
00171 }
00172
00173
00174 TypeBase *TypeFactory::findType(const Char8 *szName,
00175 const UInt32 uiNameSpace)
00176 {
00177 UInt32 uiTypeId = findTypeId(szName, uiNameSpace);
00178
00179 return findType(uiTypeId);
00180 }
00181
00182
00183 UInt32 TypeFactory::getNumTypes(void)
00184 {
00185 return _vTypeStore.size();
00186 }
00187
00188
00189 void TypeFactory::writeTypeGraph(const Char8 *szFilename)
00190 {
00191 if(szFilename == NULL)
00192 return;
00193
00194 FILE *pOut = fopen(szFilename, "w");
00195
00196 if(pOut == NULL)
00197 return;
00198
00199 fprintf(pOut, "digraph OSGTypeGraph\n{\n");
00200
00201
00202
00203
00204
00205
00206
00207 fprintf(pOut, " rankdir=LR;\n");
00208 fprintf(pOut, " size=\"8,60\";\n");
00209 fprintf(pOut, " page=\"8.2677,11.69\";\n");
00210 fprintf(pOut, " radio=auto;\n");
00211
00212 for(UInt32 i = 1; i < _vTypeStore.size(); i++)
00213 {
00214 writeTypeDot(pOut, _vTypeStore[i]);
00215 }
00216
00217 fprintf(pOut, "}\n");
00218 fclose(pOut);
00219 }
00220
00221
00222
00223
00224 TypeFactory::TypeFactory(void) :
00225 _vTypeNameMaps(),
00226 _vTypeStore ()
00227 {
00228 _vTypeStore.reserve (512 );
00229 _vTypeStore.push_back(NULL);
00230
00231 _vTypeNameMaps.push_back(new TypeNameMap);
00232
00233
00234 }
00235
00236
00237
00238
00239 TypeFactory::~TypeFactory(void)
00240 {
00241 }
00242
00243
00244
00245
00246
00247 void TypeFactory::writeTypeDot(FILE *pOut,
00248 TypeBase *pTypeBase)
00249 {
00250 fprintf(pOut, " OpenSG%s [shape=record,label=\"%s\"]\n",
00251 pTypeBase->getCName(),
00252 pTypeBase->getCName());
00253
00254 if(pTypeBase->getCParentName() != NULL)
00255 {
00256 fprintf(pOut,
00257 " OpenSG%s -> OpenSG%s\n",
00258 pTypeBase->getCParentName(),
00259 pTypeBase->getCName());
00260 }
00261 }
00262
00263
00264
00265
00266
00267 #ifdef __sgi
00268 #pragma set woff 1174
00269 #endif
00270
00271 #ifdef OSG_LINUX_ICC
00272 #pragma warning( disable : 177 )
00273 #endif
00274
00275 namespace
00276 {
00277 static Char8 cvsid_cpp[] = "@(#)$Id: $";
00278 static Char8 cvsid_hpp[] = OSGTYPEFACTORY_HEADER_CVSID;
00279 }