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->removeAllChildren();
40 _props->removeChangeListener(this);
43 //----------------------------------------------------------------------------
44 void PropertyBasedMgr::update(double delta_time_sec)
46 for( size_t i = 0; i < _elements.size(); ++i )
48 _elements[i]->update(delta_time_sec);
51 //----------------------------------------------------------------------------
52 PropertyBasedElementPtr
53 PropertyBasedMgr::createElement(const std::string& name)
55 SGPropertyNode* node = _props->addChild(_name_elements, 0, false);
57 node->setStringValue("name", name);
59 return getElement( node->getIndex() );
62 //----------------------------------------------------------------------------
63 PropertyBasedElementPtr PropertyBasedMgr::getElement(size_t index) const
65 if( index >= _elements.size() )
66 return PropertyBasedElementPtr();
68 return _elements[index];
71 //----------------------------------------------------------------------------
72 PropertyBasedElementPtr
73 PropertyBasedMgr::getElement(const std::string& name) const
76 return PropertyBasedElementPtr();
78 BOOST_FOREACH(PropertyBasedElementPtr el, _elements)
79 if( el && el->getProps()->getStringValue("name") == name )
82 return PropertyBasedElementPtr();
85 //----------------------------------------------------------------------------
86 const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
91 //----------------------------------------------------------------------------
92 PropertyBasedMgr::PropertyBasedMgr( SGPropertyNode_ptr props,
93 const std::string& name_elements,
94 ElementFactory element_factory ):
96 _name_elements( name_elements ),
97 _element_factory( element_factory )
102 //----------------------------------------------------------------------------
103 PropertyBasedMgr::~PropertyBasedMgr()
108 //----------------------------------------------------------------------------
109 void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
110 SGPropertyNode * child )
112 if( parent != _props || child->getNameString() != _name_elements )
115 size_t index = child->getIndex();
117 if( index >= _elements.size() )
119 if( index > _elements.size() )
124 "Skipping unused " << _name_elements << " slot(s)!"
127 _elements.resize(index + 1);
129 else if( _elements[index] )
135 _name_elements << "[" << index << "] already exists!"
138 // Give anything holding a reference to this element to release it
139 _elements[index]->onDestroy();
142 PropertyBasedElementPtr el = _element_factory(child);
143 _elements[index] = el;
144 elementCreated( el );
147 //----------------------------------------------------------------------------
148 void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
149 SGPropertyNode * child )
151 if( parent != _props )
152 return child->fireChildrenRemovedRecursive();
153 else if( child->getNameString() != _name_elements )
156 size_t index = child->getIndex();
158 if( index >= _elements.size() )
163 "can't removed unknown " << _name_elements << "[" << index << "]!"
167 // remove the element...
168 _elements[index]->onDestroy();
169 _elements[index].reset();
173 } // namespace simgear