JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
FGPropertyReader.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Module: FGPropertyReader.cpp
4  Author: Bertrand Coconnier
5  Date started: 12/30/13
6  Purpose: Read and manage properties from XML data
7 
8  ------------- Copyright (C) 2013 Bertrand Coconnier -------------
9 
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14 
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  details.
19 
20  You should have received a copy of the GNU Lesser General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24  Further information about the GNU Lesser General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26 
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 This class reads and manages properties defined in XML data
30 
31 HISTORY
32 --------------------------------------------------------------------------------
33 12/30/13 BC Created
34 
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38 
39 #include "FGPropertyReader.h"
40 #include "FGPropertyManager.h"
41 #include "FGXMLElement.h"
42 #include "FGJSBBase.h"
43 
44 using namespace std;
45 
46 namespace JSBSim {
47 
48 IDENT(IdSrc,"$Id: FGPropertyReader.cpp,v 1.5 2014/06/14 11:58:31 bcoconni Exp $");
49 IDENT(IdHdr,ID_PROPERTYREADER);
50 
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 
55 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 
57 bool FGPropertyReader::ResetToIC(void)
58 {
59  map<SGPropertyNode_ptr, double>::iterator it = interface_prop_initial_value.begin();
60  for (;it != interface_prop_initial_value.end(); ++it) {
61  SGPropertyNode* node = it->first;
62  if (!node->getAttribute(SGPropertyNode::PRESERVE))
63  node->setDoubleValue(it->second);
64  }
65 
66  return true;
67 }
68 
69 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 
71 void FGPropertyReader::Load(Element* el, FGPropertyManager* PM, bool override)
72 {
73  // Interface properties are all stored in the interface properties array.
74  string interface_property_string = "";
75 
76  Element *property_element = el->FindElement("property");
77  if (property_element && FGJSBBase::debug_lvl > 0) {
78  cout << endl << " ";
79  if (override)
80  cout << "Overriding";
81  else
82  cout << "Declared";
83  cout << " properties" << endl << endl;
84  }
85 
86  while (property_element) {
87  SGPropertyNode* node = 0;
88  double value=0.0;
89  if ( ! property_element->GetAttributeValue("value").empty())
90  value = property_element->GetAttributeValueAsNumber("value");
91 
92  interface_property_string = property_element->GetDataLine();
93  if (PM->HasNode(interface_property_string)) {
94  if (override) {
95  node = PM->GetNode(interface_property_string);
96 
97  if (FGJSBBase::debug_lvl > 0) {
98  if (interface_prop_initial_value.find(node) == interface_prop_initial_value.end()) {
99  cout << property_element->ReadFrom()
100  << " The following property will be overridden but it has not been" << endl
101  << " defined in the current model '" << el->GetName() << "'" << endl;
102  }
103 
104  cout << " " << "Overriding value for property " << interface_property_string << endl
105  << " (old value: " << node->getDoubleValue() << " new value: " << value << ")"
106  << endl << endl;
107  }
108 
109  node->setDoubleValue(value);
110  }
111  else {
112  cerr << property_element->ReadFrom()
113  << " Property " << interface_property_string
114  << " is already defined." << endl;
115  property_element = el->FindNextElement("property");
116  continue;
117  }
118  } else {
119  node = PM->GetNode(interface_property_string, true);
120  if (node) {
121  node->setDoubleValue(value);
122 
123  if (FGJSBBase::debug_lvl > 0)
124  cout << " " << interface_property_string << " (initial value: "
125  << value << ")" << endl << endl;
126  }
127  else {
128  cerr << "Could not create property " << interface_property_string
129  << endl;
130  property_element = el->FindNextElement("property");
131  continue;
132  }
133  }
134  interface_prop_initial_value[node] = value;
135  if (property_element->GetAttributeValue("persistent") == string("true"))
136  node->setAttribute(SGPropertyNode::PRESERVE, true);
137 
138  property_element = el->FindNextElement("property");
139  }
140 
141  // End of interface property loading logic
142 }
143 }
STL namespace.