fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Material / Base / OSGMaterialMapFields.cpp
blob1b130c0939a691bc29a89836de281bb63a82c520
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 "OSGMaterialMapFields.h"
45 #include "OSGPrimeMaterial.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, MaterialMap)
60 const FieldType &GetSFieldHandle<SFMaterialPtrMap>::getType(void) const
62 return SFMaterialPtrMap::getClassType();
65 const FieldType &EditSFieldHandle<SFMaterialPtrMap>::getType(void) const
67 return SFMaterialPtrMap::getClassType();
70 SizeT FieldTraits<MaterialMap>::getBinSize(const MaterialMap &aMap)
72 // number of elements in map + binding and pointer id for each element
73 MaterialMap::const_iterator mapIt = aMap.begin();
74 MaterialMap::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<MaterialMap>::copyToBin( BinaryDataHandler &pMem,
99 const MaterialMap &aMap )
101 MaterialMap::const_iterator mapIt = aMap.begin();
102 MaterialMap::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<MaterialMap>::copyFromBin(BinaryDataHandler &pMem,
134 MaterialMap &aMap )
136 PrimeMaterial *matPtr;
137 MaterialMapKey key = KeyPool::the()->getDefault();
138 UInt32 fcId;
139 UInt32 uiNumObjects;
141 pMem.getValue(uiNumObjects);
143 MaterialMap::const_iterator mapIt = aMap.begin();
144 MaterialMap::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 MaterialMapKey tmpKey = KeyPool::the()->getAuto(tmpString.c_str());
172 key |= tmpKey;
175 pMem.getValue(fcId);
177 matPtr = dynamic_cast<PrimeMaterial *>(
178 FieldContainerFactory::the()->getMappedContainer(fcId));
180 if(matPtr != NULL)
182 UnrecordedRefCountPolicy::addRef(matPtr);
184 aMap.insert(MaterialMap::value_type(key, matPtr));
189 void EditSFieldHandle<SFMaterialPtrMap>::add(
190 FieldContainer *rhs,
191 const std::string &szBindings)
193 PrimeMaterial *pVal = dynamic_cast<PrimeMaterial *>(rhs);
194 KeyType oKey = 0x0000;
196 if(pVal == NULL)
198 MapHelper *pMHelper = dynamic_cast<MapHelper *>(rhs);
200 if(pMHelper != NULL)
202 pVal = dynamic_cast<PrimeMaterial *>(pMHelper->getContainer());
204 const MapHelper::MFKeysType *pKeys = pMHelper->getMFKeys();
206 if(pKeys->empty() == false)
208 KeyType tmpKey = KeyPool::the()->getDefault();
210 for(UInt32 i = 0; i < pKeys->size(); ++i)
212 const std::string &szKey = pMHelper->getKeys(i);
214 tmpKey = KeyPool::the()->getAuto(szKey.c_str());
216 oKey |= tmpKey;
221 else
223 if(szBindings.empty() == false)
225 oKey = KeyPool::the()->getAuto(szBindings.c_str());
229 if(rhs != NULL && pVal == NULL)
230 return;
233 // for whatever reason VS2003 does not like == NULL
234 if(_fAddMethod)
236 _fAddMethod(pVal, oKey);
240 void EditSFieldHandle<SFMaterialPtrMap>::traverse(TraverseCallback oCallback)
242 const SFMaterialPtrMap *pMap = static_cast<SFMaterialPtrMap *>(_pField);
244 if(oCallback && pMap != NULL)
246 MaterialMap::const_iterator mapIt = pMap->getValue().begin();
247 MaterialMap::const_iterator mapEnd = pMap->getValue().end ();
249 for(; mapIt != mapEnd; ++mapIt)
251 oCallback(mapIt->second);
256 void EditSFieldHandle<SFMaterialPtrMap>::flatten(MapList &vList)
258 vList.clear();
260 const SFMaterialPtrMap *pMap = static_cast<SFMaterialPtrMap *>(_pField);
262 if(pMap != NULL)
264 MaterialMap::const_iterator mapIt = pMap->getValue().begin();
265 MaterialMap::const_iterator mapEnd = pMap->getValue().end ();
267 for(; mapIt != mapEnd; ++mapIt)
269 ListEntry tmpEntry;
271 KeyPool::the()->keyToStringList(mapIt->first,
272 tmpEntry.first);
274 tmpEntry.second = mapIt->second;
276 vList.push_back(tmpEntry);
281 void EditSFieldHandle<SFMaterialPtrMap>::flatten(ContainerList &vList)
283 vList.clear();
285 const SFMaterialPtrMap *pMap = static_cast<SFMaterialPtrMap *>(_pField);
287 if(pMap != NULL)
289 MaterialMap::const_iterator mapIt = pMap->getValue().begin();
290 MaterialMap::const_iterator mapEnd = pMap->getValue().end ();
292 for(; mapIt != mapEnd; ++mapIt)
294 vList.push_back(mapIt->second);
299 bool EditSFieldHandle<SFMaterialPtrMap>::loadFromBin(
300 BinaryDataHandler *pMem,
301 UInt32 uiNumElements,
302 bool hasBindingInfo,
303 std::vector<UInt16> &vBindings,
304 std::vector<UInt32> &vIds )
306 SFMaterialPtrMap *pMap = static_cast<SFMaterialPtrMap *>(_pField);
308 if(pMap == NULL)
309 return true;
311 MaterialMap::const_iterator mapIt = pMap->getValue().begin();
312 MaterialMap::const_iterator mapEnd = pMap->getValue().end ();
314 for(; mapIt != mapEnd; ++mapIt)
316 if((*mapIt).second != NULL)
318 Thread::getCurrentChangeList()->addDelayedSubRef<
319 UnrecordedRefCountPolicy>((*mapIt).second);
323 pMap->getValue().clear();
326 UInt32 fcId;
328 union
330 PrimeMaterial *pointerVal;
331 UIntPointer uiVal;
332 } tmpEntry;
334 for(UInt32 i = 0; i < uiNumElements; ++i)
336 MaterialMapKey key = KeyPool::the()->getDefault();
337 UInt32 uiNumKeys;
339 pMem->getValue(uiNumKeys);
341 for(UInt32 j = 0; j < uiNumKeys; ++j)
343 std::string tmpString;
345 FieldTraits<std::string>::copyFromBin(*pMem, tmpString);
347 MaterialMapKey tmpKey = KeyPool::the()->getAuto(tmpString.c_str());
349 key |= tmpKey;
352 pMem->getValue(fcId);
354 tmpEntry.uiVal = fcId;
356 pMap->getValue().insert(MaterialMap::value_type(key,
357 tmpEntry.pointerVal));
360 UInt16 uiCount = 0;
362 MaterialMap::iterator mapWIt = pMap->getValue().begin();
363 mapEnd = pMap->getValue().end ();
365 for(; mapWIt != mapEnd; ++mapWIt, ++uiCount)
367 tmpEntry.pointerVal = mapWIt->second;
369 vIds .push_back(tmpEntry.uiVal);
370 vBindings.push_back(uiCount );
372 mapWIt->second = NULL;
375 return true;
378 void EditSFieldHandle<SFMaterialPtrMap>::fillFrom(
379 const std::vector<UInt16> &vBindings,
380 const std::vector<UInt32> &vIds,
381 const std::map<UInt32, UInt32> &mIdMap )
383 SFMaterialPtrMap *pMap = static_cast<SFMaterialPtrMap *>(_pField);
385 if(pMap == NULL)
386 return;
388 MaterialMap::iterator mapIt = pMap->getValue().begin();
389 MaterialMap::const_iterator mapEnd = pMap->getValue().end ();
391 std::vector<UInt32 >::const_iterator idIt = vIds.begin();
392 std::map <UInt32, UInt32>::const_iterator imapIt;
393 std::map <UInt32, UInt32>::const_iterator imapEnd = mIdMap.end();
395 OSG_ASSERT(vIds .size() == pMap->getValue().size() &&
396 vBindings.size() == pMap->getValue().size() );
398 PrimeMaterial *pMat = NULL;
400 for(; mapIt != mapEnd; ++mapIt, ++idIt)
402 imapIt = mIdMap.find(*idIt);
404 if(imapIt != imapEnd)
406 pMat = dynamic_cast<PrimeMaterial *>(
407 FieldContainerFactory::the()->getContainer(imapIt->second));
409 UnrecordedRefCountPolicy::addRef(pMat);
411 else
413 pMat = NULL;
416 mapIt->second = pMat;
421 void EditSFieldHandle<SFMaterialPtrMap>::cloneValues(
422 GetFieldHandlePtr pSrc,
423 const TypePtrVector &shareTypes,
424 const TypePtrVector &ignoreTypes,
425 const TypeIdVector &shareGroupIds,
426 const TypeIdVector &ignoreGroupIds) const
428 SFMaterialPtrMap::GetHandlePtr pGetHandle =
429 boost::dynamic_pointer_cast<
430 SFMaterialPtrMap::GetHandle>(pSrc);
432 if(pGetHandle == NULL || pGetHandle->isValid() == false)
433 return;
435 const SFMaterialPtrMap &pAttMap = **pGetHandle;
437 MaterialMap::const_iterator mapIt = pAttMap.getValue().begin();
438 MaterialMap::const_iterator mapEnd = pAttMap.getValue().end();
440 for(; mapIt != mapEnd; ++mapIt)
442 PrimeMaterialUnrecPtr att = mapIt->second;
443 MaterialMapKey key = mapIt->first;
445 if(att != NULL)
447 const FieldContainerType &attType = att->getType();
449 // test if att type should NOT be ignored
450 if(!TypePredicates::typeInGroupIds (ignoreGroupIds.begin(),
451 ignoreGroupIds.end(),
452 attType ) &&
453 !TypePredicates::typeDerivedFrom(ignoreTypes.begin(),
454 ignoreTypes.end(),
455 attType ) )
457 // test if att should cloned
458 if(!TypePredicates::typeInGroupIds (shareGroupIds.begin(),
459 shareGroupIds.end(),
460 attType ) &&
461 !TypePredicates::typeDerivedFrom(shareTypes.begin(),
462 shareTypes.end(),
463 attType ) )
465 att = dynamic_pointer_cast<PrimeMaterial>(
466 OSG::deepClone(att, shareTypes, ignoreTypes,
467 shareGroupIds, ignoreGroupIds));
472 if(_fAddMethod)
474 _fAddMethod(att, key);
480 void GetSFieldHandle<SFMaterialPtrMap>::traverse(TraverseCallback oCallback)
482 SFMaterialPtrMap const *pMap =
483 static_cast<SFMaterialPtrMap const *>(_pField);
485 if(oCallback && pMap != NULL)
487 MaterialMap::const_iterator mapIt = pMap->getValue().begin();
488 MaterialMap::const_iterator mapEnd = pMap->getValue().end ();
490 for(; mapIt != mapEnd; ++mapIt)
492 oCallback(mapIt->second);
497 void GetSFieldHandle<SFMaterialPtrMap>::flatten(MapList &vList)
499 vList.clear();
501 SFMaterialPtrMap const *pMap =
502 static_cast<SFMaterialPtrMap const *>(_pField);
504 if(pMap != NULL)
506 MaterialMap::const_iterator mapIt = pMap->getValue().begin();
507 MaterialMap::const_iterator mapEnd = pMap->getValue().end ();
509 for(; mapIt != mapEnd; ++mapIt)
511 ListEntry tmpEntry;
513 KeyPool::the()->keyToStringList(mapIt->first,
514 tmpEntry.first);
516 tmpEntry.second = mapIt->second;
518 vList.push_back(tmpEntry);
523 void GetSFieldHandle<SFMaterialPtrMap>::flatten(ContainerList &vList)
525 vList.clear();
527 SFMaterialPtrMap const *pMap =
528 static_cast<SFMaterialPtrMap const *>(_pField);
530 if(pMap != NULL)
532 MaterialMap::const_iterator mapIt = pMap->getValue().begin();
533 MaterialMap::const_iterator mapEnd = pMap->getValue().end ();
535 for(; mapIt != mapEnd; ++mapIt)
537 vList.push_back(mapIt->second);
542 #if !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)
544 DataType FieldTraits<MaterialMap>::_type(
545 "MaterialMap",
546 "FieldContainerPtrMap");
548 OSG_FIELDTRAITS_GETTYPE(MaterialMap)
550 #endif // !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)
552 OSG_END_NAMESPACE