]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/digitalcomponent.cxx
Autopilot: add interface properties and property-root.
[flightgear.git] / src / Autopilot / digitalcomponent.cxx
1 // digitalcomponent.cxx - Base class for digital 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
24 #include "digitalcomponent.hxx"
25 #include <Main/fg_props.hxx>
26
27 using std::string;
28 using namespace FGXMLAutopilot;
29
30 DigitalComponent::DigitalComponent() :
31   _inverted(false)
32 {
33 }
34
35 bool DigitalComponent::InputMap::get_value( const std::string & name ) const
36 {
37   // can't use map::operator[] here since it's not const
38   const_iterator __i = lower_bound( name );
39   if (__i == end() || key_comp()(name, (*__i).first))
40     return false; // does not exist, return false
41
42   return (*__i).second->test();
43 }
44
45 /*
46   <input>
47     <name>Foo</name>
48     <condition>
49      <and>...</and>
50     </condition>
51   </input>
52   <output>
53     <name>Bar</name>
54     <property>/foo/bar</property>
55     <inverted>true</inverted>
56   </output>
57   <output>/some/property</output>
58 */
59 bool DigitalComponent::configure( SGPropertyNode& cfg_node,
60                                   const std::string& cfg_name,
61                                   SGPropertyNode& prop_root )
62 {
63   if( Component::configure(cfg_node, cfg_name, prop_root) )
64     return true;
65
66   if (cfg_name == "input") {
67     SGPropertyNode_ptr nameNode = cfg_node.getNode("name");
68     string name;
69     if( nameNode != NULL ) {
70       name = nameNode->getStringValue();
71     } else {
72       std::ostringstream buf;
73       buf << "Input" << _input.size();
74       name = buf.str();
75     }
76     _input[name] = sgReadCondition(&prop_root, &cfg_node);
77     return true;
78   } 
79
80   if (cfg_name == "output") {
81     SGPropertyNode_ptr n = cfg_node.getNode("name");
82     string name;
83     if( n != NULL ) {
84       name = n->getStringValue();
85     } else {
86       std::ostringstream buf;
87       buf << "Output" << _output.size();
88       name = buf.str();
89     }
90
91     DigitalOutput_ptr o = new DigitalOutput();
92     _output[name] = o;
93
94     if( (n = cfg_node.getNode("inverted")) != NULL )
95       o->setInverted( n->getBoolValue() );
96
97     if( (n = cfg_node.getNode("property")) != NULL )
98       o->setProperty( prop_root.getNode(n->getStringValue(), true) );
99
100     if( cfg_node.nChildren() == 0 )
101       o->setProperty( prop_root.getNode(cfg_node.getStringValue(), true) );
102
103     return true;
104   } 
105
106   if (cfg_name == "inverted") {
107     _inverted = cfg_node.getBoolValue();
108     return true;
109   }
110   
111   return false;
112 }