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<SGPropertyNode_ptr> FGPropertyManager::tied_properties;
54 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 void FGPropertyManager::Unbind(void)
58 vector<SGPropertyNode_ptr>::iterator it;
60 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 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 bool FGPropertyManager::HasNode (const string &path)
107 // Checking if a node exists shouldn't write a warning if it doesn't exist
108 suppress_warning = true;
109 bool has_node = (GetNode(path, false) != 0);
110 suppress_warning = false;
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 string FGPropertyManager::GetName( void )
118 return string( getName() );
121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 string FGPropertyManager::GetPrintableName( void )
125 string temp_string(getName());
126 size_t initial_location=0;
127 size_t found_location;
129 found_location = temp_string.rfind("/");
130 if (found_location != string::npos)
131 temp_string = temp_string.substr(found_location);
133 found_location = temp_string.find('_',initial_location);
134 while (found_location != string::npos) {
135 temp_string.replace(found_location,1," ");
136 initial_location = found_location+1;
137 found_location = temp_string.find('_',initial_location);
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 string FGPropertyManager::GetFullyQualifiedName(void) {
145 vector<string> stack;
146 stack.push_back( getDisplayName(true) );
147 SGPropertyNode* tmpn=getParent();
150 stack.push_back( tmpn->getDisplayName(true) );
151 if( !tmpn->getParent() )
154 tmpn=tmpn->getParent();
158 for(unsigned i=stack.size()-1;i>0;i--) {
167 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 string FGPropertyManager::GetRelativeName( const string &path )
171 string temp_string = GetFullyQualifiedName();
172 size_t len = path.length();
173 if ( (len > 0) && (temp_string.substr(0,len) == path) ) {
174 temp_string = temp_string.erase(0,len);
181 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
185 return getBoolValue(name.c_str(), defaultValue);
188 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 int FGPropertyManager::GetInt (const string &name, int defaultValue )
192 return getIntValue(name.c_str(), defaultValue);
195 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 int FGPropertyManager::GetLong (const string &name, long defaultValue )
199 return getLongValue(name.c_str(), defaultValue);
202 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
206 return getFloatValue(name.c_str(), defaultValue);
209 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
213 return getDoubleValue(name.c_str(), defaultValue);
216 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 string FGPropertyManager::GetString (const string &name, string defaultValue )
220 return string(getStringValue(name.c_str(), defaultValue.c_str()));
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 bool FGPropertyManager::SetBool (const string &name, bool val)
227 return setBoolValue(name.c_str(), val);
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 bool FGPropertyManager::SetInt (const string &name, int val)
234 return setIntValue(name.c_str(), val);
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 bool FGPropertyManager::SetLong (const string &name, long val)
241 return setLongValue(name.c_str(), val);
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 bool FGPropertyManager::SetFloat (const string &name, float val)
248 return setFloatValue(name.c_str(), val);
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 bool FGPropertyManager::SetDouble (const string &name, double val)
255 return setDoubleValue(name.c_str(), val);
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 bool FGPropertyManager::SetString (const string &name, const string &val)
262 return setStringValue(name.c_str(), val.c_str());
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 void FGPropertyManager::SetArchivable (const string &name, bool state )
269 SGPropertyNode * node = getNode(name.c_str());
272 "Attempt to set archive flag for non-existent property "
275 node->setAttribute(SGPropertyNode::ARCHIVE, state);
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 void FGPropertyManager::SetReadable (const string &name, bool state )
282 SGPropertyNode * node = getNode(name.c_str());
285 "Attempt to set read flag for non-existant property "
288 node->setAttribute(SGPropertyNode::READ, state);
291 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 void FGPropertyManager::SetWritable (const string &name, bool state )
295 SGPropertyNode * node = getNode(name.c_str());
298 "Attempt to set write flag for non-existant property "
301 node->setAttribute(SGPropertyNode::WRITE, state);
304 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 void FGPropertyManager::Untie (const string &name)
308 if (!untie(name.c_str()))
309 cerr << "Failed to untie property " << name << endl;
312 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
316 SGPropertyNode* property = getNode(name.c_str(), true);
318 cerr << "Could not get or create property " << name << endl;
322 if (!property->tie(SGRawValuePointer<bool>(pointer), useDefault))
323 cerr << "Failed to tie property " << name << " to a pointer" << endl;
325 tied_properties.push_back(property);
326 if (debug_lvl & 0x20) cout << name << endl;
330 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 void FGPropertyManager::Tie (const string &name, int *pointer,
335 SGPropertyNode* property = getNode(name.c_str(), true);
337 cerr << "Could not get or create property " << name << endl;
341 if (!property->tie(SGRawValuePointer<int>(pointer), useDefault))
342 cerr << "Failed to tie property " << name << " to a pointer" << endl;
344 tied_properties.push_back(property);
345 if (debug_lvl & 0x20) cout << name << endl;
349 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 void FGPropertyManager::Tie (const string &name, long *pointer,
354 SGPropertyNode* property = getNode(name.c_str(), true);
356 cerr << "Could not get or create property " << name << endl;
360 if (!property->tie(SGRawValuePointer<long>(pointer), useDefault))
361 cerr << "Failed to tie property " << name << " to a pointer" << endl;
363 tied_properties.push_back(property);
364 if (debug_lvl & 0x20) cout << name << endl;
368 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 void FGPropertyManager::Tie (const string &name, float *pointer,
373 SGPropertyNode* property = getNode(name.c_str(), true);
375 cerr << "Could not get or create property " << name << endl;
379 if (!property->tie(SGRawValuePointer<float>(pointer), useDefault))
380 cerr << "Failed to tie property " << name << " to a pointer" << endl;
382 tied_properties.push_back(property);
383 if (debug_lvl & 0x20) cout << name << endl;
387 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 void FGPropertyManager::Tie (const string &name, double *pointer, bool useDefault)
391 SGPropertyNode* property = getNode(name.c_str(), true);
393 cerr << "Could not get or create property " << name << endl;
397 if (!property->tie(SGRawValuePointer<double>(pointer), useDefault))
398 cerr << "Failed to tie property " << name << " to a pointer" << endl;
400 tied_properties.push_back(property);
401 if (debug_lvl & 0x20) cout << name << endl;
405 } // namespace JSBSim