]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/property_based_mgr.cxx
PerformanceDB improvements.
[flightgear.git] / src / Canvas / property_based_mgr.cxx
1 // Base class for all property controlled subsystems
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #include "property_based_mgr.hxx"
20 #include "property_helper.hxx"
21 #include <Main/fg_props.hxx>
22
23 #include <stdexcept>
24 #include <string>
25
26 //------------------------------------------------------------------------------
27 void PropertyBasedMgr::init()
28 {
29   _props->addChangeListener(this);
30   canvas::triggerChangeRecursive(_props);
31 }
32
33 //------------------------------------------------------------------------------
34 void PropertyBasedMgr::shutdown()
35 {
36   _props->removeChangeListener(this);
37 }
38
39 //------------------------------------------------------------------------------
40 void PropertyBasedMgr::update(double delta_time_sec)
41 {
42   for( size_t i = 0; i < _elements.size(); ++i )
43     if( _elements[i] )
44       _elements[i]->update(delta_time_sec);
45 }
46
47 //------------------------------------------------------------------------------
48 void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
49                                    SGPropertyNode * child )
50 {
51   if( parent != _props || child->getNameString() != _name_elements )
52     return;
53
54   size_t index = child->getIndex();
55
56   if( index >= _elements.size() )
57   {
58     if( index > _elements.size() )
59       SG_LOG
60       (
61         SG_GENERAL,
62         SG_WARN,
63         "Skipping unused " << _name_elements << " slot(s)!"
64       );
65
66     _elements.resize(index + 1);
67   }
68   else if( _elements[index] )
69     SG_LOG
70     (
71       SG_GENERAL,
72       SG_WARN,
73       _name_elements << "[" << index << "] already exists!"
74     );
75
76   PropertyBasedElementPtr el = _element_factory(child);
77   el->_self = el;
78   _elements[index] = el;
79   elementCreated( el );
80 }
81
82 //------------------------------------------------------------------------------
83 void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
84                                      SGPropertyNode * child )
85 {
86   if( parent != _props )
87     return canvas::triggerRemoveRecursive(child);
88   else if( child->getNameString() != _name_elements )
89     return;
90
91   size_t index = child->getIndex();
92
93   if( index >= _elements.size() )
94     SG_LOG
95     (
96       SG_GENERAL,
97       SG_WARN,
98       "can't removed unknown " << _name_elements << "[" << index << "]!"
99     );
100   else
101     // remove the element...
102     _elements[index].reset();
103 }
104
105 //------------------------------------------------------------------------------
106 const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
107 {
108   return _props;
109 }
110
111 //------------------------------------------------------------------------------
112 PropertyBasedMgr::PropertyBasedMgr( const std::string& path_root,
113                                     const std::string& name_elements,
114                                     ElementFactory element_factory ):
115   _props( fgGetNode(path_root, true) ),
116   _name_elements( name_elements ),
117   _element_factory( element_factory )
118 {
119
120 }
121
122 //------------------------------------------------------------------------------
123 PropertyBasedMgr::~PropertyBasedMgr()
124 {
125
126 }