FlightGear next
component.cxx
Go to the documentation of this file.
1// component.cxx - Base class for autopilot components
2//
3// Written by Torsten Dreyer
4// Based heavily on work created by Curtis Olson, started January 2004.
5//
6// Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7// Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de
8//
9// This program is free software; you can redistribute it and/or
10// modify it under the terms of the GNU General Public License as
11// published by the Free Software Foundation; either version 2 of the
12// License, or (at your option) any later version.
13//
14// This program is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22//
23#include "component.hxx"
24#include <Main/fg_props.hxx>
25#include <simgear/structure/exception.hxx>
26#include <simgear/props/condition.hxx>
27
28using namespace FGXMLAutopilot;
29
31 _enable_value(NULL),
32 _enabled(false),
33 _debug(false),
34 _honor_passive(false)
35{
36}
37
39{
40 delete _enable_value;
41}
42
43//------------------------------------------------------------------------------
44bool Component::configure( SGPropertyNode& prop_root,
45 SGPropertyNode& cfg )
46{
47 for( int i = 0; i < cfg.nChildren(); ++i )
48 {
49 SGPropertyNode_ptr child = cfg.getChild(i);
50 std::string cname(child->getNameString());
51
52 if( !configure(*child, cname, prop_root)
53 && cname != "params" ) // 'params' is usually used to specify parameters
54 // in PropertList files.
55
56 // consider using the error reporting mechanism here, at level warning
57 SG_LOG(
58 SG_AUTOPILOT,
59 SG_DEV_WARN,
60 "Component::configure: unknown node: " << cname);
61 }
62
63 return true;
64}
65
66//------------------------------------------------------------------------------
67bool Component::configure( SGPropertyNode& cfg_node,
68 const std::string& cfg_name,
69 SGPropertyNode& prop_root )
70{
71 if ( cfg_name == "name" )
72 {
73 set_name(cfg_node.getStringValue());
74 return true;
75 }
76
77 if( cfg_name == "update-interval-secs" )
78 // This is handled in autopilot.cxx
79 return true;
80
81 if ( cfg_name == "debug" )
82 {
83 _debug = cfg_node.getBoolValue();
84 return true;
85 }
86
87 if ( cfg_name == "enable" )
88 {
89 SGPropertyNode_ptr prop;
90
91 if( (prop = cfg_node.getChild("condition")) != NULL ) {
92 _condition = sgReadCondition(fgGetNode("/"), prop);
93 return true;
94 }
95 if ( (prop = cfg_node.getChild( "property" )) != NULL ) {
96 _enable_prop = fgGetNode( prop->getStringValue(), true );
97 }
98
99 if ( (prop = cfg_node.getChild( "prop" )) != NULL ) {
100 _enable_prop = fgGetNode( prop->getStringValue(), true );
101 }
102
103 if ( (prop = cfg_node.getChild( "value" )) != NULL ) {
104 delete _enable_value;
105 _enable_value = new std::string(prop->getStringValue());
106 }
107
108 if ( (prop = cfg_node.getChild( "honor-passive" )) != NULL ) {
109 _honor_passive = prop->getBoolValue();
110 }
111
112 return true;
113 }
114
115 return false;
116}
117
118//------------------------------------------------------------------------------
120{
121 if( _condition )
122 return _condition->test();
123
124 if( _enable_prop ) {
125 if( _enable_value ) {
126 return *_enable_value == _enable_prop->getStringValue();
127 } else {
128 return _enable_prop->getBoolValue();
129 }
130 }
131 return true;
132}
133
134void Component::update( double dt )
135{
136 bool firstTime = false;
137 if( isPropertyEnabled() ) {
138 firstTime = !_enabled;
139 _enabled = true;
140 } else {
141 _enabled = false;
142 }
143
144 if( _enabled ) update( firstTime, dt );
145 else disabled( dt );
146}
#define i(x)
bool _honor_passive
a (historic) flag signalling the derived class that it should compute it's internal state but shall n...
Definition component.hxx:73
virtual void update(bool firstTime, double dt)=0
pure virtual function to be implemented by the derived classes.
virtual bool configure(SGPropertyNode &cfg_node, const std::string &cfg_name, SGPropertyNode &prop_root)
Definition component.cxx:67
virtual void disabled(double dt)
overideable method being called from the update() method if this component is disabled.
Definition component.hxx:60
Component()
A constructor for an empty Component.
Definition component.cxx:30
bool isPropertyEnabled()
check if this component is enabled as configured in the <enable> section
virtual ~Component()
virtual destructor to clean up resources
Definition component.cxx:38
bool _debug
debug flag, true if this component should generate some useful output on every iteration
Definition component.hxx:66
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27