OpenSceneGraph 3.6.5
Object
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
25namespace osg {
26
27// forward declare
28class UserDataContainer;
29class Node;
30class NodeVisitor;
31class StateAttribute;
32class Uniform;
33class Drawable;
34class Camera;
35class Callback;
36class CallbackObject;
37class ValueObject;
38
39#define _ADDQUOTES(def) #def
40#define ADDQUOTES(def) _ADDQUOTES(def)
41
46#define META_Object(library,name) \
47 virtual osg::Object* cloneType() const { return new name (); } \
48 virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
49 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
50 virtual const char* libraryName() const { return #library; }\
51 virtual const char* className() const { return #name; }
52
54#define OSG_INIT_SINGLETON_PROXY(ProxyName, Func) static struct ProxyName{ ProxyName() { Func; } } s_##ProxyName;
55
61{
62 public:
63
64
70
71 inline explicit Object(bool threadSafeRefUnref):Referenced(threadSafeRefUnref),_dataVariance(UNSPECIFIED),_userDataContainer(0) {}
72
76
79 virtual Object* cloneType() const = 0;
80
83 virtual Object* clone(const CopyOp&) const = 0;
84
85 virtual bool isSameKindAs(const Object*) const { return true; }
86
87
91 virtual const char* libraryName() const = 0;
92
95 virtual const char* className() const = 0;
96
98 std::string getCompoundClassName() const { return std::string(libraryName()) + std::string("::") + std::string(className()); }
99
100
103 virtual Node* asNode() { return 0; }
104
107 virtual const Node* asNode() const { return 0; }
108
111 virtual NodeVisitor* asNodeVisitor() { return 0; }
112
115 virtual const NodeVisitor* asNodeVisitor() const { return 0; }
116
119 virtual StateSet* asStateSet() { return 0; }
120
123 virtual const StateSet* asStateSet() const { return 0; }
124
127 virtual StateAttribute* asStateAttribute() { return 0; }
128
131 virtual const StateAttribute* asStateAttribute() const { return 0; }
132
135 virtual Uniform* asUniform() { return 0; }
136
139 virtual const Uniform* asUniform() const { return 0; }
140
143 virtual Camera* asCamera() { return 0; }
144
147 virtual const Camera* asCamera() const { return 0; }
148
151 virtual Drawable* asDrawable() { return 0; }
152
155 virtual const Drawable* asDrawable() const { return 0; }
156
159 virtual Callback* asCallback() { return 0; }
160
163 virtual const Callback* asCallback() const { return 0; }
164
167 virtual CallbackObject* asCallbackObject() { return 0; }
168
171 virtual const CallbackObject* asCallbackObject() const { return 0; }
172
175 virtual UserDataContainer* asUserDataContainer() { return 0; }
176
179 virtual const UserDataContainer* asUserDataContainer() const { return 0; }
180
183 virtual ValueObject* asValueObject() { return 0; }
184
187 virtual const ValueObject* asValueObject() const { return 0; }
188
191 virtual Image* asImage() { return 0; }
192
195 virtual const Image* asImage() const { return 0; }
196
197
198
200 virtual void setThreadSafeRefUnref(bool threadSafe);
201
203 virtual void setName( const std::string& name ) { _name = name; }
204
206 inline void setName( const char* name )
207 {
208 if (name) setName(std::string(name));
209 else setName(std::string());
210 }
211
213 inline const std::string& getName() const { return _name; }
214
215
222
229
231 inline DataVariance getDataVariance() const { return _dataVariance; }
232
234 virtual void computeDataVariance() {}
235
236
239
240 template<class T> void setUserDataContainer(const ref_ptr<T>& udc) { setUserDataContainer(udc.get()); }
241
244
247
251
252
259 virtual void setUserData(Referenced* obj);
260
261 template<class T> void setUserData(const ref_ptr<T>& ud) { setUserData(ud.get()); }
262
265
267 virtual const Referenced* getUserData() const;
268
269
270
273 template<typename T>
274 bool getUserValue(const std::string& name, T& value) const;
275
279 template<typename T>
280 void setUserValue(const std::string& name, const T& value);
281
282
284 virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
285
289 virtual void releaseGLObjects(osg::State* = 0) const {}
290
291
292 protected:
293
301 virtual ~Object();
302
303 std::string _name;
305
307
308 private:
309
311 Object& operator = (const Object&) { return *this; }
312};
313
314template<typename T>
315T* clone(const T* t, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)
316{
317 if (t)
318 {
319 osg::ref_ptr<osg::Object> obj = t->clone(copyop);
320
321 T* ptr = dynamic_cast<T*>(obj.get());
322 if (ptr)
323 {
324 obj.release();
325 return ptr;
326 }
327 else
328 {
329 OSG_WARN<<"Warning: osg::clone(const T*, osg::CopyOp&) cloned object not of type T, returning NULL."<<std::endl;
330 return 0;
331 }
332 }
333 else
334 {
335 OSG_WARN<<"Warning: osg::clone(const T*, osg::CopyOp&) passed null object to clone, returning NULL."<<std::endl;
336 return 0;
337 }
338}
339
340template<typename T>
341T* clone(const T* t, const std::string& name, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)
342{
343 T* newObject = osg::clone(t, copyop);
344 if (newObject)
345 {
346 newObject->setName(name);
347 return newObject;
348 }
349 else
350 {
351 OSG_WARN<<"Warning: osg::clone(const T*, const std::string&, const osg::CopyOp) passed null object to clone, returning NULL."<<std::endl;
352 return 0;
353 }
354}
355
356template<typename T>
357T* cloneType(const T* t)
358{
359 if (t)
360 {
361 osg::ref_ptr<osg::Object> obj = t->cloneType();
362
363 T* ptr = dynamic_cast<T*>(obj.get());
364 if (ptr)
365 {
366 obj.release();
367 return ptr;
368 }
369 else
370 {
371 OSG_WARN<<"Warning: osg::cloneType(const T*) cloned object not of type T, returning NULL."<<std::endl;
372 return 0;
373 }
374 }
375 else
376 {
377 OSG_WARN<<"Warning: osg::cloneType(const T*) passed null object to clone, returning NULL."<<std::endl;
378 return 0;
379 }
380}
381
384{
385public:
387 DummyObject(const DummyObject& org, const CopyOp& copyop) :
388 Object(org, copyop) {}
389
391protected:
392 virtual ~DummyObject() {}
393};
394
395
396inline void resizeGLObjectBuffers(osg::Object* object, unsigned int maxSize) { if (object) object->resizeGLObjectBuffers(maxSize); }
397
398template<class T> void resizeGLObjectBuffers(const osg::ref_ptr<T>& object, unsigned int maxSize) { if (object.valid()) object->resizeGLObjectBuffers(maxSize); }
399
400inline void releaseGLObjects(osg::Object* object, osg::State* state = 0) { if (object) object->releaseGLObjects(state); }
401
402template<class T> void releaseGLObjects(const osg::ref_ptr<T>& object, osg::State* state = 0) { if (object.valid()) object->releaseGLObjects(state); }
403
404}
405
406#endif
#define OSG_WARN
Definition Notify:85
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
void resizeGLObjectBuffers(osg::Object *object, unsigned int maxSize)
Definition Object:396
void releaseGLObjects(osg::Object *object, osg::State *state=0)
Definition Object:400
T * clone(const T *t, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Definition Object:315
T * cloneType(const T *t)
Definition Object:357
Definition Callback:34
Callback for attaching a script to a Node's via there UserDataContainer for the purpose of overriding...
Definition Callback:134
Camera - is a subclass of Transform which represents encapsulates the settings of a Camera.
Definition Camera:45
Copy Op(erator) used to control whether shallow or deep copy is used during copy construction and clo...
Definition CopyOp:41
@ SHALLOW_COPY
Definition CopyOp:47
Pure virtual base class for drawable geometry.
Definition Drawable:89
Image class for encapsulating the storage texture image data.
Definition Image:179
Base class for all internal nodes in the scene graph.
Definition Node:72
Visitor for type safe operations on osg::Nodes.
Definition NodeVisitor:82
Base class/standard interface for objects which require IO support, cloning and reference counting.
Definition Object:61
void setUserData(const ref_ptr< T > &ud)
Definition Object:261
virtual const char * libraryName() const =0
return the name of the object's library.
void setUserDataContainer(osg::UserDataContainer *udc)
set the UserDataContainer object.
void setDataVariance(DataVariance dv)
Set the data variance of this object.
Definition Object:228
void setName(const char *name)
Set the name of object using a C style string.
Definition Object:206
void setUserValue(const std::string &name, const T &value)
Convenience method that creates the osg::TemplateValueObject<T> to store the specified value and adds...
Definition ValueObject:377
virtual const UserDataContainer * asUserDataContainer() const
convert 'const this' into a const UserDataContainer pointer if Object is a UserDataContainer,...
Definition Object:179
virtual NodeVisitor * asNodeVisitor()
Convert 'this' into a NodeVisitor pointer if Object is a NodeVisitor, otherwise return 0.
Definition Object:111
virtual void setName(const std::string &name)
Set the name of object using C++ style string.
Definition Object:203
virtual Drawable * asDrawable()
Convert 'this' into a Drawable pointer if Object is a Drawable, otherwise return 0.
Definition Object:151
virtual const StateAttribute * asStateAttribute() const
convert 'const this' into a const StateAttribute pointer if Object is a StateAttribute,...
Definition Object:131
virtual UserDataContainer * asUserDataContainer()
Convert 'this' into a UserDataContainer pointer if Object is a UserDataContainer, otherwise return 0.
Definition Object:175
virtual ValueObject * asValueObject()
Convert 'this' into a ValueObject pointer if Object is a ValueObject, otherwise return 0.
Definition Object:183
virtual const ValueObject * asValueObject() const
Convert 'this' into a ValueObject pointer if Object is a ValueObject, otherwise return 0.
Definition Object:187
virtual const Image * asImage() const
Convert 'this' into a Image pointer if Object is a Image, otherwise return 0.
Definition Object:195
virtual ~Object()
Object destructor.
virtual Object * clone(const CopyOp &) const =0
Clone an object, with Object* return type.
virtual bool isSameKindAs(const Object *) const
Definition Object:85
Object()
Construct an object.
Definition Object:69
virtual Object * cloneType() const =0
Clone the type of an object, with Object* return type.
Object(bool threadSafeRefUnref)
Definition Object:71
virtual const Uniform * asUniform() const
convert 'const this' into a const Uniform pointer if Object is a Uniform, otherwise return 0.
Definition Object:139
virtual CallbackObject * asCallbackObject()
Convert 'this' into a CallbackObject pointer if Object is a CallbackObject, otherwise return 0.
Definition Object:167
virtual const Camera * asCamera() const
convert 'const this' into a const Camera pointer if Node is a Camera, otherwise return 0.
Definition Object:147
virtual StateSet * asStateSet()
Convert 'this' into a StateSet pointer if Object is a StateSet, otherwise return 0.
Definition Object:119
DataVariance
Definition Object:217
@ DYNAMIC
Definition Object:218
@ STATIC
Definition Object:219
@ UNSPECIFIED
Definition Object:220
virtual const char * className() const =0
return the name of the object's class type.
virtual Referenced * getUserData()
Get user data.
virtual Node * asNode()
Convert 'this' into a Node pointer if Object is a Node, otherwise return 0.
Definition Object:103
osg::UserDataContainer * getUserDataContainer()
get the UserDataContainer attached to this object.
Definition Object:243
virtual StateAttribute * asStateAttribute()
Convert 'this' into a StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
Definition Object:127
void setUserDataContainer(const ref_ptr< T > &udc)
Definition Object:240
const osg::UserDataContainer * getUserDataContainer() const
get the const UserDataContainer attached to this object.
Definition Object:246
virtual void computeDataVariance()
Compute the DataVariance based on an assessment of callback etc.
Definition Object:234
virtual Callback * asCallback()
Convert 'this' into a Callback pointer if Object is a Callback, otherwise return 0.
Definition Object:159
virtual const Drawable * asDrawable() const
convert 'const this' into a const Drawable pointer if Object is a Drawable, otherwise return 0.
Definition Object:155
DataVariance _dataVariance
Definition Object:304
virtual const NodeVisitor * asNodeVisitor() const
convert 'const this' into a const NodeVisitor pointer if Object is a NodeVisitor, otherwise return 0.
Definition Object:115
virtual const Callback * asCallback() const
convert 'const this' into a const Callback pointer if Object is a Callback, otherwise return 0.
Definition Object:163
virtual void resizeGLObjectBuffers(unsigned int)
Resize any per context GLObject buffers to specified size.
Definition Object:284
virtual const Referenced * getUserData() const
Get const user data.
osg::UserDataContainer * _userDataContainer
Definition Object:306
virtual Image * asImage()
Convert 'this' into a Image pointer if Object is a Image, otherwise return 0.
Definition Object:191
virtual const Node * asNode() const
convert 'const this' into a const Node pointer if Object is a Node, otherwise return 0.
Definition Object:107
virtual Uniform * asUniform()
Convert 'this' into a Uniform pointer if Object is a Uniform, otherwise return 0.
Definition Object:135
std::string getCompoundClassName() const
return the compound class name that combines the library name and class name.
Definition Object:98
virtual Camera * asCamera()
Convert 'this' into a Camera pointer if Node is a Camera, otherwise return 0.
Definition Object:143
virtual const CallbackObject * asCallbackObject() const
convert 'const this' into a const CallbackObject pointer if Object is a CallbackObject,...
Definition Object:171
const std::string & getName() const
Get the name of object.
Definition Object:213
virtual void setThreadSafeRefUnref(bool threadSafe)
Set whether to use a mutex to ensure ref() and unref() are thread safe.
Object(const Object &, const CopyOp &copyop=CopyOp::SHALLOW_COPY)
Copy constructor, optional CopyOp object can be used to control shallow vs deep copying of dynamic da...
virtual const StateSet * asStateSet() const
convert 'const this' into a const StateSet pointer if Object is a StateSet, otherwise return 0.
Definition Object:123
virtual void releaseGLObjects(osg::State *=0) const
If State is non-zero, this function releases any associated OpenGL objects for the specified graphics...
Definition Object:289
bool getUserValue(const std::string &name, T &value) const
Convenience method that casts the named UserObject to osg::TemplateValueObject<T> and gets the value.
osg::UserDataContainer * getOrCreateUserDataContainer()
Convenience method that returns the UserDataContainer, and if one doesn't already exist creates and a...
std::string _name
Definition Object:303
virtual void setUserData(Referenced *obj)
Set user data, data must be subclassed from Referenced to allow automatic memory handling.
DataVariance getDataVariance() const
Get the data variance of this object.
Definition Object:231
DummyObject()
Definition Object:386
META_Object(osg, DummyObject) protected
Definition Object:390
DummyObject(const DummyObject &org, const CopyOp &copyop)
Definition Object:387
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
T * get() const
Definition ref_ptr:117
T * release()
release the pointer from ownership by this ref_ptr<>, decrementing the objects refencedCount() via un...
Definition ref_ptr:126
Encapsulates the current applied OpenGL modes, attributes and vertex arrays settings,...
Definition State:80
Base class for state attributes.
Definition StateAttribute:77
Stores a set of modes and attributes which represent a set of OpenGL state.
Definition StateSet:46
Uniform encapsulates glUniform values.
Definition Uniform:414
Internal structure for storing all user data.
Definition UserDataContainer:26
Definition ValueObject:67
#define OSG_EXPORT
Definition Export:39

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.