Introduced new user object support into osg::Object that allows assignment of a list of user objects to an osg::Object.

Refactored original UserData and Descriptions strings to be managed alongside the new user object suppport within
a single osg::Object::UserDataContainer.
This commit is contained in:
Robert Osfield 2011-06-02 22:04:08 +00:00
parent 4103319059
commit c703f39459
7 changed files with 535 additions and 46 deletions

View File

@ -56,6 +56,7 @@ class OSG_EXPORT CopyOp
DEEP_COPY_SHAPES = 1<<9,
DEEP_COPY_UNIFORMS = 1<<10,
DEEP_COPY_CALLBACKS = 1<<11,
DEEP_COPY_USERDATA = 1<<12,
DEEP_COPY_ALL = 0x7FFFFFFF
};

View File

@ -304,31 +304,6 @@ class OSG_EXPORT Node : public Object
/** A vector of std::string's which are used to describe the object.*/
typedef std::vector<std::string> DescriptionList;
/** Set the description list of the node.*/
inline void setDescriptions(const DescriptionList& descriptions) { _descriptions=descriptions; }
/** Get the description list of the node.*/
inline DescriptionList& getDescriptions() { return _descriptions; }
/** Get the const description list of the const node.*/
inline const DescriptionList& getDescriptions() const { return _descriptions; }
/** Get a single const description of the const node.*/
inline const std::string& getDescription(unsigned int i) const { return _descriptions[i]; }
/** Get a single description of the node.*/
inline std::string& getDescription(unsigned int i) { return _descriptions[i]; }
/** Get the number of descriptions of the node.*/
inline unsigned int getNumDescriptions() const { return static_cast<unsigned int>(_descriptions.size()); }
/** Add a description string to the node.*/
void addDescription(const std::string& desc) { _descriptions.push_back(desc); }
/** Set the node's StateSet.*/
void setStateSet(osg::StateSet* stateset);
@ -454,8 +429,6 @@ class OSG_EXPORT Node : public Object
NodeMask _nodeMask;
DescriptionList _descriptions;
ref_ptr<StateSet> _stateset;
};

View File

@ -20,6 +20,7 @@
#include <osg/Notify>
#include <string>
#include <vector>
namespace osg {
@ -149,6 +150,9 @@ class OSG_EXPORT Object : public Referenced
virtual const char* className() const = 0;
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
/** Set the name of object using C++ style string.*/
virtual void setName( const std::string& name ) { _name = name; }
@ -190,13 +194,82 @@ class OSG_EXPORT Object : public Referenced
* subclassed from Referenced then create an adapter object
* which points to your own object and handles the memory addressing.
*/
inline void setUserData(Referenced* obj) { _userData = obj; }
void setUserData(Referenced* obj);
/** Get user data.*/
inline Referenced* getUserData() { return _userData.get(); }
Referenced* getUserData();
/** Get const user data.*/
inline const Referenced* getUserData() const { return _userData.get(); }
const Referenced* getUserData() const;
/** Add user data object.*/
void addUserObject(Object* obj);
/** Add element to list of user data objects.*/
void setUserObject(unsigned int i, Object* obj);
/** Remove element from the list of user data objects.*/
void removeUserObject(unsigned int i);
/** Get the index position of specified user data object.*/
unsigned int getUserObjectIndex(const osg::Object* obj) const;
/** Get the index position of first user data object that matches specified name.*/
unsigned int getUserObjectIndex(const std::string& name) const;
/** Get user data object as specified index position. */
Object* getUserObject(unsigned int i);
/** Get const user data object as specified index position. */
const Object* getUserObject(unsigned int i) const;
/** Get first user data object with specified name. */
Object* getUserObject(const std::string& name);
/** Get first const user data object with specified name. */
const Object* getUserObject(const std::string& name) const;
/** Get number of user objects assigned to this object.*/
unsigned int getNumUserObjects() const;
/** Convinience method that casts the named UserObject to osg::TemplateValueObject<T> and gets the value.
* To use this template method you need to include the osg/ValueObject header.*/
template<typename T>
bool getUserValue(const std::string& name, T& value) const;
/** Convinience method that creates the osg::TemplateValueObject<T> to store the
* specified value and adds it as a named UserObject.
* To use this template method you need to include the osg/ValueObject header. */
template<typename T>
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<std::string> DescriptionList;
/** Set the list of string descriptions.*/
void setDescriptions(const DescriptionList& descriptions);
/** Get the description list of the node.*/
DescriptionList& getDescriptions();
/** Get the const description list of the const node.*/
const DescriptionList& getDescriptions() const;
/** Get a single const description of the const node.*/
const std::string& getDescription(unsigned int i) const;
/** Get a single description of the node.*/
std::string& getDescription(unsigned int i);
/** Get the number of descriptions of the node.*/
unsigned int getNumDescriptions() const;
/** 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*/) {}
@ -206,6 +279,7 @@ class OSG_EXPORT Object : public Referenced
* for all graphics contexts. */
virtual void releaseGLObjects(osg::State* = 0) const {}
protected:
/** Object destructor. Note, is protected so that Objects cannot
@ -219,7 +293,35 @@ 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<osg::Object> > ObjectList;
ref_ptr<Referenced> _userData;
DescriptionList _descriptionList;
ObjectList _objectList;
protected:
virtual ~UserDataContainer() {}
};
ref_ptr<UserDataContainer> _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();
}
private:
@ -227,6 +329,7 @@ class OSG_EXPORT Object : public Referenced
Object& operator = (const Object&) { return *this; }
};
}
#endif

199
include/osg/ValueObject Normal file
View File

@ -0,0 +1,199 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 2011 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_VALUEOBJECT
#define OSG_VALUEOBJECT 1
#include <osg/Object>
namespace osg {
// foward declare core OSG math classes
class Vec2f;
class Vec3f;
class Vec4f;
class Vec2d;
class Vec3d;
class Vec4d;
class Quat;
class Plane;
class Matrixf;
class Matrixd;
class ValueObject : public Object
{
public:
ValueObject() : Object(true) {}
ValueObject(const std::string& name) : Object(true) { setName(name); }
ValueObject(const ValueObject& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY): Object(rhs,copyop) {}
META_Object(osg, ValueObject)
class GetValueVisitor
{
public:
virtual void apply(bool value) {}
virtual void apply(char value) {}
virtual void apply(unsigned char value) {}
virtual void apply(short value) {}
virtual void apply(unsigned short value) {}
virtual void apply(int value) {}
virtual void apply(unsigned int value) {}
virtual void apply(float value) {}
virtual void apply(double value) {}
virtual void apply(const std::string& value) {}
virtual void apply(const osg::Vec2f& value) {}
virtual void apply(const osg::Vec3f& value) {}
virtual void apply(const osg::Vec4f& value) {}
virtual void apply(const osg::Vec2d& value) {}
virtual void apply(const osg::Vec3d& value) {}
virtual void apply(const osg::Vec4d& value) {}
virtual void apply(const osg::Quat& value) {}
virtual void apply(const osg::Plane& value) {}
virtual void apply(const osg::Matrixf& value) {}
virtual void apply(const osg::Matrixd& value) {}
};
class SetValueVisitor
{
public:
virtual void apply(bool& value) {}
virtual void apply(char& value) {}
virtual void apply(unsigned char& value) {}
virtual void apply(short& value) {}
virtual void apply(unsigned short& value) {}
virtual void apply(int& value) {}
virtual void apply(unsigned int& value) {}
virtual void apply(float& value) {}
virtual void apply(double& value) {}
virtual void apply(std::string& value) {}
virtual void apply(osg::Vec2f& value) {}
virtual void apply(osg::Vec3f& value) {}
virtual void apply(osg::Vec4f& value) {}
virtual void apply(osg::Vec2d& value) {}
virtual void apply(osg::Vec3d& value) {}
virtual void apply(osg::Vec4d& value) {}
virtual void apply(osg::Quat& value) {}
virtual void apply(osg::Plane& value) {}
virtual void apply(osg::Matrixf& value) {}
virtual void apply(osg::Matrixd& value) {}
};
virtual bool get(GetValueVisitor& gvv) const { return false; }
virtual bool set(SetValueVisitor& gvv) { return false; }
};
template< typename T >
struct ValueObjectClassNameTrait
{
static const char* s_className;
};
template< typename T >
const char* ValueObjectClassNameTrait<T>::s_className = "TemplateValueObject";
template< typename T >
class TemplateValueObject : public ValueObject
{
public:
TemplateValueObject():
ValueObject(),
_value() {}
TemplateValueObject(const std::string& name, const T& value) :
ValueObject(name),
_value(value) {}
TemplateValueObject(const TemplateValueObject& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY) :
ValueObject(rhs,copyop),
_value(rhs._value) {}
virtual Object* cloneType() const { return new TemplateValueObject(); }
virtual Object* clone(const CopyOp& copyop) const { return new TemplateValueObject(*this, copyop); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const TemplateValueObject*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return ValueObjectClassNameTrait<T>::s_className; }
void setValue(const T& value) { _value = value; }
const T& getValue() const { return _value; }
virtual bool get(GetValueVisitor& gvv) const { gvv.apply(_value); return true; }
virtual bool set(SetValueVisitor& svv) { svv.apply(_value); return true; }
protected:
static const char* s_TemplateValueObject_className;
T _value;
};
#define META_ValueObject(TYPE,NAME) \
template<> struct ValueObjectClassNameTrait<TYPE> { static const char* s_className; }; \
const char* ValueObjectClassNameTrait<TYPE>::s_className = #NAME; \
typedef TemplateValueObject<TYPE> NAME;
META_ValueObject(std::string, StringValueObject)
META_ValueObject(bool, BoolValueObject)
META_ValueObject(char, CharValueObject)
META_ValueObject(unsigned char, UCharValueObject)
META_ValueObject(short, ShortValueObject)
META_ValueObject(unsigned short, UShortValueObject)
META_ValueObject(int, IntValueObject)
META_ValueObject(unsigned int, UIntValueObject)
META_ValueObject(float, FloatValueObject)
META_ValueObject(double, DoubleValueObject)
META_ValueObject(Vec2f, Vec2fValueObject)
META_ValueObject(Vec3f, Vec3fValueObject)
META_ValueObject(Vec4f, Vec4fValueObject)
META_ValueObject(Vec2d, Vec2dValueObject)
META_ValueObject(Vec3d, Vec3dValueObject)
META_ValueObject(Vec4d, Vec4dValueObject)
META_ValueObject(Quat, QuatValueObject)
META_ValueObject(Plane, PlaneValueObject)
META_ValueObject(Matrixf, MatrixfValueObject)
META_ValueObject(Matrixd, MatrixdValueObject)
/** provide implementation og osg::Object::getUserValue(..) template*/
template<typename T>
bool osg::Object::getUserValue(const std::string& name, T& value) const
{
typedef TemplateValueObject<T> UserValueObject;
const UserValueObject* uvo = dynamic_cast<const UserValueObject*>(getUserObject(name));
if (uvo)
{
value = uvo->getValue();
return true;
}
else
{
return false;
}
}
/** provide implementation og osg::Object::setUserValue(..) template.*/
template<typename T>
void osg::Object::setUserValue(const std::string& name, const T& value)
{
typedef TemplateValueObject<T> UserValueObject;
unsigned int i = getUserObjectIndex(name);
if (i<getNumUserObjects()) setUserObject(i, new UserValueObject(name,value));
else addUserObject(new UserValueObject(name,value));
}
}
#endif

View File

@ -500,13 +500,10 @@ void Drawable::setThreadSafeRefUnref(bool threadSafe)
Object::setThreadSafeRefUnref(threadSafe);
if (_stateset.valid()) _stateset->setThreadSafeRefUnref(threadSafe);
if (_updateCallback.valid()) _updateCallback->setThreadSafeRefUnref(threadSafe);
if (_eventCallback.valid()) _eventCallback->setThreadSafeRefUnref(threadSafe);
if (_cullCallback.valid()) _cullCallback->setThreadSafeRefUnref(threadSafe);
if (_drawCallback.valid()) _drawCallback->setThreadSafeRefUnref(threadSafe);
if (_userData.valid()) _userData->setThreadSafeRefUnref(threadSafe);
}
void Drawable::resizeGLObjectBuffers(unsigned int maxSize)

View File

@ -81,8 +81,7 @@ Node::Node(const Node& node,const CopyOp& copyop):
_cullingActive(node._cullingActive),
_numChildrenWithCullingDisabled(0), // assume no children yet.
_numChildrenWithOccluderNodes(0),
_nodeMask(node._nodeMask),
_descriptions(node._descriptions)
_nodeMask(node._nodeMask)
{
setStateSet(copyop(node._stateset.get()));
}
@ -501,12 +500,9 @@ void Node::setThreadSafeRefUnref(bool threadSafe)
Object::setThreadSafeRefUnref(threadSafe);
if (_stateset.valid()) _stateset->setThreadSafeRefUnref(threadSafe);
if (_updateCallback.valid()) _updateCallback->setThreadSafeRefUnref(threadSafe);
if (_eventCallback.valid()) _eventCallback->setThreadSafeRefUnref(threadSafe);
if (_cullCallback.valid()) _cullCallback->setThreadSafeRefUnref(threadSafe);
if (_userData.valid()) _userData->setThreadSafeRefUnref(threadSafe);
}
void Node::resizeGLObjectBuffers(unsigned int maxSize)

View File

@ -15,11 +15,231 @@
namespace osg
{
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Object
//
Object::Object(const Object& obj,const CopyOp& copyop):
Referenced(),
_name(obj._name),
_dataVariance(obj._dataVariance),
_userData(copyop(obj._userData.get())) {}
_dataVariance(obj._dataVariance)
{
if (copyop.getCopyFlags()&osg::CopyOp::DEEP_COPY_USERDATA)
{
_userDataContainer = new UserDataContainer(*obj._userDataContainer, copyop);
}
else
{
_userDataContainer = obj._userDataContainer;
}
}
void Object::setThreadSafeRefUnref(bool threadSafe)
{
Referenced::setThreadSafeRefUnref(threadSafe);
if (_userDataContainer.valid()) _userDataContainer->setThreadSafeRefUnref(threadSafe);
}
void Object::setUserData(Referenced* obj)
{
getOrCreateUserDataContainer()->_userData = obj;
}
Referenced* Object::getUserData()
{
return _userDataContainer.valid() ? _userDataContainer->_userData.get() : 0;
}
const Referenced* Object::getUserData() const
{
return _userDataContainer.valid() ? _userDataContainer->_userData.get() : 0;
}
void 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;
}
// object not already on user data container so add it in.
_userDataContainer->_objectList.push_back(obj);
}
void Object::removeUserObject(unsigned int i)
{
if (_userDataContainer.valid() && i<_userDataContainer->_objectList.size())
{
_userDataContainer->_objectList.erase(_userDataContainer->_objectList.begin()+i);
}
}
void Object::setUserObject(unsigned int i, Object* obj)
{
if (_userDataContainer.valid() && i<_userDataContainer->_objectList.size())
{
_userDataContainer->_objectList[i] = obj;
}
}
Object* Object::getUserObject(unsigned int i)
{
if (_userDataContainer.valid() && i<_userDataContainer->_objectList.size())
{
return _userDataContainer->_objectList[i].get();
}
return 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) const
{
if (_userDataContainer.valid())
{
for(unsigned int i = 0; 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) const
{
if (_userDataContainer.valid())
{
for(unsigned int i = 0; 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)
{
if (_userDataContainer.valid())
{
unsigned int i = getUserObjectIndex(name);
return (i<_userDataContainer->_objectList.size()) ? _userDataContainer->_objectList[i].get() : 0;
}
else
{
return 0;
}
}
const Object* Object::getUserObject(const std::string& name) const
{
if (_userDataContainer.valid())
{
unsigned int i = getUserObjectIndex(name);
return (i<_userDataContainer->_objectList.size()) ? _userDataContainer->_objectList[i].get() : 0;
}
else
{
return 0;
}
}
unsigned int Object::getNumUserObjects() const
{
return _userDataContainer.valid() ? _userDataContainer->_objectList.size() : 0;
}
void Object::setDescriptions(const DescriptionList& descriptions)
{
getOrCreateUserDataContainer()->_descriptionList = descriptions;
}
Object::DescriptionList& Object::getDescriptions()
{
return getOrCreateUserDataContainer()->_descriptionList;
}
static OpenThreads::Mutex s_mutex_StaticDescriptionList;
static const Object::DescriptionList& getStaticDescriptionList()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_mutex_StaticDescriptionList);
static Object::DescriptionList s_descriptionList;
return s_descriptionList;
}
const Object::DescriptionList& Object::getDescriptions() const
{
if (_userDataContainer.valid()) return _userDataContainer->_descriptionList;
else return getStaticDescriptionList();
}
std::string& Object::getDescription(unsigned int i)
{
return getOrCreateUserDataContainer()->_descriptionList[i];
}
const std::string& Object::getDescription(unsigned int i) const
{
if (_userDataContainer.valid()) return _userDataContainer->_descriptionList[i];
else return getStaticDescriptionList()[i];
}
unsigned int Object::getNumDescriptions() const
{
return _userDataContainer.valid() ? _userDataContainer->_descriptionList.size() : 0;
}
void Object::addDescription(const std::string& desc)
{
getOrCreateUserDataContainer()->_descriptionList.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