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"
27 //----------------------------------------------------------------------------
28 void PropertyBasedMgr::init()
30 _props->addChangeListener(this);
31 _props->fireCreatedRecursive();
34 //----------------------------------------------------------------------------
35 void PropertyBasedMgr::shutdown()
37 _props->removeChangeListener(this);
40 //----------------------------------------------------------------------------
41 void PropertyBasedMgr::update(double delta_time_sec)
43 for( size_t i = 0; i < _elements.size(); ++i )
45 _elements[i]->update(delta_time_sec);
48 //----------------------------------------------------------------------------
49 PropertyBasedElementPtr
50 PropertyBasedMgr::createElement(const std::string& name)
52 SGPropertyNode* node = _props->addChild(_name_elements, 0, false);
54 node->setStringValue("name", name);
56 return getElement( node->getIndex() );
59 //----------------------------------------------------------------------------
60 PropertyBasedElementPtr PropertyBasedMgr::getElement(size_t index) const
62 if( index >= _elements.size() )
63 return PropertyBasedElementPtr();
65 return _elements[index];
68 //----------------------------------------------------------------------------
69 const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
74 //----------------------------------------------------------------------------
75 PropertyBasedMgr::PropertyBasedMgr( SGPropertyNode_ptr props,
76 const std::string& name_elements,
77 ElementFactory element_factory ):
79 _name_elements( name_elements ),
80 _element_factory( element_factory )
85 //----------------------------------------------------------------------------
86 PropertyBasedMgr::~PropertyBasedMgr()
91 //----------------------------------------------------------------------------
92 void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
93 SGPropertyNode * child )
95 if( parent != _props || child->getNameString() != _name_elements )
98 size_t index = child->getIndex();
100 if( index >= _elements.size() )
102 if( index > _elements.size() )
107 "Skipping unused " << _name_elements << " slot(s)!"
110 _elements.resize(index + 1);
112 else if( _elements[index] )
117 _name_elements << "[" << index << "] already exists!"
120 PropertyBasedElementPtr el = _element_factory(child);
122 _elements[index] = el;
123 elementCreated( el );
126 //----------------------------------------------------------------------------
127 void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
128 SGPropertyNode * child )
130 if( parent != _props )
131 return child->fireChildrenRemovedRecursive();
132 else if( child->getNameString() != _name_elements )
135 size_t index = child->getIndex();
137 if( index >= _elements.size() )
142 "can't removed unknown " << _name_elements << "[" << index << "]!"
145 // remove the element...
146 _elements[index].reset();
149 } // namespace simgear