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);
@@ -453,8 +428,6 @@ class OSG_EXPORT Node : public Object
void setNumChildrenWithOccluderNodes(unsigned int num);
NodeMask _nodeMask;
DescriptionList _descriptions;
ref_ptr<StateSet> _stateset;

View File

@@ -20,6 +20,7 @@
#include <osg/Notify>
#include <string>
#include <vector>
namespace osg {
@@ -147,7 +148,10 @@ class OSG_EXPORT Object : public Referenced
/** return the name of the object's class type. Must be defined
by derived classes.*/
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,14 +194,83 @@ 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; }
/** Get user data.*/
inline Referenced* getUserData() { return _userData.get(); }
/** Get const user data.*/
inline const Referenced* getUserData() const { return _userData.get(); }
void setUserData(Referenced* obj);
/** Get user data.*/
Referenced* getUserData();
/** Get const user data.*/
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;
ref_ptr<Referenced> _userData;
/** 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