]> git.mxchange.org Git - simgear.git/commitdiff
Canvas: More helper functions and cleanup.
authorThomas Geymayer <tomgey@gmail.com>
Fri, 7 Jun 2013 14:39:03 +0000 (16:39 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Fri, 7 Jun 2013 14:45:34 +0000 (16:45 +0200)
simgear/canvas/Canvas.cxx
simgear/canvas/elements/CanvasGroup.cxx
simgear/canvas/elements/CanvasGroup.hxx
simgear/canvas/elements/CanvasImage.cxx
simgear/canvas/elements/CanvasImage.hxx
simgear/canvas/elements/CanvasMap.cxx
simgear/canvas/elements/CanvasMap.hxx
simgear/canvas/elements/CanvasPath.cxx
simgear/canvas/elements/CanvasPath.hxx
simgear/canvas/elements/CanvasText.cxx
simgear/canvas/elements/CanvasText.hxx

index a124642c4e111110f880ec4df680d467950797d5..37877d5f3abbb63b5922b4e2da1c287da870399d 100644 (file)
@@ -173,29 +173,19 @@ namespace canvas
   //----------------------------------------------------------------------------
   GroupPtr Canvas::createGroup(const std::string& name)
   {
-    return boost::dynamic_pointer_cast<Group>
-    (
-      _root_group->createChild("group", name)
-    );
+    return _root_group->createChild<Group>(name);
   }
 
   //----------------------------------------------------------------------------
   GroupPtr Canvas::getGroup(const std::string& name)
   {
-    return boost::dynamic_pointer_cast<Group>
-    (
-      _root_group->getChild(name)
-    );
+    return _root_group->getChild<Group>(name);
   }
 
   //----------------------------------------------------------------------------
   GroupPtr Canvas::getOrCreateGroup(const std::string& name)
   {
-    GroupPtr group = getGroup(name);
-    if( group )
-      return group;
-
-    return createGroup(name);
+    return _root_group->getOrCreateChild<Group>(name);
   }
 
   //----------------------------------------------------------------------------
index 3d29c77ee68447abc7530ce275947d6083fb5da0..4b6591f9aca5937a5f824710f4c294d25d6f8587 100644 (file)
@@ -46,8 +46,18 @@ namespace canvas
     return el;
   }
 
+  /**
+   * Add canvas Element type to factory map
+   */
+  template<typename T>
+  void add(ElementFactories& factories)
+  {
+    factories[T::TYPE_NAME] = &createElement<T>;
+  }
+
   //----------------------------------------------------------------------------
   ElementFactories Group::_child_factories;
+  const std::string Group::TYPE_NAME = "group";
 
   //----------------------------------------------------------------------------
   Group::Group( const CanvasWeakPtr& canvas,
@@ -58,11 +68,11 @@ namespace canvas
   {
     if( !isInit<Group>() )
     {
-      _child_factories["group"] = &createElement<Group>;
-      _child_factories["image"] = &createElement<Image>;
-      _child_factories["map"  ] = &createElement<Map  >;
-      _child_factories["path" ] = &createElement<Path >;
-      _child_factories["text" ] = &createElement<Text >;
+      add<Group>(_child_factories);
+      add<Image>(_child_factories);
+      add<Map  >(_child_factories);
+      add<Path >(_child_factories);
+      add<Text >(_child_factories);
     }
   }
 
@@ -107,6 +117,31 @@ namespace canvas
     return ElementPtr();
   }
 
+  //----------------------------------------------------------------------------
+  ElementPtr Group::getOrCreateChild( const std::string& type,
+                                      const std::string& id )
+  {
+    ElementPtr child = getChild(id);
+    if( child )
+    {
+      if( child->getProps()->getNameString() == type )
+        return child;
+
+      SG_LOG
+      (
+        SG_GENERAL,
+        SG_WARN,
+        "Group::getOrCreateChild: type missmatch! "
+        "('" << type << "' != '" << child->getProps()->getName() << "', "
+        "id = '" << id << "')"
+      );
+
+      return ElementPtr();
+    }
+
+    return createChild(type, id);
+  }
+
   //----------------------------------------------------------------------------
   ElementPtr Group::getElementById(const std::string& id)
   {
index 8a40eaaba814eeeb62b43fc1fcbf70359688dd0d..43fa36812c9edd360ba7e0007680c4edf0adb181 100644 (file)
@@ -34,6 +34,8 @@ namespace canvas
     public Element
   {
     public:
+      static const std::string TYPE_NAME;
+
       typedef std::list< std::pair< const SGPropertyNode*,
                                     ElementPtr
                                   >
@@ -49,6 +51,33 @@ namespace canvas
                               const std::string& id = "" );
       ElementPtr getChild(const SGPropertyNode* node);
       ElementPtr getChild(const std::string& id);
+      ElementPtr getOrCreateChild( const std::string& type,
+                                   const std::string& id );
+
+      template<class T>
+      boost::shared_ptr<T> createChild(const std::string& id = "")
+      {
+        return boost::dynamic_pointer_cast<T>( createChild(T::TYPE_NAME, id) );
+      }
+
+      template<class T>
+      boost::shared_ptr<T> getChild(const SGPropertyNode* node)
+      {
+        return boost::dynamic_pointer_cast<T>( getChild(node) );
+      }
+
+      template<class T>
+      boost::shared_ptr<T> getChild(const std::string& id)
+      {
+        return boost::dynamic_pointer_cast<T>( getChild(id) );
+      }
+
+      template<class T>
+      boost::shared_ptr<T> getOrCreateChild(const std::string& id)
+      {
+        return
+          boost::dynamic_pointer_cast<T>( getOrCreateChild(T::TYPE_NAME, id) );
+      }
 
       /**
        * Get first child with given id (breadth-first search)
index bd3e74038db260e85722f703042976fb4867b249..077d004289895c5523f3181e598749d9755f04c0 100644 (file)
@@ -86,6 +86,9 @@ namespace canvas
     return true;
   }
 
+  //----------------------------------------------------------------------------
+  const std::string Image::TYPE_NAME = "image";
+
   //----------------------------------------------------------------------------
   Image::Image( const CanvasWeakPtr& canvas,
                 const SGPropertyNode_ptr& node,
index 741d74d80c3ddfec4e075bd863b7e8adc9605aa3..491ac19d37df3b7f063bdc030302e91ffc9f02b0 100644 (file)
@@ -34,6 +34,8 @@ namespace canvas
     public Element
   {
     public:
+      static const std::string TYPE_NAME;
+
       /**
        * @param node    Property node containing settings for this image:
        *                  rect/[left/right/top/bottom]  Dimensions of source
index 184ac8193bc30d3863333b8e953354819c48ea15..f0484ce4295f77f712b09ae6998a1cc030d3ff0f 100644 (file)
@@ -42,7 +42,9 @@ namespace simgear
 namespace canvas
 {
 
+  //----------------------------------------------------------------------------
   const std::string GEO = "-geo";
+  const std::string Map::TYPE_NAME = "map";
 
   //----------------------------------------------------------------------------
   Map::Map( const CanvasWeakPtr& canvas,
index dda9c4828a61b39e2192f36e5f4f32ce5db927fa..57e12f2cb374a6520afe3300c3905f6e2d1ffcc6 100644 (file)
@@ -35,6 +35,8 @@ namespace canvas
     public Group
   {
     public:
+      static const std::string TYPE_NAME;
+
       Map( const CanvasWeakPtr& canvas,
            const SGPropertyNode_ptr& node,
            const Style& parent_style,
index e2281905c5f07d72ca0c971b2c4f82ae562f74f9..1580285ed561b56a93e99988c6af91eefb4cb2d7 100644 (file)
@@ -469,6 +469,9 @@ namespace canvas
       };
   };
 
+  //----------------------------------------------------------------------------
+  const std::string Path::TYPE_NAME = "path";
+
   //----------------------------------------------------------------------------
   Path::Path( const CanvasWeakPtr& canvas,
               const SGPropertyNode_ptr& node,
index de7c65240d5cf8dffb728e765b8ddec8e7777961..f00e0a60ede400c06d3d942a78f95bcb54152240 100644 (file)
@@ -30,6 +30,8 @@ namespace canvas
     public Element
   {
     public:
+      static const std::string TYPE_NAME;
+
       Path( const CanvasWeakPtr& canvas,
             const SGPropertyNode_ptr& node,
             const Style& parent_style,
index 469b19aa6fc596947ebe62566d642a927a2ce902..38f116114836e692d3f5d9f1774b26e22389d39f 100644 (file)
@@ -244,6 +244,9 @@ namespace canvas
     return osgText::Text::computePositions(contextID);
   }
 
+  //----------------------------------------------------------------------------
+  const std::string Text::TYPE_NAME = "text";
+
   //----------------------------------------------------------------------------
   Text::Text( const CanvasWeakPtr& canvas,
               const SGPropertyNode_ptr& node,
index 7e90c453c6db86b9d00c86ddb00f9c3d6e0c2e8d..1866e6888f0ca7bce3f21de5684be0c15b1ee996 100644 (file)
@@ -33,6 +33,8 @@ namespace canvas
     public Element
   {
     public:
+      static const std::string TYPE_NAME;
+
       Text( const CanvasWeakPtr& canvas,
             const SGPropertyNode_ptr& node,
             const Style& parent_style,