]> git.mxchange.org Git - simgear.git/blob - simgear/props/PropertyBasedMgr.cxx
b3d3ed747581450cac288e7b6f96760c7858a16c
[simgear.git] / simgear / props / PropertyBasedMgr.cxx
1 // Base class for property controlled subsystems
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #include "PropertyBasedMgr.hxx"
20
21 #include <stdexcept>
22 #include <string>
23
24 namespace simgear
25 {
26
27   //----------------------------------------------------------------------------
28   void PropertyBasedMgr::init()
29   {
30     _props->addChangeListener(this);
31     _props->fireCreatedRecursive();
32   }
33
34   //----------------------------------------------------------------------------
35   void PropertyBasedMgr::shutdown()
36   {
37     _props->removeChangeListener(this);
38   }
39
40   //----------------------------------------------------------------------------
41   void PropertyBasedMgr::update(double delta_time_sec)
42   {
43     for( size_t i = 0; i < _elements.size(); ++i )
44       if( _elements[i] )
45         _elements[i]->update(delta_time_sec);
46   }
47
48   //----------------------------------------------------------------------------
49   PropertyBasedElementPtr
50   PropertyBasedMgr::createElement(const std::string& name)
51   {
52     SGPropertyNode* node = _props->addChild(_name_elements, 0, false);
53     if( !name.empty() )
54       node->setStringValue("name", name);
55
56     return getElement( node->getIndex() );
57   }
58
59   //----------------------------------------------------------------------------
60   PropertyBasedElementPtr PropertyBasedMgr::getElement(size_t index) const
61   {
62     if( index >= _elements.size() )
63       return PropertyBasedElementPtr();
64
65     return _elements[index];
66   }
67
68   //----------------------------------------------------------------------------
69   const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
70   {
71     return _props;
72   }
73
74   //----------------------------------------------------------------------------
75   PropertyBasedMgr::PropertyBasedMgr( SGPropertyNode_ptr props,
76                                       const std::string& name_elements,
77                                       ElementFactory element_factory ):
78     _props( props ),
79     _name_elements( name_elements ),
80     _element_factory( element_factory )
81   {
82
83   }
84
85   //----------------------------------------------------------------------------
86   PropertyBasedMgr::~PropertyBasedMgr()
87   {
88
89   }
90
91   //----------------------------------------------------------------------------
92   void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
93                                      SGPropertyNode * child )
94   {
95     if( parent != _props || child->getNameString() != _name_elements )
96       return;
97
98     size_t index = child->getIndex();
99
100     if( index >= _elements.size() )
101     {
102       if( index > _elements.size() )
103         SG_LOG
104         (
105           SG_GENERAL,
106           SG_WARN,
107           "Skipping unused " << _name_elements << " slot(s)!"
108         );
109
110       _elements.resize(index + 1);
111     }
112     else if( _elements[index] )
113       SG_LOG
114       (
115         SG_GENERAL,
116         SG_WARN,
117         _name_elements << "[" << index << "] already exists!"
118       );
119
120     PropertyBasedElementPtr el = _element_factory(child);
121     el->setSelf( el );
122     _elements[index] = el;
123     elementCreated( el );
124   }
125
126   //----------------------------------------------------------------------------
127   void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
128                                        SGPropertyNode * child )
129   {
130     if( parent != _props )
131       return child->fireChildrenRemovedRecursive();
132     else if( child->getNameString() != _name_elements )
133       return;
134
135     size_t index = child->getIndex();
136
137     if( index >= _elements.size() )
138       SG_LOG
139       (
140         SG_GENERAL,
141         SG_WARN,
142         "can't removed unknown " << _name_elements << "[" << index << "]!"
143       );
144     else
145       // remove the element...
146       _elements[index].reset();
147   }
148
149 } // namespace simgear