Added osg::Object::asStateSet() implementation and usage to avoid use of dynamic_cast<>

This commit is contained in:
Robert Osfield
2016-01-18 20:05:20 +00:00
parent 340615de55
commit 3e92c1fc43
4 changed files with 22 additions and 6 deletions

View File

@@ -112,6 +112,14 @@ class OSG_EXPORT Object : public Referenced
* Equivalent to dynamic_cast<const NodeVisitor*>(this).*/
virtual const NodeVisitor* asNodeVisitor() const { return 0; }
/** Convert 'this' into a StateSet pointer if Object is a StateSet, otherwise return 0.
* Equivalent to dynamic_cast<StateSet*>(this).*/
virtual StateSet* asStateSet() { return 0; }
/** convert 'const this' into a const StateSet pointer if Object is a StateSet, otherwise return 0.
* Equivalent to dynamic_cast<const StateSet*>(this).*/
virtual const StateSet* asStateSet() const { return 0; }
/** Convert 'this' into a StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
* Equivalent to dynamic_cast<StateAttribute*>(this).*/
virtual StateAttribute* asStateAttribute() { return 0; }
@@ -152,7 +160,7 @@ class OSG_EXPORT Object : public Referenced
* Equivalent to dynamic_cast<const CallbackObject*>(this).*/
virtual const CallbackObject* asCallbackObject() const { return 0; }
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);

View File

@@ -63,6 +63,14 @@ class OSG_EXPORT StateSet : public Object
bool operator == (const StateSet& rhs) const { return compare(rhs)==0; }
bool operator != (const StateSet& rhs) const { return compare(rhs)!=0; }
/** Convert 'this' into a StateSet pointer if Object is a StateSet, otherwise return 0.
* Equivalent to dynamic_cast<StateSet*>(this).*/
virtual StateSet* asStateSet() { return this; }
/** convert 'const this' into a const StateSet pointer if Object is a StateSet, otherwise return 0.
* Equivalent to dynamic_cast<const StateSet*>(this).*/
virtual const StateSet* asStateSet() const { return this; }
/** A vector of osg::Object pointers which is used to store the parent(s) of this Stateset, the parents could be osg::Node or osg::Drawable.*/
typedef std::vector<Node*> ParentList;

View File

@@ -92,8 +92,8 @@ bool osg::isTextureMode(StateAttribute::GLMode mode)
//
bool StateSet::Callback::run(osg::Object* object, osg::Object* data)
{
osg::StateSet* ss = dynamic_cast<osg::StateSet*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
osg::StateSet* ss = object->asStateSet();
osg::NodeVisitor* nv = data->asNodeVisitor();
if (ss && nv)
{
operator()(ss, nv);
@@ -313,7 +313,7 @@ int StateSet::compare(const StateSet& rhs,bool compareAttributeContents) const
if (_defineList.size()<rhs._defineList.size()) return -1;
if (_defineList.size()>rhs._defineList.size()) return 1;
// check render bin details
if ( _binMode < rhs._binMode ) return -1;
@@ -512,7 +512,7 @@ int StateSet::compare(const StateSet& rhs,bool compareAttributeContents) const
}
else if (rhs_define_itr == rhs._defineList.end()) return 1;
return 0;
}

View File

@@ -66,7 +66,7 @@ class TransformVisitor : public NodeVisitor
ritr != nodePath.rend();
++ritr, --i)
{
const osg::Camera* camera = dynamic_cast<const osg::Camera*>(*ritr);
const osg::Camera* camera = (*ritr)->asCamera();
if (camera &&
(camera->getReferenceFrame()!=osg::Transform::RELATIVE_RF || camera->getParents().empty()))
{