ShapeAttribute 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGSIM_SHAPEATTRIBUTE
  14. #define OSGSIM_SHAPEATTRIBUTE 1
  15. #include <osg/Object>
  16. #include <osg/MixinVector>
  17. #include <osgSim/Export>
  18. namespace osgSim
  19. {
  20. class OSGSIM_EXPORT ShapeAttribute
  21. {
  22. public:
  23. /// ShapeAttribute data type.
  24. enum Type
  25. {
  26. UNKNOWN,
  27. INTEGER,
  28. DOUBLE,
  29. STRING
  30. };
  31. ShapeAttribute();
  32. ShapeAttribute(const char * name);
  33. ShapeAttribute(const char * name, int value);
  34. ShapeAttribute(const char * name, double value);
  35. /** Note, ShapeAttribute takes a copy of both name and value, the calling code should manage its own clean up of the original strings.*/
  36. ShapeAttribute(const char * name, const char * value);
  37. ShapeAttribute(const ShapeAttribute & sa);
  38. ~ShapeAttribute();
  39. ShapeAttribute& operator = (const ShapeAttribute& sa);
  40. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  41. int compare(const osgSim::ShapeAttribute& sa) const;
  42. inline bool operator == (const osgSim::ShapeAttribute& sa) const { return compare(sa)==0; }
  43. inline bool operator != (const osgSim::ShapeAttribute& sa) const { return compare(sa)!=0; }
  44. inline bool operator < (const osgSim::ShapeAttribute& sa) const { return compare(sa)<0; }
  45. /// Get the attribute name.
  46. const std::string & getName() const { return _name; }
  47. /// Set the attribute name.
  48. void setName(const std::string& name) { _name = name; }
  49. /// Get the attribute data type.
  50. Type getType() const { return _type; }
  51. /// Get the attribute data as an int.
  52. int getInt() const { return _integer; }
  53. /// Get the attribute data as a double.
  54. double getDouble() const { return _double; }
  55. /// Get the attribute data as a string.
  56. const char * getString() const { return _string; }
  57. /// Set an integer attribute data.
  58. void setValue(int value) { free(); _type = INTEGER; _integer = value; }
  59. /// Set a double attribute data.
  60. void setValue(double value) { free(); _type = DOUBLE; _double = value; }
  61. /// Set a string attribute data.
  62. void setValue(const char * value);
  63. private:
  64. void free();
  65. void copy(const ShapeAttribute& sa);
  66. std::string _name;
  67. Type _type;
  68. union
  69. {
  70. int _integer;
  71. double _double;
  72. char* _string;
  73. };
  74. };
  75. class OSGSIM_EXPORT ShapeAttributeList : public osg::Object, public osg::MixinVector<ShapeAttribute>
  76. {
  77. public:
  78. META_Object(osgSim, ShapeAttributeList)
  79. ShapeAttributeList():
  80. Object()
  81. {}
  82. /** Copy constructor, optional CopyOp object can be used to control
  83. * shallow vs deep copying of dynamic data.*/
  84. ShapeAttributeList(const ShapeAttributeList& sal,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
  85. osg::Object(sal, copyop),
  86. osg::MixinVector<ShapeAttribute>(sal)
  87. {
  88. }
  89. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  90. virtual int compare(const osgSim::ShapeAttributeList& sal) const;
  91. protected:
  92. virtual ~ShapeAttributeList() {}
  93. };
  94. }
  95. #endif // ** SHAPEATTRIBUTE_ ** //