fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Base / Field / OSGBaseFieldTraits.h
blob0e0a370963c85a9b8b581e0112a15054578a1d10
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 _OSGBASEFIELDTRAITS_H_
40 #define _OSGBASEFIELDTRAITS_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 #include "OSGSysFieldTraits.h"
46 #include "OSGDataType.h"
48 #include "OSGColor.h"
49 #include "OSGTime.h"
50 #include "OSGSphereVolume.h"
51 #include "OSGBoxVolume.h"
52 #include "OSGPlane.h"
53 #include "OSGGL.h"
54 #include "OSGGLDefineMapper.h"
56 #include <iomanip>
58 OSG_BEGIN_NAMESPACE
60 /*! \ingroup GrpBaseFieldTraits
61 \ingroup GrpLibOSGBase
64 template <>
65 struct FieldTraits<Color3f> : public FieldTraitsVec3TemplateBase<Color3f>
67 private:
69 static DataType _type;
71 public:
73 typedef FieldTraits<Color3f> Self;
75 enum { Convertible = (Self::ToStreamConvertible |
76 Self::FromStringConvertible) };
78 static OSG_BASE_DLLMAPPING
79 DataType &getType (void);
81 static const Char8 *getSName (void) { return "SFColor3f"; }
83 static const Char8 *getMName (void) { return "MFColor3f"; }
85 static const Color3f getDefault (void) { return Color3f(); }
89 /*! \ingroup GrpBaseFieldTraits
90 \ingroup GrpLibOSGBase
93 template <>
94 struct FieldTraits<Color3fx> : public FieldTraitsVec3TemplateBase<Color3fx>
96 private:
98 static DataType _type;
100 public:
102 typedef FieldTraits<Color3fx> Self;
104 enum { Convertible = (Self::ToStreamConvertible |
105 Self::FromStringConvertible) };
107 static OSG_BASE_DLLMAPPING
108 DataType &getType (void);
110 static const Char8 *getSName (void) { return "SFColor3fx"; }
112 static const Char8 *getMName (void) { return "MFColor3fx"; }
114 static const Color3fx getDefault (void) { return Color3fx(); }
118 /*! \ingroup GrpBaseFieldTraits
119 \ingroup GrpLibOSGBase
122 template <>
123 struct FieldTraits<Color4f> : public FieldTraitsVec4TemplateBase<Color4f>
125 private:
127 static DataType _type;
129 public:
131 typedef FieldTraits<Color4f> Self;
133 enum { Convertible = (Self::ToStreamConvertible |
134 Self::FromStringConvertible) };
136 static OSG_BASE_DLLMAPPING
137 DataType &getType (void);
139 static const Char8 *getSName (void) { return "SFColor4f"; }
141 static const Char8 *getMName (void) { return "MFColor4f"; }
143 static Color4f getDefault (void){ return Color4f(); }
147 /*! \ingroup GrpBaseFieldTraits
148 \ingroup GrpLibOSGBase
151 template <>
152 struct FieldTraits<Color4fx> : public FieldTraitsVec4TemplateBase<Color4fx>
154 private:
156 static DataType _type;
158 public:
160 typedef FieldTraits<Color4fx> Self;
162 enum { Convertible = (Self::ToStreamConvertible |
163 Self::FromStringConvertible) };
165 static OSG_BASE_DLLMAPPING
166 DataType &getType (void);
168 static const Char8 *getSName (void) { return "SFColor4fx"; }
170 static const Char8 *getMName (void) { return "MFColor4fx"; }
172 static Color4fx getDefault (void){ return Color4fx(); }
176 /*! \ingroup GrpBaseFieldTraits
177 \ingroup GrpLibOSGBase
180 template <>
181 struct FieldTraits<Color3ub> : public FieldTraitsVec3TemplateBase<Color3ub>
183 private:
185 static DataType _type;
187 public:
189 typedef FieldTraits<Color3ub> Self;
191 enum { Convertible = (Self::ToStreamConvertible |
192 Self::FromStringConvertible) };
194 static OSG_BASE_DLLMAPPING
195 DataType &getType (void);
197 static const Char8 *getSName (void) { return "SFColor3ub"; }
198 static const Char8 *getMName (void) { return "MFColor3ub"; }
200 static Color3ub getDefault (void) { return Color3ub(); }
204 /*! \ingroup GrpBaseFieldTraits
205 \ingroup GrpLibOSGBase
208 template <>
209 struct FieldTraits<Color4ub> : public FieldTraitsVec4TemplateBase<Color4ub>
211 private:
213 static DataType _type;
215 public:
217 typedef FieldTraits<Color4ub> Self;
219 enum { Convertible = (Self::ToStreamConvertible |
220 Self::FromStringConvertible) };
222 static OSG_BASE_DLLMAPPING
223 DataType &getType (void);
225 static const Char8 *getSName (void) { return "SFColor4ub"; }
227 static const Char8 *getMName (void) { return "MFColor4ub"; }
229 static Color4ub getDefault (void) { return Color4ub(); }
233 /*! \ingroup GrpBaseFieldTraits
234 \ingroup GrpLibOSGBase
237 template <>
238 struct FieldTraits<std::string> : public FieldTraitsTemplateBase<std::string>
240 private:
242 static DataType _type;
244 public:
246 typedef FieldTraits<std::string> Self;
248 enum { Convertible = (Self::FromStringConvertible |
249 Self::ToStreamConvertible ) };
251 static OSG_BASE_DLLMAPPING
252 DataType &getType (void);
254 static const Char8 *getSName (void) { return "SFString"; }
256 static const Char8 *getMName (void) { return "MFString"; }
258 static std::string getDefault (void) { return std::string(); }
260 static void putToStream(const std::string &val,
261 OutStream &outStr)
263 outStr << "\"";
264 outStr << val;
265 outStr << "\"";
268 static bool getFromCString( std::string &outVal,
269 const Char8 *&inVal)
271 outVal.assign(inVal);
273 return true;
276 static SizeT getBinSize (const std::string &oObject)
278 return oObject.length() + 1 + sizeof(UInt32);
281 static SizeT getBinSize (const std::string *pObjectStore,
282 SizeT uiNumObjects)
284 SizeT size = 0;
286 for(SizeT i = 0; i < uiNumObjects; ++i)
288 size += getBinSize(pObjectStore[i]);
291 return size;
295 static void copyToBin( BinaryDataHandler &pMem,
296 const std::string &oObject)
298 pMem.putValue(oObject);
301 static void copyToBin( BinaryDataHandler &pMem,
302 const std::string *pObjectStore,
303 SizeT uiNumObjects)
305 for(SizeT i = 0; i < uiNumObjects; ++i)
307 copyToBin(pMem, pObjectStore[i]);
311 static void copyFromBin( BinaryDataHandler &pMem,
312 std::string &oObject)
314 pMem.getValue(oObject);
317 static void copyFromBin( BinaryDataHandler &pMem,
318 std::string *pObjectStore,
319 SizeT uiNumObjects)
321 for(SizeT i = 0; i < uiNumObjects; ++i)
323 copyFromBin(pMem, pObjectStore[i]);
329 /*! \ingroup GrpBaseFieldTraits
330 \ingroup GrpLibOSGBase
333 template <>
334 struct FieldTraits<Time, 1> : public FieldTraitsPODTemplateBase<Time, 1>
336 private:
338 static DataType _type;
340 public:
342 typedef FieldTraits<Time, 1> Self;
344 enum { Convertible = (Self::ToStreamConvertible |
345 Self::FromStringConvertible) };
347 static OSG_BASE_DLLMAPPING
348 DataType &getType (void);
350 static const Char8 *getSName (void) { return "SFTime"; }
352 static const Char8 *getMName (void) { return "MFTime"; }
354 static Time getDefault (void) { return Time(); }
358 /*! \ingroup GrpBaseFieldTraits
359 \ingroup GrpLibOSGBase
362 template <>
363 struct FieldTraits<BoxVolume> :
364 public FieldTraitsTemplateBase<BoxVolume>
366 private:
368 static DataType _type;
370 public:
372 typedef FieldTraits<BoxVolume> Self;
374 enum { Convertible = (Self::FromStringConvertible |
375 Self::ToStreamConvertible ) };
377 static OSG_BASE_DLLMAPPING
378 DataType &getType (void);
380 static const Char8 *getSName (void) { return "SFBoxVolume"; }
382 static const Char8 *getMName (void) { return "MFBoxVolume"; }
384 static const BoxVolume getDefault (void) { return BoxVolume(); }
387 static bool getFromCString( BoxVolume &outVal,
388 const Char8 *&inVal)
390 Real32 valStore[ 6];
391 Char8 str [256];
393 SizeT length = strlen(inVal);
394 Char8 *c = str;
396 if(length > 256)
398 std::cerr << "FieldDataTraits<BoxVolume>::getFromString(): "
399 << "Input too long" << std::endl;
401 return false;
404 strncpy(str, inVal, length);
406 while(*c != '\0')
408 if(*c == '[')
409 *c = ' ';
410 if(*c == ']')
411 *c = ' ';
412 if(*c == ',')
413 *c = ' ';
415 c++;
418 Int32 iState;
420 Int16 count = sscanf(str, "%d %f %f %f %f %f %f",
421 &iState,
422 &valStore[0],
423 &valStore[1],
424 &valStore[2],
425 &valStore[3],
426 &valStore[4],
427 &valStore[5]);
429 if(count == 7)
431 outVal.setState(iState);
433 outVal.setBounds(valStore[0],
434 valStore[1],
435 valStore[2],
436 valStore[3],
437 valStore[4],
438 valStore[5]);
440 return true;
442 else
444 outVal.setBounds(0.f, 0.f, 0.f,
445 0.f, 0.f, 0.f);
447 return false;
452 static void putToStream(const BoxVolume &val,
453 OutStream &str)
455 Pnt3f min, max;
457 typedef TypeTraits<Pnt3f::ValueType> TypeTrait;
458 typedef TypeTraits<UInt16> StateTypeTrait;
460 StateTypeTrait::putToStream(val.getState(), str);
461 str << " ";
463 val.getBounds(min, max);
465 TypeTrait::putToStream(min[0], str);
466 str << " ";
468 TypeTrait::putToStream(min[1], str);
469 str << " ";
471 TypeTrait::putToStream(min[2], str);
472 str << " ";
474 TypeTrait::putToStream(max[0], str);
475 str << " ";
477 TypeTrait::putToStream(max[1], str);
478 str << " ";
480 TypeTrait::putToStream(max[2], str);
483 static SizeT getBinSize(const BoxVolume &oObject)
485 SizeT size = sizeof(UInt16);
487 size += sizeof(Pnt3f) + sizeof(Pnt3f);
489 return size;
493 static SizeT getBinSize(const BoxVolume *pObjectStore,
494 SizeT uiNumObjects)
496 SizeT size = 0;
498 for(SizeT i = 0; i < uiNumObjects; ++i)
500 size += getBinSize(pObjectStore[i]);
503 return size;
506 static void copyToBin( BinaryDataHandler &pMem,
507 const BoxVolume &oObject)
510 UInt16 state = oObject.getState();
512 pMem.putValue(state);
514 pMem.putValues(&(oObject.getMin()[0]), 3);
515 pMem.putValues(&(oObject.getMax()[0]), 3);
518 static void copyToBin( BinaryDataHandler &pMem,
519 const BoxVolume *pObjectStore,
520 SizeT uiNumObjects)
522 for(SizeT i = 0; i < uiNumObjects; ++i)
524 copyToBin(pMem, pObjectStore[i]);
528 static void copyFromBin(BinaryDataHandler &pMem,
529 BoxVolume &oObject)
531 Pnt3f min,max;
532 UInt16 state;
534 pMem.getValue (state );
535 pMem.getValues(&(min[0]), 3);
536 pMem.getValues(&(max[0]), 3);
538 oObject.setBounds(min, max);
539 oObject.setState (state );
542 static void copyFromBin(BinaryDataHandler &pMem,
543 BoxVolume *pObjectStore,
544 SizeT uiNumObjects)
546 for(SizeT i = 0; i < uiNumObjects; ++i)
548 copyFromBin(pMem, pObjectStore[i]);
554 /*! \ingroup GrpBaseFieldTraits
555 \ingroup GrpLibOSGBase
558 template <>
559 struct FieldTraits<BitVector, 1> :
560 public FieldTraitsPODTemplateBase<BitVector, 1>
562 private:
564 static DataType _type;
566 public:
568 typedef FieldTraits<BitVector, 1> Self;
570 enum { Convertible = Self::NotConvertible };
572 static OSG_BASE_DLLMAPPING
573 DataType &getType (void);
575 static const Char8 *getSName(void) { return "SFBitVector"; }
577 static const Char8 *getMName(void) { return "MFBitVector"; }
579 static BitVector getDefault(void) { return BitVector(); }
583 /*! \ingroup GrpBaseFieldTraits
584 \ingroup GrpLibOSGBase
587 template <>
588 struct FieldTraits<GLenum, 1> : public FieldTraitsPODTemplateBase<GLenum, 1>
590 private:
592 static DataType _type;
594 public:
596 typedef FieldTraits<GLenum, 1> Self;
598 enum { Convertible = (Self::ToStreamConvertible |
599 Self::FromStringConvertible) };
601 static OSG_BASE_DLLMAPPING
602 DataType &getType (void);
604 static const Char8 *getSName (void) { return "SFGLenum"; }
605 static const Char8 *getMName (void) { return "MFGLenum"; }
607 static GLenum getDefault (void) { return 0; }
609 static void putToStream(const GLenum &val,
610 OutStream &str)
612 #ifdef OSG_GL_DEFMAPPER
613 const std::string &oVal = GLDefineMapper::the()->toString(val);
615 str << "GL_" << oVal;
616 #else
617 str << std::setbase(16);
619 str << "0x";
620 TypeTraits<GLenum>::putToStream(val, str);
622 str << std::setbase(10);
623 #endif
626 #ifdef OSG_GL_DEFMAPPER
627 static bool getFromCString( GLenum &outVal,
628 const Char8 *&inVal )
630 outVal = GLDefineMapper::the()->fromString(inVal);
632 return true;
634 #endif
638 /*! \ingroup GrpBaseFieldTraits
639 \ingroup GrpLibOSGBase
642 template <>
643 struct FieldTraits<Plane> : public FieldTraitsTemplateBase<Plane>
645 private:
647 static DataType _type;
649 public:
651 typedef FieldTraits<Plane> Self;
653 enum { Convertible = Self::NotConvertible };
655 static OSG_BASE_DLLMAPPING
656 DataType &getType (void);
658 static const Char8 *getSName (void) { return "SFPlane"; }
660 static const Char8 *getMName (void) { return "MFPlane"; }
662 static Plane getDefault (void) { return Plane(); }
664 static SizeT getBinSize (const Plane &)
666 return sizeof(Real32) * 4;
669 static SizeT getBinSize (const Plane *,
670 SizeT uiNumObjects)
672 return sizeof(Real32) * 4 * uiNumObjects;
675 static void copyToBin ( BinaryDataHandler &pMem,
676 const Plane &oObject)
678 const Vec3f &normal = oObject.getNormal ();
679 Real32 distance = oObject.getDistanceFromOrigin();
681 pMem.putValues(&normal[0], 3);
682 pMem.putValue ( distance );
685 static void copyToBin( BinaryDataHandler &pMem,
686 const Plane *pObjectStore,
687 SizeT uiNumObjects)
689 for(SizeT i = 0; i < uiNumObjects; ++i)
691 copyToBin(pMem, pObjectStore[i]);
695 static void copyFromBin( BinaryDataHandler &pMem,
696 Plane &oObject)
698 Vec3f normal;
699 Real32 distance;
701 pMem.getValues(&normal[0], 3);
702 pMem.getValue ( distance );
704 oObject.set(normal, distance);
707 static void copyFromBin(BinaryDataHandler &pMem,
708 Plane *pObjectStore,
709 SizeT uiNumObjects)
711 for(SizeT i = 0; i < uiNumObjects; ++i)
713 copyFromBin(pMem, pObjectStore[i]);
719 /*! \ingroup GrpBaseFieldTraits
720 \ingroup GrpLibOSGBase
723 template <>
724 struct FieldTraits<RenderPropBitVector, 3> :
725 public FieldTraitsPODTemplateBase<RenderPropBitVector, 3>
727 private:
729 static DataType _type;
731 public:
733 typedef FieldTraits<RenderPropBitVector, 3> Self;
735 enum { Convertible = (Self::ToStreamConvertible |
736 Self::FromStringConvertible) };
738 static OSG_BASE_DLLMAPPING
739 DataType &getType (void);
741 static const Char8 *getSName (void) { return "SFRenderPropBitVector"; }
742 static const Char8 *getMName (void) { return "MFRenderPropBitVector"; }
744 static RenderPropBitVector getDefault(void) { return 0; }
746 static void putToStream(const RenderPropBitVector &val,
747 OutStream &str)
749 FieldTraits<RenderPropBitVector, 0>::putToStream(val, str);
752 static bool getFromCString( RenderPropBitVector &outVal,
753 const Char8 *&inVal )
755 return FieldTraits<RenderPropBitVector, 0>::getFromCString(outVal,
756 inVal);
760 OSG_END_NAMESPACE
762 #endif /* _OSGBASEFIELDTRAITS_H_ */