ClassInterface
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGDB_CLASSINTERFACE
#define OSGDB_CLASSINTERFACE 1
#include <osgDB/ObjectWrapper>
#include <osgDB/Registry>
#include <osg/ValueObject>
namespace osgDB
{
template<typename T>
static osgDB::BaseSerializer::Type getTypeEnum()
{
return osgDB::BaseSerializer::RW_UNDEFINED;
}
template<typename T>
static osgDB::BaseSerializer::Type getTypeEnumFrom(T)
{
return getTypeEnum<T>();
}
template<typename T>
static const char* getTypeString()
{
return "UNDEFINED";
}
template<typename T>
static const char* getTypeStringFrom(T)
{
return getTypeString<T>();
}
extern OSGDB_EXPORT osgDB::BaseSerializer::Type getTypeEnumFromPtr(const osg::Object*);
extern OSGDB_EXPORT const char* getTypeStringFromPtr(const osg::Object*);
extern OSGDB_EXPORT osgDB::BaseSerializer::Type getTypeEnumFromPtr(const osg::Image*);
extern OSGDB_EXPORT const char* getTypeStringFromPtr(const osg::Image*);
#define DECLARE_TYPE(A,B) \
template<> inline osgDB::BaseSerializer::Type getTypeEnum<A>() { return osgDB::BaseSerializer::RW_##B; } \
template<> inline const char* getTypeString<A>() { return #B; }
DECLARE_TYPE(osg::Image*, IMAGE)
DECLARE_TYPE(osg::Object*, OBJECT)
DECLARE_TYPE(bool, BOOL)
DECLARE_TYPE(char, CHAR)
DECLARE_TYPE(unsigned char, UCHAR)
DECLARE_TYPE(short, SHORT)
DECLARE_TYPE(unsigned short, USHORT)
DECLARE_TYPE(int, INT)
DECLARE_TYPE(unsigned int, UINT)
DECLARE_TYPE(float, FLOAT)
DECLARE_TYPE(double, DOUBLE)
DECLARE_TYPE(osg::Vec2f, VEC2F)
DECLARE_TYPE(osg::Vec2d, VEC2D)
DECLARE_TYPE(osg::Vec3f, VEC3F)
DECLARE_TYPE(osg::Vec3d, VEC3D)
DECLARE_TYPE(osg::Vec4f, VEC4F)
DECLARE_TYPE(osg::Vec4d, VEC4D)
DECLARE_TYPE(osg::Quat, QUAT)
DECLARE_TYPE(osg::Plane, PLANE)
DECLARE_TYPE(osg::Matrixf, MATRIXF)
DECLARE_TYPE(osg::Matrixd, MATRIXD)
DECLARE_TYPE(std::string, STRING)
DECLARE_TYPE(osg::Vec2b, VEC2B)
DECLARE_TYPE(osg::Vec2ub, VEC2UB)
DECLARE_TYPE(osg::Vec2s, VEC2S)
DECLARE_TYPE(osg::Vec2us, VEC2US)
DECLARE_TYPE(osg::Vec2i, VEC2I)
DECLARE_TYPE(osg::Vec2ui, VEC2UI)
DECLARE_TYPE(osg::Vec3b, VEC3B)
DECLARE_TYPE(osg::Vec3ub, VEC3UB)
DECLARE_TYPE(osg::Vec3s, VEC3S)
DECLARE_TYPE(osg::Vec3us, VEC3US)
DECLARE_TYPE(osg::Vec3i, VEC3I)
DECLARE_TYPE(osg::Vec3ui, VEC3UI)
DECLARE_TYPE(osg::Vec4b, VEC4B)
DECLARE_TYPE(osg::Vec4ub, VEC4UB)
DECLARE_TYPE(osg::Vec4s, VEC4S)
DECLARE_TYPE(osg::Vec4us, VEC4US)
DECLARE_TYPE(osg::Vec4i, VEC4I)
DECLARE_TYPE(osg::Vec4ui, VEC4UI)
DECLARE_TYPE(osg::BoundingBoxf, BOUNDINGBOXF)
DECLARE_TYPE(osg::BoundingBoxd, BOUNDINGBOXD)
DECLARE_TYPE(osg::BoundingSpheref, BOUNDINGSPHEREF)
DECLARE_TYPE(osg::BoundingSphered, BOUNDINGSPHERED)
// forward declare
class PropertyOutputIterator;
class PropertyInputIterator;
class OSGDB_EXPORT ClassInterface
{
public:
ClassInterface();
bool getPropertyType(const osg::Object* object, const std::string& propertyName, osgDB::BaseSerializer::Type& type) const;
bool areTypesCompatible(osgDB::BaseSerializer::Type lhs, osgDB::BaseSerializer::Type rhs) const;
osg::Object* createObject(const std::string& compoundClassdName) const;
template<typename T>
bool getProperty(const osg::Object* object, const std::string& propertyName, T& value);
template<typename T>
bool setProperty(osg::Object* object, const std::string& propertyName, const T& value);
std::string getTypeName(osgDB::BaseSerializer::Type type) const;
osgDB::BaseSerializer::Type getType(const std::string& typeName) const;
typedef std::map<std::string, osgDB::BaseSerializer::Type> PropertyMap;
bool getSupportedProperties(const osg::Object* object, PropertyMap& properties, bool searchAssociates=true) const;
bool isObjectOfType(const osg::Object* object, const std::string& compoundClassName) const;
bool run(void* objectPtr, const std::string& compoundClassName, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
bool run(osg::Object* object, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
bool hasMethod(const std::string& compoundClassName, const std::string& methodName) const;
bool hasMethod(const osg::Object* object, const std::string& methodName) const;
typedef std::map<std::string, PropertyMap> ObjectPropertyMap;
ObjectPropertyMap& getWhiteList() { return _whiteList; }
const ObjectPropertyMap& getWhiteList() const { return _whiteList; }
ObjectPropertyMap& getBlackList() { return _blackList; }
const ObjectPropertyMap& getBlackList() const { return _blackList; }
osgDB::ObjectWrapper* getObjectWrapper(const osg::Object* object) const;
osgDB::BaseSerializer* getSerializer(const osg::Object* object, const std::string& propertyName, osgDB::BaseSerializer::Type& type) const;
protected:
bool copyPropertyDataFromObject(const osg::Object* object, const std::string& propertyName, void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType);
bool copyPropertyDataToObject(osg::Object* object, const std::string& propertyName, const void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType);
bool copyPropertyObjectFromObject(const osg::Object* object, const std::string& propertyName, void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType);
bool copyPropertyObjectToObject(osg::Object* object, const std::string& propertyName, const void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType);
osgDB::OutputStream _outputStream;
PropertyOutputIterator* _poi;
osgDB::InputStream _inputStream;
PropertyInputIterator* _pii;
typedef std::map<std::string, osgDB::BaseSerializer::Type> TypeNameToTypeMap;
typedef std::map<osgDB::BaseSerializer::Type, std::string> TypeToTypeNameMap;
TypeNameToTypeMap _typeNameToTypeMap;
TypeToTypeNameMap _typeToTypeNameMap;
ObjectPropertyMap _whiteList;
ObjectPropertyMap _blackList;
};
template<typename T>
bool ClassInterface::getProperty(const osg::Object* object, const std::string& propertyName, T& value)
{
if (copyPropertyDataFromObject(object, propertyName, &value, sizeof(T), getTypeEnum<T>())) return true;
else return object->getUserValue(propertyName, value); // fallback to check user data for property
}
template<typename T>
bool ClassInterface::setProperty(osg::Object* object, const std::string& propertyName, const T& value)
{
if (copyPropertyDataToObject(object, propertyName, &value, sizeof(T), getTypeEnum<T>())) return true;
else
{
// fallback to using user data to store property data
object->setUserValue(propertyName, value);
return false;
}
}
typedef osg::Object* ObjectPtr;
template<>
inline bool ClassInterface::getProperty(const osg::Object* object, const std::string& propertyName, ObjectPtr& value)
{
if (copyPropertyObjectFromObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum<ObjectPtr>())) return true;
else
{
OSG_INFO<<"ClassInterface::getProperty("<<propertyName<<", Checking UserDataContainer for object ptr"<<std::endl;
const osg::UserDataContainer* udc = object->getUserDataContainer();
if (udc)
{
OSG_INFO<<" Checking UserDataContainer for object ptr"<<std::endl;
const osg::Object* ptr = udc->getUserObject(propertyName);
if (ptr)
{
value = const_cast<ObjectPtr>(ptr);
return true;
}
}
return false;
}
}
template<>
inline bool ClassInterface::setProperty(osg::Object* object, const std::string& propertyName, const ObjectPtr& value)
{
osgDB::BaseSerializer::Type type = dynamic_cast<osg::Image*>(value) ? osgDB::BaseSerializer::RW_IMAGE : getTypeEnum<ObjectPtr>();
// osgDB::BaseSerializer::Type type = getTypeEnum<ObjectPtr>();
if (copyPropertyObjectToObject(object, propertyName, &value, sizeof(ObjectPtr), type)) return true;
else
{
// fallback to using user data to store property data
osg::UserDataContainer* udc = object->getOrCreateUserDataContainer();
unsigned int objectIndex = udc->getUserObjectIndex(propertyName);
if (objectIndex < udc->getNumUserObjects())
{
const osg::Object* outgoingObject = udc->getUserObject(objectIndex);
if (outgoingObject==value) return true;
OSG_INFO<<"ClassInterface::setProperty("<<propertyName<<", "<<value->className()<<") replace object on UserDataContainer"<<std::endl;
value->setName(propertyName);
udc->setUserObject(objectIndex, value);
}
else
{
OSG_INFO<<"ClassInterface::setProperty("<<propertyName<<", "<<value->className()<<") Adding object to UserDataContainer"<<std::endl;
value->setName(propertyName);
udc->addUserObject(value);
}
return true;
}
}
}
#endif
DECLARE_TYPE
#define DECLARE_TYPE(A, B)
Definition ClassInterface:56
OSG_INFO
#define OSG_INFO
Definition Notify:87
ObjectWrapper
ValueObject
osgDB::BaseSerializer
Definition Serializer:141
osgDB::BaseSerializer::Type
Type
Definition Serializer:145
osgDB::BaseSerializer::RW_IMAGE
@ RW_IMAGE
Definition Serializer:146
osgDB::BaseSerializer::RW_UNDEFINED
@ RW_UNDEFINED
Definition Serializer:146
osgDB::ClassInterface::getType
osgDB::BaseSerializer::Type getType(const std::string &typeName) const
get the enum value of type given the human readable name.
osgDB::ClassInterface::_blackList
ObjectPropertyMap _blackList
Definition ClassInterface:220
osgDB::ClassInterface::copyPropertyDataFromObject
bool copyPropertyDataFromObject(const osg::Object *object, const std::string &propertyName, void *valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)
osgDB::ClassInterface::getBlackList
const ObjectPropertyMap & getBlackList() const
Get the const list of properties that are explicitly defined as not supported.
Definition ClassInterface:189
osgDB::ClassInterface::PropertyMap
std::map< std::string, osgDB::BaseSerializer::Type > PropertyMap
Properties supported for a single class.
Definition ClassInterface:154
osgDB::ClassInterface::_inputStream
osgDB::InputStream _inputStream
Definition ClassInterface:210
osgDB::ClassInterface::getSupportedProperties
bool getSupportedProperties(const osg::Object *object, PropertyMap &properties, bool searchAssociates=true) const
Get the list of of properties supported by object.
osgDB::ClassInterface::TypeNameToTypeMap
std::map< std::string, osgDB::BaseSerializer::Type > TypeNameToTypeMap
Definition ClassInterface:213
osgDB::ClassInterface::_pii
PropertyInputIterator * _pii
Definition ClassInterface:211
osgDB::ClassInterface::copyPropertyObjectFromObject
bool copyPropertyObjectFromObject(const osg::Object *object, const std::string &propertyName, void *valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)
osgDB::ClassInterface::getPropertyType
bool getPropertyType(const osg::Object *object, const std::string &propertyName, osgDB::BaseSerializer::Type &type) const
get the Type of the specified property, return true if property is supported, otherwise false.
osgDB::ClassInterface::copyPropertyDataToObject
bool copyPropertyDataToObject(osg::Object *object, const std::string &propertyName, const void *valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)
osgDB::ClassInterface::_poi
PropertyOutputIterator * _poi
Definition ClassInterface:208
osgDB::ClassInterface::_outputStream
osgDB::OutputStream _outputStream
Definition ClassInterface:207
osgDB::ClassInterface::getObjectWrapper
osgDB::ObjectWrapper * getObjectWrapper(const osg::Object *object) const
osgDB::ClassInterface::run
bool run(osg::Object *object, const std::string &methodName, osg::Parameters &inputParameters, osg::Parameters &outputParameters) const
run method of object
osgDB::ClassInterface::getSerializer
osgDB::BaseSerializer * getSerializer(const osg::Object *object, const std::string &propertyName, osgDB::BaseSerializer::Type &type) const
osgDB::ClassInterface::_whiteList
ObjectPropertyMap _whiteList
Definition ClassInterface:219
osgDB::ClassInterface::isObjectOfType
bool isObjectOfType(const osg::Object *object, const std::string &compoundClassName) const
return true if the object can be cast to the specified class specified by compoundClassName
osgDB::ClassInterface::TypeToTypeNameMap
std::map< osgDB::BaseSerializer::Type, std::string > TypeToTypeNameMap
Definition ClassInterface:214
osgDB::ClassInterface::run
bool run(void *objectPtr, const std::string &compoundClassName, const std::string &methodName, osg::Parameters &inputParameters, osg::Parameters &outputParameters) const
run method of object
osgDB::ClassInterface::hasMethod
bool hasMethod(const std::string &compoundClassName, const std::string &methodName) const
checked for support of specified method
osgDB::ClassInterface::getBlackList
ObjectPropertyMap & getBlackList()
Get the list of properties that are explicitly defined as not supported.
Definition ClassInterface:186
osgDB::ClassInterface::_typeToTypeNameMap
TypeToTypeNameMap _typeToTypeNameMap
Definition ClassInterface:217
osgDB::ClassInterface::_typeNameToTypeMap
TypeNameToTypeMap _typeNameToTypeMap
Definition ClassInterface:216
osgDB::ClassInterface::getWhiteList
const ObjectPropertyMap & getWhiteList() const
Get the const list of properties that are explicitly defined as supported.
Definition ClassInterface:183
osgDB::ClassInterface::ObjectPropertyMap
std::map< std::string, PropertyMap > ObjectPropertyMap
Properties supported for a range of classes, used for white and black lists.
Definition ClassInterface:177
osgDB::ClassInterface::getTypeName
std::string getTypeName(osgDB::BaseSerializer::Type type) const
get the human readable name of type.
osgDB::ClassInterface::areTypesCompatible
bool areTypesCompatible(osgDB::BaseSerializer::Type lhs, osgDB::BaseSerializer::Type rhs) const
return type of two types are compatible
osgDB::ClassInterface::ClassInterface
ClassInterface()
osgDB::ClassInterface::createObject
osg::Object * createObject(const std::string &compoundClassdName) const
osgDB::ClassInterface::copyPropertyObjectToObject
bool copyPropertyObjectToObject(osg::Object *object, const std::string &propertyName, const void *valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)
osgDB::ClassInterface::getWhiteList
ObjectPropertyMap & getWhiteList()
Get the list of properties that are explicitly defined as supported.
Definition ClassInterface:180
osgDB::ClassInterface::hasMethod
bool hasMethod(const osg::Object *object, const std::string &methodName) const
checked for support of specified method
osgDB::ClassInterface::getProperty
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...
Definition ClassInterface:225
osgDB::ClassInterface::setProperty
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...
Definition ClassInterface:232
osgDB::InputStream
Definition InputStream:64
osgDB::ObjectWrapper
Definition ObjectWrapper:62
osgDB::OutputStream
Definition OutputStream:57
osg::Image
Definition Image:179
osg::Matrixd
Definition Matrixd:27
osg::Matrixf
Definition Matrixf:27
osg::Object
Definition Object:61
osg::Object::setName
virtual void setName(const std::string &name)
Definition Object:203
osg::Object::className
virtual const char * className() const =0
osg::Object::getUserDataContainer
osg::UserDataContainer * getUserDataContainer()
Definition Object:243
osg::Object::getOrCreateUserDataContainer
osg::UserDataContainer * getOrCreateUserDataContainer()
osg::Plane
A plane class. It can be used to represent an infinite plane.
Definition Plane:34
osg::Quat
Definition Quat:30
osg::UserDataContainer
Definition UserDataContainer:26
osg::UserDataContainer::addUserObject
virtual unsigned int addUserObject(Object *obj)=0
osg::UserDataContainer::getUserObjectIndex
virtual unsigned int getUserObjectIndex(const osg::Object *obj, unsigned int startPos=0) const =0
osg::UserDataContainer::setUserObject
virtual void setUserObject(unsigned int i, Object *obj)=0
osg::UserDataContainer::getUserObject
virtual Object * getUserObject(unsigned int i)=0
osg::Vec2b
Definition Vec2b:26
osg::Vec2d
Definition Vec2d:29
osg::Vec2f
Definition Vec2f:29
osg::Vec2i
Definition Vec2i:22
osg::Vec2s
Definition Vec2s:20
osg::Vec2ub
Definition Vec2ub:22
osg::Vec2ui
Definition Vec2ui:22
osg::Vec2us
Definition Vec2us:20
osg::Vec3b
Definition Vec3b:26
osg::Vec3d
Definition Vec3d:30
osg::Vec3f
Definition Vec3f:29
osg::Vec3i
Definition Vec3i:22
osg::Vec3s
Definition Vec3s:20
osg::Vec3ub
Definition Vec3ub:26
osg::Vec3ui
Definition Vec3ui:22
osg::Vec3us
Definition Vec3us:20
osg::Vec4b
Definition Vec4b:26
osg::Vec4d
Definition Vec4d:29
osg::Vec4f
Definition Vec4f:28
osg::Vec4i
Definition Vec4i:22
osg::Vec4s
Definition Vec4s:26
osg::Vec4ub
Definition Vec4ub:28
osg::Vec4ui
Definition Vec4ui:22
osg::Vec4us
Definition Vec4us:26
osgDB
Definition Archive:24
osgDB::getTypeStringFromPtr
OSGDB_EXPORT const char * getTypeStringFromPtr(const osg::Object *)
osgDB::ObjectPtr
osg::Object * ObjectPtr
Definition ClassInterface:243
osgDB::getTypeEnumFromPtr
OSGDB_EXPORT osgDB::BaseSerializer::Type getTypeEnumFromPtr(const osg::Object *)
osg::BoundingBoxd
BoundingBoxImpl< Vec3d > BoundingBoxd
Definition BoundingBox:252
osg::BoundingBoxf
BoundingBoxImpl< Vec3f > BoundingBoxf
Definition BoundingBox:251
osg::BoundingSpheref
BoundingSphereImpl< Vec3f > BoundingSpheref
Definition BoundingSphere:308
osg::Parameters
std::vector< osg::ref_ptr< osg::Object > > Parameters
Definition Callback:130
osg::BoundingSphered
BoundingSphereImpl< Vec3d > BoundingSphered
Definition BoundingSphere:309
OSGDB_EXPORT
#define OSGDB_EXPORT
Definition osgDB/Export:39
Registry