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 \*---------------------------------------------------------------------------*/
41 /*-------------------------------------------------------------------------*/
44 template <class ValueT, Int32 iNamespace, class AllocT> inline
45 MField<ValueT, iNamespace, AllocT>::MField(void) :
48 #ifdef OSG_MT_CPTR_ASPECT
54 template <class ValueT, Int32 iNamespace, class AllocT> inline
55 MField<ValueT, iNamespace, AllocT>::MField(const MField &obj) :
58 #ifdef OSG_MT_CPTR_ASPECT
64 template <class ValueT, Int32 iNamespace, class AllocT> inline
65 MField<ValueT, iNamespace, AllocT>::MField(const UInt32 initSize) :
68 #ifdef OSG_MT_CPTR_ASPECT
72 _values.resize(initSize);
75 /*-------------------------------------------------------------------------*/
78 template <class ValueT, Int32 iNamespace, class AllocT> inline
79 MField<ValueT, iNamespace, AllocT>::~MField(void)
83 /*-------------------------------------------------------------------------*/
86 /*! Return a reference to the value store
89 template <class ValueT, Int32 iNamespace, class AllocT> inline
90 typename MField<ValueT, iNamespace, AllocT>::StorageType &
91 MField<ValueT, iNamespace, AllocT>::getValues(void)
96 /*! Return a const reference to the value store
99 template <class ValueT, Int32 iNamespace, class AllocT> inline
100 const typename MField<ValueT, iNamespace, AllocT>::StorageType &
101 MField<ValueT, iNamespace, AllocT>::getValues(void) const
106 /*-------------------------------------------------------------------------*/
109 template <class ValueT, Int32 iNamespace, class AllocT> inline
110 void MField<ValueT, iNamespace, AllocT>::setValues(const StorageType &value)
115 template <class ValueT, Int32 iNamespace, class AllocT> inline
116 void MField<ValueT, iNamespace, AllocT>::setValues(
117 const StorageTypeParent &value)
119 // Hack probably move it to MFieldVector (GV)
121 *(static_cast<StorageTypeParent *>(&_values)) = value;
124 template <class ValueT, Int32 iNamespace, class AllocT> inline
125 void MField<ValueT, iNamespace, AllocT>::setValues(const Self &obj)
127 _values = obj._values;
130 template <class ValueT, Int32 iNamespace, class AllocT> inline
131 void MField<ValueT, iNamespace, AllocT>::addValueFromCString(const Char8 *str)
135 typedef typename boost::mpl::if_<boost::mpl::bool_<
136 static_cast<bool>(MFieldTraits ::Convertible &
137 FieldTraitsBase ::FromStringConvertible)>,
139 StringConversionError<ValueT,
140 iNamespace> >::type Converter;
142 Converter::getFromCString(tmpVal, str);
148 template <class ValueT, Int32 iNamespace, class AllocT> inline
151 AllocT >::pushValuesToString(std::string &str) const
153 typedef typename boost::mpl::if_<boost::mpl::bool_<
154 static_cast<bool>(MFieldTraits ::Convertible &
155 FieldTraitsBase ::ToStringConvertible)>,
157 StringConversionError<ValueT,
158 iNamespace> >::type Converter;
160 for(SizeT i = 0; i < size(); ++i)
162 Converter::putToString(_values[i], str);
172 template <class ValueT, Int32 iNamespace, class AllocT> inline
175 AllocT >::pushValuesFromStream(std::istream &str)
179 typedef typename boost::mpl::if_<boost::mpl::bool_<
180 static_cast<bool>(MFieldTraits ::Convertible &
181 FieldTraitsBase ::FromStreamConvertible)>,
183 StreamConversionError<ValueT,
184 iNamespace> >::type Converter;
186 Converter::getFromStream(tmpVal, str);
191 template <class ValueT, Int32 iNamespace, class AllocT> inline
194 AllocT >::pushIndexedValueFromCString(Char8 const *str,
199 typedef typename boost::mpl::if_<boost::mpl::bool_<
200 static_cast<bool>(MFieldTraits ::Convertible &
201 FieldTraitsBase ::FromStringConvertible)>,
203 StringConversionError<ValueT,
204 iNamespace> >::type Converter;
206 Converter::getFromCString(tmpVal, str);
208 _values[index] = tmpVal;
211 template <class ValueT, Int32 iNamespace, class AllocT> inline
214 AllocT >::pushIndexedValueToStream(OutStream &str,
217 typedef typename boost::mpl::if_<boost::mpl::bool_<
218 static_cast<bool>(MFieldTraits ::Convertible &
219 FieldTraitsBase ::ToStreamConvertible)>,
221 StreamConversionError<ValueT,
222 iNamespace> >::type Converter;
224 Converter::putToStream(_values[index], str);
227 template <class ValueT, Int32 iNamespace, class AllocT> inline
230 AllocT >::pushValuesToStream(OutStream &str) const
232 typedef typename boost::mpl::if_<boost::mpl::bool_<
233 static_cast<bool>(MFieldTraits ::Convertible &
234 FieldTraitsBase ::ToStreamConvertible)>,
236 StreamConversionError<ValueT,
237 iNamespace> >::type Converter;
239 for(SizeT i = 0; i < size(); ++i)
241 Converter::putToStream(_values[i], str);
251 template <class ValueT, Int32 iNamespace, class AllocT> inline
252 void MField<ValueT, iNamespace, AllocT>::pushSizeToStream(OutStream &str) const
254 str << _values.size();
257 /*-------------------------------------------------------------------------*/
258 /* Binary Interface */
260 template <class ValueTypeT, Int32 iNameSpace, class AllocT> inline
261 SizeT MField<ValueTypeT, iNameSpace, AllocT>::getBinSize(void) const
264 sizeof(UInt32) + // num elements
266 MFieldTraits::getBinSize(&(_values[0]), _values.size()) : 0);
269 template <class ValueTypeT, Int32 iNameSpace, class AllocT> inline
270 void MField<ValueTypeT,
272 AllocT>::copyToBin(BinaryDataHandler &pMem) const
274 UInt32 n = UInt32(_values.size());
280 MFieldTraits::copyToBin( pMem,
286 template <class ValueTypeT, Int32 iNameSpace, class AllocT> inline
287 void MField<ValueTypeT,
289 AllocT >::copyFromBin(BinaryDataHandler &pMem)
299 _values.resize(n, tmpVal);
306 MFieldTraits::copyFromBin( pMem,
312 /*-------------------------------------------------------------------------*/
315 template <class ValueT, Int32 iNamespace, class AllocT> inline
316 typename MField<ValueT,
318 AllocT >::iterator MField<ValueT,
320 AllocT >::begin (void)
322 return _values.begin();
325 template <class ValueT, Int32 iNamespace, class AllocT> inline
326 typename MField<ValueT,
328 AllocT >::iterator MField<ValueT,
332 return _values.end();
335 template <class ValueT, Int32 iNamespace, class AllocT> inline
336 typename MField<ValueT,
338 AllocT >::reverse_iterator
341 AllocT >::rbegin(void)
343 return _values.rbegin();
346 template <class ValueT, Int32 iNamespace, class AllocT> inline
347 typename MField<ValueT,
349 AllocT >::reverse_iterator
354 return _values.rend();
357 template <class ValueT, Int32 iNamespace, class AllocT> inline
358 typename MField<ValueT,
360 AllocT >::const_iterator MField<ValueT,
365 return _values.begin();
368 template <class ValueT, Int32 iNamespace, class AllocT> inline
369 typename MField<ValueT,
371 AllocT >::const_iterator MField<ValueT,
373 AllocT>::end(void) const
375 return _values.end();
378 template <class ValueT, Int32 iNamespace, class AllocT> inline
379 typename MField<ValueT,
381 AllocT >::const_reverse_iterator
384 AllocT>::rbegin(void) const
386 return _values.rbegin();
389 template <class ValueT, Int32 iNamespace, class AllocT> inline
390 typename MField<ValueT,
392 AllocT >::const_reverse_iterator
395 AllocT >::rend(void) const
397 return _values.rend();
400 template <class ValueT, Int32 iNamespace, class AllocT> inline
401 typename MField<ValueT,
403 AllocT >::reference MField<ValueT,
405 AllocT >::front(void)
407 return _values.front();
410 template <class ValueT, Int32 iNamespace, class AllocT> inline
411 typename MField<ValueT,
413 AllocT >::const_reference MField<ValueT,
418 return _values.front();
421 template <class ValueT, Int32 iNamespace, class AllocT> inline
422 typename MField<ValueT,
424 AllocT >::reference MField<ValueT,
428 return _values.back();
431 template <class ValueT, Int32 iNamespace, class AllocT> inline
432 typename MField<ValueT,
434 AllocT >::const_reference MField<ValueT,
439 return _values.back();
442 template <class ValueT, Int32 iNamespace, class AllocT> inline
443 void MField<ValueT, iNamespace, AllocT>::clear(void)
448 template <class ValueT, Int32 iNamespace, class AllocT> inline
449 typename MField<ValueT,
451 AllocT >::iterator MField<ValueT,
457 iterator posItor = _values.begin();
461 return _values.insert(posItor, value);
464 template <class ValueT, Int32 iNamespace, class AllocT> inline
465 typename MField<ValueT,
467 AllocT >::iterator MField<ValueT,
470 iterator pos, ArgumentType value)
472 return _values.insert(pos, value);
475 template <class ValueT, Int32 iNamespace, class AllocT> inline
476 void MField<ValueT, iNamespace, AllocT>::insert(iterator pos,
480 _values.insert(pos, n, value);
483 template <class ValueT, Int32 iNamespace, class AllocT>
484 template <class InputIterator> inline
485 void MField<ValueT, iNamespace, AllocT>::insert(iterator pos,
489 _values.insert(pos, first, last);
492 template <class ValueT, Int32 iNamespace, class AllocT> inline
493 typename MField<ValueT,
495 AllocT >::iterator MField<ValueT,
497 AllocT >::erase(iterator pos)
499 return _values.erase(pos);
502 template <class ValueT, Int32 iNamespace, class AllocT> inline
505 AllocT >::erase(size_type index)
507 iterator pos = _values.begin();
514 template <class ValueT, Int32 iNamespace, class AllocT> inline
515 typename MField<ValueT,
517 AllocT >::iterator MField<ValueT,
519 AllocT >::erase(iterator first,
522 return _values.erase(first, last);
526 template <class ValueT, Int32 iNamespace, class AllocT> inline
527 typename MField<ValueT,
529 AllocT >::iterator MField<ValueT,
534 return std::find(_values.begin(), _values.end(), value);
537 template <class ValueT, Int32 iNamespace, class AllocT> inline
538 typename MField<ValueT,
540 AllocT >::const_iterator MField<ValueT,
543 ArgumentType value) const
545 return std::find(_values.begin(), _values.end(), value);
548 template <class ValueT, Int32 iNamespace, class AllocT> inline
549 Int32 MField<ValueT, iNamespace, AllocT>::findIndex(ArgumentType value) const
551 const_iterator it = std::find(_values.begin(), _values.end(), value);
553 if(it != _values.end())
555 return it - _values.begin();
563 template <class ValueT, Int32 iNamespace, class AllocT> inline
564 void MField<ValueT, iNamespace, AllocT>::push_back(ArgumentType value)
566 _values.push_back(value);
569 template <class ValueT, Int32 iNamespace, class AllocT> inline
570 void MField<ValueT, iNamespace, AllocT>::resize(size_type newsize,
573 _values.resize(newsize, t);
576 template <class ValueT, Int32 iNamespace, class AllocT> inline
577 void MField<ValueT, iNamespace, AllocT>::reserve(size_type newsize)
579 _values.reserve(newsize);
582 template <class ValueT, Int32 iNamespace, class AllocT> inline
583 typename MField<ValueT, iNamespace, AllocT>::size_type
584 MField<ValueT, iNamespace, AllocT>::size(void) const
586 return _values.size();
589 template <class ValueT, Int32 iNamespace, class AllocT> inline
590 UInt32 MField<ValueT, iNamespace, AllocT>::size32(void) const
592 return UInt32(_values.size());
595 template <class ValueT, Int32 iNamespace, class AllocT> inline
596 typename MField<ValueT, iNamespace, AllocT>::size_type
597 MField<ValueT, iNamespace, AllocT>::capacity(void) const
599 return _values.capacity();
602 template <class ValueT, Int32 iNamespace, class AllocT> inline
603 bool MField<ValueT, iNamespace, AllocT>::empty(void) const
605 return _values.empty();
608 template <class FieldTypeT, Int32 fieldNameSpace, class AllocT> inline
609 void MField<FieldTypeT, fieldNameSpace, AllocT>::swap(MField &right)
611 _values.swap(right._values);
614 template <class FieldTypeT, Int32 fieldNameSpace, class AllocT> inline
615 void MField<FieldTypeT, fieldNameSpace, AllocT>::replace(
616 size_type index, ArgumentType value)
618 this->replace(this->begin() + index, value);
621 template <class FieldTypeT, Int32 fieldNameSpace, class AllocT> inline
622 void MField<FieldTypeT, fieldNameSpace, AllocT>::replace(
623 iterator pos, ArgumentType value)
629 template <class FieldTypeT, Int32 fieldNameSpace, class AllocT> inline
630 void MField<FieldTypeT, fieldNameSpace, AllocT>::addValue (ArgumentType value)
632 _values.push_back(value);
637 /*-------------------------------------------------------------------------*/
640 template <class ValueT, Int32 iNamespace, class AllocT> inline
641 typename MField<ValueT, iNamespace, AllocT>::reference
642 MField<ValueT, iNamespace, AllocT>::operator [](size_type index)
644 return _values[index];
647 template <class ValueT, Int32 iNamespace, class AllocT> inline
648 typename MField<ValueT, iNamespace, AllocT>::const_reference
649 MField<ValueT, iNamespace, AllocT>::operator [](size_type index) const
651 return _values[index];
654 template <class ValueT, Int32 iNamespace, class AllocT> inline
657 AllocT >::operator ==(const MField &source) const
659 return _values == source._values;
662 template <class ValueT, Int32 iNamespace, class AllocT> inline
665 AllocT > &MField<ValueT,
667 AllocT >::operator =(const MField &source)
672 _values = source._values;
677 /*-------------------------------------------------------------------------*/
680 #ifdef OSG_MT_CPTR_ASPECT
681 template <class ValueT, Int32 iNamespace, class AllocT> inline
684 AllocT >::syncWith(Self &source,
685 ConstFieldMaskArg syncMode,
687 AspectOffsetStore &oOffsets )
689 if(syncMode != 0x0000)
695 UInt32 uiFromAspect = (uiSyncInfo & 0xFF000000) >> 24;
696 UInt32 uiToAspect = (uiSyncInfo & 0x00FF0000) >> 16;
698 bool bTargetDelete = true;
700 if(_uiSharedWith != 0x0000)
702 bTargetDelete = false;
704 resolveShare(uiToAspect, oOffsets);
707 Char8 *pOtherMem = reinterpret_cast<Char8 *>(this);
709 UInt32 uiShared = source._uiSharedWith;
712 // pOtherMem -= uiToAspect * uiCopyOffset;
714 for(UInt32 i = 0; i < oOffsets.size(); ++i)
716 if(oOffsets[i] == -1)
722 if(0x0000 != (uiShared & uiCheck))
725 reinterpret_cast<Self *>(
726 pOtherMem + (oOffsets[i]));
728 pOther->_uiSharedWith |= (1 << uiToAspect);
730 uiShared &= ~uiCheck;
733 if(0x0000 == uiShared)
741 _uiSharedWith |= source._uiSharedWith;
742 _uiSharedWith |= (1 << uiFromAspect);
744 source._uiSharedWith |= (1 << uiToAspect );
746 _values.shareValues(source._values, bTargetDelete);
750 template <class ValueT, Int32 iNamespace, class AllocT> inline
751 void MField<ValueT, iNamespace, AllocT>::beginEdit(UInt32 uiAspect,
752 AspectOffsetStore &oOffsets)
754 if(_uiSharedWith != 0x0000)
756 Self *pOther = resolveShare(uiAspect, oOffsets);
758 _values.resolveShare();
765 template <class ValueT, Int32 iNamespace, class AllocT> inline
766 typename MField<ValueT,
771 AllocT >::resolveShare(UInt32 uiAspect,
772 AspectOffsetStore &oOffsets)
774 Char8 *pOtherMem = reinterpret_cast<Char8 *>(this);
777 UInt32 uiShared = _uiSharedWith;
779 UInt32 uiOwn = 1 << uiAspect;
781 for(UInt32 i = 0; i < oOffsets.size(); ++i)
783 if(oOffsets[i] == -1)
789 if(0x0000 != (uiShared & uiCheck))
792 reinterpret_cast<Self *>(pOtherMem + oOffsets[i]);
794 pOther->_uiSharedWith &= ~uiOwn;
795 _uiSharedWith &= ~uiCheck;
797 uiShared &= ~uiCheck;
800 if(0x0000 == uiShared)
811 template <class ValueT, Int32 iNamespace, class AllocT> inline
814 AllocT >::terminateShare(UInt32 uiAspect,
815 AspectOffsetStore &oOffsets)
817 if(_uiSharedWith != 0x0000)
819 /*Self *pOther = */ resolveShare(uiAspect, oOffsets);
821 _values.resolveShare();
825 template <class ValueT, Int32 iNamespace, class AllocT> inline
826 bool MField<ValueT, iNamespace, AllocT>::isShared(void)
828 return _uiSharedWith != 0x0000;
834 template <class ValueT, Int32 iNamespace, class AllocT> inline
835 void MField<ValueT, iNamespace, AllocT>::dump( UInt32 uiIndent,
836 const BitVector bvFlags ) const
838 for(UInt32 i = 0; i < uiIndent; ++i)
839 fprintf(stderr, " ");
841 fprintf(stderr, "Share with : %08x\n", _uiSharedWith);
843 _values.dump(uiIndent, bvFlags);