| 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 | |
|---|
| 31 | #define _ADDQUOTES(def) #def |
|---|
| 32 | #define ADDQUOTES(def) _ADDQUOTES(def) |
|---|
| 33 | |
|---|
| 34 | /** META_Object macro define the standard clone, isSameKindAs and className methods. |
|---|
| 35 | * Use when subclassing from Object to make it more convenient to define |
|---|
| 36 | * the standard pure virtual clone, isSameKindAs and className methods |
|---|
| 37 | * which are required for all Object subclasses.*/ |
|---|
| 38 | #define META_Object(library,name) \ |
|---|
| 39 | virtual osg::Object* cloneType() const { return new name (); } \ |
|---|
| 40 | virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \ |
|---|
| 41 | virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \ |
|---|
| 42 | virtual const char* libraryName() const { return #library; }\ |
|---|
| 43 | virtual const char* className() const { return #name; } |
|---|
| 44 | |
|---|
| 45 | /** Base class/standard interface for objects which require IO support, |
|---|
| 46 | cloning and reference counting. |
|---|
| 47 | Based on GOF Composite, Prototype and Template Method patterns. |
|---|
| 48 | */ |
|---|
| 49 | class OSG_EXPORT Object : public Referenced |
|---|
| 50 | { |
|---|
| 51 | public: |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /** Construct an object. Note Object is a pure virtual base class |
|---|
| 55 | and therefore cannot be constructed on its own, only derived |
|---|
| 56 | classes which override the clone and className methods are |
|---|
| 57 | concrete classes and can be constructed.*/ |
|---|
| 58 | inline Object():Referenced(),_dataVariance(UNSPECIFIED), _userDataContainer(0) {} |
|---|
| 59 | |
|---|
| 60 | inline explicit Object(bool threadSafeRefUnref):Referenced(threadSafeRefUnref),_dataVariance(UNSPECIFIED),_userDataContainer(0) {} |
|---|
| 61 | |
|---|
| 62 | /** Copy constructor, optional CopyOp object can be used to control |
|---|
| 63 | * shallow vs deep copying of dynamic data.*/ |
|---|
| 64 | Object(const Object&,const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
|---|
| 65 | |
|---|
| 66 | /** Clone the type of an object, with Object* return type. |
|---|
| 67 | Must be defined by derived classes.*/ |
|---|
| 68 | virtual Object* cloneType() const = 0; |
|---|
| 69 | |
|---|
| 70 | /** Clone an object, with Object* return type. |
|---|
| 71 | Must be defined by derived classes.*/ |
|---|
| 72 | virtual Object* clone(const CopyOp&) const = 0; |
|---|
| 73 | |
|---|
| 74 | virtual bool isSameKindAs(const Object*) const { return true; } |
|---|
| 75 | |
|---|
| 76 | /** return the name of the object's library. Must be defined |
|---|
| 77 | by derived classes. The OpenSceneGraph convention is that the |
|---|
| 78 | namespace of a library is the same as the library name.*/ |
|---|
| 79 | virtual const char* libraryName() const = 0; |
|---|
| 80 | |
|---|
| 81 | /** return the name of the object's class type. Must be defined |
|---|
| 82 | by derived classes.*/ |
|---|
| 83 | virtual const char* className() const = 0; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
|---|
| 87 | virtual void setThreadSafeRefUnref(bool threadSafe); |
|---|
| 88 | |
|---|
| 89 | /** Set the name of object using C++ style string.*/ |
|---|
| 90 | virtual void setName( const std::string& name ) { _name = name; } |
|---|
| 91 | |
|---|
| 92 | /** Set the name of object using a C style string.*/ |
|---|
| 93 | inline void setName( const char* name ) |
|---|
| 94 | { |
|---|
| 95 | if (name) setName(std::string(name)); |
|---|
| 96 | else setName(std::string()); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** Get the name of object.*/ |
|---|
| 100 | inline const std::string& getName() const { return _name; } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | enum DataVariance |
|---|
| 104 | { |
|---|
| 105 | DYNAMIC, |
|---|
| 106 | STATIC, |
|---|
| 107 | UNSPECIFIED |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | /** Set the data variance of this object. |
|---|
| 111 | * Can be set to either STATIC for values that do not change over the lifetime of the object, |
|---|
| 112 | * or DYNAMIC for values that vary over the lifetime of the object. The DataVariance value |
|---|
| 113 | * can be used by routines such as optimization codes that wish to share static data. |
|---|
| 114 | * UNSPECIFIED is used to specify that the DataVariance hasn't been set yet. */ |
|---|
| 115 | inline void setDataVariance(DataVariance dv) { _dataVariance = dv; } |
|---|
| 116 | |
|---|
| 117 | /** Get the data variance of this object.*/ |
|---|
| 118 | inline DataVariance getDataVariance() const { return _dataVariance; } |
|---|
| 119 | |
|---|
| 120 | /** Compute the DataVariance based on an assessment of callback etc.*/ |
|---|
| 121 | virtual void computeDataVariance() {} |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | /** set the UserDataContainer object.*/ |
|---|
| 125 | void setUserDataContainer(osg::UserDataContainer* udc); |
|---|
| 126 | |
|---|
| 127 | /** get the UserDataContainer attached to this object.*/ |
|---|
| 128 | osg::UserDataContainer* getUserDataContainer() { return _userDataContainer; } |
|---|
| 129 | |
|---|
| 130 | /** get the const UserDataContainer attached to this object.*/ |
|---|
| 131 | const osg::UserDataContainer* getUserDataContainer() const { return _userDataContainer; } |
|---|
| 132 | |
|---|
| 133 | /** Convinience method that returns the UserDataContainer, and if one doesn't already exist creates and assigns |
|---|
| 134 | * a DefaultUserDataContainer to the Object and then return this new UserDataContainer.*/ |
|---|
| 135 | osg::UserDataContainer* getOrCreateUserDataContainer(); |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * Set user data, data must be subclassed from Referenced to allow |
|---|
| 140 | * automatic memory handling. If your own data isn't directly |
|---|
| 141 | * subclassed from Referenced then create an adapter object |
|---|
| 142 | * which points to your own object and handles the memory addressing. |
|---|
| 143 | */ |
|---|
| 144 | virtual void setUserData(Referenced* obj); |
|---|
| 145 | |
|---|
| 146 | /** Get user data.*/ |
|---|
| 147 | virtual Referenced* getUserData(); |
|---|
| 148 | |
|---|
| 149 | /** Get const user data.*/ |
|---|
| 150 | virtual const Referenced* getUserData() const; |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /** Convinience method that casts the named UserObject to osg::TemplateValueObject<T> and gets the value. |
|---|
| 155 | * To use this template method you need to include the osg/ValueObject header.*/ |
|---|
| 156 | template<typename T> |
|---|
| 157 | bool getUserValue(const std::string& name, T& value) const; |
|---|
| 158 | |
|---|
| 159 | /** Convinience method that creates the osg::TemplateValueObject<T> to store the |
|---|
| 160 | * specified value and adds it as a named UserObject. |
|---|
| 161 | * To use this template method you need to include the osg/ValueObject header. */ |
|---|
| 162 | template<typename T> |
|---|
| 163 | void setUserValue(const std::string& name, const T& value); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 167 | virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {} |
|---|
| 168 | |
|---|
| 169 | /** If State is non-zero, this function releases any associated OpenGL objects for |
|---|
| 170 | * the specified graphics context. Otherwise, releases OpenGL objects |
|---|
| 171 | * for all graphics contexts. */ |
|---|
| 172 | virtual void releaseGLObjects(osg::State* = 0) const {} |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | protected: |
|---|
| 176 | |
|---|
| 177 | /** Object destructor. Note, is protected so that Objects cannot |
|---|
| 178 | be deleted other than by being dereferenced and the reference |
|---|
| 179 | count being zero (see osg::Referenced), preventing the deletion |
|---|
| 180 | of nodes which are still in use. This also means that |
|---|
| 181 | Nodes cannot be created on stack i.e Node node will not compile, |
|---|
| 182 | forcing all nodes to be created on the heap i.e Node* node |
|---|
| 183 | = new Node().*/ |
|---|
| 184 | virtual ~Object(); |
|---|
| 185 | |
|---|
| 186 | std::string _name; |
|---|
| 187 | DataVariance _dataVariance; |
|---|
| 188 | |
|---|
| 189 | osg::UserDataContainer* _userDataContainer; |
|---|
| 190 | |
|---|
| 191 | private: |
|---|
| 192 | |
|---|
| 193 | /** disallow any copy operator.*/ |
|---|
| 194 | Object& operator = (const Object&) { return *this; } |
|---|
| 195 | }; |
|---|
| 196 | |
|---|
| 197 | template<typename T> |
|---|
| 198 | T* clone(const T* t, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) |
|---|
| 199 | { |
|---|
| 200 | if (t) |
|---|
| 201 | { |
|---|
| 202 | osg::ref_ptr<osg::Object> obj = t->clone(copyop); |
|---|
| 203 | |
|---|
| 204 | T* ptr = dynamic_cast<T*>(obj.get()); |
|---|
| 205 | if (ptr) |
|---|
| 206 | { |
|---|
| 207 | obj.release(); |
|---|
| 208 | return ptr; |
|---|
| 209 | } |
|---|
| 210 | else |
|---|
| 211 | { |
|---|
| 212 | OSG_WARN<<"Warning: osg::clone(const T*, osg::CopyOp&) cloned object not of type T, returning NULL."<<std::endl; |
|---|
| 213 | return 0; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | else |
|---|
| 217 | { |
|---|
| 218 | OSG_WARN<<"Warning: osg::clone(const T*, osg::CopyOp&) passed null object to clone, returning NULL."<<std::endl; |
|---|
| 219 | return 0; |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | template<typename T> |
|---|
| 224 | T* clone(const T* t, const std::string& name, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) |
|---|
| 225 | { |
|---|
| 226 | T* newObject = osg::clone(t, copyop); |
|---|
| 227 | if (newObject) |
|---|
| 228 | { |
|---|
| 229 | newObject->setName(name); |
|---|
| 230 | return newObject; |
|---|
| 231 | } |
|---|
| 232 | else |
|---|
| 233 | { |
|---|
| 234 | OSG_WARN<<"Warning: osg::clone(const T*, const std::string&, const osg::CopyOp) passed null object to clone, returning NULL."<<std::endl; |
|---|
| 235 | return 0; |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | template<typename T> |
|---|
| 240 | T* cloneType(const T* t) |
|---|
| 241 | { |
|---|
| 242 | if (t) |
|---|
| 243 | { |
|---|
| 244 | osg::ref_ptr<osg::Object> obj = t->cloneType(); |
|---|
| 245 | |
|---|
| 246 | T* ptr = dynamic_cast<T*>(obj.get()); |
|---|
| 247 | if (ptr) |
|---|
| 248 | { |
|---|
| 249 | obj.release(); |
|---|
| 250 | return ptr; |
|---|
| 251 | } |
|---|
| 252 | else |
|---|
| 253 | { |
|---|
| 254 | OSG_WARN<<"Warning: osg::cloneType(const T*) cloned object not of type T, returning NULL."<<std::endl; |
|---|
| 255 | return 0; |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | else |
|---|
| 259 | { |
|---|
| 260 | OSG_WARN<<"Warning: osg::cloneType(const T*) passed null object to clone, returning NULL."<<std::endl; |
|---|
| 261 | return 0; |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | #endif |
|---|