Object 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. #ifndef OSG_OBJECT
  14. #define OSG_OBJECT 1
  15. #include <osg/Referenced>
  16. #include <osg/CopyOp>
  17. #include <osg/ref_ptr>
  18. #include <osg/Notify>
  19. #include <string>
  20. #include <vector>
  21. namespace osg {
  22. // forward declare
  23. class UserDataContainer;
  24. class Node;
  25. class NodeVisitor;
  26. class StateAttribute;
  27. class Uniform;
  28. class Drawable;
  29. class Camera;
  30. class Callback;
  31. class CallbackObject;
  32. class ValueObject;
  33. #define _ADDQUOTES(def) #def
  34. #define ADDQUOTES(def) _ADDQUOTES(def)
  35. /** META_Object macro define the standard clone, isSameKindAs and className methods.
  36. * Use when subclassing from Object to make it more convenient to define
  37. * the standard pure virtual clone, isSameKindAs and className methods
  38. * which are required for all Object subclasses.*/
  39. #define META_Object(library,name) \
  40. virtual osg::Object* cloneType() const { return new name (); } \
  41. virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
  42. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
  43. virtual const char* libraryName() const { return #library; }\
  44. virtual const char* className() const { return #name; }
  45. /** Helper macro that creates a static proxy object to call singleton function on it's construction, ensuring that the singleton gets initialized at startup.*/
  46. #define OSG_INIT_SINGLETON_PROXY(ProxyName, Func) static struct ProxyName{ ProxyName() { Func; } } s_##ProxyName;
  47. /** Base class/standard interface for objects which require IO support,
  48. cloning and reference counting.
  49. Based on GOF Composite, Prototype and Template Method patterns.
  50. */
  51. class OSG_EXPORT Object : public Referenced
  52. {
  53. public:
  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. inline explicit Object(bool threadSafeRefUnref):Referenced(threadSafeRefUnref),_dataVariance(UNSPECIFIED),_userDataContainer(0) {}
  60. /** Copy constructor, optional CopyOp object can be used to control
  61. * shallow vs deep copying of dynamic data.*/
  62. Object(const Object&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  63. /** Clone the type of an object, with Object* return type.
  64. Must be defined by derived classes.*/
  65. virtual Object* cloneType() const = 0;
  66. /** Clone an object, with Object* return type.
  67. Must be defined by derived classes.*/
  68. virtual Object* clone(const CopyOp&) const = 0;
  69. virtual bool isSameKindAs(const Object*) const { return true; }
  70. /** return the name of the object's library. Must be defined
  71. by derived classes. The OpenSceneGraph convention is that the
  72. namespace of a library is the same as the library name.*/
  73. virtual const char* libraryName() const = 0;
  74. /** return the name of the object's class type. Must be defined
  75. by derived classes.*/
  76. virtual const char* className() const = 0;
  77. /** return the compound class name that combines the library name and class name.*/
  78. std::string getCompoundClassName() const { return std::string(libraryName()) + std::string("::") + std::string(className()); }
  79. /** Convert 'this' into a Node pointer if Object is a Node, otherwise return 0.
  80. * Equivalent to dynamic_cast<Node*>(this).*/
  81. virtual Node* asNode() { return 0; }
  82. /** convert 'const this' into a const Node pointer if Object is a Node, otherwise return 0.
  83. * Equivalent to dynamic_cast<const Node*>(this).*/
  84. virtual const Node* asNode() const { return 0; }
  85. /** Convert 'this' into a NodeVisitor pointer if Object is a NodeVisitor, otherwise return 0.
  86. * Equivalent to dynamic_cast<NodeVisitor*>(this).*/
  87. virtual NodeVisitor* asNodeVisitor() { return 0; }
  88. /** convert 'const this' into a const NodeVisitor pointer if Object is a NodeVisitor, otherwise return 0.
  89. * Equivalent to dynamic_cast<const NodeVisitor*>(this).*/
  90. virtual const NodeVisitor* asNodeVisitor() const { return 0; }
  91. /** Convert 'this' into a StateSet pointer if Object is a StateSet, otherwise return 0.
  92. * Equivalent to dynamic_cast<StateSet*>(this).*/
  93. virtual StateSet* asStateSet() { return 0; }
  94. /** convert 'const this' into a const StateSet pointer if Object is a StateSet, otherwise return 0.
  95. * Equivalent to dynamic_cast<const StateSet*>(this).*/
  96. virtual const StateSet* asStateSet() const { return 0; }
  97. /** Convert 'this' into a StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
  98. * Equivalent to dynamic_cast<StateAttribute*>(this).*/
  99. virtual StateAttribute* asStateAttribute() { return 0; }
  100. /** convert 'const this' into a const StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
  101. * Equivalent to dynamic_cast<const StateAttribute*>(this).*/
  102. virtual const StateAttribute* asStateAttribute() const { return 0; }
  103. /** Convert 'this' into a Uniform pointer if Object is a Uniform, otherwise return 0.
  104. * Equivalent to dynamic_cast<Uniform*>(this).*/
  105. virtual Uniform* asUniform() { return 0; }
  106. /** convert 'const this' into a const Uniform pointer if Object is a Uniform, otherwise return 0.
  107. * Equivalent to dynamic_cast<const Uniform*>(this).*/
  108. virtual const Uniform* asUniform() const { return 0; }
  109. /** Convert 'this' into a Camera pointer if Node is a Camera, otherwise return 0.
  110. * Equivalent to dynamic_cast<Camera*>(this).*/
  111. virtual Camera* asCamera() { return 0; }
  112. /** convert 'const this' into a const Camera pointer if Node is a Camera, otherwise return 0.
  113. * Equivalent to dynamic_cast<const Camera*>(this).*/
  114. virtual const Camera* asCamera() const { return 0; }
  115. /** Convert 'this' into a Drawable pointer if Object is a Drawable, otherwise return 0.
  116. * Equivalent to dynamic_cast<Drawable*>(this).*/
  117. virtual Drawable* asDrawable() { return 0; }
  118. /** convert 'const this' into a const Drawable pointer if Object is a Drawable, otherwise return 0.
  119. * Equivalent to dynamic_cast<const Drawable*>(this).*/
  120. virtual const Drawable* asDrawable() const { return 0; }
  121. /** Convert 'this' into a Callback pointer if Object is a Callback, otherwise return 0.
  122. * Equivalent to dynamic_cast<Callback*>(this).*/
  123. virtual Callback* asCallback() { return 0; }
  124. /** convert 'const this' into a const Callback pointer if Object is a Callback, otherwise return 0.
  125. * Equivalent to dynamic_cast<const Callback*>(this).*/
  126. virtual const Callback* asCallback() const { return 0; }
  127. /** Convert 'this' into a CallbackObject pointer if Object is a CallbackObject, otherwise return 0.
  128. * Equivalent to dynamic_cast<CallbackObject*>(this).*/
  129. virtual CallbackObject* asCallbackObject() { return 0; }
  130. /** convert 'const this' into a const CallbackObject pointer if Object is a CallbackObject, otherwise return 0.
  131. * Equivalent to dynamic_cast<const CallbackObject*>(this).*/
  132. virtual const CallbackObject* asCallbackObject() const { return 0; }
  133. /** Convert 'this' into a UserDataContainer pointer if Object is a UserDataContainer, otherwise return 0.
  134. * Equivalent to dynamic_cast<UserDataContainer*>(this).*/
  135. virtual UserDataContainer* asUserDataContainer() { return 0; }
  136. /** convert 'const this' into a const UserDataContainer pointer if Object is a UserDataContainer, otherwise return 0.
  137. * Equivalent to dynamic_cast<const UserDataContainer*>(this).*/
  138. virtual const UserDataContainer* asUserDataContainer() const { return 0; }
  139. /** Convert 'this' into a ValueObject pointer if Object is a ValueObject, otherwise return 0.
  140. * Equivalent to dynamic_cast<ValueObject*>(this).*/
  141. virtual ValueObject* asValueObject() { return 0; }
  142. /** Convert 'this' into a ValueObject pointer if Object is a ValueObject, otherwise return 0.
  143. * Equivalent to dynamic_cast<ValueObject*>(this).*/
  144. virtual const ValueObject* asValueObject() const { return 0; }
  145. /** Convert 'this' into a Image pointer if Object is a Image, otherwise return 0.
  146. * Equivalent to dynamic_cast<Image*>(this).*/
  147. virtual Image* asImage() { return 0; }
  148. /** Convert 'this' into a Image pointer if Object is a Image, otherwise return 0.
  149. * Equivalent to dynamic_cast<Image*>(this).*/
  150. virtual const Image* asImage() const { return 0; }
  151. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  152. virtual void setThreadSafeRefUnref(bool threadSafe);
  153. /** Set the name of object using C++ style string.*/
  154. virtual void setName( const std::string& name ) { _name = name; }
  155. /** Set the name of object using a C style string.*/
  156. inline void setName( const char* name )
  157. {
  158. if (name) setName(std::string(name));
  159. else setName(std::string());
  160. }
  161. /** Get the name of object.*/
  162. inline const std::string& getName() const { return _name; }
  163. enum DataVariance
  164. {
  165. DYNAMIC,
  166. STATIC,
  167. UNSPECIFIED
  168. };
  169. /** Set the data variance of this object.
  170. * Can be set to either STATIC for values that do not change over the lifetime of the object,
  171. * or DYNAMIC for values that vary over the lifetime of the object. The DataVariance value
  172. * can be used by routines such as optimization codes that wish to share static data.
  173. * UNSPECIFIED is used to specify that the DataVariance hasn't been set yet. */
  174. inline void setDataVariance(DataVariance dv) { _dataVariance = dv; }
  175. /** Get the data variance of this object.*/
  176. inline DataVariance getDataVariance() const { return _dataVariance; }
  177. /** Compute the DataVariance based on an assessment of callback etc.*/
  178. virtual void computeDataVariance() {}
  179. /** set the UserDataContainer object.*/
  180. void setUserDataContainer(osg::UserDataContainer* udc);
  181. template<class T> void setUserDataContainer(const ref_ptr<T>& udc) { setUserDataContainer(udc.get()); }
  182. /** get the UserDataContainer attached to this object.*/
  183. osg::UserDataContainer* getUserDataContainer() { return _userDataContainer; }
  184. /** get the const UserDataContainer attached to this object.*/
  185. const osg::UserDataContainer* getUserDataContainer() const { return _userDataContainer; }
  186. /** Convenience method that returns the UserDataContainer, and if one doesn't already exist creates and assigns
  187. * a DefaultUserDataContainer to the Object and then return this new UserDataContainer.*/
  188. osg::UserDataContainer* getOrCreateUserDataContainer();
  189. /**
  190. * Set user data, data must be subclassed from Referenced to allow
  191. * automatic memory handling. If your own data isn't directly
  192. * subclassed from Referenced then create an adapter object
  193. * which points to your own object and handles the memory addressing.
  194. */
  195. virtual void setUserData(Referenced* obj);
  196. template<class T> void setUserData(const ref_ptr<T>& ud) { setUserData(ud.get()); }
  197. /** Get user data.*/
  198. virtual Referenced* getUserData();
  199. /** Get const user data.*/
  200. virtual const Referenced* getUserData() const;
  201. /** Convenience method that casts the named UserObject to osg::TemplateValueObject<T> and gets the value.
  202. * To use this template method you need to include the osg/ValueObject header.*/
  203. template<typename T>
  204. bool getUserValue(const std::string& name, T& value) const;
  205. /** Convenience method that creates the osg::TemplateValueObject<T> to store the
  206. * specified value and adds it as a named UserObject.
  207. * To use this template method you need to include the osg/ValueObject header. */
  208. template<typename T>
  209. void setUserValue(const std::string& name, const T& value);
  210. /** Resize any per context GLObject buffers to specified size. */
  211. virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
  212. /** If State is non-zero, this function releases any associated OpenGL objects for
  213. * the specified graphics context. Otherwise, releases OpenGL objects
  214. * for all graphics contexts. */
  215. virtual void releaseGLObjects(osg::State* = 0) const {}
  216. protected:
  217. /** Object destructor. Note, is protected so that Objects cannot
  218. be deleted other than by being dereferenced and the reference
  219. count being zero (see osg::Referenced), preventing the deletion
  220. of nodes which are still in use. This also means that
  221. Nodes cannot be created on stack i.e Node node will not compile,
  222. forcing all nodes to be created on the heap i.e Node* node
  223. = new Node().*/
  224. virtual ~Object();
  225. std::string _name;
  226. DataVariance _dataVariance;
  227. osg::UserDataContainer* _userDataContainer;
  228. private:
  229. /** disallow any copy operator.*/
  230. Object& operator = (const Object&) { return *this; }
  231. };
  232. template<typename T>
  233. T* clone(const T* t, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)
  234. {
  235. if (t)
  236. {
  237. osg::ref_ptr<osg::Object> obj = t->clone(copyop);
  238. T* ptr = dynamic_cast<T*>(obj.get());
  239. if (ptr)
  240. {
  241. obj.release();
  242. return ptr;
  243. }
  244. else
  245. {
  246. OSG_WARN<<"Warning: osg::clone(const T*, osg::CopyOp&) cloned object not of type T, returning NULL."<<std::endl;
  247. return 0;
  248. }
  249. }
  250. else
  251. {
  252. OSG_WARN<<"Warning: osg::clone(const T*, osg::CopyOp&) passed null object to clone, returning NULL."<<std::endl;
  253. return 0;
  254. }
  255. }
  256. template<typename T>
  257. T* clone(const T* t, const std::string& name, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)
  258. {
  259. T* newObject = osg::clone(t, copyop);
  260. if (newObject)
  261. {
  262. newObject->setName(name);
  263. return newObject;
  264. }
  265. else
  266. {
  267. OSG_WARN<<"Warning: osg::clone(const T*, const std::string&, const osg::CopyOp) passed null object to clone, returning NULL."<<std::endl;
  268. return 0;
  269. }
  270. }
  271. template<typename T>
  272. T* cloneType(const T* t)
  273. {
  274. if (t)
  275. {
  276. osg::ref_ptr<osg::Object> obj = t->cloneType();
  277. T* ptr = dynamic_cast<T*>(obj.get());
  278. if (ptr)
  279. {
  280. obj.release();
  281. return ptr;
  282. }
  283. else
  284. {
  285. OSG_WARN<<"Warning: osg::cloneType(const T*) cloned object not of type T, returning NULL."<<std::endl;
  286. return 0;
  287. }
  288. }
  289. else
  290. {
  291. OSG_WARN<<"Warning: osg::cloneType(const T*) passed null object to clone, returning NULL."<<std::endl;
  292. return 0;
  293. }
  294. }
  295. /** DummyObject that can be used as placeholder but otherwise has no other functionality.*/
  296. class DummyObject : public osg::Object
  297. {
  298. public:
  299. DummyObject() {}
  300. DummyObject(const DummyObject& org, const CopyOp& copyop) :
  301. Object(org, copyop) {}
  302. META_Object(osg, DummyObject)
  303. protected:
  304. virtual ~DummyObject() {}
  305. };
  306. inline void resizeGLObjectBuffers(osg::Object* object, unsigned int maxSize) { if (object) object->resizeGLObjectBuffers(maxSize); }
  307. template<class T> void resizeGLObjectBuffers(const osg::ref_ptr<T>& object, unsigned int maxSize) { if (object.valid()) object->resizeGLObjectBuffers(maxSize); }
  308. inline void releaseGLObjects(osg::Object* object, osg::State* state = 0) { if (object) object->releaseGLObjects(state); }
  309. template<class T> void releaseGLObjects(const osg::ref_ptr<T>& object, osg::State* state = 0) { if (object.valid()) object->releaseGLObjects(state); }
  310. }
  311. #endif