]> git.mxchange.org Git - simgear.git/blob - simgear/props/PropertyBasedMgr.hxx
cppbind: automatic conversion of SGReferenced derived pointers.
[simgear.git] / simgear / props / PropertyBasedMgr.hxx
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 #ifndef SG_PROPERTY_BASED_MGR_HXX_
20 #define SG_PROPERTY_BASED_MGR_HXX_
21
22 #include "PropertyBasedElement.hxx"
23 #include <simgear/structure/subsystem_mgr.hxx>
24
25 #include <boost/shared_ptr.hpp>
26 #include <boost/function.hpp>
27 #include <vector>
28
29 namespace simgear
30 {
31
32   class PropertyBasedMgr:
33     public SGSubsystem,
34     public SGPropertyChangeListener
35   {
36     public:
37       virtual void init();
38       virtual void shutdown();
39
40       virtual void update (double delta_time_sec);
41
42       /**
43        * Create a new PropertyBasedElement
44        *
45        * @param name    Name of the new element
46        */
47       PropertyBasedElementPtr createElement(const std::string& name = "");
48
49       /**
50        * Get an existing PropertyBasedElement by its index
51        *
52        * @param index   Index of element node in property tree
53        */
54       PropertyBasedElementPtr getElement(size_t index) const;
55
56       /**
57        * Get an existing PropertyBasedElement by its name
58        *
59        * @param name    Name (value of child node "name" will be matched)
60        */
61       PropertyBasedElementPtr getElement(const std::string& name) const;
62
63       virtual const SGPropertyNode* getPropertyRoot() const;
64
65     protected:
66
67       typedef boost::function<PropertyBasedElementPtr(SGPropertyNode*)>
68               ElementFactory;
69
70       /** Branch in the property tree for this property managed subsystem */
71       SGPropertyNode_ptr      _props;
72
73       /** Property name of managed elements */
74       const std::string       _name_elements;
75
76       /** The actually managed elements */
77       std::vector<PropertyBasedElementPtr> _elements;
78
79       /** Function object which creates a new element */
80       ElementFactory          _element_factory;
81
82       /**
83        * @param props         Root node of property branch used for controlling
84        *                      this subsystem
85        * @param name_elements The name of the nodes for the managed elements
86        */
87       PropertyBasedMgr( SGPropertyNode_ptr props,
88                         const std::string& name_elements,
89                         ElementFactory element_factory );
90       virtual ~PropertyBasedMgr() = 0;
91
92       virtual void childAdded( SGPropertyNode * parent,
93                                SGPropertyNode * child );
94       virtual void childRemoved( SGPropertyNode * parent,
95                                  SGPropertyNode * child );
96
97       virtual void elementCreated(PropertyBasedElementPtr element) {}
98
99   };
100
101 } // namespace simgear
102
103 #endif /* SG_PROPERTY_BASED_MGR_HXX_ */