From 4e75587332fe0181f7e00a87fa04c8a0d49c1dc5 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Mon, 29 Oct 2012 16:15:10 +0100 Subject: [PATCH] Refactor PropertyBasedElement and Manager --- src/Canvas/CMakeLists.txt | 4 - src/Canvas/canvas.hxx | 4 +- src/Canvas/canvas_fwd.hpp | 4 - src/Canvas/canvas_mgr.cxx | 6 +- src/Canvas/canvas_mgr.hxx | 4 +- src/Canvas/gui_mgr.cxx | 7 +- src/Canvas/gui_mgr.hxx | 6 +- src/Canvas/property_based_element.cxx | 44 --------- src/Canvas/property_based_element.hxx | 49 ---------- src/Canvas/property_based_mgr.cxx | 126 -------------------------- src/Canvas/property_based_mgr.hxx | 77 ---------------- src/Canvas/property_helper.cxx | 23 ----- src/Canvas/property_helper.hxx | 12 --- src/Canvas/window.hxx | 4 +- 14 files changed, 19 insertions(+), 351 deletions(-) delete mode 100644 src/Canvas/property_based_element.cxx delete mode 100644 src/Canvas/property_based_element.hxx delete mode 100644 src/Canvas/property_based_mgr.cxx delete mode 100644 src/Canvas/property_based_mgr.hxx diff --git a/src/Canvas/CMakeLists.txt b/src/Canvas/CMakeLists.txt index ac1f66275..2483f353d 100644 --- a/src/Canvas/CMakeLists.txt +++ b/src/Canvas/CMakeLists.txt @@ -11,8 +11,6 @@ set(SOURCES elements/CanvasImage.cxx gui_mgr.cxx placement.cxx - property_based_element.cxx - property_based_mgr.cxx property_helper.cxx window.cxx ) @@ -28,8 +26,6 @@ set(HEADERS elements/CanvasImage.hxx gui_mgr.hxx placement.hxx - property_based_element.hxx - property_based_mgr.hxx property_helper.hxx window.hxx ) diff --git a/src/Canvas/canvas.hxx b/src/Canvas/canvas.hxx index 207145299..85f233cd4 100644 --- a/src/Canvas/canvas.hxx +++ b/src/Canvas/canvas.hxx @@ -20,11 +20,11 @@ #define CANVAS_HXX_ #include "placement.hxx" -#include "property_based_element.hxx" #include #include +#include #include #include #include @@ -33,7 +33,7 @@ #include class Canvas: - public PropertyBasedElement + public simgear::PropertyBasedElement { public: diff --git a/src/Canvas/canvas_fwd.hpp b/src/Canvas/canvas_fwd.hpp index 004fb253b..356ea1c57 100644 --- a/src/Canvas/canvas_fwd.hpp +++ b/src/Canvas/canvas_fwd.hpp @@ -30,10 +30,6 @@ class Canvas; typedef boost::shared_ptr CanvasPtr; typedef boost::weak_ptr CanvasWeakPtr; -class PropertyBasedElement; -typedef boost::shared_ptr PropertyBasedElementPtr; -typedef boost::weak_ptr PropertyBasedElementWeakPtr; - namespace canvas { const std::string VG_INIT_SIGNAL = "/sim/signals/vg-initialized-frame"; diff --git a/src/Canvas/canvas_mgr.cxx b/src/Canvas/canvas_mgr.cxx index a17507623..028d70d04 100644 --- a/src/Canvas/canvas_mgr.cxx +++ b/src/Canvas/canvas_mgr.cxx @@ -19,6 +19,8 @@ #include "canvas_mgr.hxx" #include "canvas.hxx" +#include
+ #include typedef boost::shared_ptr CanvasPtr; @@ -30,7 +32,9 @@ CanvasPtr canvasFactory(SGPropertyNode* node) //------------------------------------------------------------------------------ CanvasMgr::CanvasMgr(): - PropertyBasedMgr("/canvas/by-index", "texture", &canvasFactory) + PropertyBasedMgr( fgGetNode("/canvas/by-index", true), + "texture", + &canvasFactory ) { Canvas::addPlacementFactory ( diff --git a/src/Canvas/canvas_mgr.hxx b/src/Canvas/canvas_mgr.hxx index 568da3d61..0ae272537 100644 --- a/src/Canvas/canvas_mgr.hxx +++ b/src/Canvas/canvas_mgr.hxx @@ -20,10 +20,10 @@ #define CANVAS_MGR_H_ #include "canvas_fwd.hpp" -#include "property_based_mgr.hxx" +#include class CanvasMgr: - public PropertyBasedMgr + public simgear::PropertyBasedMgr { public: CanvasMgr(); diff --git a/src/Canvas/gui_mgr.cxx b/src/Canvas/gui_mgr.cxx index 44cad8bd0..f3436dbaf 100644 --- a/src/Canvas/gui_mgr.cxx +++ b/src/Canvas/gui_mgr.cxx @@ -20,6 +20,7 @@ #include #include +#include
#include
#include #include @@ -105,7 +106,9 @@ WindowPtr windowFactory(SGPropertyNode* node) //------------------------------------------------------------------------------ GUIMgr::GUIMgr(): - PropertyBasedMgr("/sim/gui/canvas", "window", &windowFactory), + PropertyBasedMgr( fgGetNode("/sim/gui/canvas", true), + "window", + &windowFactory ), _event_handler( new GUIEventHandler(this) ), _transform( new osg::MatrixTransform ), _width(_props, "size[0]"), @@ -164,7 +167,7 @@ void GUIMgr::shutdown() } //------------------------------------------------------------------------------ -void GUIMgr::elementCreated(PropertyBasedElementPtr element) +void GUIMgr::elementCreated(simgear::PropertyBasedElementPtr element) { canvas::WindowPtr window = boost::static_pointer_cast(element); diff --git a/src/Canvas/gui_mgr.hxx b/src/Canvas/gui_mgr.hxx index 8a1ccf786..927d73eb1 100644 --- a/src/Canvas/gui_mgr.hxx +++ b/src/Canvas/gui_mgr.hxx @@ -19,10 +19,10 @@ #ifndef CANVAS_GUI_MGR_HXX_ #define CANVAS_GUI_MGR_HXX_ -#include "property_based_mgr.hxx" #include #include +#include #include #include @@ -36,7 +36,7 @@ namespace osgGA class GUIEventHandler; class GUIMgr: - public PropertyBasedMgr + public simgear::PropertyBasedMgr { public: GUIMgr(); @@ -44,7 +44,7 @@ class GUIMgr: virtual void init(); virtual void shutdown(); - virtual void elementCreated(PropertyBasedElementPtr element); + virtual void elementCreated(simgear::PropertyBasedElementPtr element); bool handleEvent(const osgGA::GUIEventAdapter& ea); diff --git a/src/Canvas/property_based_element.cxx b/src/Canvas/property_based_element.cxx deleted file mode 100644 index 8672539c0..000000000 --- a/src/Canvas/property_based_element.cxx +++ /dev/null @@ -1,44 +0,0 @@ -// Base class for elements of property controlled subsystems -// -// Copyright (C) 2012 Thomas Geymayer -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -#include "property_based_element.hxx" - -//------------------------------------------------------------------------------ -PropertyBasedElement::PropertyBasedElement(SGPropertyNode* node): - _node(node) -{ - _node->addChangeListener(this); -} - -//------------------------------------------------------------------------------ -PropertyBasedElement::~PropertyBasedElement() -{ - _node->removeChangeListener(this); -} - -//------------------------------------------------------------------------------ -SGConstPropertyNode_ptr PropertyBasedElement::getProps() const -{ - return _node; -} - -//------------------------------------------------------------------------------ -SGPropertyNode_ptr PropertyBasedElement::getProps() -{ - return _node; -} diff --git a/src/Canvas/property_based_element.hxx b/src/Canvas/property_based_element.hxx deleted file mode 100644 index a41914df9..000000000 --- a/src/Canvas/property_based_element.hxx +++ /dev/null @@ -1,49 +0,0 @@ -// Base class for elements of property controlled subsystems -// -// Copyright (C) 2012 Thomas Geymayer -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -#ifndef PROPERTY_BASED_ELEMENT_HXX_ -#define PROPERTY_BASED_ELEMENT_HXX_ - -#include -#include - -/** - * Base class for a property controlled element - */ -class PropertyBasedElement: - public SGPropertyChangeListener -{ - public: - PropertyBasedElement(SGPropertyNode* node); - virtual ~PropertyBasedElement(); - - virtual void update(double delta_time_sec) = 0; - - SGConstPropertyNode_ptr getProps() const; - SGPropertyNode_ptr getProps(); - - protected: - - friend class PropertyBasedMgr; - - SGPropertyNode_ptr _node; - PropertyBasedElementWeakPtr _self; -}; - - -#endif /* PROPERTY_BASED_ELEMENT_HXX_ */ diff --git a/src/Canvas/property_based_mgr.cxx b/src/Canvas/property_based_mgr.cxx deleted file mode 100644 index 148c96e32..000000000 --- a/src/Canvas/property_based_mgr.cxx +++ /dev/null @@ -1,126 +0,0 @@ -// Base class for all property controlled subsystems -// -// Copyright (C) 2012 Thomas Geymayer -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -#include "property_based_mgr.hxx" -#include "property_helper.hxx" -#include
- -#include -#include - -//------------------------------------------------------------------------------ -void PropertyBasedMgr::init() -{ - _props->addChangeListener(this); - canvas::triggerChangeRecursive(_props); -} - -//------------------------------------------------------------------------------ -void PropertyBasedMgr::shutdown() -{ - _props->removeChangeListener(this); -} - -//------------------------------------------------------------------------------ -void PropertyBasedMgr::update(double delta_time_sec) -{ - for( size_t i = 0; i < _elements.size(); ++i ) - if( _elements[i] ) - _elements[i]->update(delta_time_sec); -} - -//------------------------------------------------------------------------------ -void PropertyBasedMgr::childAdded( SGPropertyNode * parent, - SGPropertyNode * child ) -{ - if( parent != _props || child->getNameString() != _name_elements ) - return; - - size_t index = child->getIndex(); - - if( index >= _elements.size() ) - { - if( index > _elements.size() ) - SG_LOG - ( - SG_GENERAL, - SG_WARN, - "Skipping unused " << _name_elements << " slot(s)!" - ); - - _elements.resize(index + 1); - } - else if( _elements[index] ) - SG_LOG - ( - SG_GENERAL, - SG_WARN, - _name_elements << "[" << index << "] already exists!" - ); - - PropertyBasedElementPtr el = _element_factory(child); - el->_self = el; - _elements[index] = el; - elementCreated( el ); -} - -//------------------------------------------------------------------------------ -void PropertyBasedMgr::childRemoved( SGPropertyNode * parent, - SGPropertyNode * child ) -{ - if( parent != _props ) - return canvas::triggerRemoveRecursive(child); - else if( child->getNameString() != _name_elements ) - return; - - size_t index = child->getIndex(); - - if( index >= _elements.size() ) - SG_LOG - ( - SG_GENERAL, - SG_WARN, - "can't removed unknown " << _name_elements << "[" << index << "]!" - ); - else - // remove the element... - _elements[index].reset(); -} - -//------------------------------------------------------------------------------ -const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const -{ - return _props; -} - -//------------------------------------------------------------------------------ -PropertyBasedMgr::PropertyBasedMgr( const std::string& path_root, - const std::string& name_elements, - ElementFactory element_factory ): - _props( fgGetNode(path_root, true) ), - _name_elements( name_elements ), - _element_factory( element_factory ) -{ - -} - -//------------------------------------------------------------------------------ -PropertyBasedMgr::~PropertyBasedMgr() -{ - -} diff --git a/src/Canvas/property_based_mgr.hxx b/src/Canvas/property_based_mgr.hxx deleted file mode 100644 index e80f85cfd..000000000 --- a/src/Canvas/property_based_mgr.hxx +++ /dev/null @@ -1,77 +0,0 @@ -// Base class for all property controlled subsystems -// -// Copyright (C) 2012 Thomas Geymayer -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -#ifndef PROPERTY_BASED_MGR_HXX_ -#define PROPERTY_BASED_MGR_HXX_ - -#include "property_based_element.hxx" -#include - -#include -#include -#include - -class PropertyBasedMgr: - public SGSubsystem, - public SGPropertyChangeListener -{ - public: - virtual void init(); - virtual void shutdown(); - - virtual void update (double delta_time_sec); - - virtual void childAdded( SGPropertyNode * parent, - SGPropertyNode * child ); - virtual void childRemoved( SGPropertyNode * parent, - SGPropertyNode * child ); - - virtual void elementCreated(PropertyBasedElementPtr element) {} - - virtual const SGPropertyNode* getPropertyRoot() const; - - protected: - - typedef boost::function - ElementFactory; - - /** Branch in the property tree for this property managed subsystem */ - SGPropertyNode* _props; - - /** Property name of managed elements */ - const std::string _name_elements; - - /** The actually managed elements */ - std::vector _elements; - - /** Function object which creates a new element */ - ElementFactory _element_factory; - - /** - * @param path_root Path to property branch used for controlling this - * subsystem - * @param name_elements The name of the nodes for the managed elements - */ - PropertyBasedMgr( const std::string& path_root, - const std::string& name_elements, - ElementFactory element_factory ); - virtual ~PropertyBasedMgr() = 0; - -}; - -#endif /* PROPERTY_BASED_MGR_HXX_ */ diff --git a/src/Canvas/property_helper.cxx b/src/Canvas/property_helper.cxx index 3579ad6e1..5b35a970c 100644 --- a/src/Canvas/property_helper.cxx +++ b/src/Canvas/property_helper.cxx @@ -132,27 +132,4 @@ namespace canvas for( size_t i = 0; i < num_channels; ++i ) nodes.push_back( getChildDefault(color, channels[i], def[i]) ); } - - //---------------------------------------------------------------------------- - void triggerChangeRecursive(SGPropertyNode* node) - { - node->getParent()->fireChildAdded(node); - - if( node->nChildren() == 0 && node->getType() != simgear::props::NONE ) - return node->fireValueChanged(); - - for( int i = 0; i < node->nChildren(); ++i ) - triggerChangeRecursive( node->getChild(i) ); - } - - //---------------------------------------------------------------------------- - void triggerRemoveRecursive(SGPropertyNode* node) - { - for( int i = 0; i < node->nChildren(); ++i ) - { - SGPropertyNode* child = node->getChild(i); - node->fireChildRemoved( child ); - triggerRemoveRecursive( child ); - } - } } diff --git a/src/Canvas/property_helper.hxx b/src/Canvas/property_helper.hxx index 92e556afc..ff2d12451 100644 --- a/src/Canvas/property_helper.hxx +++ b/src/Canvas/property_helper.hxx @@ -92,18 +92,6 @@ namespace canvas std::vector& nodes, const osg::Vec4& def = osg::Vec4(0,0,0,1) ); - /** - * Trigger a childAdded and valueChanged event for every child of node - * (Unlimited depth) and node itself. - */ - void triggerChangeRecursive(SGPropertyNode* node); - - /** - * Trigger a childRemoved event for every child of node (Unlimited depth) and - * node itself. - */ - void triggerRemoveRecursive(SGPropertyNode* node); - } // namespace canvas #endif /* PROPERTY_HELPER_HXX_ */ diff --git a/src/Canvas/window.hxx b/src/Canvas/window.hxx index cce5b1aa9..fdc0eb513 100644 --- a/src/Canvas/window.hxx +++ b/src/Canvas/window.hxx @@ -19,10 +19,10 @@ #ifndef CANVAS_WINDOW_HXX_ #define CANVAS_WINDOW_HXX_ -#include "property_based_element.hxx" #include #include +#include #include #include @@ -31,7 +31,7 @@ namespace canvas { class Window: - public PropertyBasedElement + public simgear::PropertyBasedElement { public: Window(SGPropertyNode* node); -- 2.39.2