From: Thomas Geymayer Date: Sun, 2 Jun 2013 19:19:37 +0000 (+0200) Subject: More helper methods for Canvas and PropertyBasedElement. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=2a6c50c893bc538bcfc5d0149ea9c59a4736fba9;p=simgear.git More helper methods for Canvas and PropertyBasedElement. --- diff --git a/simgear/canvas/Canvas.cxx b/simgear/canvas/Canvas.cxx index 74ccb60b..58528100 100644 --- a/simgear/canvas/Canvas.cxx +++ b/simgear/canvas/Canvas.cxx @@ -177,6 +177,15 @@ namespace canvas ); } + //---------------------------------------------------------------------------- + GroupPtr Canvas::getGroup(const std::string& name) + { + return boost::dynamic_pointer_cast + ( + _root_group->getChild(name) + ); + } + //---------------------------------------------------------------------------- GroupPtr Canvas::getRootGroup() { diff --git a/simgear/canvas/Canvas.hxx b/simgear/canvas/Canvas.hxx index 8e735528..1d508a89 100644 --- a/simgear/canvas/Canvas.hxx +++ b/simgear/canvas/Canvas.hxx @@ -103,6 +103,7 @@ namespace canvas void removeChildCanvas(const CanvasWeakPtr& canvas); GroupPtr createGroup(const std::string& name = ""); + GroupPtr getGroup(const std::string& name); GroupPtr getRootGroup(); /** diff --git a/simgear/canvas/elements/CanvasGroup.cxx b/simgear/canvas/elements/CanvasGroup.cxx index 9bfb8c2f..05e752a3 100644 --- a/simgear/canvas/elements/CanvasGroup.cxx +++ b/simgear/canvas/elements/CanvasGroup.cxx @@ -93,6 +93,20 @@ namespace canvas return child->second; } + //---------------------------------------------------------------------------- + ElementPtr Group::getChild(const std::string& id) + { + for( ChildList::iterator child = _children.begin(); + child != _children.end(); + ++child ) + { + if( child->second->get("id") == id ) + return child->second; + } + + return ElementPtr(); + } + //---------------------------------------------------------------------------- ElementPtr Group::getElementById(const std::string& id) { diff --git a/simgear/canvas/elements/CanvasGroup.hxx b/simgear/canvas/elements/CanvasGroup.hxx index 524b5dfb..d1b86b7b 100644 --- a/simgear/canvas/elements/CanvasGroup.hxx +++ b/simgear/canvas/elements/CanvasGroup.hxx @@ -48,6 +48,7 @@ namespace canvas ElementPtr createChild( const std::string& type, const std::string& id = "" ); ElementPtr getChild(const SGPropertyNode* node); + ElementPtr getChild(const std::string& id); /** * Get first child with given id (breadth-first search) diff --git a/simgear/props/PropertyBasedElement.hxx b/simgear/props/PropertyBasedElement.hxx index f402b959..bdae0537 100644 --- a/simgear/props/PropertyBasedElement.hxx +++ b/simgear/props/PropertyBasedElement.hxx @@ -55,6 +55,17 @@ namespace simgear setValue(_node->getNode(name, true), val); } + template + T get( const std::string& name, + typename boost::call_traits::param_type def = T() ) + { + SGPropertyNode const* child = _node->getNode(name); + if( !child ) + return def; + + return getValue(child); + } + virtual void setSelf(const PropertyBasedElementPtr& self); protected: diff --git a/simgear/props/props.hxx b/simgear/props/props.hxx index d06f4855..5103e8c0 100644 --- a/simgear/props/props.hxx +++ b/simgear/props/props.hxx @@ -1796,6 +1796,12 @@ inline const char * getValue(const SGPropertyNode* node) return node->getStringValue (); } +template<> +inline std::string getValue(const SGPropertyNode* node) +{ + return node->getStringValue(); +} + inline bool setValue(SGPropertyNode* node, bool value) { return node->setBoolValue(value);