changed: gcc8 base update
[opensg.git] / Source / Base / FieldContainer / Fields / MemObjPointerFields / OSGMemObjPointerMFieldCommon.inl
blob51bc9dc215de81c8bfd7e4c888d83fef0727938f
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *           Copyright (C) 2008 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 /*! \class MemObjPointerMFieldCommon
43     Common interface implementation for pointer MFields. This class exists
44     only to facilitate code reuse; to actually use the provided ptrStore
45     interface use the derived classes ( \c ChildPointerMField,
46     \c ParentPointerMField and \c PointerMField ).
47     All non-const functions of this class call static member functions of the
48     \c AccessHandlerT template parameter (onAdd, onSub, onReplace), whenever
49     values are added, removed or replaced respectively. This is used to
50     implement reference counting and child/parent linking.
51  */
53 /*-------------------------------------------------------------------------*/
54 /* Constructors                                                            */
56 template <class AccessHandlerT, Int32 NamespaceI> inline
57 MemObjPointerMFieldCommon<AccessHandlerT,
58                           NamespaceI    >::MemObjPointerMFieldCommon(void) : 
60     Inherited()
64 template <class AccessHandlerT, Int32 NamespaceI> inline
65 MemObjPointerMFieldCommon<AccessHandlerT,
66                           NamespaceI     >::MemObjPointerMFieldCommon(
67                               const Self &source) :
69     Inherited()
71     if(source.size() > 0)
72     {
73         _ptrStore.resize(source.size(), NULL);
75         PtrStoreItType      sI = _ptrStore.begin();
76         PtrStoreConstItType sE = _ptrStore.end  ();
78         PtrStoreConstItType sSE = source._ptrStore.begin();
80         for(; sI != sE; ++sI, ++sSE)
81         {
82             AccessHandler::onAdd(this, *sI);
84             *sI = *sSE;
85         }
86     }
89 template <class AccessHandlerT, Int32 NamespaceI> inline
90 MemObjPointerMFieldCommon<AccessHandlerT,
91                           NamespaceI    >::MemObjPointerMFieldCommon(
92                               const UInt32 initSize) :
94     Inherited()
96     Self::ptrStoreResize(initSize, NULL);
99 /*-------------------------------------------------------------------------*/
100 /* Destructor                                                              */
102 template <class AccessHandlerT, Int32 NamespaceI> inline
103 MemObjPointerMFieldCommon<AccessHandlerT,
104                           NamespaceI    >::~MemObjPointerMFieldCommon(void)
106     Self::ptrStoreClear();
109 /*-------------------------------------------------------------------------*/
110 /* Store Interface                                                         */
112 /*! This interface fully handles reference counting and child/parent linking,
113     but uses FieldContainerPtr instead of the real pointer type stored in the
114     field.
116     The biggest caveat with this interface is, when you attempt to use it
117     with ParentPointerMField, as that will only give you the pointers,
118     \em NOT the child field ids as well. To access those, use the
119     functions of ParentPointerMFieldBase, that have a "idStore" instead of
120     a "ptrStore" prefix.
121  */
123 /*-------------------------------------------------------------------------*/
124 /* Reading Values                                                          */
126 template <class AccessHandlerT, Int32 NamespaceI> inline 
127 typename MemObjPointerMFieldCommon<AccessHandlerT,
128                                    NamespaceI    >::value_type
129     MemObjPointerMFieldCommon<AccessHandlerT,
130                               NamespaceI    >::ptrStoreGet(
131                                   const size_type index) const
133     return AccessHandler::validate(_ptrStore[index]);
136 template <class AccessHandlerT, Int32 NamespaceI> inline 
137 typename MemObjPointerMFieldCommon<AccessHandlerT,
138                              NamespaceI    >::value_type
139     MemObjPointerMFieldCommon<AccessHandlerT,
140                               NamespaceI    >::ptrStoreGet(
141                                   PtrStoreItType pos) const
143     return AccessHandler::validate(*pos);
146 template <class AccessHandlerT, Int32 NamespaceI> inline
147 typename MemObjPointerMFieldCommon<AccessHandlerT,
148                                    NamespaceI    >::value_type
149     MemObjPointerMFieldCommon<AccessHandlerT,
150                               NamespaceI    >::ptrStoreGet(
151                                   PtrStoreConstItType pos) const
153     return AccessHandler::validate(*pos);
156 /*-------------------------------------------------------------------------*/
157 /* Adding Values                                                           */
159 template <class AccessHandlerT, Int32 NamespaceI> inline 
160 void MemObjPointerMFieldCommon<AccessHandlerT,
161                                NamespaceI    >::ptrStoreAppend(
162                                    const_value pNewObj)
164     AccessHandler::onAdd(this, pNewObj);
166     _ptrStore.push_back(pNewObj);
169 template <class AccessHandlerT, Int32 NamespaceI>
170 template <class InputIteratorT                  > inline 
171 void MemObjPointerMFieldCommon<AccessHandlerT,
172                                NamespaceI     >::ptrStoreAssign(
173                                    InputIteratorT first,
174                                    InputIteratorT last )
176     PtrStoreItType sI = _ptrStore.begin();
177     PtrStoreItType sE = _ptrStore.end  ();
178     
179     InputIteratorT iI = first;
180     InputIteratorT iE = last;
181     
182     for(; (iI != iE) && (sI != sE); ++iI, ++sI)
183     {
184         AccessHandler::onReplace(this, *sI, *iI);
186         *sI = *iI;
187     }
188     
189     if(iI != iE)
190     {
191         for(; iI != iE; ++iI)
192             this->ptrStoreAppend(*iI);
193     }
194     else if(sI != sE)
195     {
196         this->ptrStoreErase(sI, sE);
197     }
200 template <class AccessHandlerT, Int32 NamespaceI> inline 
201 typename MemObjPointerMFieldCommon<AccessHandlerT,
202                                    NamespaceI     >::PtrStoreItType
203      MemObjPointerMFieldCommon<AccessHandlerT,
204                                NamespaceI     >::ptrStoreInsert(
205                                    PtrStoreItType pos, 
206                                    const_value    pNewObj)
208     AccessHandler::onAdd(this, pNewObj);
210     return _ptrStore.insert(pos, pNewObj);
213 template <class AccessHandlerT, Int32 NamespaceI> inline
214 void MemObjPointerMFieldCommon<AccessHandlerT,
215                                NamespaceI     >::ptrStoreInsert(
216                                    PtrStoreItType pos,
217                                    size_type      n,
218                                    const_value    pNewObj)
220     for(size_type i = 0; i < n; ++i)
221         AccessHandler::onAdd(this, pNewObj);
223     _ptrStore.insert(pos, n, pNewObj);
226 template <class AccessHandlerT, Int32 NamespaceI> inline 
227 void MemObjPointerMFieldCommon<AccessHandlerT,
228                                NamespaceI     >::ptrStoreInsert(
229                                    const size_type   index, 
230                                          const_value pNewObj)
232     this->ptrStoreInsert(_ptrStore.begin() + index, pNewObj);
235 template <class AccessHandlerT, Int32 NamespaceI>
236 template <class InputIteratorT                  > inline 
237 void MemObjPointerMFieldCommon<AccessHandlerT,
238                                NamespaceI    >::ptrStoreInsert(
239                                    PtrStoreItType pos, 
240                                    InputIteratorT first, 
241                                    InputIteratorT last)
243     InputIteratorT iI = first;
244     InputIteratorT iE = last;
246     for(; iI != iE; ++iI)
247         AccessHandler::onAdd(this, *iI);
249     _ptrStore.insert(pos, first.base(), last.base());
252 /*-------------------------------------------------------------------------*/
253 /* Changing Values                                                         */
255 template <class AccessHandlerT, Int32 NamespaceI> inline 
256 void MemObjPointerMFieldCommon<AccessHandlerT,
257                                NamespaceI    >::ptrStoreReplace(
258                                    PtrStoreItType pos, 
259                                    const_value    pNewObj)
261     AccessHandler::onReplace(this, *pos, pNewObj);
263     *pos = pNewObj;
266 template <class AccessHandlerT, Int32 NamespaceI> inline 
267 void MemObjPointerMFieldCommon<AccessHandlerT,
268                                NamespaceI    >::ptrStoreReplace(
269                                    const size_type   index, 
270                                          const_value pNewObj)
272     this->ptrStoreReplace(_ptrStore.begin() + index, pNewObj);
275 /*-------------------------------------------------------------------------*/
276 /* Removing Values                                                         */
279 template <class AccessHandlerT, Int32 NamespaceI> inline 
280 typename MemObjPointerMFieldCommon<AccessHandlerT,
281                                    NamespaceI    >::PtrStoreItType
282     MemObjPointerMFieldCommon<AccessHandlerT,
283                               NamespaceI    >::ptrStoreErase(PtrStoreItType pos)
285     AccessHandler::onSub(this, *pos);
287     return _ptrStore.erase(pos);
291 template <class AccessHandlerT, Int32 NamespaceI> inline 
292 typename MemObjPointerMFieldCommon<AccessHandlerT,
293                                    NamespaceI    >::PtrStoreItType
294     MemObjPointerMFieldCommon<AccessHandlerT,
295                               NamespaceI    >::ptrStoreErase(
296                                   PtrStoreItType beginIt, 
297                                   PtrStoreItType endIt  )
299     for(PtrStoreConstItType sI = beginIt; sI != endIt; ++sI)
300         AccessHandler::onSub(this, *sI);
302     return _ptrStore.erase(beginIt, endIt);
305 template <class AccessHandlerT, Int32 NamespaceI> inline 
306 void MemObjPointerMFieldCommon<AccessHandlerT,
307                                NamespaceI     >::ptrStoreErase(
308                                    const size_type index)
310     this->ptrStoreErase(_ptrStore.begin() + index);
313 template <class AccessHandlerT, Int32 NamespaceI> inline 
314 void MemObjPointerMFieldCommon<AccessHandlerT,
315                                NamespaceI    >::ptrStoreErase(
316                                    const size_type beginIndex, 
317                                    const size_type endIndex  )
319     this->ptrStoreErase(_ptrStore.begin() + beginIndex,
320                         _ptrStore.begin() + endIndex   );
323 template <class AccessHandlerT, Int32 NamespaceI> inline 
324 void MemObjPointerMFieldCommon<AccessHandlerT,
325                                NamespaceI     >::ptrStoreClear(void)
327     PtrStoreConstItType sI = _ptrStore.begin();
328     PtrStoreConstItType sE = _ptrStore.end  ();
330     for(; sI != sE; ++sI)
331         AccessHandler::onSub(this, *sI);
333     _ptrStore.clear();
336 /*-------------------------------------------------------------------------*/
337 /* Resizing                                                                */
339 template <class AccessHandlerT, Int32 NamespaceI> inline
340 void MemObjPointerMFieldCommon<AccessHandlerT,
341                                NamespaceI    >::ptrStoreResize(
342                                    const size_type   newSize, 
343                                          const_value pNewObj)
345     size_type oldSize = _ptrStore.size();
347     if(newSize > oldSize)
348     {
349         _ptrStore.resize(newSize, pNewObj);
351         PtrStoreConstItType sI = _ptrStore.begin() + oldSize;
352         PtrStoreConstItType sE = _ptrStore.end  ();
354         for(; sI != sE; ++sI)
355             AccessHandler::onAdd(this, *sI);
356     }
357     else if(newSize < oldSize)
358     {
359         PtrStoreConstItType sI = _ptrStore.begin() + newSize;
360         PtrStoreConstItType sE = _ptrStore.end  ();
362         for(; sI != sE; ++sI)
363             AccessHandler::onSub(this, *sI);
365         _ptrStore.resize(newSize);
366     }
369 /*-------------------------------------------------------------------------*/
370 /* Finding Values                                                          */
373 template <class AccessHandlerT, Int32 NamespaceI> inline 
374 typename MemObjPointerMFieldCommon<AccessHandlerT,
375                                    NamespaceI    >::PtrStoreItType
376     MemObjPointerMFieldCommon<AccessHandlerT,
377                               NamespaceI    >::ptrStoreFind(const_value pObj)
379     return std::find(_ptrStore.begin(),
380                      _ptrStore.end  (), pObj);
383 template <class AccessHandlerT, Int32 NamespaceI> inline 
384 typename MemObjPointerMFieldCommon<AccessHandlerT,
385                                    NamespaceI    >::PtrStoreConstItType
386     MemObjPointerMFieldCommon<AccessHandlerT,
387                               NamespaceI    >::ptrStoreFind(
388                                   const_value pObj) const
390     return std::find(_ptrStore.begin(),
391                      _ptrStore.end  (), pObj);
394 /*-------------------------------------------------------------------------*/
395 /* Std library interface                                                   */
398 /*-------------------------------------------------------------------------*/
399 /* Binary IO                                                               */
402 template <class AccessHandlerT, Int32 NamespaceI> inline
403 void MemObjPointerMFieldCommon<AccessHandlerT,
404                                NamespaceI    >::copyFromBin(
405                                    BinaryDataHandler &pMem)
407     UInt32 n;
409     pMem.getValue(n);
411     if(n != 0)
412     {
413         if(_ptrStore.size() == 0)
414         {
415             _ptrStore.resize(n, NULL);
417             MFieldTraits::copyFromBin(   pMem, 
418                                       &(_ptrStore[0]),
419                                          n);
420             
421             PtrStoreConstItType sIt  = _ptrStore.begin();
422             PtrStoreConstItType sEnd = _ptrStore.end  ();
423             
424             for(;sIt != sEnd; ++sIt)
425                 AccessHandler::onSyncAdd(this, *sIt);
426         }
427         else
428         {
429             if(n > _ptrStore.size())
430             {
431                 _ptrStore.resize(n, NULL);
432             }
433             
434             PtrStoreItType      sIt  = _ptrStore.begin();
435             PtrStoreConstItType sEnd = _ptrStore.end  ();
437             MemoryObject *tmpVal;
439             for(UInt32 i = 0; i < n; ++i)
440             {
441                 MFieldTraits::copyFromBin(pMem, 
442                                           tmpVal);
444                 AccessHandler::onSyncReplace(this, *sIt, tmpVal);
446                 *sIt = tmpVal;
447                 
448                 ++sIt;
449             }
451             if(n < _ptrStore.size())
452             {
453                 for(; sIt != sEnd; ++sIt)
454                 {
455                     AccessHandler::onSyncSub(this, *sIt);
456                 };
458                 _ptrStore.resize(n);
459             }
460         }
461     }
462     else
463     {
464         Self::ptrStoreClear();
465     }
468 /*-------------------------------------------------------------------------*/
469 /* MT Sync                                                                 */
471 /*! Syncronizes this field with a different Aspect.
473     Two things need to be carefully handled here: First, depending on whether
474     the size increases or decreases some values are replaced, added or removed
475     and their reference count has to be adjusted accordingly.
476     Second, the onSync{Add,Sub,Replace} functions of the AccessHandler have to
477     be called.
478  */
480 template <class AccessHandlerT, Int32 NamespaceI> inline
481 void MemObjPointerMFieldCommon<AccessHandlerT,
482                                NamespaceI    >::syncWith(
483                                    Self              &source,     
484                                    ConstFieldMaskArg  syncMode,
485                                    UInt32             uiSyncInfo, 
486                                    AspectOffsetStore &oOffsets )
488     size_type n = source.size();
490     if(n != 0)
491     {
492         if(n > _ptrStore.size())
493         {
494             _ptrStore.resize(n, NULL);
495         }
497         PtrStoreConstItType sIt     = source._ptrStore.begin();
498         PtrStoreConstItType sEnd    = source._ptrStore.end  ();
500         PtrStoreItType      fIt     =        _ptrStore.begin();
501         
502         n = 0;
503         for(; sIt != sEnd; ++sIt)
504         {
505             MemoryObject *pNewObj = *sIt;
506             
507             if(pNewObj != NULL                                ||
508                0x0000  == (syncMode & Field::MFNullCheckSync)  )
509             {
510                 AccessHandler::onSyncReplace(this, *fIt, pNewObj);
511             
512                 *fIt = pNewObj;
514                 ++fIt;
515                 ++n;
516             }
517         }
518         
519         if(n < _ptrStore.size())
520         {
521             PtrStoreConstItType fEnd  = _ptrStore.end();
522             
523             for(; fIt != fEnd; ++fIt)
524                 AccessHandler::onSyncSub(this, *fIt);
526             _ptrStore.resize(n);
527         }
528     }
529     else
530     {
531         this->ptrStoreClear();
532     }
535 OSG_END_NAMESPACE