changed: gcc8 base update
[opensg.git] / Source / System / State / Base / OSGChunkBlockMapFields.cpp
blob6166695e5d46fa7c7c94f212878f17bd380d25fb
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2003 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include "OSGFieldContainer.h"
44 #include "OSGChunkBlockMapFields.h"
45 #include "OSGChunkBlock.h"
46 #include "OSGContainerPtrFuncs.h"
48 #include "OSGTypeBasePredicates.h"
49 #include "OSGReflexiveContainerTypePredicates.h"
50 #include "OSGMapHelper.h"
52 #include "OSGSField.ins"
55 OSG_BEGIN_NAMESPACE
57 OSG_FIELD_DLLEXPORT_DEF1(SField, ChunkBlockMap)
60 const FieldType &GetSFieldHandle<SFChunkBlockPtrMap>::getType(void) const
62 return SFChunkBlockPtrMap::getClassType();
65 const FieldType &EditSFieldHandle<SFChunkBlockPtrMap>::getType(void) const
67 return SFChunkBlockPtrMap::getClassType();
70 SizeT FieldTraits<ChunkBlockMap>::getBinSize(const ChunkBlockMap &aMap)
72 // number of elements in map + binding and pointer id for each element
73 ChunkBlockMap::const_iterator mapIt = aMap.begin();
74 ChunkBlockMap::const_iterator mapEnd = aMap.end ();
76 SizeT uiKeySize = 0;
78 for(;mapIt != mapEnd; ++mapIt)
80 std::vector<std::string> vKeyList;
82 KeyPool::the()->keyToStringList(mapIt->first, vKeyList);
84 std::vector<std::string>::const_iterator kIt = vKeyList.begin();
85 std::vector<std::string>::const_iterator kEnd = vKeyList.end ();
87 uiKeySize += sizeof(UInt32);
89 for(; kIt != kEnd; ++kIt)
91 uiKeySize += FieldTraits<std::string>::getBinSize(*kIt);
95 return sizeof(UInt32) + aMap.size() * sizeof(UInt32) + uiKeySize;
98 void FieldTraits<ChunkBlockMap>::copyToBin( BinaryDataHandler &pMem,
99 const ChunkBlockMap &aMap )
101 ChunkBlockMap::const_iterator mapIt = aMap.begin();
102 ChunkBlockMap::const_iterator mapEnd = aMap.end ();
104 UInt32 numPublicObjects = UInt32(aMap.size());
105 UInt32 fcId;
107 pMem.putValue(numPublicObjects);
109 for(; mapIt != mapEnd; ++mapIt)
111 std::vector<std::string> vKeyList;
113 KeyPool::the()->keyToStringList(mapIt->first, vKeyList);
115 std::vector<std::string>::const_iterator kIt = vKeyList.begin();
116 std::vector<std::string>::const_iterator kEnd = vKeyList.end ();
118 UInt32 uiNumKeys = UInt32(vKeyList.size());
120 pMem.putValue(uiNumKeys);
122 for(; kIt != kEnd; ++kIt)
124 FieldTraits<std::string>::copyToBin(pMem, *kIt);
127 fcId = mapIt->second->getId();
129 pMem.putValue(fcId);
133 void FieldTraits<ChunkBlockMap>::copyFromBin(BinaryDataHandler &pMem,
134 ChunkBlockMap &aMap )
136 ChunkBlock *matPtr;
137 ChunkBlockMapKey key = KeyPool::the()->getDefault();
138 UInt32 fcId;
139 UInt32 uiNumObjects;
141 pMem.getValue(uiNumObjects);
143 ChunkBlockMap::const_iterator mapIt = aMap.begin();
144 ChunkBlockMap::const_iterator mapEnd = aMap.end ();
146 for(; mapIt != mapEnd; ++mapIt)
148 if((*mapIt).second != NULL)
150 Thread::getCurrentChangeList()->addDelayedSubRef<
151 UnrecordedRefCountPolicy>((*mapIt).second);
155 aMap.clear();
158 for(UInt32 i = 0; i < uiNumObjects; ++i)
160 UInt32 uiNumKeys;
162 pMem.getValue(uiNumKeys);
164 for(UInt32 j = 0; j < uiNumKeys; ++j)
166 std::string tmpString;
168 FieldTraits<std::string>::copyFromBin(pMem, tmpString);
170 ChunkBlockMapKey tmpKey =
171 KeyPool::the()->getAuto(tmpString.c_str());
173 key |= tmpKey;
176 pMem.getValue(fcId);
178 matPtr = dynamic_cast<ChunkBlock *>(
179 FieldContainerFactory::the()->getMappedContainer(fcId));
181 if(matPtr != NULL)
183 UnrecordedRefCountPolicy::addRef(matPtr);
185 aMap.insert(ChunkBlockMap::value_type(key, matPtr));
190 void EditSFieldHandle<SFChunkBlockPtrMap>::add(
191 FieldContainer *rhs,
192 const std::string &szBindings)
194 ChunkBlock *pVal = dynamic_cast<ChunkBlock *>(rhs);
195 KeyType oKey = 0x0000;
197 if(pVal == NULL)
199 MapHelper *pMHelper = dynamic_cast<MapHelper *>(rhs);
201 if(pMHelper != NULL)
203 pVal = dynamic_cast<ChunkBlock *>(pMHelper->getContainer());
205 const MapHelper::MFKeysType *pKeys = pMHelper->getMFKeys();
207 if(pKeys->empty() == false)
209 KeyType tmpKey = KeyPool::the()->getDefault();
211 for(UInt32 i = 0; i < pKeys->size(); ++i)
213 const std::string &szKey = pMHelper->getKeys(i);
215 tmpKey = KeyPool::the()->getAuto(szKey.c_str());
217 oKey |= tmpKey;
222 else
224 if(szBindings.empty() == false)
226 oKey = KeyPool::the()->getAuto(szBindings.c_str());
230 if(rhs != NULL && pVal == NULL)
231 return;
234 // for whatever reason VS2003 does not like == NULL
235 if(_fAddMethod)
237 _fAddMethod(pVal, oKey);
241 void EditSFieldHandle<SFChunkBlockPtrMap>::traverse(TraverseCallback oCallback)
243 const SFChunkBlockPtrMap *pMap = static_cast<SFChunkBlockPtrMap *>(_pField);
245 if(oCallback && pMap != NULL)
247 ChunkBlockMap::const_iterator mapIt = pMap->getValue().begin();
248 ChunkBlockMap::const_iterator mapEnd = pMap->getValue().end ();
250 for(; mapIt != mapEnd; ++mapIt)
252 oCallback(mapIt->second);
257 void EditSFieldHandle<SFChunkBlockPtrMap>::flatten(MapList &vList)
259 vList.clear();
261 const SFChunkBlockPtrMap *pMap = static_cast<SFChunkBlockPtrMap *>(_pField);
263 if(pMap != NULL)
265 ChunkBlockMap::const_iterator mapIt = pMap->getValue().begin();
266 ChunkBlockMap::const_iterator mapEnd = pMap->getValue().end ();
268 for(; mapIt != mapEnd; ++mapIt)
270 ListEntry tmpEntry;
272 KeyPool::the()->keyToStringList(mapIt->first,
273 tmpEntry.first);
275 tmpEntry.second = mapIt->second;
277 vList.push_back(tmpEntry);
282 void EditSFieldHandle<SFChunkBlockPtrMap>::flatten(ContainerList &vList)
284 vList.clear();
286 const SFChunkBlockPtrMap *pMap = static_cast<SFChunkBlockPtrMap *>(_pField);
288 if(pMap != NULL)
290 ChunkBlockMap::const_iterator mapIt = pMap->getValue().begin();
291 ChunkBlockMap::const_iterator mapEnd = pMap->getValue().end ();
293 for(; mapIt != mapEnd; ++mapIt)
295 vList.push_back(mapIt->second);
300 bool EditSFieldHandle<SFChunkBlockPtrMap>::loadFromBin(
301 BinaryDataHandler *pMem,
302 UInt32 uiNumElements,
303 bool hasBindingInfo,
304 std::vector<UInt16> &vBindings,
305 std::vector<UInt32> &vIds )
307 SFChunkBlockPtrMap *pMap = static_cast<SFChunkBlockPtrMap *>(_pField);
309 if(pMap == NULL)
310 return true;
312 ChunkBlockMap::const_iterator mapIt = pMap->getValue().begin();
313 ChunkBlockMap::const_iterator mapEnd = pMap->getValue().end ();
315 for(; mapIt != mapEnd; ++mapIt)
317 if((*mapIt).second != NULL)
319 Thread::getCurrentChangeList()->addDelayedSubRef<
320 UnrecordedRefCountPolicy>((*mapIt).second);
324 pMap->getValue().clear();
327 UInt32 fcId;
329 union
331 ChunkBlock *pointerVal;
332 UIntPointer uiVal;
333 } tmpEntry;
335 for(UInt32 i = 0; i < uiNumElements; ++i)
337 ChunkBlockMapKey key = KeyPool::the()->getDefault();
338 UInt32 uiNumKeys;
340 pMem->getValue(uiNumKeys);
342 for(UInt32 j = 0; j < uiNumKeys; ++j)
344 std::string tmpString;
346 FieldTraits<std::string>::copyFromBin(*pMem, tmpString);
348 ChunkBlockMapKey tmpKey =
349 KeyPool::the()->getAuto(tmpString.c_str());
351 key |= tmpKey;
354 pMem->getValue(fcId);
356 tmpEntry.uiVal = fcId;
358 pMap->getValue().insert(ChunkBlockMap::value_type(key,
359 tmpEntry.pointerVal));
362 UInt16 uiCount = 0;
364 ChunkBlockMap::iterator mapWIt = pMap->getValue().begin();
365 mapEnd = pMap->getValue().end ();
367 for(; mapWIt != mapEnd; ++mapWIt, ++uiCount)
369 tmpEntry.pointerVal = mapWIt->second;
371 vIds .push_back(tmpEntry.uiVal);
372 vBindings.push_back(uiCount );
374 mapWIt->second = NULL;
377 return true;
380 void EditSFieldHandle<SFChunkBlockPtrMap>::fillFrom(
381 const std::vector<UInt16> &vBindings,
382 const std::vector<UInt32> &vIds,
383 const std::map<UInt32, UInt32> &mIdMap )
385 SFChunkBlockPtrMap *pMap = static_cast<SFChunkBlockPtrMap *>(_pField);
387 if(pMap == NULL)
388 return;
390 ChunkBlockMap::iterator mapIt = pMap->getValue().begin();
391 ChunkBlockMap::const_iterator mapEnd = pMap->getValue().end ();
393 std::vector<UInt32 >::const_iterator idIt = vIds.begin();
394 std::map <UInt32, UInt32>::const_iterator imapIt;
395 std::map <UInt32, UInt32>::const_iterator imapEnd = mIdMap.end();
397 OSG_ASSERT(vIds .size() == pMap->getValue().size() &&
398 vBindings.size() == pMap->getValue().size() );
400 ChunkBlock *pMat = NULL;
402 for(; mapIt != mapEnd; ++mapIt, ++idIt)
404 imapIt = mIdMap.find(*idIt);
406 if(imapIt != imapEnd)
408 pMat = dynamic_cast<ChunkBlock *>(
409 FieldContainerFactory::the()->getContainer(imapIt->second));
411 UnrecordedRefCountPolicy::addRef(pMat);
413 else
415 pMat = NULL;
418 mapIt->second = pMat;
423 void EditSFieldHandle<SFChunkBlockPtrMap>::cloneValues(
424 GetFieldHandlePtr pSrc,
425 const TypePtrVector &shareTypes,
426 const TypePtrVector &ignoreTypes,
427 const TypeIdVector &shareGroupIds,
428 const TypeIdVector &ignoreGroupIds) const
430 SFChunkBlockPtrMap::GetHandlePtr pGetHandle =
431 boost::dynamic_pointer_cast<
432 SFChunkBlockPtrMap::GetHandle>(pSrc);
434 if(pGetHandle == NULL || pGetHandle->isValid() == false)
435 return;
437 const SFChunkBlockPtrMap &pAttMap = **pGetHandle;
439 ChunkBlockMap::const_iterator mapIt = pAttMap.getValue().begin();
440 ChunkBlockMap::const_iterator mapEnd = pAttMap.getValue().end();
442 for(; mapIt != mapEnd; ++mapIt)
444 ChunkBlockUnrecPtr att = mapIt->second;
445 ChunkBlockMapKey key = mapIt->first;
447 if(att != NULL)
449 const FieldContainerType &attType = att->getType();
451 // test if att type should NOT be ignored
452 if(!TypePredicates::typeInGroupIds (ignoreGroupIds.begin(),
453 ignoreGroupIds.end(),
454 attType ) &&
455 !TypePredicates::typeDerivedFrom(ignoreTypes.begin(),
456 ignoreTypes.end(),
457 attType ) )
459 // test if att should cloned
460 if(!TypePredicates::typeInGroupIds (shareGroupIds.begin(),
461 shareGroupIds.end(),
462 attType ) &&
463 !TypePredicates::typeDerivedFrom(shareTypes.begin(),
464 shareTypes.end(),
465 attType ) )
467 att = dynamic_pointer_cast<ChunkBlock>(
468 OSG::deepClone(att, shareTypes, ignoreTypes,
469 shareGroupIds, ignoreGroupIds));
474 if(_fAddMethod)
476 _fAddMethod(att, key);
482 void GetSFieldHandle<SFChunkBlockPtrMap>::traverse(TraverseCallback oCallback)
484 SFChunkBlockPtrMap const *pMap =
485 static_cast<SFChunkBlockPtrMap const *>(_pField);
487 if(oCallback && pMap != NULL)
489 ChunkBlockMap::const_iterator mapIt = pMap->getValue().begin();
490 ChunkBlockMap::const_iterator mapEnd = pMap->getValue().end ();
492 for(; mapIt != mapEnd; ++mapIt)
494 oCallback(mapIt->second);
499 void GetSFieldHandle<SFChunkBlockPtrMap>::flatten(MapList &vList)
501 vList.clear();
503 SFChunkBlockPtrMap const *pMap =
504 static_cast<SFChunkBlockPtrMap const *>(_pField);
506 if(pMap != NULL)
508 ChunkBlockMap::const_iterator mapIt = pMap->getValue().begin();
509 ChunkBlockMap::const_iterator mapEnd = pMap->getValue().end ();
511 for(; mapIt != mapEnd; ++mapIt)
513 ListEntry tmpEntry;
515 KeyPool::the()->keyToStringList(mapIt->first,
516 tmpEntry.first);
518 tmpEntry.second = mapIt->second;
520 vList.push_back(tmpEntry);
525 void GetSFieldHandle<SFChunkBlockPtrMap>::flatten(ContainerList &vList)
527 vList.clear();
529 SFChunkBlockPtrMap const *pMap =
530 static_cast<SFChunkBlockPtrMap const *>(_pField);
532 if(pMap != NULL)
534 ChunkBlockMap::const_iterator mapIt = pMap->getValue().begin();
535 ChunkBlockMap::const_iterator mapEnd = pMap->getValue().end ();
537 for(; mapIt != mapEnd; ++mapIt)
539 vList.push_back(mapIt->second);
544 #if !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)
546 DataType FieldTraits<ChunkBlockMap>::_type(
547 "ChunkBlockMap",
548 "FieldContainerPtrMap");
550 OSG_FIELDTRAITS_GETTYPE(ChunkBlockMap)
552 #endif // !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)
554 OSG_END_NAMESPACE