OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ClassInterface.h
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 
15 #ifndef OSGDB_CLASSINTERFACE
16 #define OSGDB_CLASSINTERFACE 1
17 
18 #include <osgDB/ObjectWrapper>
19 #include <osgDB/Registry>
20 #include <osg/ValueObject>
21 
22 namespace osgDB
23 {
24 
25 template<typename T>
26 static osgDB::BaseSerializer::Type getTypeEnum()
27 {
29 }
30 
31 template<typename T>
32 static osgDB::BaseSerializer::Type getTypeEnumFrom(T)
33 {
34  return getTypeEnum<T>();
35 }
36 
37 template<typename T>
38 static const char* getTypeString()
39 {
40  return "UNDEFINED";
41 }
42 
43 template<typename T>
44 static const char* getTypeStringFrom(T)
45 {
46  return getTypeString<T>();
47 }
48 
50 extern OSGDB_EXPORT const char* getTypeStringFromPtr(const osg::Object*);
51 
53 extern OSGDB_EXPORT const char* getTypeStringFromPtr(const osg::Image*);
54 
55 
56 #define DECLARE_TYPE(A,B) \
57  template<> inline osgDB::BaseSerializer::Type getTypeEnum<A>() { return osgDB::BaseSerializer::RW_##B; } \
58  template<> inline const char* getTypeString<A>() { return #B; }
59 
60 DECLARE_TYPE(osg::Image*, IMAGE)
61 DECLARE_TYPE(osg::Object*, OBJECT)
62 
63 DECLARE_TYPE(bool, BOOL)
64 DECLARE_TYPE(char, CHAR)
65 DECLARE_TYPE(unsigned char, UCHAR)
66 DECLARE_TYPE(short, SHORT)
67 DECLARE_TYPE(unsigned short, USHORT)
68 DECLARE_TYPE(int, INT)
69 DECLARE_TYPE(unsigned int, UINT)
70 DECLARE_TYPE(float, FLOAT)
71 DECLARE_TYPE(double, DOUBLE)
72 
73 DECLARE_TYPE(osg::Vec2f, VEC2F)
74 DECLARE_TYPE(osg::Vec2d, VEC2D)
75 DECLARE_TYPE(osg::Vec3f, VEC3F)
76 DECLARE_TYPE(osg::Vec3d, VEC3D)
77 DECLARE_TYPE(osg::Vec4f, VEC4F)
78 DECLARE_TYPE(osg::Vec4d, VEC4D)
79 DECLARE_TYPE(osg::Quat, QUAT)
80 DECLARE_TYPE(osg::Plane, PLANE)
81 
82 DECLARE_TYPE(osg::Matrixf, MATRIXF)
83 DECLARE_TYPE(osg::Matrixd, MATRIXD)
84 DECLARE_TYPE(std::string, STRING)
85 
86 DECLARE_TYPE(osg::Vec2b, VEC2B)
87 DECLARE_TYPE(osg::Vec2ub, VEC2UB)
88 DECLARE_TYPE(osg::Vec2s, VEC2S)
89 DECLARE_TYPE(osg::Vec2us, VEC2US)
90 DECLARE_TYPE(osg::Vec2i, VEC2I)
91 DECLARE_TYPE(osg::Vec2ui, VEC2UI)
92 
93 DECLARE_TYPE(osg::Vec3b, VEC3B)
94 DECLARE_TYPE(osg::Vec3ub, VEC3UB)
95 DECLARE_TYPE(osg::Vec3s, VEC3S)
96 DECLARE_TYPE(osg::Vec3us, VEC3US)
97 DECLARE_TYPE(osg::Vec3i, VEC3I)
98 DECLARE_TYPE(osg::Vec3ui, VEC3UI)
99 
100 DECLARE_TYPE(osg::Vec4b, VEC4B)
101 DECLARE_TYPE(osg::Vec4ub, VEC4UB)
102 DECLARE_TYPE(osg::Vec4s, VEC4S)
103 DECLARE_TYPE(osg::Vec4us, VEC4US)
104 DECLARE_TYPE(osg::Vec4i, VEC4I)
105 DECLARE_TYPE(osg::Vec4ui, VEC4UI)
106 
107 DECLARE_TYPE(osg::BoundingBoxf, BOUNDINGBOXF)
108 DECLARE_TYPE(osg::BoundingBoxd, BOUNDINGBOXD)
109 
110 DECLARE_TYPE(osg::BoundingSpheref, BOUNDINGSPHEREF)
111 DECLARE_TYPE(osg::BoundingSphered, BOUNDINGSPHERED)
112 
113 // forward decalare
114 class PropertyOutputIterator;
115 class PropertyInputIterator;
116 
117 
122 {
123 public:
124  ClassInterface();
125 
126 
128  bool getPropertyType(const osg::Object* object, const std::string& propertyName, osgDB::BaseSerializer::Type& type) const;
129 
131  bool areTypesCompatible(osgDB::BaseSerializer::Type lhs, osgDB::BaseSerializer::Type rhs) const;
132 
133 
135  osg::Object* createObject(const std::string& compoundClassdName) const;
136 
138  template<typename T>
139  bool getProperty(const osg::Object* object, const std::string& propertyName, T& value);
140 
142  template<typename T>
143  bool setProperty(osg::Object* object, const std::string& propertyName, const T& value);
144 
145 
147  std::string getTypeName(osgDB::BaseSerializer::Type type) const;
148 
150  osgDB::BaseSerializer::Type getType(const std::string& typeName) const;
151 
152 
154  typedef std::map<std::string, osgDB::BaseSerializer::Type> PropertyMap;
155 
157  bool getSupportedProperties(const osg::Object* object, PropertyMap& properties, bool searchAssociates=true) const;
158 
160  bool isObjectOfType(const osg::Object* object, const std::string& compoundClassName) const;
161 
162 
164  bool run(void* objectPtr, const std::string& compoundClassName, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
165 
167  bool run(osg::Object* object, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
168 
170  bool hasMethod(const std::string& compoundClassName, const std::string& methodName) const;
171 
173  bool hasMethod(const osg::Object* object, const std::string& methodName) const;
174 
175 
177  typedef std::map<std::string, PropertyMap> ObjectPropertyMap;
178 
180  ObjectPropertyMap& getWhiteList() { return _whiteList; }
181 
183  const ObjectPropertyMap& getWhiteList() const { return _whiteList; }
184 
186  ObjectPropertyMap& getBlackList() { return _blackList; }
187 
189  const ObjectPropertyMap& getBlackList() const { return _blackList; }
190 
191  osgDB::ObjectWrapper* getObjectWrapper(const osg::Object* object) const;
192 
193  osgDB::BaseSerializer* getSerializer(const osg::Object* object, const std::string& propertyName, osgDB::BaseSerializer::Type& type) const;
194 
195 protected:
196 
197  bool copyPropertyDataFromObject(const osg::Object* object, const std::string& propertyName, void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType);
198 
199  bool copyPropertyDataToObject(osg::Object* object, const std::string& propertyName, const void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType);
200 
201  bool copyPropertyObjectFromObject(const osg::Object* object, const std::string& propertyName, void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType);
202 
203  bool copyPropertyObjectToObject(osg::Object* object, const std::string& propertyName, const void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType);
204 
205 
206 
208  PropertyOutputIterator* _poi;
209 
211  PropertyInputIterator* _pii;
212 
213  typedef std::map<std::string, osgDB::BaseSerializer::Type> TypeNameToTypeMap;
214  typedef std::map<osgDB::BaseSerializer::Type, std::string> TypeToTypeNameMap;
215 
216  TypeNameToTypeMap _typeNameToTypeMap;
217  TypeToTypeNameMap _typeToTypeNameMap;
218 
219  ObjectPropertyMap _whiteList;
220  ObjectPropertyMap _blackList;
221 };
222 
223 
224 template<typename T>
225 bool ClassInterface::getProperty(const osg::Object* object, const std::string& propertyName, T& value)
226 {
227  if (copyPropertyDataFromObject(object, propertyName, &value, sizeof(T), getTypeEnum<T>())) return true;
228  else return object->getUserValue(propertyName, value); // fallback to check user data for property
229 }
230 
231 template<typename T>
232 bool ClassInterface::setProperty(osg::Object* object, const std::string& propertyName, const T& value)
233 {
234  if (copyPropertyDataToObject(object, propertyName, &value, sizeof(T), getTypeEnum<T>())) return true;
235  else
236  {
237  // fallback to using user data to store property data
238  object->setUserValue(propertyName, value);
239  return false;
240  }
241 }
242 
244 
245 template<>
246 inline bool ClassInterface::getProperty(const osg::Object* object, const std::string& propertyName, ObjectPtr& value)
247 {
248  if (copyPropertyObjectFromObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum<ObjectPtr>())) return true;
249  else
250  {
251  OSG_INFO<<"ClassInterface::getProperty("<<propertyName<<", Checking UserDataContainer for object ptr"<<std::endl;
252  const osg::UserDataContainer* udc = object->getUserDataContainer();
253  if (udc)
254  {
255  OSG_INFO<<" Checking UserDataContainer for object ptr"<<std::endl;
256  const osg::Object* ptr = udc->getUserObject(propertyName);
257  if (ptr)
258  {
259  value = const_cast<ObjectPtr>(ptr);
260  return true;
261  }
262  }
263  return false;
264  }
265 }
266 
267 template<>
268 inline bool ClassInterface::setProperty(osg::Object* object, const std::string& propertyName, const ObjectPtr& value)
269 {
270  osgDB::BaseSerializer::Type type = dynamic_cast<osg::Image*>(value) ? osgDB::BaseSerializer::RW_IMAGE : getTypeEnum<ObjectPtr>();
271  // osgDB::BaseSerializer::Type type = getTypeEnum<ObjectPtr>();
272  if (copyPropertyObjectToObject(object, propertyName, &value, sizeof(ObjectPtr), type)) return true;
273  else
274  {
275  // fallback to using user data to store property data
277  unsigned int objectIndex = udc->getUserObjectIndex(propertyName);
278  if (objectIndex < udc->getNumUserObjects())
279  {
280  const osg::Object* outgoingObject = udc->getUserObject(objectIndex);
281  if (outgoingObject==value) return true;
282 
283  OSG_INFO<<"ClassInterface::setProperty("<<propertyName<<", "<<value->className()<<") replace object on UserDataContainer"<<std::endl;
284  value->setName(propertyName);
285  udc->setUserObject(objectIndex, value);
286  }
287  else
288  {
289  OSG_INFO<<"ClassInterface::setProperty("<<propertyName<<", "<<value->className()<<") Adding object to UserDataContainer"<<std::endl;
290  value->setName(propertyName);
291  udc->addUserObject(value);
292  }
293  return true;
294  }
295 }
296 
297 
298 }
299 
300 #endif
osgDB::InputStream _inputStream
osgDB::OutputStream _outputStream
virtual unsigned int getUserObjectIndex(const osg::Object *obj, unsigned int startPos=0) const =0
BoundingSphereImpl< Vec3d > BoundingSphered
BoundingSphereImpl< Vec3f > BoundingSpheref
OSGDB_EXPORT osgDB::BaseSerializer::Type getTypeEnumFromPtr(const osg::Object *)
PropertyOutputIterator * _poi
OSGDB_EXPORT const char * getTypeStringFromPtr(const osg::Object *)
bool copyPropertyDataToObject(osg::Object *object, const std::string &propertyName, const void *valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)
virtual unsigned int addUserObject(Object *obj)=0
BoundingBoxImpl< Vec3f > BoundingBoxf
Definition: BoundingBox.h:251
TypeToTypeNameMap _typeToTypeNameMap
#define OSG_INFO
Definition: Notify.h:87
const ObjectPropertyMap & getWhiteList() const
Get the const list of properties that are explicitly defined as supported.
std::map< std::string, osgDB::BaseSerializer::Type > TypeNameToTypeMap
std::vector< osg::ref_ptr< osg::Object > > Parameters
Definition: Callback.h:92
std::map< std::string, PropertyMap > ObjectPropertyMap
Properties supported for a range of classes, used for white and black lists.
virtual Object * getUserObject(unsigned int i)=0
std::map< std::string, osgDB::BaseSerializer::Type > PropertyMap
Properties supported for a single class.
ObjectPropertyMap _whiteList
BoundingBoxImpl< Vec3d > BoundingBoxd
Definition: BoundingBox.h:252
PropertyInputIterator * _pii
ObjectPropertyMap & getWhiteList()
Get the list of properties that are explicitly defined as supported.
virtual void setName(const std::string &name)
Definition: Object.h:134
osg::Object * ObjectPtr
Definition: Archive.h:24
virtual const char * className() const =0
bool copyPropertyObjectFromObject(const osg::Object *object, const std::string &propertyName, void *valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: GLU.h:71
#define OSGDB_EXPORT
Definition: Export.h:39
osg::UserDataContainer * getOrCreateUserDataContainer()
bool setProperty(osg::Object *object, const std::string &propertyName, const T &value)
template method for setting property data, return true if property available and the type is compatib...
osg::UserDataContainer * getUserDataContainer()
Definition: Object.h:172
#define DECLARE_TYPE(A, B)
Definition: AlphaFunc.h:19
bool copyPropertyDataFromObject(const osg::Object *object, const std::string &propertyName, void *valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)
std::map< osgDB::BaseSerializer::Type, std::string > TypeToTypeNameMap
virtual void setUserObject(unsigned int i, Object *obj)=0
ObjectPropertyMap _blackList
const ObjectPropertyMap & getBlackList() const
Get the const list of properties that are explicitly defined as not supported.
ObjectPropertyMap & getBlackList()
Get the list of properties that are explicitly defined as not supported.
bool getProperty(const osg::Object *object, const std::string &propertyName, T &value)
template method for getting property data, return true if property available and the type is compatib...
TypeNameToTypeMap _typeNameToTypeMap
bool copyPropertyObjectToObject(osg::Object *object, const std::string &propertyName, const void *valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)