1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
42 #include "OSGConfig.h"
43 #include "OSGFieldDescFactory.h"
44 #include "OSGFieldType.h"
45 #include "OSGBaseFunctions.h"
47 #include "OSGFactoryController.h"
49 #include "OSGSingletonHolder.ins"
53 OSG_SINGLETON_INST(FieldDescFactoryBase
, addPostFactoryExitFunction
)
55 template class SingletonHolder
<FieldDescFactoryBase
>;
61 /*-------------------------------------------------------------------------*/
64 //std::map<UInt32, FieldType *> *FieldDescFactory::_fieldTypeM = NULL;
66 /*-------------------------------------------------------------------------*/
69 FieldDescFactoryBase::~FieldDescFactoryBase(void)
71 // At this point, the Log is already destroyed
72 //SINFO << "INFO: Destroy Singleton FieldDescFactory" << std::endl;
75 /*-------------------------------------------------------------------------*/
78 Field
*FieldDescFactoryBase::createField(UInt32 typeId
)
81 FieldType
*pType
= getFieldType(typeId
);
85 (pType
->_createMethod
!= NULL
))
87 return pType
->_createMethod();
98 Field
*FieldDescFactoryBase::createField(const Char8
*szName
)
101 FieldType
*pType
= getFieldType(szName
);
103 if((pType
!= NULL
) &&
104 (pType
->_createMethod
!= NULL
))
106 return pType
->_createMethod();
117 /*-------------------------------------------------------------------------*/
120 UInt32
FieldDescFactoryBase::getNumFieldTypes(void)
123 if(_fieldTypeM
!= NULL
)
124 return _fieldTypeM
->size();
130 FieldType
*FieldDescFactoryBase::getFieldType(const Char8
*szName
)
133 std::map
<UInt32
, FieldType
*>::iterator mIt
;
134 FieldType
*returnValue
= NULL
;
136 if(_fieldTypeM
!= NULL
)
138 mIt
= _fieldTypeM
->begin();
140 while(mIt
!= _fieldTypeM
->end())
142 if(strcmp(szName
, (*mIt
).second
->getCName()) == 0)
144 returnValue
= (*mIt
).second
;
158 FieldType
*FieldDescFactoryBase::getFieldType(UInt32 typeId
)
161 std::map
<UInt32
, FieldType
*>::iterator mIt
;
163 if(_fieldTypeM
== NULL
)
166 mIt
= _fieldTypeM
->find(typeId
);
168 if(mIt
!= _fieldTypeM
->end())
170 return (*mIt
).second
;
181 const Char8
*FieldDescFactoryBase::getFieldTypeName(UInt32 typeId
)
184 FieldType
*pFieldType
= getFieldType(typeId
);
186 return pFieldType
? pFieldType
->getCName() : NULL
;
192 FieldDescriptionBase
*FieldDescFactoryBase::createByName(
193 const Char8
*szFieldTypename
,
194 const Char8
*szFieldname
,
195 FieldEditMethod fEditMethod
,
196 FieldGetMethod fGetMethod
,
197 UInt32 uiFieldFlags
)
199 if(szFieldTypename
== NULL
)
201 FWARNING(("FieldDescFactory::create, no typename given\n"));
206 FieldDescriptionBase
*returnValue
= NULL
;
207 std::string tmpStr
= szFieldTypename
;
209 NameCreatorMapConstIt ncIt
= _mNameCreatorMap
.find(tmpStr
);
211 if(ncIt
!= _mNameCreatorMap
.end())
213 if(ncIt
->second
.first
)
215 returnValue
= ncIt
->second
.first(szFieldname
,
223 FWARNING(("no desc creator registered for %s\n",
229 FWARNING(("could not find desc creator for %s\n",
236 FieldDescriptionBase
*FieldDescFactoryBase::createByNameIdx(
237 const Char8
*szFieldTypename
,
238 const Char8
*szFieldname
,
239 FieldIndexEditMethod fEditMethod
,
240 FieldIndexGetMethod fGetMethod
,
241 UInt32 uiFieldFlags
)
243 if(szFieldTypename
== NULL
)
245 FWARNING(("FieldDescFactory::createIdx, no typename given\n"));
250 FieldDescriptionBase
*returnValue
= NULL
;
251 std::string tmpStr
= szFieldTypename
;
253 NameCreatorMapConstIt ncIt
= _mNameCreatorMap
.find(tmpStr
);
255 if(ncIt
!= _mNameCreatorMap
.end())
257 if(ncIt
->second
.second
)
259 returnValue
= ncIt
->second
.second(szFieldname
,
267 FWARNING(("no desc idx creator registered for %s\n",
273 FWARNING(("could not find desc idx creator for %s\n",
281 FieldDescriptionBase
*FieldDescFactoryBase::create(
282 UInt32 uiFieldTypeId
,
283 const Char8
*szFieldname
,
284 FieldEditMethod fEditMethod
,
285 FieldGetMethod fGetMethod
,
286 UInt32 uiFieldFlags
)
288 if(uiFieldTypeId
>= _vCreators
.size())
290 FWARNING(("FieldDescFactory::create, unknown type id %d (%"PRISize
")\n",
297 FieldDescriptionBase
*returnValue
= NULL
;
299 if(_vCreators
[uiFieldTypeId
].first
)
301 returnValue
= _vCreators
[uiFieldTypeId
].first(szFieldname
,
309 FWARNING(("no desc creator registered for %d\n",
316 FieldDescriptionBase
*FieldDescFactoryBase::createIdx(
317 UInt32 uiFieldTypeId
,
318 const Char8
*szFieldname
,
319 FieldIndexEditMethod fEditMethod
,
320 FieldIndexGetMethod fGetMethod
,
321 UInt32 uiFieldFlags
)
323 if(uiFieldTypeId
>= _vCreators
.size())
325 FWARNING(("FieldDescFactory::createIdx, unknown type id %d (%"
334 FieldDescriptionBase
*returnValue
= NULL
;
336 if(_vCreators
[uiFieldTypeId
].second
)
338 returnValue
= _vCreators
[uiFieldTypeId
].second(szFieldname
,
346 FWARNING(("no desc idx creator registered for %d\n",
353 /*-------------------------------------------------------------------------*/
356 void FieldDescFactoryBase::registerDescription(
357 const Char8
*szFieldTypename
,
359 FieldDescCreator fCreator
,
360 IndexedFieldDescCreator fIdxCreator
)
362 if(szFieldTypename
== NULL
)
365 // fprintf(stderr, "register %s (%d)\n", szFieldTypename, uiTypeId);
367 std::string tmpStr
= szFieldTypename
;
369 NameCreatorMapConstIt ncIt
= _mNameCreatorMap
.find(tmpStr
);
371 if(ncIt
== _mNameCreatorMap
.end())
373 DescCreators tmpCreators
;
375 tmpCreators
.first
= fCreator
;
376 tmpCreators
.second
= fIdxCreator
;
378 _mNameCreatorMap
[tmpStr
] = tmpCreators
;
380 if(uiTypeId
>= _vCreators
.size())
382 _vCreators
.resize(uiTypeId
+ 1);
385 _vCreators
[uiTypeId
] = tmpCreators
;
389 FWARNING(("could not add second desc creator for %s\n",
394 /*-------------------------------------------------------------------------*/
397 FieldDescFactoryBase::FieldDescFactoryBase(void) :
398 Inherited ("FieldDescFactory"),
402 FactoryController::the()->registerFactory(this);
406 bool FieldDescFactoryBase::initialize(void)
408 // fprintf(stderr, "FieldDescFactoryBase::initialize\n");
414 bool FieldDescFactoryBase::terminate(void)
416 // fprintf(stderr, "FieldDescFactoryBase::terminate\n");
422 bool FieldDescFactoryBase::initializePendingElements(void)
424 // fprintf(stderr, "FieldDescFactoryBase::initializePendingElements\n");
430 bool FieldDescFactoryBase::initializeFactoryPost(void)
432 // fprintf(stderr, "FieldDescFactoryBase::initializeFactoryPost\n");
437 bool FieldDescFactoryBase::initializePendingElementsFactoryPost(void)
440 // "FieldDescFactoryBase::initializePendingElementsFactoryPost\n");
445 /*-------------------------------------------------------------------------*/
449 void FieldDescFactoryBase::registerType(FieldType
*pType
)
453 void FieldDescFactoryBase::addType(FieldType
*pType
)
458 if(getFieldType(pType
->getId()) != NULL
)
461 if(_fieldTypeM
== NULL
)
462 _fieldTypeM
= new std::map
<UInt32
, FieldType
*>();
464 (*_fieldTypeM
)[pType
->getId()] = pType
;