Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGTypeFactory.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
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 /*                                Get                                      */
00060 
00061 TypeFactory *TypeFactory::the(void)
00062 {
00063     if(_the == NULL)
00064         _the = new TypeFactory;
00065 
00066     return _the;
00067 }
00068 
00069 /*-------------------------------------------------------------------------*/
00070 /*                              Type Info                                  */
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 /* CHECK
00202     for_each(_vTypeStore.begin(), 
00203              _vTypeStore.end(),
00204              bind1st(ptr_fun(writeTypeDot), pOut));
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 /*                            Constructors                                 */
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 //    FactoryController::the()->registerTypeFactory(this);
00234 }
00235 
00236 /*-------------------------------------------------------------------------*/
00237 /*                             Destructor                                  */
00238 
00239 TypeFactory::~TypeFactory(void)
00240 {
00241 }
00242 
00243 
00244 /*-------------------------------------------------------------------------*/
00245 /*                              Helper                                     */
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 /*                              cvs id's                                   */
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 }

Generated on Thu Aug 25 04:11:55 2005 for OpenSG by  doxygen 1.4.3