From 7f9a6aa49ddfd93f95362f91524204b371faf884 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 7 Jun 2011 13:02:20 +0000 Subject: [PATCH] Added ability to subclass from osg::Object to provide custom user data functionality. A new UserDataContainer provides the default implementation of the user data functionality. --- examples/osguserdata/osguserdata.cpp | 50 +++++ include/osg/Object | 86 ++++----- include/osg/UserDataContainer | 99 ++++++++++ src/osg/CMakeLists.txt | 3 + src/osg/Object.cpp | 178 +++++------------- src/osg/UserDataContainer.cpp | 156 +++++++++++++++ src/osgWrappers/serializers/osg/Node.cpp | 8 +- src/osgWrappers/serializers/osg/Object.cpp | 34 +--- .../serializers/osg/UserDataContainer.cpp | 102 ++++++++++ 9 files changed, 502 insertions(+), 214 deletions(-) create mode 100644 include/osg/UserDataContainer create mode 100644 src/osg/UserDataContainer.cpp create mode 100644 src/osgWrappers/serializers/osg/UserDataContainer.cpp diff --git a/examples/osguserdata/osguserdata.cpp b/examples/osguserdata/osguserdata.cpp index 0712b9cc8..52e25c927 100644 --- a/examples/osguserdata/osguserdata.cpp +++ b/examples/osguserdata/osguserdata.cpp @@ -21,10 +21,53 @@ #include #include + #include +#include +#include #include +namespace MyNamespace +{ + +/** Provide an simple example of customizing the default UserDataContainer.*/ +class OSG_EXPORT MyUserDataContainer : public osg::UserDataContainer +{ + public: + MyUserDataContainer() {} + MyUserDataContainer(const MyUserDataContainer& udc, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): + UserDataContainer(udc, copyop) {} + + META_Object(MyNamespace, MyUserDataContainer) + + virtual Object* getUserObject(unsigned int i) + { + OSG_NOTICE<<"MyUserDataContainer::getUserObject("< node = new osg::Group; + if (arguments.read("--MyUserDataContainer") || arguments.read("--mydc")) + { + node->setUserDataContainer(new MyNamespace::MyUserDataContainer); + } + int i = 10; node->setUserValue("Int value",i); diff --git a/include/osg/Object b/include/osg/Object index 7d8620e70..2411a7389 100644 --- a/include/osg/Object +++ b/include/osg/Object @@ -188,50 +188,66 @@ class OSG_EXPORT Object : public Referenced virtual void computeDataVariance() {} + /** set the UserDataContainer object.*/ + void setUserDataContainer(osg::Object* udc) { _userDataContainer = udc; } + + /** get the UserDataContainer attached to this object.*/ + osg::Object* getUserDataContainer() { return _userDataContainer.get(); } + + /** get the const UserDataContainer attached to this object.*/ + const osg::Object* getUserDataContainer() const { return _userDataContainer.get(); } + + /** Convinience method that returns the UserDataContainer, and if one doesn't already exist creates and assigns + * one to the Object and then return this new UserDataContainer.*/ + osg::Object* getOrCreateUserDataContainer(); + + /** * Set user data, data must be subclassed from Referenced to allow * automatic memory handling. If your own data isn't directly * subclassed from Referenced then create an adapter object * which points to your own object and handles the memory addressing. */ - void setUserData(Referenced* obj); + virtual void setUserData(Referenced* obj); /** Get user data.*/ - Referenced* getUserData(); + virtual Referenced* getUserData(); /** Get const user data.*/ - const Referenced* getUserData() const; - + virtual const Referenced* getUserData() const; /** Add user data object. Returns the index position of object added. */ - unsigned int addUserObject(Object* obj); + virtual unsigned int addUserObject(Object* obj); /** Add element to list of user data objects.*/ - void setUserObject(unsigned int i, Object* obj); + virtual void setUserObject(unsigned int i, Object* obj); /** Remove element from the list of user data objects.*/ - void removeUserObject(unsigned int i); + virtual void removeUserObject(unsigned int i); - /** Get the index position of specified user data object.*/ - unsigned int getUserObjectIndex(const osg::Object* obj, unsigned int startPos=0) const; - - /** Get the index position of first user data object that matches specified name.*/ - unsigned int getUserObjectIndex(const std::string& name, unsigned int startPos=0) const; /** Get user data object as specified index position. */ - Object* getUserObject(unsigned int i); + virtual Object* getUserObject(unsigned int i); /** Get const user data object as specified index position. */ - const Object* getUserObject(unsigned int i) const; + virtual const Object* getUserObject(unsigned int i) const; + /** Get number of user objects assigned to this object.*/ + virtual unsigned int getNumUserObjects() const; + + /** Get the index position of specified user data object.*/ + virtual unsigned int getUserObjectIndex(const osg::Object* obj, unsigned int startPos=0) const; + + /** Get the index position of first user data object that matches specified name.*/ + virtual unsigned int getUserObjectIndex(const std::string& name, unsigned int startPos=0) const; + + /** Get first user data object with specified name. */ Object* getUserObject(const std::string& name, unsigned int startPos=0); /** Get first const user data object with specified name. */ const Object* getUserObject(const std::string& name, unsigned int startPos=0) const; - /** Get number of user objects assigned to this object.*/ - unsigned int getNumUserObjects() const; /** Convinience method that casts the named UserObject to osg::TemplateValueObject and gets the value. * To use this template method you need to include the osg/ValueObject header.*/ @@ -245,18 +261,18 @@ class OSG_EXPORT Object : public Referenced void setUserValue(const std::string& name, const T& value); - /** A vector of std::string's which are used to describe the object.*/ typedef std::vector DescriptionList; /** Set the list of string descriptions.*/ - void setDescriptions(const DescriptionList& descriptions); + virtual void setDescriptions(const DescriptionList& descriptions); /** Get the description list of the node.*/ - DescriptionList& getDescriptions(); + virtual DescriptionList& getDescriptions(); /** Get the const description list of the const node.*/ - const DescriptionList& getDescriptions() const; + virtual const DescriptionList& getDescriptions() const; + /** Get a single const description of the const node.*/ const std::string& getDescription(unsigned int i) const; @@ -270,6 +286,7 @@ class OSG_EXPORT Object : public Referenced /** Add a description string to the node.*/ void addDescription(const std::string& desc); + /** Resize any per context GLObject buffers to specified size. */ virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {} @@ -294,34 +311,7 @@ class OSG_EXPORT Object : public Referenced std::string _name; DataVariance _dataVariance; - /** Internal structure for storing all user data.*/ - class OSG_EXPORT UserDataContainer : public osg::Referenced - { - public: - UserDataContainer(); - UserDataContainer(const UserDataContainer& udc, const osg::CopyOp& copyop=CopyOp::SHALLOW_COPY); - - virtual void setThreadSafeRefUnref(bool threadSafe); - - typedef std::vector< osg::ref_ptr > ObjectList; - - ref_ptr _userData; - DescriptionList _descriptionList; - ObjectList _objectList; - - protected: - virtual ~UserDataContainer() {} - }; - - ref_ptr _userDataContainer; - - /** Convinience method that returns the UserDataContainer, and if one doesn't already exist creates and assigns - * one to the Object and then return this new UserDataContainer.*/ - UserDataContainer* getOrCreateUserDataContainer() - { - if (!_userDataContainer.valid()) _userDataContainer = new UserDataContainer; - return _userDataContainer.get(); - } + ref_ptr _userDataContainer; private: diff --git a/include/osg/UserDataContainer b/include/osg/UserDataContainer new file mode 100644 index 000000000..7a1829994 --- /dev/null +++ b/include/osg/UserDataContainer @@ -0,0 +1,99 @@ +/* -*-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 OSG_USERDATACONTAINER +#define OSG_USERDATACONTAINER 1 + +#include + +#include +#include + +namespace osg { + +/** Internal structure for storing all user data.*/ +class OSG_EXPORT UserDataContainer : public osg::Object +{ + public: + UserDataContainer(); + UserDataContainer(const UserDataContainer& udc, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + + META_Object(osg, UserDataContainer) + + + virtual void setThreadSafeRefUnref(bool threadSafe); + + /** + * Set user data, data must be subclassed from Referenced to allow + * automatic memory handling. If your own data isn't directly + * subclassed from Referenced then create an adapter object + * which points to your own object and handles the memory addressing. + */ + virtual void setUserData(Referenced* obj); + + /** Get user data.*/ + virtual Referenced* getUserData(); + + /** Get const user data.*/ + virtual const Referenced* getUserData() const; + + /** Add user data object. Returns the index position of object added. */ + virtual unsigned int addUserObject(Object* obj); + + /** Add element to list of user data objects.*/ + virtual void setUserObject(unsigned int i, Object* obj); + + /** Remove element from the list of user data objects.*/ + virtual void removeUserObject(unsigned int i); + + + /** Get user data object as specified index position. */ + virtual Object* getUserObject(unsigned int i); + + /** Get const user data object as specified index position. */ + virtual const Object* getUserObject(unsigned int i) const; + + /** Get number of user objects assigned to this object.*/ + virtual unsigned int getNumUserObjects() const; + + /** Get the index position of specified user data object.*/ + virtual unsigned int getUserObjectIndex(const osg::Object* obj, unsigned int startPos=0) const; + + /** Get the index position of first user data object that matches specified name.*/ + virtual unsigned int getUserObjectIndex(const std::string& name, unsigned int startPos=0) const; + + + /** Set the list of string descriptions.*/ + virtual void setDescriptions(const DescriptionList& descriptions); + + /** Get the description list of the node.*/ + virtual DescriptionList& getDescriptions(); + + /** Get the const description list of the const node.*/ + virtual const DescriptionList& getDescriptions() const; + + + protected: + virtual ~UserDataContainer() {} + + typedef std::vector< osg::ref_ptr > ObjectList; + + ref_ptr _userData; + DescriptionList _descriptionList; + ObjectList _objectList; +}; + + +} + +#endif diff --git a/src/osg/CMakeLists.txt b/src/osg/CMakeLists.txt index f6e1a71fc..7e9fb6738 100644 --- a/src/osg/CMakeLists.txt +++ b/src/osg/CMakeLists.txt @@ -172,6 +172,8 @@ SET(TARGET_H ${HEADER_PATH}/TriangleFunctor ${HEADER_PATH}/TriangleIndexFunctor ${HEADER_PATH}/Uniform + ${HEADER_PATH}/UserDataContainer + ${HEADER_PATH}/ValueObject ${HEADER_PATH}/Vec2 ${HEADER_PATH}/Vec2b ${HEADER_PATH}/Vec2d @@ -337,6 +339,7 @@ SET(TARGET_SRC TransferFunction.cpp Transform.cpp Uniform.cpp + UserDataContainer.cpp Version.cpp VertexProgram.cpp View.cpp diff --git a/src/osg/Object.cpp b/src/osg/Object.cpp index b66e2a6a9..b3f453fbb 100644 --- a/src/osg/Object.cpp +++ b/src/osg/Object.cpp @@ -11,6 +11,7 @@ * OpenSceneGraph Public License for more details. */ #include +#include namespace osg { @@ -28,7 +29,7 @@ Object::Object(const Object& obj,const CopyOp& copyop): { if (copyop.getCopyFlags()&osg::CopyOp::DEEP_COPY_USERDATA) { - _userDataContainer = new UserDataContainer(*obj._userDataContainer, copyop); + _userDataContainer = obj.getUserDataContainer()->clone(copyop); } else { @@ -43,146 +44,87 @@ void Object::setThreadSafeRefUnref(bool threadSafe) if (_userDataContainer.valid()) _userDataContainer->setThreadSafeRefUnref(threadSafe); } +osg::Object* Object::getOrCreateUserDataContainer() +{ + if (!_userDataContainer) _userDataContainer = new UserDataContainer(); + return _userDataContainer.get(); +} + void Object::setUserData(Referenced* obj) { - getOrCreateUserDataContainer()->_userData = obj; + getOrCreateUserDataContainer()->setUserData(obj); } Referenced* Object::getUserData() { - return _userDataContainer.valid() ? _userDataContainer->_userData.get() : 0; + return _userDataContainer.valid() ? _userDataContainer->getUserData() : 0; } const Referenced* Object::getUserData() const { - return _userDataContainer.valid() ? _userDataContainer->_userData.get() : 0; + return _userDataContainer.valid() ? _userDataContainer->getUserData() : 0; } unsigned int Object::addUserObject(Object* obj) { // make sure the UserDataContainer exists - getOrCreateUserDataContainer(); - - // make sure that the object isn't already in the container - unsigned int i = getUserObjectIndex(obj); - if (i<_userDataContainer->_objectList.size()) - { - // object already in container so just return. - return i; - } - - unsigned int pos = _userDataContainer->_objectList.size(); - - // object not already on user data container so add it in. - _userDataContainer->_objectList.push_back(obj); - - return pos; + return getOrCreateUserDataContainer()->addUserObject(obj); } void Object::removeUserObject(unsigned int i) { - if (_userDataContainer.valid() && i<_userDataContainer->_objectList.size()) - { - _userDataContainer->_objectList.erase(_userDataContainer->_objectList.begin()+i); - } + if (_userDataContainer.valid()) _userDataContainer->removeUserObject(i); } void Object::setUserObject(unsigned int i, Object* obj) { - // make sure the UserDataContainer exists - getOrCreateUserDataContainer(); - - if (i<_userDataContainer->_objectList.size()) - { - _userDataContainer->_objectList[i] = obj; - } + // make sure the UserDataContainer exists + getOrCreateUserDataContainer()->setUserObject(i,obj); } Object* Object::getUserObject(unsigned int i) { - if (_userDataContainer.valid() && i<_userDataContainer->_objectList.size()) - { - return _userDataContainer->_objectList[i].get(); - } - return 0; + return _userDataContainer.valid() ? _userDataContainer->getUserObject(i) : 0; } const Object* Object::getUserObject(unsigned int i) const { - if (_userDataContainer.valid() && i<_userDataContainer->_objectList.size()) - { - return _userDataContainer->_objectList[i].get(); - } - return 0; -} - -unsigned int Object::getUserObjectIndex(const osg::Object* obj, unsigned int startPos) const -{ - if (_userDataContainer.valid()) - { - for(unsigned int i = startPos; i < _userDataContainer->_objectList.size(); ++i) - { - if (_userDataContainer->_objectList[i]==obj) return i; - } - return _userDataContainer->_objectList.size(); - } - return 0; -} - -unsigned int Object::getUserObjectIndex(const std::string& name, unsigned int startPos) const -{ - if (_userDataContainer.valid()) - { - for(unsigned int i = startPos; i < _userDataContainer->_objectList.size(); ++i) - { - Object* obj = _userDataContainer->_objectList[i].get(); - if (obj && obj->getName()==name) return i; - } - return _userDataContainer->_objectList.size(); - } - return 0; -} - -Object* Object::getUserObject(const std::string& name, unsigned int startPos) -{ - if (_userDataContainer.valid()) - { - unsigned int i = getUserObjectIndex(name, startPos); - return (i<_userDataContainer->_objectList.size()) ? _userDataContainer->_objectList[i].get() : 0; - } - else - { - return 0; - } -} - -const Object* Object::getUserObject(const std::string& name, unsigned int startPos) const -{ - if (_userDataContainer.valid()) - { - unsigned int i = getUserObjectIndex(name, startPos); - return (i<_userDataContainer->_objectList.size()) ? _userDataContainer->_objectList[i].get() : 0; - } - else - { - return 0; - } + return _userDataContainer.valid() ? _userDataContainer->getUserObject(i) : 0; } unsigned int Object::getNumUserObjects() const { - return _userDataContainer.valid() ? _userDataContainer->_objectList.size() : 0; + return _userDataContainer.valid() ? _userDataContainer->getNumUserObjects() : 0; } +unsigned int Object::getUserObjectIndex(const osg::Object* obj, unsigned int startPos) const +{ + return _userDataContainer.valid() ? _userDataContainer->getUserObjectIndex(obj, startPos) : 0; +} + +unsigned int Object::getUserObjectIndex(const std::string& name, unsigned int startPos) const +{ + return _userDataContainer.valid() ? _userDataContainer->getUserObjectIndex(name, startPos) : 0; +} + +Object* Object::getUserObject(const std::string& name, unsigned int startPos) +{ + return getUserObject(getUserObjectIndex(name, startPos)); +} + +const Object* Object::getUserObject(const std::string& name, unsigned int startPos) const +{ + return getUserObject(getUserObjectIndex(name, startPos)); +} void Object::setDescriptions(const DescriptionList& descriptions) { - getOrCreateUserDataContainer()->_descriptionList = descriptions; + getOrCreateUserDataContainer()->setDescriptions(descriptions); } Object::DescriptionList& Object::getDescriptions() { - return getOrCreateUserDataContainer()->_descriptionList; + return getOrCreateUserDataContainer()->getDescriptions(); } static OpenThreads::Mutex s_mutex_StaticDescriptionList; @@ -195,61 +137,29 @@ static const Object::DescriptionList& getStaticDescriptionList() const Object::DescriptionList& Object::getDescriptions() const { - if (_userDataContainer.valid()) return _userDataContainer->_descriptionList; + if (_userDataContainer.valid()) return _userDataContainer->getDescriptions(); else return getStaticDescriptionList(); } std::string& Object::getDescription(unsigned int i) { - return getOrCreateUserDataContainer()->_descriptionList[i]; + return getOrCreateUserDataContainer()->getDescriptions()[i]; } const std::string& Object::getDescription(unsigned int i) const { - if (_userDataContainer.valid()) return _userDataContainer->_descriptionList[i]; + if (_userDataContainer.valid()) return _userDataContainer->getDescriptions()[i]; else return getStaticDescriptionList()[i]; } unsigned int Object::getNumDescriptions() const { - return _userDataContainer.valid() ? _userDataContainer->_descriptionList.size() : 0; + return _userDataContainer.valid() ? _userDataContainer->getDescriptions().size() : 0; } void Object::addDescription(const std::string& desc) { - getOrCreateUserDataContainer()->_descriptionList.push_back(desc); + getOrCreateUserDataContainer()->getDescriptions().push_back(desc); } -/////////////////////////////////////////////////////////////////////////////////////////////////// -// -// UserDataContainer -// -Object::UserDataContainer::UserDataContainer(): - Referenced(true) -{ -} - -Object::UserDataContainer::UserDataContainer(const UserDataContainer& udc, const osg::CopyOp& copyop): - Referenced(true) -{ - _userData = udc._userData; - _descriptionList = udc._descriptionList; -} - -void Object::UserDataContainer::setThreadSafeRefUnref(bool threadSafe) -{ - Referenced::setThreadSafeRefUnref(threadSafe); - - if (_userData.valid()) _userData->setThreadSafeRefUnref(threadSafe); - - for(ObjectList::iterator itr = _objectList.begin(); - itr != _objectList.end(); - ++itr) - { - (*itr)->setThreadSafeRefUnref(threadSafe); - } -} - - - } // end of namespace osg diff --git a/src/osg/UserDataContainer.cpp b/src/osg/UserDataContainer.cpp new file mode 100644 index 000000000..c3464c78a --- /dev/null +++ b/src/osg/UserDataContainer.cpp @@ -0,0 +1,156 @@ +/* -*-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. +*/ +#include + +namespace osg +{ + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// UserDataContainer +// +UserDataContainer::UserDataContainer(): + Object(true) +{ +} + +UserDataContainer::UserDataContainer(const UserDataContainer& udc, const osg::CopyOp& copyop): + Object(udc, copyop) +{ + _userData = udc._userData; + _descriptionList = udc._descriptionList; +} + +void UserDataContainer::setThreadSafeRefUnref(bool threadSafe) +{ + Object::setThreadSafeRefUnref(threadSafe); + + if (_userData.valid()) _userData->setThreadSafeRefUnref(threadSafe); + + for(ObjectList::iterator itr = _objectList.begin(); + itr != _objectList.end(); + ++itr) + { + (*itr)->setThreadSafeRefUnref(threadSafe); + } +} + +void UserDataContainer::setUserData(Referenced* obj) +{ + _userData = obj; +} + +Referenced* UserDataContainer::getUserData() +{ + return _userData.get(); +} + +const Referenced* UserDataContainer::getUserData() const +{ + return _userData.get(); +} + +unsigned int UserDataContainer::addUserObject(Object* obj) +{ + // make sure that the object isn't already in the container + unsigned int i = getUserObjectIndex(obj); + if (i<_objectList.size()) + { + // object already in container so just return. + return i; + } + + unsigned int pos = _objectList.size(); + + // object not already on user data container so add it in. + _objectList.push_back(obj); + + return pos; +} + +void UserDataContainer::removeUserObject(unsigned int i) +{ + if (i<_objectList.size()) + { + _objectList.erase(_objectList.begin()+i); + } +} + +void UserDataContainer::setUserObject(unsigned int i, Object* obj) +{ + if (i<_objectList.size()) + { + _objectList[i] = obj; + } +} + +Object* UserDataContainer::getUserObject(unsigned int i) +{ + if (i<_objectList.size()) + { + return _objectList[i].get(); + } + return 0; +} + +const Object* UserDataContainer::getUserObject(unsigned int i) const +{ + if (i<_objectList.size()) + { + return _objectList[i].get(); + } + return 0; +} + +unsigned int UserDataContainer::getNumUserObjects() const +{ + return _objectList.size(); +} + +unsigned int UserDataContainer::getUserObjectIndex(const osg::Object* obj, unsigned int startPos) const +{ + for(unsigned int i = startPos; i < _objectList.size(); ++i) + { + if (_objectList[i]==obj) return i; + } + return _objectList.size(); +} + +unsigned int UserDataContainer::getUserObjectIndex(const std::string& name, unsigned int startPos) const +{ + for(unsigned int i = startPos; i < _objectList.size(); ++i) + { + Object* obj = _objectList[i].get(); + if (obj && obj->getName()==name) return i; + } + return _objectList.size(); +} + +void UserDataContainer::setDescriptions(const DescriptionList& descriptions) +{ + _descriptionList = descriptions; +} + +Object::DescriptionList& UserDataContainer::getDescriptions() +{ + return _descriptionList; +} + +const Object::DescriptionList& UserDataContainer::getDescriptions() const +{ + return _descriptionList; +} + + + +} // end of namespace osg diff --git a/src/osgWrappers/serializers/osg/Node.cpp b/src/osgWrappers/serializers/osg/Node.cpp index 718e1052b..66d6bd0a3 100644 --- a/src/osgWrappers/serializers/osg/Node.cpp +++ b/src/osgWrappers/serializers/osg/Node.cpp @@ -77,6 +77,12 @@ REGISTER_OBJECT_WRAPPER( Node, ADD_OBJECT_SERIALIZER( CullCallback, osg::NodeCallback, NULL ); // _cullCallback ADD_BOOL_SERIALIZER( CullingActive, true ); // _cullingActive ADD_HEXINT_SERIALIZER( NodeMask, 0xffffffff ); // _nodeMask - ADD_USER_SERIALIZER( Descriptions ); // _descriptions + + ADD_USER_SERIALIZER( Descriptions ); // _descriptions, deprecated + UPDATE_TO_VERSION( 77 ) + { + REMOVE_SERIALIZER( Descriptions ); + } + ADD_OBJECT_SERIALIZER( StateSet, osg::StateSet, NULL ); // _stateset } diff --git a/src/osgWrappers/serializers/osg/Object.cpp b/src/osgWrappers/serializers/osg/Object.cpp index 9a9973a5a..06333dd16 100644 --- a/src/osgWrappers/serializers/osg/Object.cpp +++ b/src/osgWrappers/serializers/osg/Object.cpp @@ -27,35 +27,6 @@ static bool writeUserData( osgDB::OutputStream& os, const osg::Object& obj ) return true; } -static bool checkUserObjects( const osg::Object& obj ) -{ - return obj.getNumUserObjects()>0; -} - -static bool readUserObjects( osgDB::InputStream& is, osg::Object& obj ) -{ - unsigned int size = is.readSize(); is >> osgDB::BEGIN_BRACKET; - for( unsigned int i=0; i> osgDB::END_BRACKET; - return true; -} - -static bool writeUserObjects( osgDB::OutputStream& os, const osg::Object& obj ) -{ - unsigned int numObjects = obj.getNumUserObjects(); - os.writeSize(numObjects); os << osgDB::BEGIN_BRACKET << std::endl; - for ( unsigned int i=0; i + +#include +#include +#include + +static bool checkUDC_UserData( const osg::UserDataContainer& udc ) +{ + return dynamic_cast(udc.getUserData())!=0; +} + +static bool readUDC_UserData( osgDB::InputStream& is, osg::UserDataContainer& udc ) +{ + is >> osgDB::BEGIN_BRACKET; + osg::Object* object = is.readObject(); + if(object) udc.setUserData(object); + is >> osgDB::END_BRACKET; + return true; +} + +static bool writeUDC_UserData( osgDB::OutputStream& os, const osg::UserDataContainer& udc ) +{ + os << osgDB::BEGIN_BRACKET << std::endl; + os.writeObject(dynamic_cast(udc.getUserData())); + os << osgDB::END_BRACKET << std::endl; + return true; +} + +// _descriptions +static bool checkUDC_Descriptions( const osg::UserDataContainer& udc ) +{ + return udc.getNumDescriptions()>0; +} + +static bool readUDC_Descriptions( osgDB::InputStream& is, osg::UserDataContainer& udc ) +{ + unsigned int size = is.readSize(); is >> osgDB::BEGIN_BRACKET; + for ( unsigned int i=0; i> osgDB::END_BRACKET; + return true; +} + +static bool writeUDC_Descriptions( osgDB::OutputStream& os, const osg::UserDataContainer& udc ) +{ + const osg::Object::DescriptionList& slist = udc.getDescriptions(); + os.writeSize(slist.size()); os << osgDB::BEGIN_BRACKET << std::endl; + for ( osg::Object::DescriptionList::const_iterator itr=slist.begin(); + itr!=slist.end(); ++itr ) + { + os.writeWrappedString( *itr ); + os << std::endl; + } + os << osgDB::END_BRACKET << std::endl; + return true; +} + + +static bool checkUDC_UserObjects( const osg::UserDataContainer& udc ) +{ + return udc.getNumUserObjects()>0; +} + +static bool readUDC_UserObjects( osgDB::InputStream& is, osg::UserDataContainer& udc ) +{ + unsigned int size = is.readSize(); is >> osgDB::BEGIN_BRACKET; + for( unsigned int i=0; i> osgDB::END_BRACKET; + return true; +} + +static bool writeUDC_UserObjects( osgDB::OutputStream& os, const osg::UserDataContainer& udc ) +{ + unsigned int numObjects = udc.getNumUserObjects(); + os.writeSize(numObjects); os << osgDB::BEGIN_BRACKET << std::endl; + for ( unsigned int i=0; i