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 void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
50 SGPropertyNode * child )
52 if( parent != _props || child->getNameString() != _name_elements )
55 size_t index = child->getIndex();
57 if( index >= _elements.size() )
59 if( index > _elements.size() )
64 "Skipping unused " << _name_elements << " slot(s)!"
67 _elements.resize(index + 1);
69 else if( _elements[index] )
74 _name_elements << "[" << index << "] already exists!"
77 PropertyBasedElementPtr el = _element_factory(child);
79 _elements[index] = el;
83 //----------------------------------------------------------------------------
84 void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
85 SGPropertyNode * child )
87 if( parent != _props )
88 return child->fireChildrenRemovedRecursive();
89 else if( child->getNameString() != _name_elements )
92 size_t index = child->getIndex();
94 if( index >= _elements.size() )
99 "can't removed unknown " << _name_elements << "[" << index << "]!"
102 // remove the element...
103 _elements[index].reset();
106 //----------------------------------------------------------------------------
107 const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
112 //----------------------------------------------------------------------------
113 PropertyBasedMgr::PropertyBasedMgr( SGPropertyNode_ptr props,
114 const std::string& name_elements,
115 ElementFactory element_factory ):
117 _name_elements( name_elements ),
118 _element_factory( element_factory )
123 //----------------------------------------------------------------------------
124 PropertyBasedMgr::~PropertyBasedMgr()
129 } // namespace simgear