Skip to content

Commit

Permalink
Added ability to subclass from osg::Object to provide custom user dat…
Browse files Browse the repository at this point in the history
…a functionality. A new UserDataContainer provides the

default implementation of the user data functionality.
  • Loading branch information
robertosfield committed Jun 7, 2011
1 parent e6c1f5b commit 62a6de1
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 203 deletions.
50 changes: 50 additions & 0 deletions examples/osguserdata/osguserdata.cpp
Expand Up @@ -21,10 +21,53 @@

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osg/io_utils>
#include <osg/ArgumentParser>
#include <osg/UserDataContainer>

#include <osg/ValueObject>

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("<<i<<")"<<std::endl;
return osg::UserDataContainer::getUserObject(i);
}

virtual const Object* getUserObject(unsigned int i) const
{
OSG_NOTICE<<"MyUserDataContainer::getUserObject("<<i<<") const"<<std::endl;
return osg::UserDataContainer::getUserObject(i);
}


protected:
virtual ~MyUserDataContainer() {}
};

}

/** Provide basic example of providing serialization support for the MyUserDataContainer.*/
REGISTER_OBJECT_WRAPPER( MyUserDataContainer,
new MyNamespace::MyUserDataContainer,
MyNamespace::MyUserDataContainer,
"osg::Object osg::UserDataContainer MyNamespace::MyUserDataContainer" )
{
}

class MyGetValueVisitor : public osg::ValueObject::GetValueVisitor
{
public:
Expand Down Expand Up @@ -134,8 +177,15 @@ void testResults(osg::Node* node)

int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc, argv);

osg::ref_ptr<osg::Group> node = new osg::Group;

if (arguments.read("--MyUserDataContainer") || arguments.read("--mydc"))
{
node->setUserDataContainer(new MyNamespace::MyUserDataContainer);
}

int i = 10;
node->setUserValue("Int value",i);

Expand Down
86 changes: 38 additions & 48 deletions include/osg/Object
Expand Up @@ -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<T> and gets the value.
* To use this template method you need to include the osg/ValueObject header.*/
Expand All @@ -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<std::string> 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;
Expand All @@ -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*/) {}
Expand All @@ -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<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();
}
ref_ptr<osg::Object> _userDataContainer;

private:

Expand Down
99 changes: 99 additions & 0 deletions 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 <osg/Object>

#include <string>
#include <vector>

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<osg::Object> > ObjectList;

ref_ptr<Referenced> _userData;
DescriptionList _descriptionList;
ObjectList _objectList;
};


}

#endif
3 changes: 3 additions & 0 deletions src/osg/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -337,6 +339,7 @@ SET(TARGET_SRC
TransferFunction.cpp
Transform.cpp
Uniform.cpp
UserDataContainer.cpp
Version.cpp
VertexProgram.cpp
View.cpp
Expand Down

0 comments on commit 62a6de1

Please sign in to comment.