Introduce osg::Object::asNode(), asNodeVisitor(), asStateAttribute() and asUniform() to replace dynamic_cast<> usage in Callback.cpp.

git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14902 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
Robert Osfield 2015-06-09 10:49:34 +00:00
parent 37d051af5e
commit 7e05d2fd54
6 changed files with 78 additions and 12 deletions

View File

@ -95,6 +95,14 @@ class OSG_EXPORT Node : public Object
/** return the name of the node's class type.*/ /** return the name of the node's class type.*/
virtual const char* className() const { return "Node"; } virtual const char* className() const { return "Node"; }
/** Convert 'this' into a Node pointer if Object is a Node, otherwise return 0.
* Equivalent to dynamic_cast<Node*>(this).*/
virtual Node* asNode() { return this; }
/** convert 'const this' into a const Node pointer if Object is a Node, otherwise return 0.
* Equivalent to dynamic_cast<const Node*>(this).*/
virtual const Node* asNode() const { return this; }
/** convert 'this' into a Drawable pointer if Node is a Drawable, otherwise return 0. /** convert 'this' into a Drawable pointer if Node is a Drawable, otherwise return 0.
* Equivalent to dynamic_cast<Group*>(this).*/ * Equivalent to dynamic_cast<Group*>(this).*/
virtual Drawable* asDrawable() { return 0; } virtual Drawable* asDrawable() { return 0; }

View File

@ -96,6 +96,15 @@ class OSG_EXPORT NodeVisitor : public virtual Object
META_Object(osg, NodeVisitor) META_Object(osg, NodeVisitor)
/** Convert 'this' into a NodeVisitor pointer if Object is a NodeVisitor, otherwise return 0.
* Equivalent to dynamic_cast<NodeVisitor*>(this).*/
virtual NodeVisitor* asNodeVisitor() { return this; }
/** convert 'const this' into a const NodeVisitor pointer if Object is a NodeVisitor, otherwise return 0.
* Equivalent to dynamic_cast<const NodeVisitor*>(this).*/
virtual const NodeVisitor* asNodeVisitor() const { return this; }
/** Method to call to reset visitor. Useful if your visitor accumulates /** Method to call to reset visitor. Useful if your visitor accumulates
state during a traversal, and you plan to reuse the visitor. state during a traversal, and you plan to reuse the visitor.
To flush that state for the next traversal: call reset() prior To flush that state for the next traversal: call reset() prior

View File

@ -27,6 +27,10 @@ namespace osg {
// forward declare // forward declare
class State; class State;
class UserDataContainer; class UserDataContainer;
class Node;
class NodeVisitor;
class StateAttribute;
class Uniform;
#define _ADDQUOTES(def) #def #define _ADDQUOTES(def) #def
#define ADDQUOTES(def) _ADDQUOTES(def) #define ADDQUOTES(def) _ADDQUOTES(def)
@ -88,6 +92,39 @@ class OSG_EXPORT Object : public Referenced
/** return the compound class name that combines the library name and class name.*/ /** return the compound class name that combines the library name and class name.*/
std::string getCompoundClassName() const { return std::string(libraryName()) + std::string("::") + std::string(className()); } std::string getCompoundClassName() const { return std::string(libraryName()) + std::string("::") + std::string(className()); }
/** Convert 'this' into a Node pointer if Object is a Node, otherwise return 0.
* Equivalent to dynamic_cast<Node*>(this).*/
virtual Node* asNode() { return 0; }
/** convert 'const this' into a const Node pointer if Object is a Node, otherwise return 0.
* Equivalent to dynamic_cast<const Node*>(this).*/
virtual const Node* asNode() const { return 0; }
/** Convert 'this' into a NodeVisitor pointer if Object is a NodeVisitor, otherwise return 0.
* Equivalent to dynamic_cast<NodeVisitor*>(this).*/
virtual NodeVisitor* asNodeVisitor() { return 0; }
/** convert 'const this' into a const NodeVisitor pointer if Object is a NodeVisitor, otherwise return 0.
* Equivalent to dynamic_cast<const NodeVisitor*>(this).*/
virtual const NodeVisitor* asNodeVisitor() 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; }
/** convert 'const this' into a const StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
* Equivalent to dynamic_cast<const StateAttribute*>(this).*/
virtual const StateAttribute* asStateAttribute() const { return 0; }
/** Convert 'this' into a Uniform pointer if Object is a Uniform, otherwise return 0.
* Equivalent to dynamic_cast<Uniform*>(this).*/
virtual Uniform* asUniform() { return 0; }
/** convert 'const this' into a const Uniform pointer if Object is a Uniform, otherwise return 0.
* Equivalent to dynamic_cast<const Uniform*>(this).*/
virtual const Uniform* asUniform() const { return 0; }
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/

View File

@ -235,6 +235,14 @@ class OSG_EXPORT StateAttribute : public Object
virtual const char* className() const { return "StateAttribute"; } virtual const char* className() const { return "StateAttribute"; }
/** Convert 'this' into a StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
* Equivalent to dynamic_cast<StateAttribute*>(this).*/
virtual StateAttribute* asStateAttribute() { return this; }
/** convert 'const this' into a const StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
* Equivalent to dynamic_cast<const StateAttribute*>(this).*/
virtual const StateAttribute* asStateAttribute() const { return this; }
/** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/ /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
virtual Texture* asTexture() { return 0; } virtual Texture* asTexture() { return 0; }

View File

@ -551,7 +551,15 @@ class OSG_EXPORT Uniform : public Object
META_Object(osg, Uniform); META_Object(osg, Uniform);
/** Convert 'this' into a Uniform pointer if Object is a Uniform, otherwise return 0.
* Equivalent to dynamic_cast<Uniform*>(this).*/
virtual Uniform* asUniform() { return this; }
/** convert 'const this' into a const Uniform pointer if Object is a Uniform, otherwise return 0.
* Equivalent to dynamic_cast<const Uniform*>(this).*/
virtual const Uniform* asUniform() const { return this; }
/** Set the type of glUniform, ensuring it is only set once.*/ /** Set the type of glUniform, ensuring it is only set once.*/
bool setType( Type t ); bool setType( Type t );

View File

@ -25,13 +25,8 @@ bool Callback::traverse(Object* object, Object* data)
if (_nestedCallback.valid()) return _nestedCallback->run(object, data); if (_nestedCallback.valid()) return _nestedCallback->run(object, data);
else else
{ {
#if 1 osg::Node* node = object ? object->asNode() : 0;
osg::Node* node = dynamic_cast<osg::Node*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
#else
osg::Node* node = object ? data->asNode() : 0;
osg::NodeVisitor* nv = data ? data->asNodeVisitor() : 0; osg::NodeVisitor* nv = data ? data->asNodeVisitor() : 0;
#endif
if (node && nv) if (node && nv)
{ {
nv->traverse(*node); nv->traverse(*node);
@ -69,8 +64,9 @@ bool CallbackObject::run(osg::Object* object, osg::Parameters& inputParameters,
// //
bool NodeCallback::run(osg::Object* object, osg::Object* data) bool NodeCallback::run(osg::Object* object, osg::Object* data)
{ {
osg::Node* node = dynamic_cast<osg::Node*>(object); osg::Node* node = object ? object->asNode() : 0;
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data); osg::NodeVisitor* nv = data ? data->asNodeVisitor() : 0;
if (node && nv) if (node && nv)
{ {
operator()(node, nv); operator()(node, nv);
@ -95,8 +91,8 @@ void NodeCallback::operator()(Node* node, NodeVisitor* nv)
// //
bool StateAttributeCallback::run(osg::Object* object, osg::Object* data) bool StateAttributeCallback::run(osg::Object* object, osg::Object* data)
{ {
osg::StateAttribute* sa = dynamic_cast<osg::StateAttribute*>(object); osg::StateAttribute* sa = object ? object->asStateAttribute() : 0;
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data); osg::NodeVisitor* nv = data ? data->asNodeVisitor() : 0;
if (sa && nv) if (sa && nv)
{ {
operator()(sa, nv); operator()(sa, nv);
@ -114,8 +110,8 @@ bool StateAttributeCallback::run(osg::Object* object, osg::Object* data)
// //
bool UniformCallback::run(osg::Object* object, osg::Object* data) bool UniformCallback::run(osg::Object* object, osg::Object* data)
{ {
osg::Uniform* uniform = dynamic_cast<osg::Uniform*>(object); osg::Uniform* uniform = object ? object->asUniform() : 0;
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data); osg::NodeVisitor* nv = data ? data->asNodeVisitor() : 0;
if (uniform && nv) if (uniform && nv)
{ {
operator()(uniform, nv); operator()(uniform, nv);