1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2008 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 \*---------------------------------------------------------------------------*/
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.
53 /*-------------------------------------------------------------------------*/
56 template <class AccessHandlerT, Int32 NamespaceI> inline
57 MemObjPointerMFieldCommon<AccessHandlerT,
58 NamespaceI >::MemObjPointerMFieldCommon(void) :
64 template <class AccessHandlerT, Int32 NamespaceI> inline
65 MemObjPointerMFieldCommon<AccessHandlerT,
66 NamespaceI >::MemObjPointerMFieldCommon(
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)
82 AccessHandler::onAdd(this, *sI);
89 template <class AccessHandlerT, Int32 NamespaceI> inline
90 MemObjPointerMFieldCommon<AccessHandlerT,
91 NamespaceI >::MemObjPointerMFieldCommon(
92 const UInt32 initSize) :
96 Self::ptrStoreResize(initSize, NULL);
99 /*-------------------------------------------------------------------------*/
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
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
123 /*-------------------------------------------------------------------------*/
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 /*-------------------------------------------------------------------------*/
159 template <class AccessHandlerT, Int32 NamespaceI> inline
160 void MemObjPointerMFieldCommon<AccessHandlerT,
161 NamespaceI >::ptrStoreAppend(
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 ();
179 InputIteratorT iI = first;
180 InputIteratorT iE = last;
182 for(; (iI != iE) && (sI != sE); ++iI, ++sI)
184 AccessHandler::onReplace(this, *sI, *iI);
191 for(; iI != iE; ++iI)
192 this->ptrStoreAppend(*iI);
196 this->ptrStoreErase(sI, sE);
200 template <class AccessHandlerT, Int32 NamespaceI> inline
201 typename MemObjPointerMFieldCommon<AccessHandlerT,
202 NamespaceI >::PtrStoreItType
203 MemObjPointerMFieldCommon<AccessHandlerT,
204 NamespaceI >::ptrStoreInsert(
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(
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,
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(
240 InputIteratorT first,
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(
261 AccessHandler::onReplace(this, *pos, pNewObj);
266 template <class AccessHandlerT, Int32 NamespaceI> inline
267 void MemObjPointerMFieldCommon<AccessHandlerT,
268 NamespaceI >::ptrStoreReplace(
269 const size_type index,
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);
336 /*-------------------------------------------------------------------------*/
339 template <class AccessHandlerT, Int32 NamespaceI> inline
340 void MemObjPointerMFieldCommon<AccessHandlerT,
341 NamespaceI >::ptrStoreResize(
342 const size_type newSize,
345 size_type oldSize = _ptrStore.size();
347 if(newSize > oldSize)
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);
357 else if(newSize < oldSize)
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);
369 /*-------------------------------------------------------------------------*/
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 /*-------------------------------------------------------------------------*/
402 template <class AccessHandlerT, Int32 NamespaceI> inline
403 void MemObjPointerMFieldCommon<AccessHandlerT,
404 NamespaceI >::copyFromBin(
405 BinaryDataHandler &pMem)
413 if(_ptrStore.size() == 0)
415 _ptrStore.resize(n, NULL);
417 MFieldTraits::copyFromBin( pMem,
421 PtrStoreConstItType sIt = _ptrStore.begin();
422 PtrStoreConstItType sEnd = _ptrStore.end ();
424 for(;sIt != sEnd; ++sIt)
425 AccessHandler::onSyncAdd(this, *sIt);
429 if(n > _ptrStore.size())
431 _ptrStore.resize(n, NULL);
434 PtrStoreItType sIt = _ptrStore.begin();
435 PtrStoreConstItType sEnd = _ptrStore.end ();
437 MemoryObject *tmpVal;
439 for(UInt32 i = 0; i < n; ++i)
441 MFieldTraits::copyFromBin(pMem,
444 AccessHandler::onSyncReplace(this, *sIt, tmpVal);
451 if(n < _ptrStore.size())
453 for(; sIt != sEnd; ++sIt)
455 AccessHandler::onSyncSub(this, *sIt);
464 Self::ptrStoreClear();
468 /*-------------------------------------------------------------------------*/
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
480 template <class AccessHandlerT, Int32 NamespaceI> inline
481 void MemObjPointerMFieldCommon<AccessHandlerT,
482 NamespaceI >::syncWith(
484 ConstFieldMaskArg syncMode,
486 AspectOffsetStore &oOffsets )
488 size_type n = source.size();
492 if(n > _ptrStore.size())
494 _ptrStore.resize(n, NULL);
497 PtrStoreConstItType sIt = source._ptrStore.begin();
498 PtrStoreConstItType sEnd = source._ptrStore.end ();
500 PtrStoreItType fIt = _ptrStore.begin();
503 for(; sIt != sEnd; ++sIt)
505 MemoryObject *pNewObj = *sIt;
507 if(pNewObj != NULL ||
508 0x0000 == (syncMode & Field::MFNullCheckSync) )
510 AccessHandler::onSyncReplace(this, *fIt, pNewObj);
519 if(n < _ptrStore.size())
521 PtrStoreConstItType fEnd = _ptrStore.end();
523 for(; fIt != fEnd; ++fIt)
524 AccessHandler::onSyncSub(this, *fIt);
531 this->ptrStoreClear();