fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FieldContainer / Mixins / OSGMapCacheHandlerMixin.inl
blob4228a17d169870358a0d345dae2a6858e37189c6
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 OSG_BEGIN_NAMESPACE
41 template <class Desc> inline
42 void MapCacheHandlerMixin<Desc>::classDescInserter(TypeObject &oType)
44     Inherited::classDescInserter(oType);
46     FieldDescriptionBase *pDesc;
48     typedef typename MapCacheField::Description SFDesc;
50     pDesc = new SFDesc(
51         MapCacheField::getClassType(),
52         Desc::getFieldName(),
53         "map cache.",
54         OSG_RC_FIELD_DESC(Self::MapCache),
55         Desc::IsInternal,
56         Field::SFDefaultFlags,
57         static_cast<FieldEditMethodSig>(&Self::editHandleMapCache),
58         static_cast<FieldGetMethodSig >(&Self::getHandleMapCache ));
60     oType.addInitialDesc(pDesc);
63 template <class Desc> inline
64 void MapCacheHandlerMixin<Desc>::changed(ConstFieldMaskArg whichField, 
65                                          UInt32            origin,
66                                          BitVector         details)
68     Inherited::changed(whichField, origin, details);
71 /*-------------------------------------------------------------------------*/
72 /*                                Set                                      */
74 template <class Desc> inline
75 typename MapCacheHandlerMixin<Desc>::FinalizedElement *
76     MapCacheHandlerMixin<Desc>::finalize(MapCacheKey oKey)
78     MapCacheFieldConstIt fcI = _fMapCache.getValue().find(oKey);
80     if(fcI == _fMapCache.getValue().end())
81     {
82         return Desc::getFallback(this); //_sfFallbackMaterial.getValue();
83     }
84     else
85     {
86         return (*fcI).second;
87     }
90 template <class Desc> inline
91 void MapCacheHandlerMixin<Desc>::addElement (
92     MapCacheElement  const pElement,
93     MapCacheKey            key     )
95     if(pElement == NULL)
96         return;
98     if(key == MapKeyPool::the()->getDefault())
99     {
100         Desc::setFallback(this, pElement);  //setFallbackMaterial(pMaterial);
102         return;
103     }
105     if(this->isMTLocal())
106     {
107         pElement->addReferenceRecorded();
108     }
109     else
110     {
111         pElement->addReferenceUnrecorded();
112     }
114 #if 0
115     pAttachment->linkParent(this, 
116                             AttachmentsFieldId, 
117                             Attachment::ParentsFieldId);
118 #endif
120     Self::editSField(MapCacheFieldMask);
122     MapCacheFieldIt fcI = _fMapCache.getValue().find(key);
124     if(fcI != _fMapCache.getValue().end())
125     {
126 #if 0
127         (*fcI).second->unlinkParent(this, 
128                                     Attachment::ParentsFieldId);
129 #endif
131         if(this->isMTLocal())
132         {
133             (*fcI).second->subReferenceRecorded();
134         }
135         else
136         {
137             (*fcI).second->subReferenceUnrecorded();
138         }
140         (*fcI).second = pElement;
141     }
142     else
143     {
144         _fMapCache.getValue()[key] = pElement;
145     }
148 template <class Desc> inline
149 void MapCacheHandlerMixin<Desc>::subElement(MapCacheKey key)
151     if(key == MapKeyPool::the()->getDefault())
152     {
153         Desc::setFallback(this, NULL); //setFallbackMaterial(NULL);
154         return;
155     }
157     Self::editSField(MapCacheFieldMask);
159     MapCacheFieldIt fcI;
161     fcI = _fMapCache.getValue().find(key);
163     if(fcI != _fMapCache.getValue().end())
164     {
165 #if 0
166         (*fcI).second->unlinkParent(this, 
167                                     Attachment::ParentsFieldId);
168 #endif
170         if(this->isMTLocal())
171         {
172             (*fcI).second->subReferenceRecorded();
173         }
174         else
175         {
176             (*fcI).second->subReferenceUnrecorded();
177         }
179         _fMapCache.getValue().erase(fcI);
180     }
183 template <class Desc> inline
184 void MapCacheHandlerMixin<Desc>::clearElements (void)
186     while(_fMapCache.getValue().size() != 0)
187     {
188         subElement(_fMapCache.getValue().begin()->first);
189     }
193 template <class Desc> inline
194 typename MapCacheHandlerMixin<Desc>::MapCacheElement 
195     MapCacheHandlerMixin<Desc>::findElement(MapCacheKey key) const
197     MapCacheFieldConstIt fcI = _fMapCache.getValue().find(key);
199     if(fcI == _fMapCache.getValue().end())
200     {
201         return NULL;
202     }
203     else
204     {
205         return (*fcI).second;
206     }
209 template <class Desc> inline
210 const typename MapCacheHandlerMixin<Desc>::MapCacheField *
211     MapCacheHandlerMixin<Desc>::getMapCacheField(void) const
213     return &_fMapCache;
216 /*-------------------------------------------------------------------------*/
217 /* Binary access                                                           */
219 template <class Desc> inline
220 SizeT MapCacheHandlerMixin<Desc>::getBinSize(ConstFieldMaskArg whichField)
222     SizeT returnValue = Inherited::getBinSize(whichField);
224     if(FieldBits::NoField != (MapCacheFieldMask & whichField))
225     {
226         returnValue += _fMapCache.getBinSize();
227     }
229     return returnValue;
232 template <class Desc> inline
233 void MapCacheHandlerMixin<Desc>::copyToBin(BinaryDataHandler  &pMem, 
234                                            ConstFieldMaskArg   whichField)
236     Inherited::copyToBin(pMem, whichField);
238     if(FieldBits::NoField != (MapCacheFieldMask & whichField))
239     {
240         _fMapCache.copyToBin(pMem);
241     }
244 template <class Desc> inline
245 void MapCacheHandlerMixin<Desc>::copyFromBin(BinaryDataHandler &pMem, 
246                                              ConstFieldMaskArg  whichField)
248     Inherited::copyFromBin(pMem, whichField);
250     if(FieldBits::NoField != (MapCacheFieldMask & whichField))
251     {
252         _fMapCache.copyFromBin(pMem);
253     }
256 /*-------------------------------------------------------------------------*/
257 /*                             Assignment                                  */
260 /*-------------------------------------------------------------------------*/
261 /*                            Constructors                                 */
263 template <class Desc> inline
264 MapCacheHandlerMixin<Desc>::MapCacheHandlerMixin(void) :
265      Inherited(),
266     _fMapCache()
270 template <class Desc> inline
271 MapCacheHandlerMixin<Desc>::MapCacheHandlerMixin(
272     const MapCacheHandlerMixin &source) :
274      Inherited(source),
275     _fMapCache(      )
279 template <class Desc> inline
280 MapCacheHandlerMixin<Desc>::~MapCacheHandlerMixin(void)
284 template <class Desc> inline
285 void MapCacheHandlerMixin<Desc>::dump(      UInt32    uiIndent,
286                                       const BitVector bvFlags ) const
290 template <class Desc> inline
291 EditFieldHandlePtr MapCacheHandlerMixin<Desc>::editHandleMapCache(void)
293     typename MapCacheField::EditHandlePtr returnValue(
294         new  typename MapCacheField::EditHandle(
295              &_fMapCache,
296              this->getType().getFieldDesc(MapCacheFieldId),
297              this));
299     returnValue->setAddMethod(
300         boost::bind(&Self::addElement,
301                     static_cast<Self *>(this), _1, _2));
303     this->editSField(MapCacheFieldMask);
305     return returnValue;
308 template <class Desc> inline
309 GetFieldHandlePtr  MapCacheHandlerMixin<Desc>::getHandleMapCache(void) const
311     typename MapCacheField::GetHandlePtr returnValue(
312         new  typename MapCacheField::GetHandle(
313              &_fMapCache,
314              this->getType().getFieldDesc(MapCacheFieldId),
315              const_cast<Self *>(this)));
317     return returnValue;
320 /*-------------------------------------------------------------------------*/
321 /*                             Destructor                                  */
324 #ifdef OSG_MT_CPTR_ASPECT
325 template <class Desc> inline
326 void MapCacheHandlerMixin<Desc>::execSync(
327           Self              *pFrom,
328           ConstFieldMaskArg  whichField,
329                              AspectOffsetStore &oOffsets,
330           ConstFieldMaskArg  syncMode  ,
331     const UInt32             uiSyncInfo)
333     Inherited::execSync(pFrom, whichField, oOffsets, syncMode, uiSyncInfo);
335     if(FieldBits::NoField != (MapCacheFieldMask & whichField))
336     {
337 //        _sfAttachments.syncWith(pFrom->_sfAttachments);
339         // needs optimizing
341         MapCache tmpMap;
343         MapCacheFieldIt fcI = pFrom->_fMapCache.getValue().begin();
344         MapCacheFieldIt fcE = pFrom->_fMapCache.getValue().end  ();
345         
346         while(fcI != fcE)
347         {
348             MapCacheElement pMat = Aspect::convertToCurrent((*fcI).second);
350             if(pMat != NULL)
351             {
352                 tmpMap[(*fcI).first] = pMat;
354                 pMat->addReferenceUnrecorded();
355             }
357             ++fcI;
358         }
360         fcI = _fMapCache.getValue().begin();
361         fcE = _fMapCache.getValue().end  ();
363         while(fcI != fcE)
364         {
365             if(this->isMTLocal())
366             {
367                 (*fcI).second->subReferenceRecorded();
368             }
369             else
370             {
371                 (*fcI).second->subReferenceUnrecorded();
372             }
373             
374             ++fcI;
375         }
377         _fMapCache.setValue(tmpMap);
378     }
380 #endif
382 template <class Desc> inline
383 void MapCacheHandlerMixin<Desc>::onCreateAspect(
384     const FinalContainer *createAspect,
385     const FinalContainer *source      )
387     Inherited::onCreateAspect(createAspect, source);
389     // avoid prototype
390     if(GlobalSystemState == OSG::Running)
391     {
392     }
395 template <class Desc> inline
396 void MapCacheHandlerMixin<Desc>::onCreate(const FinalContainer *source)
398     Inherited::onCreate(source);
400     // avoid prototype
401     if(GlobalSystemState == OSG::Running)
402     {
403     }
406 template <class Desc> inline
407 void MapCacheHandlerMixin<Desc>::onDestroy(UInt32 uiContainerId)
409     Inherited::onDestroy(uiContainerId);
411     // avoid prototype
412     if(GlobalSystemState == OSG::Running)
413     {
414     }
417 template <class Desc> inline
418 void MapCacheHandlerMixin<Desc>::onDestroyAspect(UInt32  uiContainerId,
419                                                  UInt32  uiAspect     )
421     Inherited::onDestroy(uiContainerId);
423     // avoid prototype
424     if(GlobalSystemState == OSG::Running)
425     {
426     }
429 template <class Desc> inline
430 void MapCacheHandlerMixin<Desc>::resolveLinks(void)
432     Inherited::resolveLinks();
434     MapCacheFieldIt fcI = _fMapCache.getValue().begin();
435     MapCacheFieldIt fcE = _fMapCache.getValue().end  ();
437     while(fcI != fcE)
438     {
439 #if 0
440         (*fcI).second->unlinkParent(this, 
441                                     Material::ParentsFieldId);
442 #endif
444         if(this->isMTLocal())
445         {
446             (*fcI).second->subReferenceRecorded();
447         }
448         else
449         {
450             (*fcI).second->subReferenceUnrecorded();
451         }
453         ++fcI;
454     }
456     _fMapCache.getValue().clear();
459 /*-------------------------------------------------------------------------*/
460 /*                             Assignment                                  */
462 /*-------------------------------------------------------------------------*/
463 /*                             Comparison                                  */
465 OSG_END_NAMESPACE