UserDataContainer 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_USERDATACONTAINER
  14. #define OSG_USERDATACONTAINER 1
  15. #include <osg/Object>
  16. #include <string>
  17. #include <vector>
  18. namespace osg {
  19. /** Internal structure for storing all user data.*/
  20. class OSG_EXPORT UserDataContainer : public osg::Object
  21. {
  22. public:
  23. UserDataContainer();
  24. UserDataContainer(const UserDataContainer& udc, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  25. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const UserDataContainer*>(obj)!=0; }
  26. /** return the name of the object's library. Must be defined
  27. by derived classes. The OpenSceneGraph convention is that the
  28. namespace of a library is the same as the library name.*/
  29. virtual const char* libraryName() const { return "osg"; }
  30. /** return the name of the object's class type. Must be defined
  31. by derived classes.*/
  32. virtual const char* className() const { return "UserDataContainer"; }
  33. /** Convert 'this' into a UserDataContainer pointer if Object is a UserDataContainer, otherwise return 0.
  34. * Equivalent to dynamic_cast<UserDataContainer*>(this).*/
  35. virtual UserDataContainer* asUserDataContainer() { return this; }
  36. /** convert 'const this' into a const UserDataContainer pointer if Object is a UserDataContainer, otherwise return 0.
  37. * Equivalent to dynamic_cast<const UserDataContainer*>(this).*/
  38. virtual const UserDataContainer* asUserDataContainer() const { return this; }
  39. /**
  40. * Set user data, data must be subclassed from Referenced to allow
  41. * automatic memory handling. If your own data isn't directly
  42. * subclassed from Referenced then create an adapter object
  43. * which points to your own object and handles the memory addressing.
  44. */
  45. virtual void setUserData(Referenced* obj) = 0;
  46. using osg::Object::setUserData;
  47. /** Get user data.*/
  48. virtual Referenced* getUserData() = 0;
  49. /** Get const user data.*/
  50. virtual const Referenced* getUserData() const = 0;
  51. /** Add user data object. Returns the index position of object added. */
  52. virtual unsigned int addUserObject(Object* obj) = 0;
  53. template<class T> unsigned int addUserObject(const osg::ref_ptr<T>& obj) { return addUserObject(obj.get()); }
  54. /** Add element to list of user data objects.*/
  55. virtual void setUserObject(unsigned int i, Object* obj) = 0;
  56. /** Remove element from the list of user data objects.*/
  57. virtual void removeUserObject(unsigned int i) = 0;
  58. /** Get user data object as specified index position. */
  59. virtual Object* getUserObject(unsigned int i) = 0;
  60. /** Get const user data object as specified index position. */
  61. virtual const Object* getUserObject(unsigned int i) const = 0;
  62. /** Get number of user objects assigned to this object.*/
  63. virtual unsigned int getNumUserObjects() const = 0;
  64. /** Get the index position of specified user data object.*/
  65. virtual unsigned int getUserObjectIndex(const osg::Object* obj, unsigned int startPos=0) const = 0;
  66. /** Get the index position of first user data object that matches specified name.*/
  67. virtual unsigned int getUserObjectIndex(const std::string& name, unsigned int startPos=0) const = 0;
  68. /** Get first user data object with specified name. */
  69. virtual Object* getUserObject(const std::string& name, unsigned int startPos=0);
  70. /** Get first const user data object with specified name. */
  71. virtual const Object* getUserObject(const std::string& name, unsigned int startPos=0) const;
  72. typedef std::vector<std::string> DescriptionList;
  73. /** Set the list of string descriptions.*/
  74. virtual void setDescriptions(const DescriptionList& descriptions) = 0;
  75. /** Get the description list.*/
  76. virtual DescriptionList& getDescriptions() = 0;
  77. /** Get the const description list.*/
  78. virtual const DescriptionList& getDescriptions() const = 0;
  79. /** Get number of description strings.*/
  80. virtual unsigned int getNumDescriptions() const = 0;
  81. /** Add a description string.*/
  82. virtual void addDescription(const std::string& desc) = 0;
  83. protected:
  84. virtual ~UserDataContainer() {}
  85. };
  86. /** Internal structure for storing all user data.*/
  87. class OSG_EXPORT DefaultUserDataContainer : public osg::UserDataContainer
  88. {
  89. public:
  90. DefaultUserDataContainer();
  91. DefaultUserDataContainer(const DefaultUserDataContainer& udc, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  92. META_Object(osg, DefaultUserDataContainer)
  93. virtual void setThreadSafeRefUnref(bool threadSafe);
  94. /**
  95. * Set user data, data must be subclassed from Referenced to allow
  96. * automatic memory handling. If your own data isn't directly
  97. * subclassed from Referenced then create an adapter object
  98. * which points to your own object and handles the memory addressing.
  99. */
  100. virtual void setUserData(Referenced* obj);
  101. using osg::Object::setUserData;
  102. using osg::UserDataContainer::addUserObject;
  103. /** Get user data.*/
  104. virtual Referenced* getUserData();
  105. /** Get const user data.*/
  106. virtual const Referenced* getUserData() const;
  107. /** Add user data object. Returns the index position of object added. */
  108. virtual unsigned int addUserObject(Object* obj);
  109. /** Add element to list of user data objects.*/
  110. virtual void setUserObject(unsigned int i, Object* obj);
  111. /** Remove element from the list of user data objects.*/
  112. virtual void removeUserObject(unsigned int i);
  113. /** Get user data object as specified index position. */
  114. virtual Object* getUserObject(unsigned int i);
  115. /** Get const user data object as specified index position. */
  116. virtual const Object* getUserObject(unsigned int i) const;
  117. /** Get number of user objects assigned to this object.*/
  118. virtual unsigned int getNumUserObjects() const;
  119. /** Get the index position of specified user data object.*/
  120. virtual unsigned int getUserObjectIndex(const osg::Object* obj, unsigned int startPos=0) const;
  121. /** Get the index position of first user data object that matches specified name.*/
  122. virtual unsigned int getUserObjectIndex(const std::string& name, unsigned int startPos=0) const;
  123. /** Set the list of string descriptions.*/
  124. virtual void setDescriptions(const DescriptionList& descriptions);
  125. /** Get the description list.*/
  126. virtual DescriptionList& getDescriptions();
  127. /** Get the const description list.*/
  128. virtual const DescriptionList& getDescriptions() const;
  129. /** Get number of description strings.*/
  130. virtual unsigned int getNumDescriptions() const;
  131. /** Add a description string.*/
  132. virtual void addDescription(const std::string& desc);
  133. protected:
  134. virtual ~DefaultUserDataContainer() {}
  135. typedef std::vector< osg::ref_ptr<osg::Object> > ObjectList;
  136. ref_ptr<Referenced> _userData;
  137. DescriptionList _descriptionList;
  138. ObjectList _objectList;
  139. };
  140. /** Convenience function for getting the User Object associated with specified name from an Object's UserDataContainer.*/
  141. inline Object* getUserObject(osg::Object* object, const std::string& name)
  142. {
  143. osg::UserDataContainer* udc = object->getUserDataContainer();
  144. return udc ? udc->getUserObject(name) : 0;
  145. }
  146. /** Convenience function for getting the User Object associated with specified name from an Object's UserDataContainer.*/
  147. inline const Object* getUserObject(const osg::Object* object, const std::string& name)
  148. {
  149. const osg::UserDataContainer* udc = object->getUserDataContainer();
  150. return udc ? udc->getUserObject(name) : 0;
  151. }
  152. }
  153. #endif