From: Thomas Geymayer Date: Tue, 6 May 2014 16:07:23 +0000 (+0200) Subject: Canvas: base Elements/Canvas on SGWeakReferenced. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=efbec8b3671f4f909461f458fb9aa96bb86d6164;p=simgear.git Canvas: base Elements/Canvas on SGWeakReferenced. --- diff --git a/simgear/canvas/Canvas.cxx b/simgear/canvas/Canvas.cxx index 9873ba6d..f7ba3228 100644 --- a/simgear/canvas/Canvas.cxx +++ b/simgear/canvas/Canvas.cxx @@ -48,8 +48,12 @@ namespace canvas void Canvas::CullCallback::operator()( osg::Node* node, osg::NodeVisitor* nv ) { - if( (nv->getTraversalMask() & simgear::MODEL_BIT) && !_canvas.expired() ) - _canvas.lock()->enableRendering(); + if( (nv->getTraversalMask() & simgear::MODEL_BIT) ) + { + CanvasPtr canvas = _canvas.lock(); + if( canvas ) + canvas->enableRendering(); + } traverse(node, nv); } @@ -72,6 +76,13 @@ namespace canvas { _status = 0; setStatusFlags(MISSING_SIZE_X | MISSING_SIZE_Y); + + _root_group.reset( new Group(this, _node) ); + + // Remove automatically created property listener as we forward them on our + // own + _root_group->removeListener(); + _cull_callback = new CullCallback(this); } //---------------------------------------------------------------------------- @@ -239,21 +250,23 @@ namespace canvas if( _visible || _render_always ) { - BOOST_FOREACH(CanvasWeakPtr canvas, _child_canvases) + BOOST_FOREACH(CanvasWeakPtr canvas_weak, _child_canvases) { // TODO should we check if the image the child canvas is displayed // within is really visible? - if( !canvas.expired() ) - canvas.lock()->_visible = true; + CanvasPtr canvas = canvas_weak.lock(); + if( canvas ) + canvas->_visible = true; } if( _render_dirty ) { // Also mark all canvases this canvas is displayed within as dirty - BOOST_FOREACH(CanvasWeakPtr canvas, _parent_canvases) + BOOST_FOREACH(CanvasWeakPtr canvas_weak, _parent_canvases) { - if( !canvas.expired() ) - canvas.lock()->_render_dirty = true; + CanvasPtr canvas = canvas_weak.lock(); + if( canvas ) + canvas->_render_dirty = true; } } @@ -296,11 +309,7 @@ namespace canvas if( placement_factory != _placement_factories.end() ) { Placements& placements = _placements[ node->getIndex() ] = - placement_factory->second - ( - node, - boost::static_pointer_cast(_self.lock()) - ); + placement_factory->second(node, this); node->setStringValue ( "status-msg", @@ -613,23 +622,6 @@ namespace canvas return _system_adapter; } - //---------------------------------------------------------------------------- - void Canvas::setSelf(const PropertyBasedElementPtr& self) - { - PropertyBasedElement::setSelf(self); - - CanvasPtr canvas = boost::static_pointer_cast(self); - - _root_group.reset( new Group(canvas, _node) ); - _root_group->setSelf(_root_group); - - // Remove automatically created property listener as we forward them on our - // own - _root_group->removeListener(); - - _cull_callback = new CullCallback(canvas); - } - //---------------------------------------------------------------------------- void Canvas::setStatusFlags(unsigned int flags, bool set) { diff --git a/simgear/canvas/Canvas.hxx b/simgear/canvas/Canvas.hxx index c350257c..a65ff018 100644 --- a/simgear/canvas/Canvas.hxx +++ b/simgear/canvas/Canvas.hxx @@ -205,7 +205,6 @@ namespace canvas typedef std::map PlacementFactoryMap; static PlacementFactoryMap _placement_factories; - virtual void setSelf(const PropertyBasedElementPtr& self); void setStatusFlags(unsigned int flags, bool set = true); private: diff --git a/simgear/canvas/CanvasEventVisitor.cxx b/simgear/canvas/CanvasEventVisitor.cxx index 065dce06..98953034 100644 --- a/simgear/canvas/CanvasEventVisitor.cxx +++ b/simgear/canvas/CanvasEventVisitor.cxx @@ -20,7 +20,6 @@ #include "CanvasEvent.hxx" #include "CanvasEventVisitor.hxx" #include -#include namespace simgear { @@ -73,7 +72,7 @@ namespace canvas && !el.hitBound(_target_path.front().local_pos, pos, local_pos) ) return false; - EventTarget target = {el.getWeakPtr(), local_pos}; + EventTarget target = {ElementPtr(&el), local_pos}; _target_path.push_back(target); if( el.traverse(*this) || &el == _root.get() ) diff --git a/simgear/canvas/CanvasMgr.cxx b/simgear/canvas/CanvasMgr.cxx index 027d84ab..1e0bb693 100644 --- a/simgear/canvas/CanvasMgr.cxx +++ b/simgear/canvas/CanvasMgr.cxx @@ -45,25 +45,25 @@ namespace canvas //---------------------------------------------------------------------------- CanvasPtr CanvasMgr::createCanvas(const std::string& name) { - return boost::static_pointer_cast( createElement(name) ); + return static_cast( createElement(name).get() ); } //---------------------------------------------------------------------------- CanvasPtr CanvasMgr::getCanvas(size_t index) const { - return boost::static_pointer_cast( getElement(index) ); + return static_cast( getElement(index).get() ); } //---------------------------------------------------------------------------- CanvasPtr CanvasMgr::getCanvas(const std::string& name) const { - return boost::static_pointer_cast( getElement(name) ); + return static_cast( getElement(name).get() ); } //---------------------------------------------------------------------------- void CanvasMgr::elementCreated(PropertyBasedElementPtr element) { - CanvasPtr canvas = boost::static_pointer_cast(element); + CanvasPtr canvas = static_cast(element.get()); canvas->setCanvasMgr(this); } diff --git a/simgear/canvas/canvas_fwd.hxx b/simgear/canvas/canvas_fwd.hxx index e90f5907..d91696eb 100644 --- a/simgear/canvas/canvas_fwd.hxx +++ b/simgear/canvas/canvas_fwd.hxx @@ -39,8 +39,8 @@ namespace canvas #define SG_FWD_DECL(name)\ class name;\ - typedef boost::shared_ptr name##Ptr;\ - typedef boost::weak_ptr name##WeakPtr; + typedef SGSharedPtr name##Ptr;\ + typedef SGWeakPtr name##WeakPtr; SG_FWD_DECL(Canvas) SG_FWD_DECL(Element) @@ -50,6 +50,13 @@ namespace canvas SG_FWD_DECL(Path) SG_FWD_DECL(Text) +#undef SG_FWD_DECL + +#define SG_FWD_DECL(name)\ + class name;\ + typedef boost::shared_ptr name##Ptr;\ + typedef boost::weak_ptr name##WeakPtr; + SG_FWD_DECL(Event) SG_FWD_DECL(MouseEvent) SG_FWD_DECL(Placement) diff --git a/simgear/canvas/elements/CanvasElement.cxx b/simgear/canvas/elements/CanvasElement.cxx index bdbcf902..82fc659c 100644 --- a/simgear/canvas/elements/CanvasElement.cxx +++ b/simgear/canvas/elements/CanvasElement.cxx @@ -143,17 +143,6 @@ namespace canvas } } - //---------------------------------------------------------------------------- - void Element::setSelf(const PropertyBasedElementPtr& self) - { - PropertyBasedElement::setSelf(self); - - _transform->setUserData - ( - new OSGUserData(boost::static_pointer_cast(self)) - ); - } - //---------------------------------------------------------------------------- void Element::onDestroy() { @@ -168,16 +157,10 @@ namespace canvas } } - //---------------------------------------------------------------------------- - ElementWeakPtr Element::getWeakPtr() const - { - return boost::static_pointer_cast(_self.lock()); - } - //---------------------------------------------------------------------------- ElementPtr Element::getParent() { - return _parent ? _parent->getWeakPtr().lock() : ElementPtr(); + return _parent; } //---------------------------------------------------------------------------- @@ -674,6 +657,8 @@ namespace canvas "PreOrderBin", osg::StateSet::OVERRIDE_RENDERBIN_DETAILS ); + + _transform->setUserData( new OSGUserData(this) ); } //---------------------------------------------------------------------------- diff --git a/simgear/canvas/elements/CanvasElement.hxx b/simgear/canvas/elements/CanvasElement.hxx index 63649a86..62f71a19 100644 --- a/simgear/canvas/elements/CanvasElement.hxx +++ b/simgear/canvas/elements/CanvasElement.hxx @@ -90,11 +90,8 @@ namespace canvas * */ virtual ~Element() = 0; - - virtual void setSelf(const PropertyBasedElementPtr& self); virtual void onDestroy(); - ElementWeakPtr getWeakPtr() const; ElementPtr getParent(); /** @@ -200,9 +197,7 @@ namespace canvas const Style& style, Element* parent ) { - ElementPtr el( new Derived(canvas, node, style, parent) ); - el->setSelf(el); - return el; + return ElementPtr( new Derived(canvas, node, style, parent) ); } protected: diff --git a/simgear/canvas/elements/CanvasGroup.cxx b/simgear/canvas/elements/CanvasGroup.cxx index 51523f42..9548c95e 100644 --- a/simgear/canvas/elements/CanvasGroup.cxx +++ b/simgear/canvas/elements/CanvasGroup.cxx @@ -146,7 +146,7 @@ namespace canvas if( el->get("id") == id ) return el; - GroupPtr group = boost::dynamic_pointer_cast(el); + Group* group = dynamic_cast(el.get()); if( group ) groups.push_back(group); } diff --git a/simgear/canvas/elements/CanvasGroup.hxx b/simgear/canvas/elements/CanvasGroup.hxx index 74adeafe..8825d909 100644 --- a/simgear/canvas/elements/CanvasGroup.hxx +++ b/simgear/canvas/elements/CanvasGroup.hxx @@ -56,28 +56,27 @@ namespace canvas const std::string& id ); template - boost::shared_ptr createChild(const std::string& id = "") + SGSharedPtr createChild(const std::string& id = "") { - return boost::dynamic_pointer_cast( createChild(T::TYPE_NAME, id) ); + return dynamic_cast( createChild(T::TYPE_NAME, id).get() ); } template - boost::shared_ptr getChild(const SGPropertyNode* node) + SGSharedPtr getChild(const SGPropertyNode* node) { - return boost::dynamic_pointer_cast( getChild(node) ); + return dynamic_cast( getChild(node).get() ); } template - boost::shared_ptr getChild(const std::string& id) + SGSharedPtr getChild(const std::string& id) { - return boost::dynamic_pointer_cast( getChild(id) ); + return dynamic_cast( getChild(id).get() ); } template - boost::shared_ptr getOrCreateChild(const std::string& id) + SGSharedPtr getOrCreateChild(const std::string& id) { - return - boost::dynamic_pointer_cast( getOrCreateChild(T::TYPE_NAME, id) ); + return dynamic_cast( getOrCreateChild(T::TYPE_NAME, id).get() ); } /** diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index 28fd2f49..c2b2315f 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -75,8 +75,9 @@ namespace canvas osg::Drawable* drawable, osg::RenderInfo* renderInfo ) const { - if( !_canvas.expired() ) - _canvas.lock()->enableRendering(); + CanvasPtr canvas = _canvas.lock(); + if( canvas ) + canvas->enableRendering(); if( !_cull_next_frame ) // TODO check if window/image should be culled diff --git a/simgear/props/PropertyBasedElement.cxx b/simgear/props/PropertyBasedElement.cxx index 44ae0128..855829c7 100644 --- a/simgear/props/PropertyBasedElement.cxx +++ b/simgear/props/PropertyBasedElement.cxx @@ -65,10 +65,4 @@ namespace simgear return _node; } - //---------------------------------------------------------------------------- - void PropertyBasedElement::setSelf(const PropertyBasedElementPtr& self) - { - _self = self; - } - } // namespace simgear diff --git a/simgear/props/PropertyBasedElement.hxx b/simgear/props/PropertyBasedElement.hxx index b2ccd30b..a0682fd0 100644 --- a/simgear/props/PropertyBasedElement.hxx +++ b/simgear/props/PropertyBasedElement.hxx @@ -20,24 +20,19 @@ #define SG_PROPERTY_BASED_ELEMENT_HXX_ #include +#include #include -#include -#include - namespace simgear { - class PropertyBasedElement; - typedef boost::shared_ptr PropertyBasedElementPtr; - typedef boost::weak_ptr PropertyBasedElementWeakPtr; - /** * Base class for a property controlled element */ class PropertyBasedElement: - public SGPropertyChangeListener + public SGPropertyChangeListener, + public SGWeakReferenced { public: PropertyBasedElement(SGPropertyNode* node); @@ -80,15 +75,20 @@ namespace simgear return getValue(child); } - virtual void setSelf(const PropertyBasedElementPtr& self); + // Unshadow what we have just hidden... + using SGWeakReferenced::get; + virtual void onDestroy() {}; protected: SGPropertyNode_ptr _node; - PropertyBasedElementWeakPtr _self; + }; + typedef SGSharedPtr PropertyBasedElementPtr; + typedef SGWeakPtr PropertyBasedElementWeakPtr; + } // namespace simgear #endif /* SG_PROPERTY_BASED_ELEMENT_HXX_ */ diff --git a/simgear/props/PropertyBasedMgr.cxx b/simgear/props/PropertyBasedMgr.cxx index 7d868d37..78d6beb5 100644 --- a/simgear/props/PropertyBasedMgr.cxx +++ b/simgear/props/PropertyBasedMgr.cxx @@ -140,7 +140,6 @@ namespace simgear } PropertyBasedElementPtr el = _element_factory(child); - el->setSelf( el ); _elements[index] = el; elementCreated( el ); } diff --git a/simgear/props/PropertyBasedMgr.hxx b/simgear/props/PropertyBasedMgr.hxx index e719ca33..15205038 100644 --- a/simgear/props/PropertyBasedMgr.hxx +++ b/simgear/props/PropertyBasedMgr.hxx @@ -22,7 +22,6 @@ #include "PropertyBasedElement.hxx" #include -#include #include #include