1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2003 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 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
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"
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 ();
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());
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();
133 void FieldTraits
<ChunkBlockMap
>::copyFromBin(BinaryDataHandler
&pMem
,
134 ChunkBlockMap
&aMap
)
137 ChunkBlockMapKey key
= KeyPool::the()->getDefault();
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
);
158 for(UInt32 i
= 0; i
< uiNumObjects
; ++i
)
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());
178 matPtr
= dynamic_cast<ChunkBlock
*>(
179 FieldContainerFactory::the()->getMappedContainer(fcId
));
183 UnrecordedRefCountPolicy::addRef(matPtr
);
185 aMap
.insert(ChunkBlockMap::value_type(key
, matPtr
));
190 void EditSFieldHandle
<SFChunkBlockPtrMap
>::add(
192 const std::string
&szBindings
)
194 ChunkBlock
*pVal
= dynamic_cast<ChunkBlock
*>(rhs
);
195 KeyType oKey
= 0x0000;
199 MapHelper
*pMHelper
= dynamic_cast<MapHelper
*>(rhs
);
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());
224 if(szBindings
.empty() == false)
226 oKey
= KeyPool::the()->getAuto(szBindings
.c_str());
230 if(rhs
!= NULL
&& pVal
== NULL
)
234 // for whatever reason VS2003 does not like == NULL
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
)
261 const SFChunkBlockPtrMap
*pMap
= static_cast<SFChunkBlockPtrMap
*>(_pField
);
265 ChunkBlockMap::const_iterator mapIt
= pMap
->getValue().begin();
266 ChunkBlockMap::const_iterator mapEnd
= pMap
->getValue().end ();
268 for(; mapIt
!= mapEnd
; ++mapIt
)
272 KeyPool::the()->keyToStringList(mapIt
->first
,
275 tmpEntry
.second
= mapIt
->second
;
277 vList
.push_back(tmpEntry
);
282 void EditSFieldHandle
<SFChunkBlockPtrMap
>::flatten(ContainerList
&vList
)
286 const SFChunkBlockPtrMap
*pMap
= static_cast<SFChunkBlockPtrMap
*>(_pField
);
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
,
304 std::vector
<UInt16
> &vBindings
,
305 std::vector
<UInt32
> &vIds
)
307 SFChunkBlockPtrMap
*pMap
= static_cast<SFChunkBlockPtrMap
*>(_pField
);
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();
331 ChunkBlock
*pointerVal
;
335 for(UInt32 i
= 0; i
< uiNumElements
; ++i
)
337 ChunkBlockMapKey key
= KeyPool::the()->getDefault();
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());
354 pMem
->getValue(fcId
);
356 tmpEntry
.uiVal
= fcId
;
358 pMap
->getValue().insert(ChunkBlockMap::value_type(key
,
359 tmpEntry
.pointerVal
));
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
;
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
);
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
);
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)
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
;
449 const FieldContainerType
&attType
= att
->getType();
451 // test if att type should NOT be ignored
452 if(!TypePredicates::typeInGroupIds (ignoreGroupIds
.begin(),
453 ignoreGroupIds
.end(),
455 !TypePredicates::typeDerivedFrom(ignoreTypes
.begin(),
459 // test if att should cloned
460 if(!TypePredicates::typeInGroupIds (shareGroupIds
.begin(),
463 !TypePredicates::typeDerivedFrom(shareTypes
.begin(),
467 att
= dynamic_pointer_cast
<ChunkBlock
>(
468 OSG::deepClone(att
, shareTypes
, ignoreTypes
,
469 shareGroupIds
, ignoreGroupIds
));
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
)
503 SFChunkBlockPtrMap
const *pMap
=
504 static_cast<SFChunkBlockPtrMap
const *>(_pField
);
508 ChunkBlockMap::const_iterator mapIt
= pMap
->getValue().begin();
509 ChunkBlockMap::const_iterator mapEnd
= pMap
->getValue().end ();
511 for(; mapIt
!= mapEnd
; ++mapIt
)
515 KeyPool::the()->keyToStringList(mapIt
->first
,
518 tmpEntry
.second
= mapIt
->second
;
520 vList
.push_back(tmpEntry
);
525 void GetSFieldHandle
<SFChunkBlockPtrMap
>::flatten(ContainerList
&vList
)
529 SFChunkBlockPtrMap
const *pMap
=
530 static_cast<SFChunkBlockPtrMap
const *>(_pField
);
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(
548 "FieldContainerPtrMap");
550 OSG_FIELDTRAITS_GETTYPE(ChunkBlockMap
)
552 #endif // !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)