From 340615de553961b8ce7963cce0be1ca90ce502cb Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 18 Jan 2016 19:04:28 +0000 Subject: [PATCH] Replaced dynamic_cast<*Callback> with as*Callback() implementation/usage. --- include/osg/Callback | 118 +++++++++++++++++- include/osg/Drawable | 58 +-------- include/osg/Node | 7 -- include/osg/Object | 26 ++++ include/osgGA/EventVisitor | 6 +- include/osgUtil/UpdateVisitor | 6 +- src/osg/Callback.cpp | 40 ++++++ src/osg/Drawable.cpp | 36 ------ .../serializers/osg/UpdateCallback.cpp | 4 +- 9 files changed, 192 insertions(+), 109 deletions(-) diff --git a/include/osg/Callback b/include/osg/Callback index 9dae40985..bbd4e5c17 100644 --- a/include/osg/Callback +++ b/include/osg/Callback @@ -19,6 +19,14 @@ 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 { @@ -32,6 +40,30 @@ class OSG_EXPORT Callback : public virtual Object { 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, * 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 @@ -100,7 +132,7 @@ public: CallbackObject(const CallbackObject& co, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY): osg::Object(co, copyop), osg::Callback(co,copyop) {} - + META_Object(osg, CallbackObject); virtual CallbackObject* asCallbackObject() { return this; } @@ -124,7 +156,12 @@ public: inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name) { osg::UserDataContainer* udc = object->getUserDataContainer(); - return udc ? dynamic_cast(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) { const osg::UserDataContainer* udc = object->getUserDataContainer(); - return udc ? dynamic_cast(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.*/ @@ -147,7 +189,7 @@ inline bool runNamedCallbackObjects(osg::Object* object, const std::string& name osg::Object* obj = udc->getUserObject(i); if (obj && obj->getName()==name) { - osg::CallbackObject* co = dynamic_cast(obj); + osg::CallbackObject* co = obj->asCallbackObject(); if (co) result = co->run(object, inputParameters, outputParameters) | result; } } @@ -168,7 +210,6 @@ class OSG_EXPORT NodeCallback : public virtual Callback { public : - NodeCallback(){} NodeCallback(const NodeCallback& nc,const CopyOp& copyop): @@ -176,6 +217,9 @@ class OSG_EXPORT NodeCallback : public virtual Callback { 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.*/ 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); + virtual StateAttributeCallback* asStateAttributeCallback() { return this; } + virtual const StateAttributeCallback* asStateAttributeCallback() const { return this; } + /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/ virtual bool run(osg::Object* object, osg::Object* data); @@ -220,6 +267,9 @@ public: 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.*/ 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 #endif diff --git a/include/osg/Drawable b/include/osg/Drawable index b65d0ec2f..7f6bbbc8b 100644 --- a/include/osg/Drawable +++ b/include/osg/Drawable @@ -266,55 +266,10 @@ class OSG_EXPORT Drawable : public Node * for all graphics contexts. */ virtual void releaseGLObjects(State* state=0) const; - struct OSG_EXPORT UpdateCallback : public virtual Callback - { - UpdateCallback() {} - - 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); } - }; - + // for backwards compatibility as local implementations are now found in osg namespace within the include/osg/Callback header + typedef DrawableUpdateCallback UpdateCallback; + typedef DrawableEventCallback EventCallback; + typedef DrawableCullCallback CullCallback; /** 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 @@ -509,13 +464,10 @@ class OSG_EXPORT Drawable : public Node typedef osg::buffered_value GLObjectList; mutable GLObjectList _globjList; - ref_ptr _drawableUpdateCallback; - ref_ptr _drawableEventCallback; - - ref_ptr _drawableCullCallback; ref_ptr _drawCallback; }; + inline void Drawable::draw(RenderInfo& renderInfo) const { #ifdef OSG_GL_DISPLAYLISTS_AVAILABLE diff --git a/include/osg/Node b/include/osg/Node index b820b48ec..c0f94771b 100644 --- a/include/osg/Node +++ b/include/osg/Node @@ -103,13 +103,6 @@ class OSG_EXPORT Node : public Object * Equivalent to dynamic_cast(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(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(this).*/ - virtual const Drawable* asDrawable() const { return 0; } - /** convert 'this' into a Geometry pointer if Node is a Geometry, otherwise return 0. * Equivalent to dynamic_cast(this).*/ virtual Geometry* asGeometry() { return 0; } diff --git a/include/osg/Object b/include/osg/Object index b2ffe7d37..6a739b8f0 100644 --- a/include/osg/Object +++ b/include/osg/Object @@ -31,6 +31,8 @@ class Node; class NodeVisitor; class StateAttribute; class Uniform; +class Callback; +class CallbackObject; #define _ADDQUOTES(def) #def #define ADDQUOTES(def) _ADDQUOTES(def) @@ -126,7 +128,31 @@ class OSG_EXPORT Object : public Referenced * Equivalent to dynamic_cast(this).*/ 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(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(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(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(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(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(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); diff --git a/include/osgGA/EventVisitor b/include/osgGA/EventVisitor index fb1aa8dd9..b57871812 100644 --- a/include/osgGA/EventVisitor +++ b/include/osgGA/EventVisitor @@ -75,9 +75,9 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor osg::Callback* callback = drawable.getEventCallback(); if (callback) { - osg::Drawable::EventCallback* drawable_callback = dynamic_cast(callback); - osg::NodeCallback* node_callback = dynamic_cast(callback); - osg::CallbackObject* callback_object = dynamic_cast(callback); + osg::DrawableEventCallback* drawable_callback = callback->asDrawableEventCallback(); + osg::NodeCallback* node_callback = callback->asNodeCallback(); + osg::CallbackObject* callback_object = callback->asCallbackObject(); if (drawable_callback) drawable_callback->event(this,&drawable); if (node_callback) (*node_callback)(&drawable, this); diff --git a/include/osgUtil/UpdateVisitor b/include/osgUtil/UpdateVisitor index ed4a62703..9e9aff7bb 100644 --- a/include/osgUtil/UpdateVisitor +++ b/include/osgUtil/UpdateVisitor @@ -53,9 +53,9 @@ class OSGUTIL_EXPORT UpdateVisitor : public osg::NodeVisitor osg::Callback* callback = drawable.getUpdateCallback(); if (callback) { - osg::Drawable::UpdateCallback* drawable_callback = dynamic_cast(callback); - osg::NodeCallback* node_callback = dynamic_cast(callback); - osg::CallbackObject* callback_object = dynamic_cast(callback); + osg::DrawableUpdateCallback* drawable_callback = callback->asDrawableUpdateCallback(); + osg::NodeCallback* node_callback = callback->asNodeCallback(); + osg::CallbackObject* callback_object = callback->asCallbackObject(); if (drawable_callback) drawable_callback->update(this,&drawable); if (node_callback) (*node_callback)(&drawable, this); diff --git a/src/osg/Callback.cpp b/src/osg/Callback.cpp index c908c088b..f8e873b8d 100644 --- a/src/osg/Callback.cpp +++ b/src/osg/Callback.cpp @@ -13,6 +13,8 @@ #include #include #include +#include +#include using namespace osg; @@ -122,3 +124,41 @@ bool UniformCallback::run(osg::Object* object, osg::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); +} diff --git a/src/osg/Drawable.cpp b/src/osg/Drawable.cpp index cde7c7bdd..6fe470105 100644 --- a/src/osg/Drawable.cpp +++ b/src/osg/Drawable.cpp @@ -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(object); - osg::NodeVisitor* nv = dynamic_cast(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(object); - osg::NodeVisitor* nv = dynamic_cast(data); - if (drawable && nv) - { - event(nv, drawable); - return true; - } - else - { - return traverse(object, data); - } -} - Drawable::Drawable() { _boundingBoxComputed = false; @@ -302,9 +272,6 @@ Drawable::Drawable(const Drawable& drawable,const CopyOp& copyop): _useDisplayList(drawable._useDisplayList), _supportsVertexBufferObjects(drawable._supportsVertexBufferObjects), _useVertexBufferObjects(drawable._useVertexBufferObjects), - _drawableUpdateCallback(drawable._drawableUpdateCallback), - _drawableEventCallback(drawable._drawableEventCallback), - _drawableCullCallback(drawable._drawableCullCallback), _drawCallback(drawable._drawCallback) { setStateSet(copyop(drawable._stateset.get())); @@ -381,9 +348,6 @@ void Drawable::setThreadSafeRefUnref(bool threadSafe) Object::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); } diff --git a/src/osgWrappers/serializers/osg/UpdateCallback.cpp b/src/osgWrappers/serializers/osg/UpdateCallback.cpp index b04164fe3..62ef4a21e 100644 --- a/src/osgWrappers/serializers/osg/UpdateCallback.cpp +++ b/src/osgWrappers/serializers/osg/UpdateCallback.cpp @@ -7,8 +7,8 @@ #include REGISTER_OBJECT_WRAPPER2(UpdateCallback, - new osg::Drawable::UpdateCallback, - osg::Drawable::UpdateCallback, + new osg::DrawableUpdateCallback, + osg::DrawableUpdateCallback, "osg::UpdateCallback", "osg::Object osg::Callback osg::UpdateCallback") {}