OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Object.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_OBJECT
15 #define OSG_OBJECT 1
16 
17 #include <osg/Referenced>
18 #include <osg/CopyOp>
19 #include <osg/ref_ptr>
20 #include <osg/Notify>
21 
22 #include <string>
23 #include <vector>
24 
25 namespace osg {
26 
27 // forward declare
28 class State;
29 class UserDataContainer;
30 class Node;
31 class NodeVisitor;
32 class StateAttribute;
33 class Uniform;
34 
35 #define _ADDQUOTES(def) #def
36 #define ADDQUOTES(def) _ADDQUOTES(def)
37 
42 #define META_Object(library,name) \
43  virtual osg::Object* cloneType() const { return new name (); } \
44  virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
45  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
46  virtual const char* libraryName() const { return #library; }\
47  virtual const char* className() const { return #name; }
48 
50 #define OSG_INIT_SINGLETON_PROXY(ProxyName, Func) static struct ProxyName{ ProxyName() { Func; } } s_##ProxyName;
51 
57 {
58  public:
59 
60 
65  inline Object():Referenced(),_dataVariance(UNSPECIFIED), _userDataContainer(0) {}
66 
67  inline explicit Object(bool threadSafeRefUnref):Referenced(threadSafeRefUnref),_dataVariance(UNSPECIFIED),_userDataContainer(0) {}
68 
71  Object(const Object&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
72 
75  virtual Object* cloneType() const = 0;
76 
79  virtual Object* clone(const CopyOp&) const = 0;
80 
81  virtual bool isSameKindAs(const Object*) const { return true; }
82 
83 
87  virtual const char* libraryName() const = 0;
88 
91  virtual const char* className() const = 0;
92 
94  std::string getCompoundClassName() const { return std::string(libraryName()) + std::string("::") + std::string(className()); }
95 
96 
99  virtual Node* asNode() { return 0; }
100 
103  virtual const Node* asNode() const { return 0; }
104 
107  virtual NodeVisitor* asNodeVisitor() { return 0; }
108 
111  virtual const NodeVisitor* asNodeVisitor() const { return 0; }
112 
115  virtual StateAttribute* asStateAttribute() { return 0; }
116 
119  virtual const StateAttribute* asStateAttribute() const { return 0; }
120 
123  virtual Uniform* asUniform() { return 0; }
124 
127  virtual const Uniform* asUniform() const { return 0; }
128 
129 
131  virtual void setThreadSafeRefUnref(bool threadSafe);
132 
134  virtual void setName( const std::string& name ) { _name = name; }
135 
137  inline void setName( const char* name )
138  {
139  if (name) setName(std::string(name));
140  else setName(std::string());
141  }
142 
144  inline const std::string& getName() const { return _name; }
145 
146 
148  {
151  UNSPECIFIED
152  };
153 
159  inline void setDataVariance(DataVariance dv) { _dataVariance = dv; }
160 
162  inline DataVariance getDataVariance() const { return _dataVariance; }
163 
165  virtual void computeDataVariance() {}
166 
167 
169  void setUserDataContainer(osg::UserDataContainer* udc);
170 
172  osg::UserDataContainer* getUserDataContainer() { return _userDataContainer; }
173 
175  const osg::UserDataContainer* getUserDataContainer() const { return _userDataContainer; }
176 
179  osg::UserDataContainer* getOrCreateUserDataContainer();
180 
181 
188  virtual void setUserData(Referenced* obj);
189 
191  virtual Referenced* getUserData();
192 
194  virtual const Referenced* getUserData() const;
195 
196 
197 
200  template<typename T>
201  bool getUserValue(const std::string& name, T& value) const;
202 
206  template<typename T>
207  void setUserValue(const std::string& name, const T& value);
208 
209 
211  virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
212 
216  virtual void releaseGLObjects(osg::State* = 0) const {}
217 
218 
219  protected:
220 
228  virtual ~Object();
229 
230  std::string _name;
232 
234 
235  private:
236 
238  Object& operator = (const Object&) { return *this; }
239 };
240 
241 template<typename T>
242 T* clone(const T* t, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)
243 {
244  if (t)
245  {
246  osg::ref_ptr<osg::Object> obj = t->clone(copyop);
247 
248  T* ptr = dynamic_cast<T*>(obj.get());
249  if (ptr)
250  {
251  obj.release();
252  return ptr;
253  }
254  else
255  {
256  OSG_WARN<<"Warning: osg::clone(const T*, osg::CopyOp&) cloned object not of type T, returning NULL."<<std::endl;
257  return 0;
258  }
259  }
260  else
261  {
262  OSG_WARN<<"Warning: osg::clone(const T*, osg::CopyOp&) passed null object to clone, returning NULL."<<std::endl;
263  return 0;
264  }
265 }
266 
267 template<typename T>
268 T* clone(const T* t, const std::string& name, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)
269 {
270  T* newObject = osg::clone(t, copyop);
271  if (newObject)
272  {
273  newObject->setName(name);
274  return newObject;
275  }
276  else
277  {
278  OSG_WARN<<"Warning: osg::clone(const T*, const std::string&, const osg::CopyOp) passed null object to clone, returning NULL."<<std::endl;
279  return 0;
280  }
281 }
282 
283 template<typename T>
284 T* cloneType(const T* t)
285 {
286  if (t)
287  {
289 
290  T* ptr = dynamic_cast<T*>(obj.get());
291  if (ptr)
292  {
293  obj.release();
294  return ptr;
295  }
296  else
297  {
298  OSG_WARN<<"Warning: osg::cloneType(const T*) cloned object not of type T, returning NULL."<<std::endl;
299  return 0;
300  }
301  }
302  else
303  {
304  OSG_WARN<<"Warning: osg::cloneType(const T*) passed null object to clone, returning NULL."<<std::endl;
305  return 0;
306  }
307 }
308 
310 class DummyObject : public osg::Object
311 {
312 public:
316 protected:
317  virtual ~DummyObject() {}
318 };
319 
320 
321 
322 }
323 
324 #endif
bool getUserValue(const osg::NodePath &nodepath, const std::string &name, T &value)
#define OSG_EXPORT
Definition: Export.h:43
osg::UserDataContainer * _userDataContainer
Definition: Object.h:233
virtual void releaseGLObjects(osg::State *=0) const
Definition: Object.h:216
virtual Node * asNode()
Definition: Object.h:99
#define OSG_WARN
Definition: Notify.h:85
virtual void resizeGLObjectBuffers(unsigned int)
Definition: Object.h:211
virtual const Node * asNode() const
Definition: Object.h:103
virtual Object * cloneType() const =0
T * clone(const T *t, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Definition: Object.h:242
void setDataVariance(DataVariance dv)
Definition: Object.h:159
std::string _name
Definition: Object.h:230
DataVariance getDataVariance() const
Definition: Object.h:162
virtual const Uniform * asUniform() const
Definition: Object.h:127
T * release()
Definition: ref_ptr.h:101
const osg::UserDataContainer * getUserDataContainer() const
Definition: Object.h:175
virtual const StateAttribute * asStateAttribute() const
Definition: Object.h:119
Object()
Definition: Object.h:65
const std::string & getName() const
Definition: Object.h:144
virtual void setName(const std::string &name)
Definition: Object.h:134
T * cloneType(const T *t)
Definition: Object.h:284
virtual Object * clone(const CopyOp &) const =0
T * get() const
Definition: ref_ptr.h:92
virtual NodeVisitor * asNodeVisitor()
Definition: Object.h:107
std::string getCompoundClassName() const
Definition: Object.h:94
virtual Uniform * asUniform()
Definition: Object.h:123
void setName(const char *name)
Definition: Object.h:137
virtual const NodeVisitor * asNodeVisitor() const
Definition: Object.h:111
DataVariance _dataVariance
Definition: Object.h:231
osg::UserDataContainer * getUserDataContainer()
Definition: Object.h:172
Definition: Node.h:71
META_Object(osg, DummyObject) protected
Definition: Object.h:315
Definition: AlphaFunc.h:19
DummyObject(const DummyObject &, const osg::CopyOp &)
Definition: Object.h:314
virtual void computeDataVariance()
Definition: Object.h:165
virtual StateAttribute * asStateAttribute()
Definition: Object.h:115
virtual bool isSameKindAs(const Object *) const
Definition: Object.h:81
Object(bool threadSafeRefUnref)
Definition: Object.h:67