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