]> git.mxchange.org Git - simgear.git/commitdiff
Canvas: base Elements/Canvas on SGWeakReferenced.
authorThomas Geymayer <tomgey@gmail.com>
Tue, 6 May 2014 16:07:23 +0000 (18:07 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Tue, 6 May 2014 16:10:42 +0000 (18:10 +0200)
14 files changed:
simgear/canvas/Canvas.cxx
simgear/canvas/Canvas.hxx
simgear/canvas/CanvasEventVisitor.cxx
simgear/canvas/CanvasMgr.cxx
simgear/canvas/canvas_fwd.hxx
simgear/canvas/elements/CanvasElement.cxx
simgear/canvas/elements/CanvasElement.hxx
simgear/canvas/elements/CanvasGroup.cxx
simgear/canvas/elements/CanvasGroup.hxx
simgear/canvas/elements/CanvasImage.cxx
simgear/props/PropertyBasedElement.cxx
simgear/props/PropertyBasedElement.hxx
simgear/props/PropertyBasedMgr.cxx
simgear/props/PropertyBasedMgr.hxx

index 9873ba6d09c8b40fc88eef0a5a74efaef31286c8..f7ba3228cef1e89c9af370a54d3ef6a5a90d2fa7 100644 (file)
@@ -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<Canvas>(_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<Canvas>(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)
   {
index c350257c0b26833df58f077ea50a5213b6d45baf..a65ff01862a9cd7f8cea0e1871347e4bfd14f44f 100644 (file)
@@ -205,7 +205,6 @@ namespace canvas
       typedef std::map<std::string, PlacementFactory> PlacementFactoryMap;
       static PlacementFactoryMap _placement_factories;
 
-      virtual void setSelf(const PropertyBasedElementPtr& self);
       void setStatusFlags(unsigned int flags, bool set = true);
 
     private:
index 065dce060e31185d787c023db57a92c22b6abc65..98953034b6842377b451b834ef681fae339d9c78 100644 (file)
@@ -20,7 +20,6 @@
 #include "CanvasEvent.hxx"
 #include "CanvasEventVisitor.hxx"
 #include <simgear/canvas/elements/CanvasElement.hxx>
-#include <iostream>
 
 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() )
index 027d84abe43e5258b3a821dc4d95fda05e8aee33..1e0bb693c7df5cd5d2dd2ffa8dfd36585310dbd2 100644 (file)
@@ -45,25 +45,25 @@ namespace canvas
   //----------------------------------------------------------------------------
   CanvasPtr CanvasMgr::createCanvas(const std::string& name)
   {
-    return boost::static_pointer_cast<Canvas>( createElement(name) );
+    return static_cast<Canvas*>( createElement(name).get() );
   }
 
   //----------------------------------------------------------------------------
   CanvasPtr CanvasMgr::getCanvas(size_t index) const
   {
-    return boost::static_pointer_cast<Canvas>( getElement(index) );
+    return static_cast<Canvas*>( getElement(index).get() );
   }
 
   //----------------------------------------------------------------------------
   CanvasPtr CanvasMgr::getCanvas(const std::string& name) const
   {
-    return boost::static_pointer_cast<Canvas>( getElement(name) );
+    return static_cast<Canvas*>( getElement(name).get() );
   }
 
   //----------------------------------------------------------------------------
   void CanvasMgr::elementCreated(PropertyBasedElementPtr element)
   {
-    CanvasPtr canvas = boost::static_pointer_cast<Canvas>(element);
+    CanvasPtr canvas = static_cast<Canvas*>(element.get());
     canvas->setCanvasMgr(this);
   }
 
index e90f590786d73582263fb48a1e7e434b5797e399..d91696eb3b28fee751a873f5f14617d9c63610f8 100644 (file)
@@ -39,8 +39,8 @@ namespace canvas
 
 #define SG_FWD_DECL(name)\
   class name;\
-  typedef boost::shared_ptr<name> name##Ptr;\
-  typedef boost::weak_ptr<name> name##WeakPtr;
+  typedef SGSharedPtr<name> name##Ptr;\
+  typedef SGWeakPtr<name> 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> name##Ptr;\
+  typedef boost::weak_ptr<name> name##WeakPtr;
+
   SG_FWD_DECL(Event)
   SG_FWD_DECL(MouseEvent)
   SG_FWD_DECL(Placement)
index bdbcf902572e68428725ae79c2539f2999b3b398..82fc659c86aecf1584a5a7bf50285a47d5d0b105 100644 (file)
@@ -143,17 +143,6 @@ namespace canvas
     }
   }
 
-  //----------------------------------------------------------------------------
-  void Element::setSelf(const PropertyBasedElementPtr& self)
-  {
-    PropertyBasedElement::setSelf(self);
-
-    _transform->setUserData
-    (
-      new OSGUserData(boost::static_pointer_cast<Element>(self))
-    );
-  }
-
   //----------------------------------------------------------------------------
   void Element::onDestroy()
   {
@@ -168,16 +157,10 @@ namespace canvas
     }
   }
 
-  //----------------------------------------------------------------------------
-  ElementWeakPtr Element::getWeakPtr() const
-  {
-    return boost::static_pointer_cast<Element>(_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) );
   }
 
   //----------------------------------------------------------------------------
index 63649a861f56caf35a5103a6b4a0c4a4ce31f2e3..62f71a199c72da20b09c507c581670670fd5b338 100644 (file)
@@ -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:
index 51523f426f2391afe196401b201e3847fc3ea6e3..9548c95e262b93a8630a2d3004acf0f96254cec0 100644 (file)
@@ -146,7 +146,7 @@ namespace canvas
       if( el->get<std::string>("id") == id )
         return el;
 
-      GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
+      Group* group = dynamic_cast<Group*>(el.get());
       if( group )
         groups.push_back(group);
     }
index 74adeafe6a7270511a1bff7b13f33b44162c257c..8825d90940dc8060b00dc88115e2fa956f90da13 100644 (file)
@@ -56,28 +56,27 @@ namespace canvas
                                    const std::string& id );
 
       template<class T>
-      boost::shared_ptr<T> createChild(const std::string& id = "")
+      SGSharedPtr<T> createChild(const std::string& id = "")
       {
-        return boost::dynamic_pointer_cast<T>( createChild(T::TYPE_NAME, id) );
+        return dynamic_cast<T*>( createChild(T::TYPE_NAME, id).get() );
       }
 
       template<class T>
-      boost::shared_ptr<T> getChild(const SGPropertyNode* node)
+      SGSharedPtr<T> getChild(const SGPropertyNode* node)
       {
-        return boost::dynamic_pointer_cast<T>( getChild(node) );
+        return dynamic_cast<T*>( getChild(node).get() );
       }
 
       template<class T>
-      boost::shared_ptr<T> getChild(const std::string& id)
+      SGSharedPtr<T> getChild(const std::string& id)
       {
-        return boost::dynamic_pointer_cast<T>( getChild(id) );
+        return dynamic_cast<T*>( getChild(id).get() );
       }
 
       template<class T>
-      boost::shared_ptr<T> getOrCreateChild(const std::string& id)
+      SGSharedPtr<T> getOrCreateChild(const std::string& id)
       {
-        return
-          boost::dynamic_pointer_cast<T>( getOrCreateChild(T::TYPE_NAME, id) );
+        return dynamic_cast<T*>( getOrCreateChild(T::TYPE_NAME, id).get() );
       }
 
       /**
index 28fd2f498e3466ca11d01f7a29a3cffcae418d76..c2b2315f1cbaeeaa6fd71f9e60bf403655d36155 100644 (file)
@@ -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
index 44ae0128544e349a5bb7377ce54cc21ae72b84d7..855829c7769d8ff01e8fadae64b5c1fba51d63d9 100644 (file)
@@ -65,10 +65,4 @@ namespace simgear
     return _node;
   }
 
-  //----------------------------------------------------------------------------
-  void PropertyBasedElement::setSelf(const PropertyBasedElementPtr& self)
-  {
-    _self = self;
-  }
-
 } // namespace simgear
index b2ccd30b74fd0c3f1802bb8b1ac647dfe62a6f58..a0682fd04bd2a33fb6faaf4aa613d5b17a46112a 100644 (file)
 #define SG_PROPERTY_BASED_ELEMENT_HXX_
 
 #include <simgear/props/props.hxx>
+#include <simgear/structure/SGWeakReferenced.hxx>
 
 #include <boost/call_traits.hpp>
-#include <boost/shared_ptr.hpp>
-#include <boost/weak_ptr.hpp>
-
 
 namespace simgear
 {
 
-  class PropertyBasedElement;
-  typedef boost::shared_ptr<PropertyBasedElement> PropertyBasedElementPtr;
-  typedef boost::weak_ptr<PropertyBasedElement> 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<T>(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<PropertyBasedElement> PropertyBasedElementPtr;
+  typedef SGWeakPtr<PropertyBasedElement> PropertyBasedElementWeakPtr;
+
 } // namespace simgear
 
 #endif /* SG_PROPERTY_BASED_ELEMENT_HXX_ */
index 7d868d37e473aa4777530690133e63e1bc1c5844..78d6beb510fe7f44853d44a90d182d835de093a8 100644 (file)
@@ -140,7 +140,6 @@ namespace simgear
     }
 
     PropertyBasedElementPtr el = _element_factory(child);
-    el->setSelf( el );
     _elements[index] = el;
     elementCreated( el );
   }
index e719ca3374d0196793e3d53617049a02497c462d..15205038db55be77cda63529f77ad27e769b450a 100644 (file)
@@ -22,7 +22,6 @@
 #include "PropertyBasedElement.hxx"
 #include <simgear/structure/subsystem_mgr.hxx>
 
-#include <boost/shared_ptr.hpp>
 #include <boost/function.hpp>
 #include <vector>