added: travMask to csm viewport
[opensg.git] / Source / Base / Field / OSGFieldTraits.h
blob043be9034926ecf498d8e08b646039a1adcf927f
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 #ifndef _OSGFIELDTRAITS_H_
40 #define _OSGFIELDTRAITS_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 #include "OSGFieldForwards.h"
46 #include "OSGLog.h"
47 #include "OSGContainerForwards.h"
48 #include "OSGFieldType.h"
49 #include "OSGPointerType.h"
51 #include <iosfwd>
53 #include <boost/mpl/if.hpp>
55 OSG_BEGIN_NAMESPACE
57 class FieldType;
58 class BasicFieldConnector;
60 template<class FieldTypeT>
61 class FieldConnector;
63 /*! \ingroup GrpBaseFieldTraits
64 \ingroup GrpLibOSGBase
67 struct FieldTraitsBase
69 enum
71 NotConvertible = 0x00,
73 ToStringConvertible = 0x01,
74 FromStringConvertible = 0x02,
76 ToStreamConvertible = 0x10,
77 FromStreamConvertible = 0x20
80 typedef void ParentType;
82 static const Char8 *getSPName(void) { return "Field"; }
83 static const Char8 *getMPName(void) { return "Field"; }
86 enum VecSize
88 Vec1,
89 Vec2,
90 Vec3,
91 Vec4
95 template<class ValueT, Int32 iNamespace = 0>
96 struct FieldTraits;
98 /*! \ingroup GrpBaseFieldTraits
99 \ingroup GrpLibOSGBase
100 \nohierarchy
103 struct InvalidTrait
107 /*! \ingroup GrpBaseFieldTraits
108 \ingroup GrpLibOSGBase
111 template<class ValueT, Int32 NamespaceI = 0>
112 struct FieldTraitsTemplateBase : public FieldTraitsBase
114 #ifndef __hpux
115 static const UInt32 uiTest = TypeTraits<ValueT>::IsPOD == true;
117 typedef typename
118 boost::mpl::if_<boost::mpl::bool_<(uiTest == 1)>,
119 const ValueT ,
120 const ValueT & >::type ArgumentType;
121 #else
122 typedef typename
123 boost::mpl::if_<boost::mpl::bool_<TypeTraits<ValueT>::IsPOD>,
124 const ValueT ,
125 const ValueT & >::type ArgumentType;
126 #endif
129 typedef ValueT ValueType;
131 typedef OSG_STL_DEFAULT_ALLOCATOR(ValueT) MFAlloc;
134 static const Int32 iNamespace = NamespaceI;
135 static const bool bIsPointerField = false;
138 /*! \ingroup GrpBaseFieldTraits
139 \ingroup GrpLibOSGBase
142 template<class ValueT, Int32 iNamespace = 0>
143 struct FieldTraitsPODTemplateBase : public FieldTraitsTemplateBase<ValueT,
144 iNamespace>
146 static bool getFromCString( ValueT &outVal,
147 const Char8 *&inVal )
149 outVal = TypeTraits<ValueT>::getFromCString(inVal);
151 return true;
154 static void putToString(const ValueT &inVal,
155 std::string &outVal)
157 TypeTraits<ValueT>::putToString(inVal, outVal);
160 static void putToStream(const ValueT &val,
161 OutStream &str)
163 TypeTraits<ValueT>::putToStream(val, str);
166 // Binary
168 static SizeT getBinSize(const ValueT &)
170 return sizeof(ValueT);
173 static SizeT getBinSize(const ValueT *,
174 SizeT uiNumObjects)
176 return sizeof(ValueT) * uiNumObjects;
179 static void copyToBin( BinaryDataHandler &pMem,
180 const ValueT &oObject)
182 pMem.putValue(oObject);
185 static void copyToBin( BinaryDataHandler &pMem,
186 const ValueT *pObjectStore,
187 SizeT uiNumObjects)
189 pMem.putValues(&(pObjectStore[0]), uiNumObjects);
192 static void copyFromBin(BinaryDataHandler &pMem,
193 ValueT &oObject)
195 pMem.getValue(oObject);
198 static void copyFromBin(BinaryDataHandler &pMem,
199 ValueT *pObjectStore,
200 SizeT uiNumObjects)
202 pMem.getValues(&(pObjectStore[0]), uiNumObjects);
206 /*! \ingroup GrpBaseFieldTraits
207 \ingroup GrpLibOSGBase
210 template<class ValueT, Int32 iNamespace = 0>
211 struct FieldTraitsVecTemplateBase : public FieldTraitsTemplateBase<ValueT,
212 iNamespace>
214 // Binary
216 typedef FieldTraitsVecTemplateBase<ValueT,
217 iNamespace> Self;
219 static const UInt32 _uiSize = ValueT::_uiSize;
221 typedef typename ValueT::ValueType SingleValueT;
223 static bool getFromCString( ValueT &outVal,
224 const Char8 *&inVal )
226 outVal.setValueFromCString(inVal);
228 return true;
231 static SizeT getBinSize(const ValueT &)
233 return sizeof(SingleValueT) * _uiSize;
236 static SizeT getBinSize(const ValueT *,
237 SizeT uiNumObjects)
239 return sizeof(SingleValueT) * _uiSize * uiNumObjects;
242 static void copyToBin( BinaryDataHandler &pMem,
243 const ValueT &oObject)
245 pMem.putValues(&(oObject[0]), _uiSize);
248 static void copyToBin( BinaryDataHandler &pMem,
249 const ValueT *pObjectStore,
250 SizeT uiNumObjects)
252 pMem.putValues(&(pObjectStore[0][0]), uiNumObjects * _uiSize);
255 static void copyFromBin(BinaryDataHandler &pMem,
256 ValueT &oObject)
258 pMem.getValues(&(oObject[0]), _uiSize);
261 static void copyFromBin(BinaryDataHandler &pMem,
262 ValueT *pObjectStore,
263 SizeT uiNumObjects)
265 pMem.getValues(&(pObjectStore[0][0]), uiNumObjects * _uiSize);
269 /*! \ingroup GrpBaseFieldTraits
270 \ingroup GrpLibOSGBase
273 template<class ValueT, Int32 iNamespace = 0>
274 struct FieldTraitsPtrToStringTemplateBase :
275 public FieldTraitsTemplateBase<ValueT, iNamespace>
277 // Binary
279 typedef FieldTraitsPtrToStringTemplateBase<ValueT,
280 iNamespace> Self;
282 static SizeT getBinSize(const ValueT &oObject)
284 typedef FieldTraits<ValueT, iNamespace> MappedTrait;
286 std::string value;
288 MappedTrait::putToString(oObject, value);
290 return value.length() + 1 + sizeof(UInt32);
293 static SizeT getBinSize(const ValueT *pObjectStore,
294 SizeT uiNumObjects)
296 typedef FieldTraits<ValueT, iNamespace> MappedTrait;
298 SizeT size = 0;
300 for(SizeT i = 0; i < uiNumObjects; ++i)
302 size += MappedTrait::getBinSize(pObjectStore[i]);
305 return size;
308 static void copyToBin( BinaryDataHandler &pMem,
309 const ValueT &oObject)
311 typedef FieldTraits<ValueT, iNamespace> MappedTrait;
313 std::string value;
315 MappedTrait::putToString(oObject, value);
317 pMem.putValue(value);
320 static void copyToBin( BinaryDataHandler &pMem,
321 const ValueT *pObjectStore,
322 SizeT uiNumObjects)
324 typedef FieldTraits<ValueT, iNamespace> MappedTrait;
326 // defaut: copy each element
327 for(SizeT i = 0; i < uiNumObjects; ++i)
329 MappedTrait::copyToBin(pMem, pObjectStore[i]);
333 static void copyFromBin(BinaryDataHandler &pMem,
334 ValueT &oObject)
336 typedef FieldTraits<ValueT, iNamespace> MappedTrait;
338 const Char8 *c = NULL;
340 std::string value;
342 pMem.getValue(value);
344 c = value.c_str();
346 MappedTrait::getFromCString(oObject, c);
349 static void copyFromBin(BinaryDataHandler &pMem,
350 ValueT *pObjectStore,
351 SizeT uiNumObjects)
353 typedef FieldTraits<ValueT, iNamespace> MappedTrait;
355 // defaut: copy each element
356 for(SizeT i = 0; i < uiNumObjects; ++i)
358 MappedTrait::copyFromBin(pMem, pObjectStore[i]);
363 /*! \ingroup GrpBaseFieldTraits
364 \ingroup GrpLibOSGBase
367 template<class ValueT, Int32 iNamespace = 0>
368 struct FieldTraitsVec1TemplateBase :
369 public FieldTraitsVecTemplateBase<ValueT,
370 iNamespace>
372 static void putToStream(const ValueT &val,
373 OutStream &str)
375 typedef typename ValueT::ValueType ValueType;
377 typedef TypeTraits<ValueType> TypeTrait;
379 TypeTrait::putToStream(val[0], str);
383 /*! \ingroup GrpBaseFieldTraits
384 \ingroup GrpLibOSGBase
387 template<class ValueT, Int32 iNamespace = 0>
388 struct FieldTraitsVec2TemplateBase :
389 public FieldTraitsVecTemplateBase<ValueT,
390 iNamespace>
392 static void putToStream(const ValueT &val,
393 OutStream &str)
395 typedef typename ValueT::ValueType ValueType;
397 typedef TypeTraits<ValueType> TypeTrait;
399 TypeTrait::putToStream(val[0], str);
401 str << " ";
403 TypeTrait::putToStream(val[1], str);
408 /*! \ingroup GrpBaseFieldTraits
409 \ingroup GrpLibOSGBase
412 template<class ValueT, Int32 iNamespace = 0>
413 struct FieldTraitsVec3TemplateBase :
414 public FieldTraitsVecTemplateBase<ValueT,
415 iNamespace>
417 static void putToStream(const ValueT &val,
418 OutStream &str)
420 typedef typename ValueT::ValueType ValueType;
422 typedef TypeTraits<ValueType> TypeTrait;
424 TypeTrait::putToStream(val[0], str);
426 str << " ";
428 TypeTrait::putToStream(val[1], str);
430 str << " ";
432 TypeTrait::putToStream(val[2], str);
437 /*! \ingroup GrpBaseFieldTraits
438 \ingroup GrpLibOSGBase
441 template<class ValueT, Int32 iNamespace = 0>
442 struct FieldTraitsVec4TemplateBase :
443 public FieldTraitsVecTemplateBase<ValueT,
444 iNamespace>
446 static void putToStream(const ValueT &val,
447 OutStream &str)
449 typedef typename ValueT::ValueType ValueType;
451 typedef TypeTraits<ValueType> TypeTrait;
453 TypeTrait::putToStream(val[0], str);
455 str << " ";
457 TypeTrait::putToStream(val[1], str);
459 str << " ";
461 TypeTrait::putToStream(val[2], str);
463 str << " ";
465 TypeTrait::putToStream(val[3], str);
470 /*! \ingroup GrpBaseFieldTraits
471 \ingroup GrpLibOSGBase
472 \nohierarchy
475 template <class T, Int32 iNamespace = 0>
476 struct StreamConversionError
478 static void getFromStream( T &,
479 std::istream &)
481 SLOG << "Error from stream conversion not available for "
482 << FieldTraits<T, iNamespace>::getType().getCName() << std::endl;
485 static void putToStream(const T &,
486 OutStream &)
488 SLOG << "Error to stream conversion not available for "
489 << FieldTraits<T, iNamespace>::getType().getCName() << std::endl;
493 /*! \ingroup GrpBaseFieldTraits
494 \ingroup GrpLibOSGBase
495 \nohierarchy
498 template <class T, Int32 iNamespace = 0>
499 struct StringConversionError
501 static void getFromCString( T &,
502 const Char8 *&)
504 SLOG << "Error from string conversion not available for "
505 << FieldTraits<T, iNamespace>::getType().getCName() << std::endl;
508 static void putToString(const T &,
509 std::string &)
511 SLOG << "Error to string conversion not available for "
512 << FieldTraits<T, iNamespace>::getType().getCName() << std::endl;
516 template<class DescT,
517 enum FieldType::Cardinality eFieldCard,
518 typename RefCountPolicy ,
519 enum FieldType::Class eFieldClass>
520 class FieldDescription;
522 OSG_END_NAMESPACE
524 #include "OSGFieldTraits.inl"
526 #endif /* _OSGFIELDTRAITS_H_ */