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 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGFIELDTRAITS_H_
40 #define _OSGFIELDTRAITS_H_
45 #include "OSGFieldForwards.h"
47 #include "OSGContainerForwards.h"
48 #include "OSGFieldType.h"
49 #include "OSGPointerType.h"
53 #include <boost/mpl/if.hpp>
58 class BasicFieldConnector
;
60 template<class FieldTypeT
>
63 /*! \ingroup GrpBaseFieldTraits
64 \ingroup GrpLibOSGBase
67 struct FieldTraitsBase
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"; }
95 template<class ValueT
, Int32 iNamespace
= 0>
98 /*! \ingroup GrpBaseFieldTraits
99 \ingroup GrpLibOSGBase
107 /*! \ingroup GrpBaseFieldTraits
108 \ingroup GrpLibOSGBase
111 template<class ValueT
, Int32 NamespaceI
= 0>
112 struct FieldTraitsTemplateBase
: public FieldTraitsBase
115 static const UInt32 uiTest
= TypeTraits
<ValueT
>::IsPOD
== true;
118 boost::mpl::if_
<boost::mpl::bool_
<(uiTest
== 1)>,
120 const ValueT
& >::type ArgumentType
;
123 boost::mpl::if_
<boost::mpl::bool_
<TypeTraits
<ValueT
>::IsPOD
>,
125 const ValueT
& >::type ArgumentType
;
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
,
146 static bool getFromCString( ValueT
&outVal
,
147 const Char8
*&inVal
)
149 outVal
= TypeTraits
<ValueT
>::getFromCString(inVal
);
154 static void putToString(const ValueT
&inVal
,
157 TypeTraits
<ValueT
>::putToString(inVal
, outVal
);
160 static void putToStream(const ValueT
&val
,
163 TypeTraits
<ValueT
>::putToStream(val
, str
);
168 static SizeT
getBinSize(const ValueT
&)
170 return sizeof(ValueT
);
173 static SizeT
getBinSize(const ValueT
*,
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
,
189 pMem
.putValues(&(pObjectStore
[0]), uiNumObjects
);
192 static void copyFromBin(BinaryDataHandler
&pMem
,
195 pMem
.getValue(oObject
);
198 static void copyFromBin(BinaryDataHandler
&pMem
,
199 ValueT
*pObjectStore
,
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
,
216 typedef FieldTraitsVecTemplateBase
<ValueT
,
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
);
231 static SizeT
getBinSize(const ValueT
&)
233 return sizeof(SingleValueT
) * _uiSize
;
236 static SizeT
getBinSize(const ValueT
*,
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
,
252 pMem
.putValues(&(pObjectStore
[0][0]), uiNumObjects
* _uiSize
);
255 static void copyFromBin(BinaryDataHandler
&pMem
,
258 pMem
.getValues(&(oObject
[0]), _uiSize
);
261 static void copyFromBin(BinaryDataHandler
&pMem
,
262 ValueT
*pObjectStore
,
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
>
279 typedef FieldTraitsPtrToStringTemplateBase
<ValueT
,
282 static SizeT
getBinSize(const ValueT
&oObject
)
284 typedef FieldTraits
<ValueT
, iNamespace
> MappedTrait
;
288 MappedTrait::putToString(oObject
, value
);
290 return value
.length() + 1 + sizeof(UInt32
);
293 static SizeT
getBinSize(const ValueT
*pObjectStore
,
296 typedef FieldTraits
<ValueT
, iNamespace
> MappedTrait
;
300 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
302 size
+= MappedTrait::getBinSize(pObjectStore
[i
]);
308 static void copyToBin( BinaryDataHandler
&pMem
,
309 const ValueT
&oObject
)
311 typedef FieldTraits
<ValueT
, iNamespace
> MappedTrait
;
315 MappedTrait::putToString(oObject
, value
);
317 pMem
.putValue(value
);
320 static void copyToBin( BinaryDataHandler
&pMem
,
321 const ValueT
*pObjectStore
,
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
,
336 typedef FieldTraits
<ValueT
, iNamespace
> MappedTrait
;
338 const Char8
*c
= NULL
;
342 pMem
.getValue(value
);
346 MappedTrait::getFromCString(oObject
, c
);
349 static void copyFromBin(BinaryDataHandler
&pMem
,
350 ValueT
*pObjectStore
,
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
,
372 static void putToStream(const ValueT
&val
,
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
,
392 static void putToStream(const ValueT
&val
,
395 typedef typename
ValueT::ValueType ValueType
;
397 typedef TypeTraits
<ValueType
> TypeTrait
;
399 TypeTrait::putToStream(val
[0], 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
,
417 static void putToStream(const ValueT
&val
,
420 typedef typename
ValueT::ValueType ValueType
;
422 typedef TypeTraits
<ValueType
> TypeTrait
;
424 TypeTrait::putToStream(val
[0], str
);
428 TypeTrait::putToStream(val
[1], 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
,
446 static void putToStream(const ValueT
&val
,
449 typedef typename
ValueT::ValueType ValueType
;
451 typedef TypeTraits
<ValueType
> TypeTrait
;
453 TypeTrait::putToStream(val
[0], str
);
457 TypeTrait::putToStream(val
[1], str
);
461 TypeTrait::putToStream(val
[2], str
);
465 TypeTrait::putToStream(val
[3], str
);
470 /*! \ingroup GrpBaseFieldTraits
471 \ingroup GrpLibOSGBase
475 template <class T
, Int32 iNamespace
= 0>
476 struct StreamConversionError
478 static void getFromStream( T
&,
481 SLOG
<< "Error from stream conversion not available for "
482 << FieldTraits
<T
, iNamespace
>::getType().getCName() << std::endl
;
485 static void putToStream(const T
&,
488 SLOG
<< "Error to stream conversion not available for "
489 << FieldTraits
<T
, iNamespace
>::getType().getCName() << std::endl
;
493 /*! \ingroup GrpBaseFieldTraits
494 \ingroup GrpLibOSGBase
498 template <class T
, Int32 iNamespace
= 0>
499 struct StringConversionError
501 static void getFromCString( T
&,
504 SLOG
<< "Error from string conversion not available for "
505 << FieldTraits
<T
, iNamespace
>::getType().getCName() << std::endl
;
508 static void putToString(const T
&,
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
;
524 #include "OSGFieldTraits.inl"
526 #endif /* _OSGFIELDTRAITS_H_ */