1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Header: FGPropertyManager.cpp
5 Based on work originally by David Megginson
8 ------------- Copyright (C) 2002 -------------
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
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
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.
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.
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
31 #include "FGPropertyManager.h"
33 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 bool FGPropertyManager::suppress_warning = true;
52 std::vector<std::string> FGPropertyManager::tied_properties;
54 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 void FGPropertyManager::Unbind(void)
58 vector<string>::iterator it;
59 for (it = tied_properties.begin();it < tied_properties.end();it++)
63 tied_properties.clear();
66 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 string FGPropertyManager::mkPropertyName(string name, bool lowercase) {
70 /* do this two pass to avoid problems with characters getting skipped
71 because the index changed */
73 for(i=0;i<name.length();i++) {
74 if( lowercase && isupper(name[i]) )
75 name[i]=tolower(name[i]);
76 else if( isspace(name[i]) )
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 FGPropertyManager::GetNode (const string &path, bool create)
88 SGPropertyNode* node=this->getNode(path.c_str(), create);
89 if (node == 0 && !suppress_warning) {
90 cerr << "FGPropertyManager::GetNode() No node found for " << path << endl;
92 return (FGPropertyManager*)node;
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 FGPropertyManager::GetNode (const string &relpath, int index, bool create)
100 return (FGPropertyManager*)getNode(relpath.c_str(),index,create);
103 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 bool FGPropertyManager::HasNode (const string &path)
108 // Checking if a node exists shouldn't write a warning if it doesn't exist
109 suppress_warning = true;
110 bool has_node = (GetNode(path, false) != 0);
111 suppress_warning = false;
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 string FGPropertyManager::GetName( void )
119 return string( getName() );
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 string FGPropertyManager::GetPrintableName( void )
126 string temp_string(getName());
127 size_t initial_location=0;
128 size_t found_location;
130 found_location = temp_string.rfind("/");
131 if (found_location != string::npos)
132 temp_string = temp_string.substr(found_location);
134 found_location = temp_string.find('_',initial_location);
135 while (found_location != string::npos) {
136 temp_string.replace(found_location,1," ");
137 initial_location = found_location+1;
138 found_location = temp_string.find('_',initial_location);
143 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 string FGPropertyManager::GetFullyQualifiedName(void) {
146 vector<string> stack;
147 stack.push_back( getDisplayName(true) );
148 SGPropertyNode* tmpn=getParent();
151 stack.push_back( tmpn->getDisplayName(true) );
152 if( !tmpn->getParent() )
155 tmpn=tmpn->getParent();
159 for(unsigned i=stack.size()-1;i>0;i--) {
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 string FGPropertyManager::GetRelativeName( const string &path )
172 string temp_string = GetFullyQualifiedName();
173 size_t len = path.length();
174 if ( (len > 0) && (temp_string.substr(0,len) == path) ) {
175 temp_string = temp_string.erase(0,len);
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
186 return getBoolValue(name.c_str(), defaultValue);
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 int FGPropertyManager::GetInt (const string &name, int defaultValue )
193 return getIntValue(name.c_str(), defaultValue);
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 int FGPropertyManager::GetLong (const string &name, long defaultValue )
200 return getLongValue(name.c_str(), defaultValue);
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
207 return getFloatValue(name.c_str(), defaultValue);
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
214 return getDoubleValue(name.c_str(), defaultValue);
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 string FGPropertyManager::GetString (const string &name, string defaultValue )
221 return string(getStringValue(name.c_str(), defaultValue.c_str()));
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 bool FGPropertyManager::SetBool (const string &name, bool val)
228 return setBoolValue(name.c_str(), val);
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 bool FGPropertyManager::SetInt (const string &name, int val)
235 return setIntValue(name.c_str(), val);
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 bool FGPropertyManager::SetLong (const string &name, long val)
242 return setLongValue(name.c_str(), val);
245 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 bool FGPropertyManager::SetFloat (const string &name, float val)
249 return setFloatValue(name.c_str(), val);
252 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 bool FGPropertyManager::SetDouble (const string &name, double val)
256 return setDoubleValue(name.c_str(), val);
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 bool FGPropertyManager::SetString (const string &name, const string &val)
263 return setStringValue(name.c_str(), val.c_str());
266 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 void FGPropertyManager::SetArchivable (const string &name, bool state )
270 SGPropertyNode * node = getNode(name.c_str());
273 "Attempt to set archive flag for non-existent property "
276 node->setAttribute(SGPropertyNode::ARCHIVE, state);
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 void FGPropertyManager::SetReadable (const string &name, bool state )
283 SGPropertyNode * node = getNode(name.c_str());
286 "Attempt to set read flag for non-existant property "
289 node->setAttribute(SGPropertyNode::READ, state);
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 void FGPropertyManager::SetWritable (const string &name, bool state )
296 SGPropertyNode * node = getNode(name.c_str());
299 "Attempt to set write flag for non-existant property "
302 node->setAttribute(SGPropertyNode::WRITE, state);
305 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 void FGPropertyManager::Untie (const string &name)
309 if (!untie(name.c_str()))
310 cerr << "Failed to untie property " << name << endl;
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
317 if (!tie(name.c_str(), SGRawValuePointer<bool>(pointer), useDefault))
318 cerr << "Failed to tie property " << name << " to a pointer" << endl;
320 tied_properties.push_back(name);
321 if (debug_lvl & 0x20) std::cout << name << std::endl;
325 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 void FGPropertyManager::Tie (const string &name, int *pointer,
330 if (!tie(name.c_str(), SGRawValuePointer<int>(pointer), useDefault))
331 cerr << "Failed to tie property " << name << " to a pointer" << endl;
333 tied_properties.push_back(name);
334 if (debug_lvl & 0x20) std::cout << name << std::endl;
338 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340 void FGPropertyManager::Tie (const string &name, long *pointer,
343 if (!tie(name.c_str(), SGRawValuePointer<long>(pointer), useDefault))
344 cerr << "Failed to tie property " << name << " to a pointer" << endl;
346 tied_properties.push_back(name);
347 if (debug_lvl & 0x20) std::cout << name << std::endl;
351 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 void FGPropertyManager::Tie (const string &name, float *pointer,
356 if (!tie(name.c_str(), SGRawValuePointer<float>(pointer), useDefault))
357 cerr << "Failed to tie property " << name << " to a pointer" << endl;
359 tied_properties.push_back(name);
360 if (debug_lvl & 0x20) std::cout << name << std::endl;
364 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 void FGPropertyManager::Tie (const string &name, double *pointer, bool useDefault)
368 if (!tie(name.c_str(), SGRawValuePointer<double>(pointer), useDefault))
369 cerr << "Failed to tie property " << name << " to a pointer" << endl;
371 tied_properties.push_back(name);
372 if (debug_lvl & 0x20) std::cout << name << std::endl;
376 } // namespace JSBSim