OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ValueObject.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 2011 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 
14 #ifndef OSG_VALUEOBJECT
15 #define OSG_VALUEOBJECT 1
16 
17 #include <osg/Object>
18 #include <osg/UserDataContainer>
19 #include <osg/BoundingBox>
20 #include <osg/BoundingSphere>
21 
22 namespace osg {
23 
24 // forward declare core OSG math classes
25 class Vec2f;
26 class Vec3f;
27 class Vec4f;
28 class Vec2d;
29 class Vec3d;
30 class Vec4d;
31 class Quat;
32 class Plane;
33 class Matrixf;
34 class Matrixd;
35 
36 class ValueObject : public Object
37 {
38  public:
39 
40  ValueObject() : Object(true) {}
41  ValueObject(const std::string& name) : Object(true) { setName(name); }
42  ValueObject(const ValueObject& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY): Object(rhs,copyop) {}
43 
45 
46  class GetValueVisitor
47  {
48  public:
49  virtual ~GetValueVisitor() {}
50  virtual void apply(bool /*value*/) {}
51  virtual void apply(char /*value*/) {}
52  virtual void apply(unsigned char /*value*/) {}
53  virtual void apply(short /*value*/) {}
54  virtual void apply(unsigned short /*value*/) {}
55  virtual void apply(int /*value*/) {}
56  virtual void apply(unsigned int /*value*/) {}
57  virtual void apply(float /*value*/) {}
58  virtual void apply(double /*value*/) {}
59  virtual void apply(const std::string& /*value*/) {}
60  virtual void apply(const osg::Vec2f& /*value*/) {}
61  virtual void apply(const osg::Vec3f& /*value*/) {}
62  virtual void apply(const osg::Vec4f& /*value*/) {}
63  virtual void apply(const osg::Vec2d& /*value*/) {}
64  virtual void apply(const osg::Vec3d& /*value*/) {}
65  virtual void apply(const osg::Vec4d& /*value*/) {}
66  virtual void apply(const osg::Quat& /*value*/) {}
67  virtual void apply(const osg::Plane& /*value*/) {}
68  virtual void apply(const osg::Matrixf& /*value*/) {}
69  virtual void apply(const osg::Matrixd& /*value*/) {}
70  virtual void apply(const osg::BoundingBoxf& /*value*/) {}
71  virtual void apply(const osg::BoundingBoxd& /*value*/) {}
72  virtual void apply(const osg::BoundingSpheref& /*value*/) {}
73  virtual void apply(const osg::BoundingSphered& /*value*/) {}
74  };
75 
77  {
78  public:
79  virtual ~SetValueVisitor() {}
80  virtual void apply(bool& /*value*/) {}
81  virtual void apply(char& /*value*/) {}
82  virtual void apply(unsigned char& /*value*/) {}
83  virtual void apply(short& /*value*/) {}
84  virtual void apply(unsigned short& /*value*/) {}
85  virtual void apply(int& /*value*/) {}
86  virtual void apply(unsigned int& /*value*/) {}
87  virtual void apply(float& /*value*/) {}
88  virtual void apply(double& /*value*/) {}
89  virtual void apply(std::string& /*value*/) {}
90  virtual void apply(osg::Vec2f& /*value*/) {}
91  virtual void apply(osg::Vec3f& /*value*/) {}
92  virtual void apply(osg::Vec4f& /*value*/) {}
93  virtual void apply(osg::Vec2d& /*value*/) {}
94  virtual void apply(osg::Vec3d& /*value*/) {}
95  virtual void apply(osg::Vec4d& /*value*/) {}
96  virtual void apply(osg::Quat& /*value*/) {}
97  virtual void apply(osg::Plane& /*value*/) {}
98  virtual void apply(osg::Matrixf& /*value*/) {}
99  virtual void apply(osg::Matrixd& /*value*/) {}
100  virtual void apply(osg::BoundingBoxf& /*value*/) {}
101  virtual void apply(osg::BoundingBoxd& /*value*/) {}
102  virtual void apply(osg::BoundingSpheref& /*value*/) {}
103  virtual void apply(osg::BoundingSphered& /*value*/) {}
104  };
105 
106  virtual bool get(GetValueVisitor& /*gvv*/) const { return false; }
107  virtual bool set(SetValueVisitor& /*gvv*/) { return false; }
108 
109 protected:
110  virtual ~ValueObject() {}
111 };
112 
113 template< typename T >
115 {
116  static const char* className() { return "TemplateValueObject"; }
117 };
118 
119 
120 template< typename T >
122 {
123  public:
124 
126  ValueObject(),
127  _value() {}
128 
129  TemplateValueObject(const std::string& name, const T& value) :
130  ValueObject(name),
131  _value(value) {}
132 
134  ValueObject(rhs,copyop),
135  _value(rhs._value) {}
136 
137  virtual Object* cloneType() const { return new TemplateValueObject(); }
138  virtual Object* clone(const CopyOp& copyop) const { return new TemplateValueObject(*this, copyop); }
139  virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const TemplateValueObject*>(obj)!=NULL; }
140  virtual const char* libraryName() const { return "osg"; }
141  virtual const char* className() const { return ValueObjectClassNameTrait<T>::className(); }
142 
143  void setValue(const T& value) { _value = value; }
144  const T& getValue() const { return _value; }
145 
146  virtual bool get(GetValueVisitor& gvv) const { gvv.apply(_value); return true; }
147  virtual bool set(SetValueVisitor& svv) { svv.apply(_value); return true; }
148 
149 protected:
150 
151  virtual ~TemplateValueObject() {}
153 
155 };
156 
157 #define META_ValueObject(TYPE,NAME) \
158  template<> struct ValueObjectClassNameTrait<TYPE> { static const char* className() { return #NAME; } }; \
159  typedef TemplateValueObject<TYPE> NAME;
160 
161 META_ValueObject(std::string, StringValueObject)
162 META_ValueObject(bool, BoolValueObject)
163 META_ValueObject(char, CharValueObject)
164 META_ValueObject(unsigned char, UCharValueObject)
165 META_ValueObject(short, ShortValueObject)
166 META_ValueObject(unsigned short, UShortValueObject)
167 META_ValueObject(int, IntValueObject)
168 META_ValueObject(unsigned int, UIntValueObject)
169 META_ValueObject(float, FloatValueObject)
170 META_ValueObject(double, DoubleValueObject)
171 META_ValueObject(Vec2f, Vec2fValueObject)
172 META_ValueObject(Vec3f, Vec3fValueObject)
173 META_ValueObject(Vec4f, Vec4fValueObject)
174 META_ValueObject(Vec2d, Vec2dValueObject)
175 META_ValueObject(Vec3d, Vec3dValueObject)
176 META_ValueObject(Vec4d, Vec4dValueObject)
177 META_ValueObject(Quat, QuatValueObject)
178 META_ValueObject(Plane, PlaneValueObject)
179 META_ValueObject(Matrixf, MatrixfValueObject)
180 META_ValueObject(Matrixd, MatrixdValueObject)
181 META_ValueObject(BoundingBoxf, BoundingBoxfValueObject)
182 META_ValueObject(BoundingBoxd, BoundingBoxdValueObject)
183 META_ValueObject(BoundingSpheref, BoundingSpherefValueObject)
184 META_ValueObject(BoundingSphered, BoundingSpheredValueObject)
185 
187 template<typename T>
188 bool osg::Object::getUserValue(const std::string& name, T& value) const
189 {
190  typedef TemplateValueObject<T> UserValueObject;
191 
192  const osg::UserDataContainer* udc = dynamic_cast<const osg::UserDataContainer*>(this);
193  if (!udc) udc = _userDataContainer;
194 
195  const UserValueObject* uvo = udc ? dynamic_cast<const UserValueObject*>(udc->getUserObject(name)) : 0;
196  if (uvo)
197  {
198  value = uvo->getValue();
199  return true;
200  }
201  else
202  {
203  return false;
204  }
205 }
206 
208 template<typename T>
209 void osg::Object::setUserValue(const std::string& name, const T& value)
210 {
211  typedef TemplateValueObject<T> UserValueObject;
212 
213  osg::UserDataContainer* udc = dynamic_cast<osg::UserDataContainer*>(this);
214  if (!udc)
215  {
216  getOrCreateUserDataContainer();
217  udc = _userDataContainer;
218  }
219 
220  unsigned int i = udc->getUserObjectIndex(name);
221  if (i<udc->getNumUserObjects()) udc->setUserObject(i, new UserValueObject(name,value));
222  else udc->addUserObject(new UserValueObject(name,value));
223 }
224 
225 
226 }
227 #endif
228 
bool getUserValue(const osg::NodePath &nodepath, const std::string &name, T &value)
TemplateValueObject(const std::string &name, const T &value)
Definition: ValueObject.h:129
const T & getValue() const
Definition: ValueObject.h:144
virtual void apply(unsigned short &)
Definition: ValueObject.h:84
#define NULL
Definition: Export.h:59
virtual unsigned int getUserObjectIndex(const osg::Object *obj, unsigned int startPos=0) const =0
BoundingSphereImpl< Vec3d > BoundingSphered
void setUserValue(const std::string &name, const T &value)
Definition: ValueObject.h:209
META_ValueObject(std::string, StringValueObject) META_ValueObject(bool
virtual void apply(short &)
Definition: ValueObject.h:83
BoundingSphereImpl< Vec3f > BoundingSpheref
static const char * s_TemplateValueObject_className
Definition: ValueObject.h:152
virtual void apply(osg::BoundingBoxf &)
Definition: ValueObject.h:100
virtual unsigned int addUserObject(Object *obj)=0
BoundingBoxImpl< Vec3f > BoundingBoxf
Definition: BoundingBox.h:251
META_Object(osg, ValueObject) class GetValueVisitor
Definition: ValueObject.h:44
virtual ~ValueObject()
Definition: ValueObject.h:110
virtual void apply(double &)
Definition: ValueObject.h:88
virtual void apply(unsigned int &)
Definition: ValueObject.h:86
ValueObject(const ValueObject &rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY)
Definition: ValueObject.h:42
virtual void apply(osg::BoundingBoxd &)
Definition: ValueObject.h:101
virtual void apply(osg::Vec2d &)
Definition: ValueObject.h:93
virtual Object * getUserObject(unsigned int i)=0
virtual void apply(osg::Vec2f &)
Definition: ValueObject.h:90
void setValue(const T &value)
Definition: ValueObject.h:143
virtual Object * clone(const CopyOp &copyop) const
Definition: ValueObject.h:138
virtual void apply(osg::Vec4f &)
Definition: ValueObject.h:92
virtual void apply(bool &)
Definition: ValueObject.h:80
BoundingBoxImpl< Vec3d > BoundingBoxd
Definition: BoundingBox.h:252
virtual void apply(osg::Matrixf &)
Definition: ValueObject.h:98
virtual void setName(const std::string &name)
Definition: Object.h:134
static const char * className()
Definition: ValueObject.h:116
virtual void apply(osg::BoundingSphered &)
Definition: ValueObject.h:103
virtual const char * className() const
Definition: ValueObject.h:141
virtual void apply(std::string &)
Definition: ValueObject.h:89
virtual void apply(osg::Quat &)
Definition: ValueObject.h:96
virtual void apply(osg::Plane &)
Definition: ValueObject.h:97
ValueObject(const std::string &name)
Definition: ValueObject.h:41
A plane class. It can be used to represent an infinite plane.
Definition: Plane.h:33
virtual void apply(osg::Vec4d &)
Definition: ValueObject.h:95
TemplateValueObject(const TemplateValueObject &rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY)
Definition: ValueObject.h:133
virtual void apply(osg::Vec3d &)
Definition: ValueObject.h:94
virtual ~TemplateValueObject()
Definition: ValueObject.h:151
virtual const char * libraryName() const
Definition: ValueObject.h:140
virtual void apply(osg::Vec3f &)
Definition: ValueObject.h:91
Definition: AlphaFunc.h:19
virtual bool set(SetValueVisitor &svv)
Definition: ValueObject.h:147
virtual bool set(SetValueVisitor &)
Definition: ValueObject.h:107
virtual void setUserObject(unsigned int i, Object *obj)=0
virtual bool isSameKindAs(const Object *obj) const
Definition: ValueObject.h:139
Definition: Quat.h:29
virtual void apply(float &)
Definition: ValueObject.h:87
virtual void apply(unsigned char &)
Definition: ValueObject.h:82
virtual void apply(osg::Matrixd &)
Definition: ValueObject.h:99
virtual void apply(char &)
Definition: ValueObject.h:81
virtual Object * cloneType() const
Definition: ValueObject.h:137
virtual void apply(osg::BoundingSpheref &)
Definition: ValueObject.h:102