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 "OSGAttachmentMapSFields.h"
45 #include "OSGAttachment.h"
46 #include "OSGAttachmentContainer.h"
47 #include "OSGContainerPtrFuncs.h"
49 #include "OSGTypeBasePredicates.h"
50 #include "OSGReflexiveContainerTypePredicates.h"
51 #include "OSGMapHelper.h"
53 #include "OSGSField.ins"
58 OSG_FIELD_DLLEXPORT_DEF1(SField
, AttachmentMap
)
61 const FieldType
&GetSFieldHandle
<SFAttachmentPtrMap
>::getType(void) const
63 return SFAttachmentPtrMap::getClassType();
66 const FieldType
&EditSFieldHandle
<SFAttachmentPtrMap
>::getType(void) const
68 return SFAttachmentPtrMap::getClassType();
71 SizeT FieldTraits
<AttachmentMap
>::getBinSize(const AttachmentMap
&aMap
)
73 AttachmentMap::const_iterator mapIt
= aMap
.begin();
74 AttachmentMap::const_iterator mapEnd
= aMap
.end ();
76 UInt32 numPublicObjects
= 0;
78 for(; mapIt
!= mapEnd
; ++mapIt
)
80 if(mapIt
->second
->getInternal() == false)
86 // number of elements in map + binding and pointer id for each element
87 return sizeof(UInt32
) +
88 numPublicObjects
* (sizeof(UInt16
) + sizeof(UInt32
));
91 void FieldTraits
<AttachmentMap
>::copyToBin( BinaryDataHandler
&pMem
,
92 const AttachmentMap
&aMap
)
94 AttachmentMap::const_iterator mapIt
= aMap
.begin();
95 AttachmentMap::const_iterator mapEnd
= aMap
.end ();
97 UInt32 numPublicObjects
= 0;
101 for(; mapIt
!= mapEnd
; ++mapIt
)
103 if(mapIt
->second
->getInternal() == false)
109 pMem
.putValue(numPublicObjects
);
111 for(mapIt
= aMap
.begin(); mapIt
!= mapEnd
; ++mapIt
)
113 if(mapIt
->second
->getInternal() == false)
115 binding
= mapIt
->first
& 0xFFFF;
116 fcId
= mapIt
->second
->getId();
118 pMem
.putValue(binding
);
119 pMem
.putValue(fcId
);
124 void FieldTraits
<AttachmentMap
>::copyFromBin(BinaryDataHandler
&pMem
,
125 AttachmentMap
&aMap
)
136 AttachmentMap::const_iterator mapIt
= aMap
.begin();
137 AttachmentMap::const_iterator mapEnd
= aMap
.end ();
139 for(; mapIt
!= mapEnd
; ++mapIt
)
141 if((*mapIt
).second
!= NULL
)
143 Thread::getCurrentChangeList()->addDelayedSubRef
<
144 UnrecordedRefCountPolicy
>((*mapIt
).second
);
151 for(UInt32 i
= 0; i
< size
; ++i
)
153 pMem
.getValue(binding
);
154 pMem
.getValue(fcId
);
156 attPtr
= dynamic_cast<Attachment
*>(
157 FieldContainerFactory::the()->getMappedContainer(fcId
));
161 key
= (static_cast<UInt32
>(attPtr
->getGroupId()) << 16) | binding
;
163 UnrecordedRefCountPolicy::addRef(attPtr
);
165 aMap
.insert(AttachmentMap::value_type(key
, attPtr
));
170 void EditSFieldHandle
<SFAttachmentPtrMap
>::add(
172 const std::string
&szBindings
)
174 Attachment
*pVal
= dynamic_cast<Attachment
*>(rhs
);
175 UInt32 uiBindings
= 0;
179 MapHelper
*pMHelper
= dynamic_cast<MapHelper
*>(rhs
);
183 pVal
= dynamic_cast<Attachment
*>(pMHelper
->getContainer());
185 if(pMHelper
->getMFKeys()->empty() == false)
187 const std::string
&szKey
= pMHelper
->getKeys(0);
189 uiBindings
= TypeTraits
<UInt32
>::getFromCString(szKey
.c_str());
195 if(szBindings
.empty() == false)
197 uiBindings
= TypeTraits
<UInt32
>::getFromCString(szBindings
.c_str());
201 if(rhs
!= NULL
&& pVal
== NULL
)
205 // for whatever reason VS2003 does not like == NULL
208 _fAddMethod(pVal
, uiBindings
);
212 void EditSFieldHandle
<SFAttachmentPtrMap
>::replaceByObj(Attachment
* const pOld
,
213 Attachment
* const pNew
)
217 _fReplaceMethod(pOld
, pNew
);
221 void EditSFieldHandle
<SFAttachmentPtrMap
>::traverse(TraverseCallback oCallback
)
223 const SFAttachmentPtrMap
*pMap
= static_cast<SFAttachmentPtrMap
*>(_pField
);
225 if(oCallback
&& pMap
!= NULL
)
227 AttachmentMap::const_iterator mapIt
= pMap
->getValue().begin();
228 AttachmentMap::const_iterator mapEnd
= pMap
->getValue().end ();
230 for(; mapIt
!= mapEnd
; ++mapIt
)
232 oCallback(mapIt
->second
);
237 void EditSFieldHandle
<SFAttachmentPtrMap
>::flatten(MapList
&vList
)
241 const SFAttachmentPtrMap
*pMap
= static_cast<SFAttachmentPtrMap
*>(_pField
);
245 AttachmentMap::const_iterator mapIt
= pMap
->getValue().begin();
246 AttachmentMap::const_iterator mapEnd
= pMap
->getValue().end ();
248 for(; mapIt
!= mapEnd
; ++mapIt
)
250 if(mapIt
->second
->getInternal() == true)
255 TypeTraits
<UInt32
>::putToString((mapIt
->first
& 0x0000FFFF),
260 tmpEntry
.first
.push_back(szKey
);
261 tmpEntry
.second
= mapIt
->second
;
263 vList
.push_back(tmpEntry
);
268 void EditSFieldHandle
<SFAttachmentPtrMap
>::flatten(ContainerList
&vList
)
272 const SFAttachmentPtrMap
*pMap
= static_cast<SFAttachmentPtrMap
*>(_pField
);
276 AttachmentMap::const_iterator mapIt
= pMap
->getValue().begin();
277 AttachmentMap::const_iterator mapEnd
= pMap
->getValue().end ();
279 for(; mapIt
!= mapEnd
; ++mapIt
)
281 if(mapIt
->second
->getInternal() == true)
284 vList
.push_back(mapIt
->second
);
289 bool EditSFieldHandle
<SFAttachmentPtrMap
>::loadFromBin(
290 BinaryDataHandler
*pMem
,
291 UInt32 uiNumElements
,
293 std::vector
<UInt16
> &vBindings
,
294 std::vector
<UInt32
> &vIds
)
299 for(UInt32 i
= 0; i
< uiNumElements
; ++i
)
301 pMem
->getValue(binding
);
302 pMem
->getValue(ptrId
);
304 FDEBUG(("EditSFieldHandle<SFAttachmentPtrMap>::loadFromBin: "
305 "attachment [%u], binding [%u], id [%u].\n",
308 vBindings
.push_back(binding
);
309 vIds
.push_back(ptrId
);
316 void EditSFieldHandle
<SFAttachmentPtrMap
>::fillFrom(
317 const std::vector
<UInt16
> &vBindings
,
318 const std::vector
<UInt32
> &vIds
,
319 const std::map
<UInt32
, UInt32
> &vIdMap
)
323 void EditSFieldHandle
<SFAttachmentPtrMap
>::cloneValues(
324 GetFieldHandlePtr pSrc
,
325 const TypePtrVector
&shareTypes
,
326 const TypePtrVector
&ignoreTypes
,
327 const TypeIdVector
&shareGroupIds
,
328 const TypeIdVector
&ignoreGroupIds
) const
330 SFAttachmentPtrMap::GetHandlePtr pGetHandle
=
331 boost::dynamic_pointer_cast
<
332 SFAttachmentPtrMap::GetHandle
>(pSrc
);
334 if(pGetHandle
== NULL
|| pGetHandle
->isValid() == false)
337 const SFAttachmentPtrMap
&pAttMap
= **pGetHandle
;
339 AttachmentMap::const_iterator mapIt
= pAttMap
.getValue().begin();
340 AttachmentMap::const_iterator mapEnd
= pAttMap
.getValue().end();
342 for(; mapIt
!= mapEnd
; ++mapIt
)
344 AttachmentUnrecPtr att
= mapIt
->second
;
345 UInt16 uiBinding
= UInt16(mapIt
->first
&
350 const FieldContainerType
&attType
= att
->getType();
352 // test if att type should NOT be ignored
353 if(!TypePredicates::typeInGroupIds (ignoreGroupIds
.begin(),
354 ignoreGroupIds
.end(),
356 !TypePredicates::typeDerivedFrom(ignoreTypes
.begin(),
360 // test if att should cloned
361 if(!TypePredicates::typeInGroupIds (shareGroupIds
.begin(),
364 !TypePredicates::typeDerivedFrom(shareTypes
.begin(),
368 att
= dynamic_pointer_cast
<Attachment
>(
369 OSG::deepClone(att
, shareTypes
, ignoreTypes
,
370 shareGroupIds
, ignoreGroupIds
));
377 _fAddMethod(att
, uiBinding
);
383 void GetSFieldHandle
<SFAttachmentPtrMap
>::traverse(TraverseCallback oCallback
)
385 SFAttachmentPtrMap
const *pMap
=
386 static_cast<SFAttachmentPtrMap
const *>(_pField
);
388 if(oCallback
&& pMap
!= NULL
)
390 AttachmentMap::const_iterator mapIt
= pMap
->getValue().begin();
391 AttachmentMap::const_iterator mapEnd
= pMap
->getValue().end ();
393 for(; mapIt
!= mapEnd
; ++mapIt
)
395 oCallback(mapIt
->second
);
400 void GetSFieldHandle
<SFAttachmentPtrMap
>::flatten(MapList
&vList
)
404 SFAttachmentPtrMap
const *pMap
=
405 static_cast<SFAttachmentPtrMap
const *>(_pField
);
409 AttachmentMap::const_iterator mapIt
= pMap
->getValue().begin();
410 AttachmentMap::const_iterator mapEnd
= pMap
->getValue().end ();
412 for(; mapIt
!= mapEnd
; ++mapIt
)
414 if(mapIt
->second
->getInternal() == true)
419 TypeTraits
<UInt32
>::putToString((mapIt
->first
& 0x0000FFFF),
424 tmpEntry
.first
.push_back(szKey
);
425 tmpEntry
.second
= mapIt
->second
;
427 vList
.push_back(tmpEntry
);
432 void GetSFieldHandle
<SFAttachmentPtrMap
>::flatten(ContainerList
&vList
)
436 SFAttachmentPtrMap
const *pMap
=
437 static_cast<SFAttachmentPtrMap
const *>(_pField
);
441 AttachmentMap::const_iterator mapIt
= pMap
->getValue().begin();
442 AttachmentMap::const_iterator mapEnd
= pMap
->getValue().end ();
444 for(; mapIt
!= mapEnd
; ++mapIt
)
446 if(mapIt
->second
->getInternal() == true)
449 vList
.push_back(mapIt
->second
);
454 #if !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)
456 DataType FieldTraits
<AttachmentMap
>::_type(
458 "FieldContainerPtrMap");
460 OSG_FIELDTRAITS_GETTYPE(AttachmentMap
)
462 #endif // !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)