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;