Replaced dynamic_cast<*Callback> with as*Callback() implementation/usage.

This commit is contained in:
Robert Osfield 2016-01-18 19:04:28 +00:00
parent 48225171e0
commit 340615de55
9 changed files with 192 additions and 109 deletions

View File

@ -19,6 +19,14 @@
namespace osg { namespace osg {
// forward declare
class CallbackObject;
class NodeCallback;
class StateAtteributeCallback;
class UniformCallback;
class DrawableUpdateCallback;
class DrawableEventCallback;
class DrawableCullCallback;
class OSG_EXPORT Callback : public virtual Object { class OSG_EXPORT Callback : public virtual Object {
@ -32,6 +40,30 @@ class OSG_EXPORT Callback : public virtual Object {
META_Object(osg, Callback); META_Object(osg, Callback);
virtual Callback* asCallback() { return this; }
virtual const Callback* asCallback() const { return this; }
virtual CallbackObject* asCallbackObject() { return 0; }
virtual const CallbackObject* asCallbackObject() const { return 0; }
virtual NodeCallback* asNodeCallback() { return 0; }
virtual const NodeCallback* asNodeCallback() const { return 0; }
virtual StateAttributeCallback* asStateAttributeCallback() { return 0; }
virtual const StateAttributeCallback* asStateAttributeCallback() const { return 0; }
virtual UniformCallback* asUniformCallback() { return 0; }
virtual const UniformCallback* asUniformCallback() const { return 0; }
virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return 0; }
virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return 0; }
virtual DrawableEventCallback* asDrawableEventCallback() { return 0; }
virtual const DrawableEventCallback* asDrawableEventCallback() const { return 0; }
virtual DrawableCullCallback* asDrawableCullCallback() { return 0; }
virtual const DrawableCullCallback* asDrawableCullCallback() const { return 0; }
/** Invoke the callback, first parameter is the Object that the callback is attached to, /** Invoke the callback, first parameter is the Object that the callback is attached to,
* the second parameter, the data, is typically the NodeVisitor that is invoking the callback. * the second parameter, the data, is typically the NodeVisitor that is invoking the callback.
* The run(..) method may be overriden by users directly, or if the user is using one of the old * The run(..) method may be overriden by users directly, or if the user is using one of the old
@ -124,7 +156,12 @@ public:
inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name) inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name)
{ {
osg::UserDataContainer* udc = object->getUserDataContainer(); osg::UserDataContainer* udc = object->getUserDataContainer();
return udc ? dynamic_cast<osg::CallbackObject*>(udc->getUserObject(name)) : 0; if (!udc) return 0;
osg::Object* obj = udc->getUserObject(name);
if (!obj) return 0;
return obj->asCallbackObject();
} }
@ -132,7 +169,12 @@ inline CallbackObject* getCallbackObject(osg::Object* object, const std::string&
inline const CallbackObject* getCallbackObject(const osg::Object* object, const std::string& name) inline const CallbackObject* getCallbackObject(const osg::Object* object, const std::string& name)
{ {
const osg::UserDataContainer* udc = object->getUserDataContainer(); const osg::UserDataContainer* udc = object->getUserDataContainer();
return udc ? dynamic_cast<const osg::CallbackObject*>(udc->getUserObject(name)) : 0; if (!udc) return 0;
const osg::Object* obj = udc->getUserObject(name);
if (!obj) return 0;
return obj->asCallbackObject();
} }
/** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/ /** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/
@ -147,7 +189,7 @@ inline bool runNamedCallbackObjects(osg::Object* object, const std::string& name
osg::Object* obj = udc->getUserObject(i); osg::Object* obj = udc->getUserObject(i);
if (obj && obj->getName()==name) if (obj && obj->getName()==name)
{ {
osg::CallbackObject* co = dynamic_cast<osg::CallbackObject*>(obj); osg::CallbackObject* co = obj->asCallbackObject();
if (co) result = co->run(object, inputParameters, outputParameters) | result; if (co) result = co->run(object, inputParameters, outputParameters) | result;
} }
} }
@ -168,7 +210,6 @@ class OSG_EXPORT NodeCallback : public virtual Callback {
public : public :
NodeCallback(){} NodeCallback(){}
NodeCallback(const NodeCallback& nc,const CopyOp& copyop): NodeCallback(const NodeCallback& nc,const CopyOp& copyop):
@ -176,6 +217,9 @@ class OSG_EXPORT NodeCallback : public virtual Callback {
META_Object(osg,NodeCallback); META_Object(osg,NodeCallback);
virtual NodeCallback* asNodeCallback() { return this; }
virtual const NodeCallback* asNodeCallback() const { return this; }
/** NodeCallback overrides the Callback::run() method to adapt it the old style NodeCallback::operator()(Node* node, NodeVisitor* nv) method.*/ /** NodeCallback overrides the Callback::run() method to adapt it the old style NodeCallback::operator()(Node* node, NodeVisitor* nv) method.*/
virtual bool run(osg::Object* object, osg::Object* data); virtual bool run(osg::Object* object, osg::Object* data);
@ -200,6 +244,9 @@ class OSG_EXPORT StateAttributeCallback : public virtual osg::Callback
META_Object(osg,StateAttributeCallback); META_Object(osg,StateAttributeCallback);
virtual StateAttributeCallback* asStateAttributeCallback() { return this; }
virtual const StateAttributeCallback* asStateAttributeCallback() const { return this; }
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/ /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
virtual bool run(osg::Object* object, osg::Object* data); virtual bool run(osg::Object* object, osg::Object* data);
@ -220,6 +267,9 @@ public:
META_Object(osg, UniformCallback); META_Object(osg, UniformCallback);
virtual UniformCallback* asUniformCallback() { return this; }
virtual const UniformCallback* asUniformCallback() const { return this; }
/** override Callback::run() entry point to adapt to UniformCallback::run(..) method.*/ /** override Callback::run() entry point to adapt to UniformCallback::run(..) method.*/
virtual bool run(osg::Object* object, osg::Object* data); virtual bool run(osg::Object* object, osg::Object* data);
@ -228,6 +278,64 @@ public:
}; };
// forward declare
class Drawable;
class State;
class RenderInfo;
struct OSG_EXPORT DrawableUpdateCallback : public virtual Callback
{
DrawableUpdateCallback() {}
DrawableUpdateCallback(const DrawableUpdateCallback&,const CopyOp&) {}
META_Object(osg,DrawableUpdateCallback);
virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return this; }
virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return this; }
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
virtual bool run(osg::Object* object, osg::Object* data);
/** do customized update code.*/
virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
};
struct OSG_EXPORT DrawableEventCallback : public virtual Callback
{
DrawableEventCallback() {}
DrawableEventCallback(const DrawableEventCallback&,const CopyOp&) {}
META_Object(osg,DrawableEventCallback);
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
virtual bool run(osg::Object* object, osg::Object* data);
/** do customized Event code. */
virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
};
struct DrawableCullCallback : public virtual Callback
{
DrawableCullCallback() {}
DrawableCullCallback(const DrawableCullCallback&,const CopyOp&) {}
META_Object(osg,DrawableCullCallback);
// just use the standard run implementation to passes run onto any nested callbacks.
using Callback::run;
/** deprecated.*/
virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }
/** do customized cull code, return true if drawable should be culled.*/
virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const;
};
} // namespace } // namespace
#endif #endif

View File

@ -266,55 +266,10 @@ class OSG_EXPORT Drawable : public Node
* for all graphics contexts. */ * for all graphics contexts. */
virtual void releaseGLObjects(State* state=0) const; virtual void releaseGLObjects(State* state=0) const;
struct OSG_EXPORT UpdateCallback : public virtual Callback // for backwards compatibility as local implementations are now found in osg namespace within the include/osg/Callback header
{ typedef DrawableUpdateCallback UpdateCallback;
UpdateCallback() {} typedef DrawableEventCallback EventCallback;
typedef DrawableCullCallback CullCallback;
UpdateCallback(const UpdateCallback&,const CopyOp&) {}
META_Object(osg,UpdateCallback);
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
virtual bool run(osg::Object* object, osg::Object* data);
/** do customized update code.*/
virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
};
struct OSG_EXPORT EventCallback : public virtual Callback
{
EventCallback() {}
EventCallback(const EventCallback&,const CopyOp&) {}
META_Object(osg,EventCallback);
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
virtual bool run(osg::Object* object, osg::Object* data);
/** do customized Event code. */
virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
};
struct CullCallback : public virtual Callback
{
CullCallback() {}
CullCallback(const CullCallback&,const CopyOp&) {}
META_Object(osg,CullCallback);
// just use the standard run implementation to passes run onto any nested callbacks.
using Callback::run;
/** deprecated.*/
virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }
/** do customized cull code, return true if drawable should be culled.*/
virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const { return cull(nv, drawable, renderInfo? renderInfo->getState():0); }
};
/** Callback attached to an Drawable which allows the users to customize the drawing of an exist Drawable object. /** Callback attached to an Drawable which allows the users to customize the drawing of an exist Drawable object.
* The draw callback is implement as a replacement to the Drawable's own drawImplementation() method, if the * The draw callback is implement as a replacement to the Drawable's own drawImplementation() method, if the
@ -509,13 +464,10 @@ class OSG_EXPORT Drawable : public Node
typedef osg::buffered_value<GLuint> GLObjectList; typedef osg::buffered_value<GLuint> GLObjectList;
mutable GLObjectList _globjList; mutable GLObjectList _globjList;
ref_ptr<UpdateCallback> _drawableUpdateCallback;
ref_ptr<EventCallback> _drawableEventCallback;
ref_ptr<CullCallback> _drawableCullCallback;
ref_ptr<DrawCallback> _drawCallback; ref_ptr<DrawCallback> _drawCallback;
}; };
inline void Drawable::draw(RenderInfo& renderInfo) const inline void Drawable::draw(RenderInfo& renderInfo) const
{ {
#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE #ifdef OSG_GL_DISPLAYLISTS_AVAILABLE

View File

@ -103,13 +103,6 @@ class OSG_EXPORT Node : public Object
* Equivalent to dynamic_cast<const Node*>(this).*/ * Equivalent to dynamic_cast<const Node*>(this).*/
virtual const Node* asNode() const { return this; } virtual const Node* asNode() const { return this; }
/** convert 'this' into a Drawable pointer if Node is a Drawable, otherwise return 0.
* Equivalent to dynamic_cast<Group*>(this).*/
virtual Drawable* asDrawable() { return 0; }
/** convert 'const this' into a const Drawable pointer if Node is a Drawable, otherwise return 0.
* Equivalent to dynamic_cast<const Group*>(this).*/
virtual const Drawable* asDrawable() const { return 0; }
/** convert 'this' into a Geometry pointer if Node is a Geometry, otherwise return 0. /** convert 'this' into a Geometry pointer if Node is a Geometry, otherwise return 0.
* Equivalent to dynamic_cast<Group*>(this).*/ * Equivalent to dynamic_cast<Group*>(this).*/
virtual Geometry* asGeometry() { return 0; } virtual Geometry* asGeometry() { return 0; }

View File

@ -31,6 +31,8 @@ class Node;
class NodeVisitor; class NodeVisitor;
class StateAttribute; class StateAttribute;
class Uniform; class Uniform;
class Callback;
class CallbackObject;
#define _ADDQUOTES(def) #def #define _ADDQUOTES(def) #def
#define ADDQUOTES(def) _ADDQUOTES(def) #define ADDQUOTES(def) _ADDQUOTES(def)
@ -126,6 +128,30 @@ class OSG_EXPORT Object : public Referenced
* Equivalent to dynamic_cast<const Uniform*>(this).*/ * Equivalent to dynamic_cast<const Uniform*>(this).*/
virtual const Uniform* asUniform() const { return 0; } virtual const Uniform* asUniform() const { return 0; }
/** Convert 'this' into a Drawable pointer if Object is a Drawable, otherwise return 0.
* Equivalent to dynamic_cast<Drawable*>(this).*/
virtual Drawable* asDrawable() { return 0; }
/** convert 'const this' into a const Drawable pointer if Object is a Drawable, otherwise return 0.
* Equivalent to dynamic_cast<const Drawable*>(this).*/
virtual const Drawable* asDrawable() const { return 0; }
/** Convert 'this' into a Callback pointer if Object is a Callback, otherwise return 0.
* Equivalent to dynamic_cast<Callback*>(this).*/
virtual Callback* asCallback() { return 0; }
/** convert 'const this' into a const Callback pointer if Object is a Callback, otherwise return 0.
* Equivalent to dynamic_cast<const Callback*>(this).*/
virtual const Callback* asCallback() const { return 0; }
/** Convert 'this' into a CallbackObject pointer if Object is a CallbackObject, otherwise return 0.
* Equivalent to dynamic_cast<CallbackObject*>(this).*/
virtual CallbackObject* asCallbackObject() { return 0; }
/** convert 'const this' into a const CallbackObject pointer if Object is a CallbackObject, otherwise return 0.
* 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.*/ /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe); virtual void setThreadSafeRefUnref(bool threadSafe);

View File

@ -75,9 +75,9 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
osg::Callback* callback = drawable.getEventCallback(); osg::Callback* callback = drawable.getEventCallback();
if (callback) if (callback)
{ {
osg::Drawable::EventCallback* drawable_callback = dynamic_cast<osg::Drawable::EventCallback*>(callback); osg::DrawableEventCallback* drawable_callback = callback->asDrawableEventCallback();
osg::NodeCallback* node_callback = dynamic_cast<osg::NodeCallback*>(callback); osg::NodeCallback* node_callback = callback->asNodeCallback();
osg::CallbackObject* callback_object = dynamic_cast<osg::CallbackObject*>(callback); osg::CallbackObject* callback_object = callback->asCallbackObject();
if (drawable_callback) drawable_callback->event(this,&drawable); if (drawable_callback) drawable_callback->event(this,&drawable);
if (node_callback) (*node_callback)(&drawable, this); if (node_callback) (*node_callback)(&drawable, this);

View File

@ -53,9 +53,9 @@ class OSGUTIL_EXPORT UpdateVisitor : public osg::NodeVisitor
osg::Callback* callback = drawable.getUpdateCallback(); osg::Callback* callback = drawable.getUpdateCallback();
if (callback) if (callback)
{ {
osg::Drawable::UpdateCallback* drawable_callback = dynamic_cast<osg::Drawable::UpdateCallback*>(callback); osg::DrawableUpdateCallback* drawable_callback = callback->asDrawableUpdateCallback();
osg::NodeCallback* node_callback = dynamic_cast<osg::NodeCallback*>(callback); osg::NodeCallback* node_callback = callback->asNodeCallback();
osg::CallbackObject* callback_object = dynamic_cast<osg::CallbackObject*>(callback); osg::CallbackObject* callback_object = callback->asCallbackObject();
if (drawable_callback) drawable_callback->update(this,&drawable); if (drawable_callback) drawable_callback->update(this,&drawable);
if (node_callback) (*node_callback)(&drawable, this); if (node_callback) (*node_callback)(&drawable, this);

View File

@ -13,6 +13,8 @@
#include <osg/Node> #include <osg/Node>
#include <osg/NodeVisitor> #include <osg/NodeVisitor>
#include <osg/ScriptEngine> #include <osg/ScriptEngine>
#include <osg/RenderInfo>
#include <osg/Drawable>
using namespace osg; using namespace osg;
@ -122,3 +124,41 @@ bool UniformCallback::run(osg::Object* object, osg::Object* data)
return traverse(object, data); return traverse(object, data);
} }
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
bool DrawableUpdateCallback::run(osg::Object* object, osg::Object* data)
{
osg::Drawable* drawable = object->asDrawable();
osg::NodeVisitor* nv = data->asNodeVisitor();
if (drawable && nv)
{
update(nv, drawable);
return true;
}
else
{
return traverse(object, data);
}
}
bool DrawableEventCallback::run(osg::Object* object, osg::Object* data)
{
osg::Drawable* drawable = object->asDrawable();
osg::NodeVisitor* nv = data->asNodeVisitor();
if (drawable && nv)
{
event(nv, drawable);
return true;
}
else
{
return traverse(object, data);
}
}
bool DrawableCullCallback::cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const
{
return cull(nv, drawable, renderInfo? renderInfo->getState():0);
}

View File

@ -239,36 +239,6 @@ void Drawable::deleteDisplayList(unsigned int contextID,GLuint globj, unsigned i
} }
bool Drawable::UpdateCallback::run(osg::Object* object, osg::Object* data)
{
osg::Drawable* drawable = dynamic_cast<osg::Drawable*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
if (drawable && nv)
{
update(nv, drawable);
return true;
}
else
{
return traverse(object, data);
}
}
bool Drawable::EventCallback::run(osg::Object* object, osg::Object* data)
{
osg::Drawable* drawable = dynamic_cast<osg::Drawable*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
if (drawable && nv)
{
event(nv, drawable);
return true;
}
else
{
return traverse(object, data);
}
}
Drawable::Drawable() Drawable::Drawable()
{ {
_boundingBoxComputed = false; _boundingBoxComputed = false;
@ -302,9 +272,6 @@ Drawable::Drawable(const Drawable& drawable,const CopyOp& copyop):
_useDisplayList(drawable._useDisplayList), _useDisplayList(drawable._useDisplayList),
_supportsVertexBufferObjects(drawable._supportsVertexBufferObjects), _supportsVertexBufferObjects(drawable._supportsVertexBufferObjects),
_useVertexBufferObjects(drawable._useVertexBufferObjects), _useVertexBufferObjects(drawable._useVertexBufferObjects),
_drawableUpdateCallback(drawable._drawableUpdateCallback),
_drawableEventCallback(drawable._drawableEventCallback),
_drawableCullCallback(drawable._drawableCullCallback),
_drawCallback(drawable._drawCallback) _drawCallback(drawable._drawCallback)
{ {
setStateSet(copyop(drawable._stateset.get())); setStateSet(copyop(drawable._stateset.get()));
@ -381,9 +348,6 @@ void Drawable::setThreadSafeRefUnref(bool threadSafe)
Object::setThreadSafeRefUnref(threadSafe); Object::setThreadSafeRefUnref(threadSafe);
if (_stateset.valid()) _stateset->setThreadSafeRefUnref(threadSafe); if (_stateset.valid()) _stateset->setThreadSafeRefUnref(threadSafe);
if (_drawableUpdateCallback.valid()) _drawableUpdateCallback->setThreadSafeRefUnref(threadSafe);
if (_drawableEventCallback.valid()) _drawableEventCallback->setThreadSafeRefUnref(threadSafe);
if (_drawableCullCallback.valid()) _drawableCullCallback->setThreadSafeRefUnref(threadSafe);
if (_drawCallback.valid()) _drawCallback->setThreadSafeRefUnref(threadSafe); if (_drawCallback.valid()) _drawCallback->setThreadSafeRefUnref(threadSafe);
} }

View File

@ -7,8 +7,8 @@
#include <osgDB/OutputStream> #include <osgDB/OutputStream>
REGISTER_OBJECT_WRAPPER2(UpdateCallback, REGISTER_OBJECT_WRAPPER2(UpdateCallback,
new osg::Drawable::UpdateCallback, new osg::DrawableUpdateCallback,
osg::Drawable::UpdateCallback, osg::DrawableUpdateCallback,
"osg::UpdateCallback", "osg::UpdateCallback",
"osg::Object osg::Callback osg::UpdateCallback") {} "osg::Object osg::Callback osg::UpdateCallback") {}