1 // Base class for property controlled subsystems
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "PropertyBasedMgr.hxx"
21 #include <boost/foreach.hpp>
29 //----------------------------------------------------------------------------
30 void PropertyBasedMgr::init()
32 _props->addChangeListener(this);
33 _props->fireCreatedRecursive();
36 //----------------------------------------------------------------------------
37 void PropertyBasedMgr::shutdown()
39 _props->removeChangeListener(this);
42 //----------------------------------------------------------------------------
43 void PropertyBasedMgr::update(double delta_time_sec)
45 for( size_t i = 0; i < _elements.size(); ++i )
47 _elements[i]->update(delta_time_sec);
50 //----------------------------------------------------------------------------
51 PropertyBasedElementPtr
52 PropertyBasedMgr::createElement(const std::string& name)
54 SGPropertyNode* node = _props->addChild(_name_elements, 0, false);
56 node->setStringValue("name", name);
58 return getElement( node->getIndex() );
61 //----------------------------------------------------------------------------
62 PropertyBasedElementPtr PropertyBasedMgr::getElement(size_t index) const
64 if( index >= _elements.size() )
65 return PropertyBasedElementPtr();
67 return _elements[index];
70 //----------------------------------------------------------------------------
71 PropertyBasedElementPtr
72 PropertyBasedMgr::getElement(const std::string& name) const
75 return PropertyBasedElementPtr();
77 BOOST_FOREACH(PropertyBasedElementPtr el, _elements)
78 if( el->getProps()->getStringValue("name") == name )
81 return PropertyBasedElementPtr();
84 //----------------------------------------------------------------------------
85 const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
90 //----------------------------------------------------------------------------
91 PropertyBasedMgr::PropertyBasedMgr( SGPropertyNode_ptr props,
92 const std::string& name_elements,
93 ElementFactory element_factory ):
95 _name_elements( name_elements ),
96 _element_factory( element_factory )
101 //----------------------------------------------------------------------------
102 PropertyBasedMgr::~PropertyBasedMgr()
107 //----------------------------------------------------------------------------
108 void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
109 SGPropertyNode * child )
111 if( parent != _props || child->getNameString() != _name_elements )
114 size_t index = child->getIndex();
116 if( index >= _elements.size() )
118 if( index > _elements.size() )
123 "Skipping unused " << _name_elements << " slot(s)!"
126 _elements.resize(index + 1);
128 else if( _elements[index] )
133 _name_elements << "[" << index << "] already exists!"
136 PropertyBasedElementPtr el = _element_factory(child);
138 _elements[index] = el;
139 elementCreated( el );
142 //----------------------------------------------------------------------------
143 void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
144 SGPropertyNode * child )
146 if( parent != _props )
147 return child->fireChildrenRemovedRecursive();
148 else if( child->getNameString() != _name_elements )
151 size_t index = child->getIndex();
153 if( index >= _elements.size() )
158 "can't removed unknown " << _name_elements << "[" << index << "]!"
161 // remove the element...
162 _elements[index].reset();
165 } // namespace simgear