]> git.mxchange.org Git - simgear.git/blob - simgear/props/PropertyBasedMgr.cxx
Refactor Canvas and add some helpers.
[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   void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
50                                      SGPropertyNode * child )
51   {
52     if( parent != _props || child->getNameString() != _name_elements )
53       return;
54
55     size_t index = child->getIndex();
56
57     if( index >= _elements.size() )
58     {
59       if( index > _elements.size() )
60         SG_LOG
61         (
62           SG_GENERAL,
63           SG_WARN,
64           "Skipping unused " << _name_elements << " slot(s)!"
65         );
66
67       _elements.resize(index + 1);
68     }
69     else if( _elements[index] )
70       SG_LOG
71       (
72         SG_GENERAL,
73         SG_WARN,
74         _name_elements << "[" << index << "] already exists!"
75       );
76
77     PropertyBasedElementPtr el = _element_factory(child);
78     el->setSelf( el );
79     _elements[index] = el;
80     elementCreated( el );
81   }
82
83   //----------------------------------------------------------------------------
84   void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
85                                        SGPropertyNode * child )
86   {
87     if( parent != _props )
88       return child->fireChildrenRemovedRecursive();
89     else if( child->getNameString() != _name_elements )
90       return;
91
92     size_t index = child->getIndex();
93
94     if( index >= _elements.size() )
95       SG_LOG
96       (
97         SG_GENERAL,
98         SG_WARN,
99         "can't removed unknown " << _name_elements << "[" << index << "]!"
100       );
101     else
102       // remove the element...
103       _elements[index].reset();
104   }
105
106   //----------------------------------------------------------------------------
107   const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
108   {
109     return _props;
110   }
111
112   //----------------------------------------------------------------------------
113   PropertyBasedMgr::PropertyBasedMgr( SGPropertyNode_ptr props,
114                                       const std::string& name_elements,
115                                       ElementFactory element_factory ):
116     _props( props ),
117     _name_elements( name_elements ),
118     _element_factory( element_factory )
119   {
120
121   }
122
123   //----------------------------------------------------------------------------
124   PropertyBasedMgr::~PropertyBasedMgr()
125   {
126
127   }
128
129 } // namespace simgear