| 1 | // fg_props.hxx - Declarations and inline methods for property handling. |
| 2 | // Written by David Megginson, started 2000. |
| 3 | // |
| 4 | // This file is in the Public Domain, and comes with no warranty. |
| 5 | |
| 6 | #ifndef __FG_PROPS_HXX |
| 7 | #define __FG_PROPS_HXX 1 |
| 8 | |
| 9 | #include <iosfwd> |
| 10 | #include <algorithm> |
| 11 | |
| 12 | #include <simgear/structure/subsystem_mgr.hxx> |
| 13 | #include <simgear/props/tiedpropertylist.hxx> |
| 14 | |
| 15 | #include <Main/globals.hxx> |
| 16 | |
| 17 | //////////////////////////////////////////////////////////////////////// |
| 18 | // Property management. |
| 19 | //////////////////////////////////////////////////////////////////////// |
| 20 | |
| 21 | class FGProperties : public SGSubsystem |
| 22 | { |
| 23 | public: |
| 24 | FGProperties (); |
| 25 | virtual ~FGProperties (); |
| 26 | |
| 27 | // Subsystem API. |
| 28 | void bind() override; |
| 29 | void init() override; |
| 30 | void unbind() override; |
| 31 | void update(double dt) override; |
| 32 | |
| 33 | // Subsystem identification. |
| 34 | static const char* staticSubsystemClassId() { return "properties"; } |
| 35 | |
| 36 | private: |
| 37 | bool getFreeze() const; |
| 38 | void setFreeze(bool freeze); |
| 39 | |
| 40 | simgear::TiedPropertyList _tiedProperties; |
| 41 | |
| 42 | static const char* getLatitudeString (); |
| 43 | static const char* getLongitudeString (); |
| 44 | |
| 45 | static SGConstPropertyNode_ptr _longDeg, _latDeg, _lonLatformat; |
| 46 | |
| 47 | SGPropertyNode_ptr _offset; |
| 48 | SGPropertyNode_ptr _uyear, _umonth, _uday, _uhour, _umin, _usec, _uwday, _udsec; |
| 49 | SGPropertyNode_ptr _ryear, _rmonth, _rday, _rhour, _rmin, _rsec, _rwday, _rdsec; |
| 50 | |
| 51 | SGPropertyNode_ptr _headingMagnetic, _trackMagnetic; |
| 52 | SGPropertyNode_ptr _magVar; |
| 53 | SGPropertyNode_ptr _trueHeading, _trueTrack; |
| 54 | |
| 55 | struct tm _lastUtc; |
| 56 | struct tm _lastRealTime; |
| 57 | |
| 58 | bool _simFreeze = false; |
| 59 | SGPropertyNode_ptr _timeGmtNode, _timeGmtStringNode; |
| 60 | SGPropertyNode_ptr _simFreezeNode; |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Save a flight to disk. |
| 66 | * |
| 67 | * This function saves all of the archivable properties to a stream |
| 68 | * so that the current flight can be restored later. |
| 69 | * |
| 70 | * @param output The output stream to write the XML save file to. |
| 71 | * @param write_all If true, write all properties rather than |
| 72 | * just the ones flagged as archivable. |
| 73 | * @return true if the flight was saved successfully. |
| 74 | */ |
| 75 | extern bool fgSaveFlight (std::ostream &output, bool write_all = false); |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * Load a flight from disk. |
| 80 | * |
| 81 | * This function loads an XML save file from a stream to restore |
| 82 | * a flight. |
| 83 | * |
| 84 | * @param input The input stream to read the XML from. |
| 85 | * @return true if the flight was restored successfully. |
| 86 | */ |
| 87 | extern bool fgLoadFlight (std::istream &input); |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Load properties from a file. |
| 92 | * |
| 93 | * @param file The relative or absolute filename. |
| 94 | * @param props The property node to load the properties into. |
| 95 | * @param in_fg_root If true, look for the file relative to |
| 96 | * $FG_ROOT; otherwise, look for the file relative to the |
| 97 | * current working directory. |
| 98 | * @return true if the properties loaded successfully, false |
| 99 | * otherwise. |
| 100 | */ |
| 101 | extern bool fgLoadProps (const std::string& path, SGPropertyNode * props, |
| 102 | bool in_fg_root = true, int default_mode = 0); |
| 103 | |
| 104 | void setLoggingClasses (const char * c); |
| 105 | void setLoggingPriority (const char * p); |
| 106 | |
| 107 | |
| 108 | //////////////////////////////////////////////////////////////////////// |
| 109 | // Convenience functions for getting property values. |
| 110 | //////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | /** |
| 113 | * Get a property node. |
| 114 | * |
| 115 | * @param path The path of the node, relative to root. |
| 116 | * @param create true to create the node if it doesn't exist. |
| 117 | * @return The node, or 0 if none exists and none was created. |
| 118 | */ |
| 119 | extern SGPropertyNode * fgGetNode (const char * path, bool create = false); |
| 120 | |
| 121 | /** |
| 122 | * Get a property node. |
| 123 | * |
| 124 | * @param path The path of the node, relative to root. |
| 125 | * @param create true to create the node if it doesn't exist. |
| 126 | * @return The node, or 0 if none exists and none was created. |
| 127 | */ |
| 128 | inline SGPropertyNode * fgGetNode (const std::string & path, bool create = false) |
| 129 | { |
| 130 | return fgGetNode(path.c_str(), create ); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Get a property node with separate index. |
| 136 | * |
| 137 | * This method separates the index from the path string, to make it |
| 138 | * easier to iterate through multiple components without using sprintf |
| 139 | * to add indices. For example, fgGetNode("foo[1]/bar", 3) will |
| 140 | * return the same result as fgGetNode("foo[1]/bar[3]"). |
| 141 | * |
| 142 | * @param path The path of the node, relative to root. |
| 143 | * @param index The index for the last member of the path (overrides |
| 144 | * any given in the string). |
| 145 | * @param create true to create the node if it doesn't exist. |
| 146 | * @return The node, or 0 if none exists and none was created. |
| 147 | */ |
| 148 | extern SGPropertyNode * fgGetNode (const char * path, |
| 149 | int index, bool create = false); |
| 150 | |
| 151 | /** |
| 152 | * Get a property node with separate index. |
| 153 | * |
| 154 | * This method separates the index from the path string, to make it |
| 155 | * easier to iterate through multiple components without using sprintf |
| 156 | * to add indices. For example, fgGetNode("foo[1]/bar", 3) will |
| 157 | * return the same result as fgGetNode("foo[1]/bar[3]"). |
| 158 | * |
| 159 | * @param path The path of the node, relative to root. |
| 160 | * @param index The index for the last member of the path (overrides |
| 161 | * any given in the string). |
| 162 | * @param create true to create the node if it doesn't exist. |
| 163 | * @return The node, or 0 if none exists and none was created. |
| 164 | */ |
| 165 | inline SGPropertyNode * fgGetNode (const std::string & path, |
| 166 | int index, bool create = false) |
| 167 | { |
| 168 | return fgGetNode(path.c_str(), index, create ); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Test whether a given node exists. |
| 174 | * |
| 175 | * @param path The path of the node, relative to root. |
| 176 | * @return true if the node exists, false otherwise. |
| 177 | */ |
| 178 | extern bool fgHasNode (const char * path); |
| 179 | |
| 180 | /** |
| 181 | * Test whether a given node exists. |
| 182 | * |
| 183 | * @param path The path of the node, relative to root. |
| 184 | * @return true if the node exists, false otherwise. |
| 185 | */ |
| 186 | inline bool fgHasNode (const std::string & path) |
| 187 | { |
| 188 | return fgHasNode( path.c_str() ); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * Add a listener to a node. |
| 194 | * |
| 195 | * @param listener The listener to add to the node. |
| 196 | * @param path The path of the node, relative to root. |
| 197 | * @param index The index for the last member of the path (overrides |
| 198 | * any given in the string). |
| 199 | */ |
| 200 | extern void fgAddChangeListener (SGPropertyChangeListener * listener, |
| 201 | const char * path); |
| 202 | |
| 203 | /** |
| 204 | * Add a listener to a node. |
| 205 | * |
| 206 | * @param listener The listener to add to the node. |
| 207 | * @param path The path of the node, relative to root. |
| 208 | * @param index The index for the last member of the path (overrides |
| 209 | * any given in the string). |
| 210 | */ |
| 211 | inline void fgAddChangeListener (SGPropertyChangeListener * listener, |
| 212 | const std::string & path) |
| 213 | { |
| 214 | fgAddChangeListener( listener, path.c_str() ); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | * Add a listener to a node. |
| 220 | * |
| 221 | * @param listener The listener to add to the node. |
| 222 | * @param path The path of the node, relative to root. |
| 223 | * @param index The index for the last member of the path (overrides |
| 224 | * any given in the string). |
| 225 | */ |
| 226 | extern void fgAddChangeListener (SGPropertyChangeListener * listener, |
| 227 | const char * path, int index); |
| 228 | |
| 229 | /** |
| 230 | * Add a listener to a node. |
| 231 | * |
| 232 | * @param listener The listener to add to the node. |
| 233 | * @param path The path of the node, relative to root. |
| 234 | * @param index The index for the last member of the path (overrides |
| 235 | * any given in the string). |
| 236 | */ |
| 237 | inline void fgAddChangeListener (SGPropertyChangeListener * listener, |
| 238 | const std::string & path, int index) |
| 239 | { |
| 240 | fgAddChangeListener( listener, path.c_str(), index ); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * Get a bool value for a property. |
| 246 | * |
| 247 | * This method is convenient but inefficient. It should be used |
| 248 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 249 | * not in the main loop. If you need to get a value frequently, |
| 250 | * it is better to look up the node itself using fgGetNode and then |
| 251 | * use the node's getBoolValue() method, to avoid the lookup overhead. |
| 252 | * |
| 253 | * @param name The property name. |
| 254 | * @param defaultValue The default value to return if the property |
| 255 | * does not exist. |
| 256 | * @return The property's value as a bool, or the default value provided. |
| 257 | */ |
| 258 | extern bool fgGetBool (const char * name, bool defaultValue = false); |
| 259 | |
| 260 | /** |
| 261 | * Get a bool value for a property. |
| 262 | * |
| 263 | * This method is convenient but inefficient. It should be used |
| 264 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 265 | * not in the main loop. If you need to get a value frequently, |
| 266 | * it is better to look up the node itself using fgGetNode and then |
| 267 | * use the node's getBoolValue() method, to avoid the lookup overhead. |
| 268 | * |
| 269 | * @param name The property name. |
| 270 | * @param defaultValue The default value to return if the property |
| 271 | * does not exist. |
| 272 | * @return The property's value as a bool, or the default value provided. |
| 273 | */ |
| 274 | inline bool fgGetBool (const std::string & name, bool defaultValue = false) |
| 275 | { |
| 276 | return fgGetBool( name.c_str(), defaultValue ); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * Get an int value for a property. |
| 282 | * |
| 283 | * This method is convenient but inefficient. It should be used |
| 284 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 285 | * not in the main loop. If you need to get a value frequently, |
| 286 | * it is better to look up the node itself using fgGetNode and then |
| 287 | * use the node's getIntValue() method, to avoid the lookup overhead. |
| 288 | * |
| 289 | * @param name The property name. |
| 290 | * @param defaultValue The default value to return if the property |
| 291 | * does not exist. |
| 292 | * @return The property's value as an int, or the default value provided. |
| 293 | */ |
| 294 | extern int fgGetInt (const char * name, int defaultValue = 0); |
| 295 | |
| 296 | /** |
| 297 | * Get an int value for a property. |
| 298 | * |
| 299 | * This method is convenient but inefficient. It should be used |
| 300 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 301 | * not in the main loop. If you need to get a value frequently, |
| 302 | * it is better to look up the node itself using fgGetNode and then |
| 303 | * use the node's getIntValue() method, to avoid the lookup overhead. |
| 304 | * |
| 305 | * @param name The property name. |
| 306 | * @param defaultValue The default value to return if the property |
| 307 | * does not exist. |
| 308 | * @return The property's value as an int, or the default value provided. |
| 309 | */ |
| 310 | inline int fgGetInt (const std::string & name, int defaultValue = 0) |
| 311 | { |
| 312 | return fgGetInt( name.c_str(), defaultValue ); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /** |
| 317 | * Get a long value for a property. |
| 318 | * |
| 319 | * This method is convenient but inefficient. It should be used |
| 320 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 321 | * not in the main loop. If you need to get a value frequently, |
| 322 | * it is better to look up the node itself using fgGetNode and then |
| 323 | * use the node's getLongValue() method, to avoid the lookup overhead. |
| 324 | * |
| 325 | * @param name The property name. |
| 326 | * @param defaultValue The default value to return if the property |
| 327 | * does not exist. |
| 328 | * @return The property's value as a long, or the default value provided. |
| 329 | */ |
| 330 | extern long fgGetLong (const char * name, long defaultValue = 0L); |
| 331 | |
| 332 | /** |
| 333 | * Get a long value for a property. |
| 334 | * |
| 335 | * This method is convenient but inefficient. It should be used |
| 336 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 337 | * not in the main loop. If you need to get a value frequently, |
| 338 | * it is better to look up the node itself using fgGetNode and then |
| 339 | * use the node's getLongValue() method, to avoid the lookup overhead. |
| 340 | * |
| 341 | * @param name The property name. |
| 342 | * @param defaultValue The default value to return if the property |
| 343 | * does not exist. |
| 344 | * @return The property's value as a long, or the default value provided. |
| 345 | */ |
| 346 | inline long fgGetLong (const std::string & name, long defaultValue = 0L) |
| 347 | { |
| 348 | return fgGetLong( name.c_str(), defaultValue ); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | /** |
| 353 | * Get a float value for a property. |
| 354 | * |
| 355 | * This method is convenient but inefficient. It should be used |
| 356 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 357 | * not in the main loop. If you need to get a value frequently, |
| 358 | * it is better to look up the node itself using fgGetNode and then |
| 359 | * use the node's getFloatValue() method, to avoid the lookup overhead. |
| 360 | * |
| 361 | * @param name The property name. |
| 362 | * @param defaultValue The default value to return if the property |
| 363 | * does not exist. |
| 364 | * @return The property's value as a float, or the default value provided. |
| 365 | */ |
| 366 | extern float fgGetFloat (const char * name, float defaultValue = 0.0); |
| 367 | |
| 368 | /** |
| 369 | * Get a float value for a property. |
| 370 | * |
| 371 | * This method is convenient but inefficient. It should be used |
| 372 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 373 | * not in the main loop. If you need to get a value frequently, |
| 374 | * it is better to look up the node itself using fgGetNode and then |
| 375 | * use the node's getFloatValue() method, to avoid the lookup overhead. |
| 376 | * |
| 377 | * @param name The property name. |
| 378 | * @param defaultValue The default value to return if the property |
| 379 | * does not exist. |
| 380 | * @return The property's value as a float, or the default value provided. |
| 381 | */ |
| 382 | inline float fgGetFloat (const std::string & name, float defaultValue = 0.0) |
| 383 | { |
| 384 | return fgGetFloat( name.c_str(), defaultValue ); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /** |
| 389 | * Get a double value for a property. |
| 390 | * |
| 391 | * This method is convenient but inefficient. It should be used |
| 392 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 393 | * not in the main loop. If you need to get a value frequently, |
| 394 | * it is better to look up the node itself using fgGetNode and then |
| 395 | * use the node's getDoubleValue() method, to avoid the lookup overhead. |
| 396 | * |
| 397 | * @param name The property name. |
| 398 | * @param defaultValue The default value to return if the property |
| 399 | * does not exist. |
| 400 | * @return The property's value as a double, or the default value provided. |
| 401 | */ |
| 402 | extern double fgGetDouble (const char * name, double defaultValue = 0.0); |
| 403 | |
| 404 | /** |
| 405 | * Get a double value for a property. |
| 406 | * |
| 407 | * This method is convenient but inefficient. It should be used |
| 408 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 409 | * not in the main loop. If you need to get a value frequently, |
| 410 | * it is better to look up the node itself using fgGetNode and then |
| 411 | * use the node's getDoubleValue() method, to avoid the lookup overhead. |
| 412 | * |
| 413 | * @param name The property name. |
| 414 | * @param defaultValue The default value to return if the property |
| 415 | * does not exist. |
| 416 | * @return The property's value as a double, or the default value provided. |
| 417 | */ |
| 418 | inline double fgGetDouble (const std::string & name, double defaultValue = 0.0) |
| 419 | { |
| 420 | return fgGetDouble( name.c_str(), defaultValue ); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | /** |
| 425 | * Get a string value for a property. |
| 426 | * |
| 427 | * This method is convenient but inefficient. It should be used |
| 428 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 429 | * not in the main loop. If you need to get a value frequently, |
| 430 | * it is better to look up the node itself using fgGetNode and then |
| 431 | * use the node's getStringValue() method, to avoid the lookup overhead. |
| 432 | * |
| 433 | * @param name The property name. |
| 434 | * @param defaultValue The default value to return if the property |
| 435 | * does not exist. |
| 436 | * @return The property's value as a string, or the default value provided. |
| 437 | */ |
| 438 | extern std::string fgGetString (const char * name, |
| 439 | const char * defaultValue = ""); |
| 440 | |
| 441 | /** |
| 442 | * Get a string value for a property. |
| 443 | * |
| 444 | * This method is convenient but inefficient. It should be used |
| 445 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 446 | * not in the main loop. If you need to get a value frequently, |
| 447 | * it is better to look up the node itself using fgGetNode and then |
| 448 | * use the node's getStringValue() method, to avoid the lookup overhead. |
| 449 | * |
| 450 | * @param name The property name. |
| 451 | * @param defaultValue The default value to return if the property |
| 452 | * does not exist. |
| 453 | * @return The property's value as a string, or the default value provided. |
| 454 | */ |
| 455 | inline std::string fgGetString (const std::string & name, |
| 456 | const std::string & defaultValue = std::string("")) |
| 457 | { |
| 458 | return fgGetString( name.c_str(), defaultValue.c_str() ); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | /** |
| 463 | * Set a bool value for a property. |
| 464 | * |
| 465 | * Assign a bool value to a property. If the property does not |
| 466 | * yet exist, it will be created and its type will be set to |
| 467 | * BOOL; if it has a type of UNKNOWN, the type will also be set to |
| 468 | * BOOL; otherwise, the bool value will be converted to the property's |
| 469 | * type. |
| 470 | * |
| 471 | * @param name The property name. |
| 472 | * @param val The new value for the property. |
| 473 | * @return true if the assignment succeeded, false otherwise. |
| 474 | */ |
| 475 | extern bool fgSetBool (const char * name, bool val); |
| 476 | |
| 477 | /** |
| 478 | * Set a bool value for a property. |
| 479 | * |
| 480 | * Assign a bool value to a property. If the property does not |
| 481 | * yet exist, it will be created and its type will be set to |
| 482 | * BOOL; if it has a type of UNKNOWN, the type will also be set to |
| 483 | * BOOL; otherwise, the bool value will be converted to the property's |
| 484 | * type. |
| 485 | * |
| 486 | * @param name The property name. |
| 487 | * @param val The new value for the property. |
| 488 | * @return true if the assignment succeeded, false otherwise. |
| 489 | */ |
| 490 | inline bool fgSetBool (const std::string & name, bool val) |
| 491 | { |
| 492 | return fgSetBool( name.c_str(), val ); |
| 493 | } |
| 494 | |
| 495 | |
| 496 | /** |
| 497 | * Set an int value for a property. |
| 498 | * |
| 499 | * Assign an int value to a property. If the property does not |
| 500 | * yet exist, it will be created and its type will be set to |
| 501 | * INT; if it has a type of UNKNOWN, the type will also be set to |
| 502 | * INT; otherwise, the bool value will be converted to the property's |
| 503 | * type. |
| 504 | * |
| 505 | * @param name The property name. |
| 506 | * @param val The new value for the property. |
| 507 | * @return true if the assignment succeeded, false otherwise. |
| 508 | */ |
| 509 | extern bool fgSetInt (const char * name, int val); |
| 510 | |
| 511 | /** |
| 512 | * Set an int value for a property. |
| 513 | * |
| 514 | * Assign an int value to a property. If the property does not |
| 515 | * yet exist, it will be created and its type will be set to |
| 516 | * INT; if it has a type of UNKNOWN, the type will also be set to |
| 517 | * INT; otherwise, the bool value will be converted to the property's |
| 518 | * type. |
| 519 | * |
| 520 | * @param name The property name. |
| 521 | * @param val The new value for the property. |
| 522 | * @return true if the assignment succeeded, false otherwise. |
| 523 | */ |
| 524 | inline bool fgSetInt (const std::string & name, int val) |
| 525 | { |
| 526 | return fgSetInt( name.c_str(), val ); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Set a long value for a property. |
| 531 | * |
| 532 | * Assign a long value to a property. If the property does not |
| 533 | * yet exist, it will be created and its type will be set to |
| 534 | * LONG; if it has a type of UNKNOWN, the type will also be set to |
| 535 | * LONG; otherwise, the bool value will be converted to the property's |
| 536 | * type. |
| 537 | * |
| 538 | * @param name The property name. |
| 539 | * @param val The new value for the property. |
| 540 | * @return true if the assignment succeeded, false otherwise. |
| 541 | */ |
| 542 | extern bool fgSetLong (const char * name, long val); |
| 543 | |
| 544 | /** |
| 545 | * Set a long value for a property. |
| 546 | * |
| 547 | * Assign a long value to a property. If the property does not |
| 548 | * yet exist, it will be created and its type will be set to |
| 549 | * LONG; if it has a type of UNKNOWN, the type will also be set to |
| 550 | * LONG; otherwise, the bool value will be converted to the property's |
| 551 | * type. |
| 552 | * |
| 553 | * @param name The property name. |
| 554 | * @param val The new value for the property. |
| 555 | * @return true if the assignment succeeded, false otherwise. |
| 556 | */ |
| 557 | inline bool fgSetLong (const std::string & name, long val) |
| 558 | { |
| 559 | return fgSetLong( name.c_str(), val ); |
| 560 | } |
| 561 | |
| 562 | |
| 563 | /** |
| 564 | * Set a float value for a property. |
| 565 | * |
| 566 | * Assign a float value to a property. If the property does not |
| 567 | * yet exist, it will be created and its type will be set to |
| 568 | * FLOAT; if it has a type of UNKNOWN, the type will also be set to |
| 569 | * FLOAT; otherwise, the bool value will be converted to the property's |
| 570 | * type. |
| 571 | * |
| 572 | * @param name The property name. |
| 573 | * @param val The new value for the property. |
| 574 | * @return true if the assignment succeeded, false otherwise. |
| 575 | */ |
| 576 | extern bool fgSetFloat (const char * name, float val); |
| 577 | |
| 578 | /** |
| 579 | * Set a float value for a property. |
| 580 | * |
| 581 | * Assign a float value to a property. If the property does not |
| 582 | * yet exist, it will be created and its type will be set to |
| 583 | * FLOAT; if it has a type of UNKNOWN, the type will also be set to |
| 584 | * FLOAT; otherwise, the bool value will be converted to the property's |
| 585 | * type. |
| 586 | * |
| 587 | * @param name The property name. |
| 588 | * @param val The new value for the property. |
| 589 | * @return true if the assignment succeeded, false otherwise. |
| 590 | */ |
| 591 | inline bool fgSetFloat (const std::string & name, float val) |
| 592 | { |
| 593 | return fgSetFloat( name.c_str(), val ); |
| 594 | } |
| 595 | |
| 596 | |
| 597 | /** |
| 598 | * Set a double value for a property. |
| 599 | * |
| 600 | * Assign a double value to a property. If the property does not |
| 601 | * yet exist, it will be created and its type will be set to |
| 602 | * DOUBLE; if it has a type of UNKNOWN, the type will also be set to |
| 603 | * DOUBLE; otherwise, the double value will be converted to the property's |
| 604 | * type. |
| 605 | * |
| 606 | * @param name The property name. |
| 607 | * @param val The new value for the property. |
| 608 | * @return true if the assignment succeeded, false otherwise. |
| 609 | */ |
| 610 | extern bool fgSetDouble (const char * name, double val); |
| 611 | |
| 612 | /** |
| 613 | * Set a double value for a property. |
| 614 | * |
| 615 | * Assign a double value to a property. If the property does not |
| 616 | * yet exist, it will be created and its type will be set to |
| 617 | * DOUBLE; if it has a type of UNKNOWN, the type will also be set to |
| 618 | * DOUBLE; otherwise, the double value will be converted to the property's |
| 619 | * type. |
| 620 | * |
| 621 | * @param name The property name. |
| 622 | * @param val The new value for the property. |
| 623 | * @return true if the assignment succeeded, false otherwise. |
| 624 | */ |
| 625 | inline bool fgSetDouble (const std::string & name, double val) |
| 626 | { |
| 627 | return fgSetDouble( name.c_str(), val ); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | /** |
| 632 | * Set a string value for a property. |
| 633 | * |
| 634 | * Assign a string value to a property. If the property does not |
| 635 | * yet exist, it will be created and its type will be set to |
| 636 | * STRING; if it has a type of UNKNOWN, the type will also be set to |
| 637 | * STRING; otherwise, the string value will be converted to the property's |
| 638 | * type. |
| 639 | * |
| 640 | * @param name The property name. |
| 641 | * @param val The new value for the property. |
| 642 | * @return true if the assignment succeeded, false otherwise. |
| 643 | */ |
| 644 | extern bool fgSetString (const char * name, const char * val); |
| 645 | |
| 646 | /** |
| 647 | * Set a string value for a property. |
| 648 | * |
| 649 | * Assign a string value to a property. If the property does not |
| 650 | * yet exist, it will be created and its type will be set to |
| 651 | * STRING; if it has a type of UNKNOWN, the type will also be set to |
| 652 | * STRING; otherwise, the string value will be converted to the property's |
| 653 | * type. |
| 654 | * |
| 655 | * @param name The property name. |
| 656 | * @param val The new value for the property. |
| 657 | * @return true if the assignment succeeded, false otherwise. |
| 658 | */ |
| 659 | inline bool fgSetString (const std::string & name, const std::string & val) |
| 660 | { |
| 661 | return fgSetString( name.c_str(), val.c_str() ); |
| 662 | } |
| 663 | |
| 664 | |
| 665 | //////////////////////////////////////////////////////////////////////// |
| 666 | // Convenience functions for setting property attributes. |
| 667 | //////////////////////////////////////////////////////////////////////// |
| 668 | |
| 669 | |
| 670 | /** |
| 671 | * Set the state of the archive attribute for a property. |
| 672 | * |
| 673 | * If the archive attribute is true, the property will be written |
| 674 | * when a flight is saved; if it is false, the property will be |
| 675 | * skipped. |
| 676 | * |
| 677 | * A warning message will be printed if the property does not exist. |
| 678 | * |
| 679 | * @param name The property name. |
| 680 | * @param state The state of the archive attribute (defaults to true). |
| 681 | */ |
| 682 | extern void fgSetArchivable (const char * name, bool state = true); |
| 683 | |
| 684 | |
| 685 | /** |
| 686 | * Set the state of the read attribute for a property. |
| 687 | * |
| 688 | * If the read attribute is true, the property value will be readable; |
| 689 | * if it is false, the property value will always be the default value |
| 690 | * for its type. |
| 691 | * |
| 692 | * A warning message will be printed if the property does not exist. |
| 693 | * |
| 694 | * @param name The property name. |
| 695 | * @param state The state of the read attribute (defaults to true). |
| 696 | */ |
| 697 | extern void fgSetReadable (const char * name, bool state = true); |
| 698 | |
| 699 | |
| 700 | /** |
| 701 | * Set the state of the write attribute for a property. |
| 702 | * |
| 703 | * If the write attribute is true, the property value may be modified |
| 704 | * (depending on how it is tied); if the write attribute is false, the |
| 705 | * property value may not be modified. |
| 706 | * |
| 707 | * A warning message will be printed if the property does not exist. |
| 708 | * |
| 709 | * @param name The property name. |
| 710 | * @param state The state of the write attribute (defaults to true). |
| 711 | */ |
| 712 | extern void fgSetWritable (const char * name, bool state = true); |
| 713 | |
| 714 | |
| 715 | //////////////////////////////////////////////////////////////////////// |
| 716 | // Convenience functions for tying properties, with logging. |
| 717 | //////////////////////////////////////////////////////////////////////// |
| 718 | |
| 719 | |
| 720 | /** |
| 721 | * Untie a property from an external data source. |
| 722 | * |
| 723 | * Classes should use this function to release control of any |
| 724 | * properties they are managing. |
| 725 | */ |
| 726 | extern void fgUntie (const char * name); |
| 727 | |
| 728 | /** |
| 729 | @brfief variant of the above which doesn't warn if the property does not exist |
| 730 | */ |
| 731 | void fgUntieIfDefined(const std::string& name); |
| 732 | |
| 733 | /** |
| 734 | * Tie a property to a pair of simple functions. |
| 735 | * |
| 736 | * Every time the property value is queried, the getter (if any) will |
| 737 | * be invoked; every time the property value is modified, the setter |
| 738 | * (if any) will be invoked. The getter can be 0 to make the property |
| 739 | * unreadable, and the setter can be 0 to make the property |
| 740 | * unmodifiable. |
| 741 | * |
| 742 | * @param name The property name to tie (full path). |
| 743 | * @param getter The getter function, or 0 if the value is unreadable. |
| 744 | * @param setter The setter function, or 0 if the value is unmodifiable. |
| 745 | * @param useDefault true if the setter should be invoked with any existing |
| 746 | * property value should be; false if the old value should be |
| 747 | * discarded; defaults to true. |
| 748 | */ |
| 749 | template <class V> |
| 750 | inline void |
| 751 | fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0, |
| 752 | bool useDefault = true) |
| 753 | { |
| 754 | if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter), |
| 755 | useDefault)) |
| 756 | SG_LOG(SG_GENERAL, SG_DEV_WARN, |
| 757 | "Failed to tie property " << name << " to functions"); |
| 758 | } |
| 759 | |
| 760 | |
| 761 | /** |
| 762 | * Tie a property to a pair of indexed functions. |
| 763 | * |
| 764 | * Every time the property value is queried, the getter (if any) will |
| 765 | * be invoked with the index provided; every time the property value |
| 766 | * is modified, the setter (if any) will be invoked with the index |
| 767 | * provided. The getter can be 0 to make the property unreadable, and |
| 768 | * the setter can be 0 to make the property unmodifiable. |
| 769 | * |
| 770 | * @param name The property name to tie (full path). |
| 771 | * @param index The integer argument to pass to the getter and |
| 772 | * setter functions. |
| 773 | * @param getter The getter function, or 0 if the value is unreadable. |
| 774 | * @param setter The setter function, or 0 if the value is unmodifiable. |
| 775 | * @param useDefault true if the setter should be invoked with any existing |
| 776 | * property value should be; false if the old value should be |
| 777 | * discarded; defaults to true. |
| 778 | */ |
| 779 | template <class V> |
| 780 | inline void |
| 781 | fgTie (const char * name, int index, V (*getter)(int), |
| 782 | void (*setter)(int, V) = 0, bool useDefault = true) |
| 783 | { |
| 784 | if (!globals->get_props()->tie(name, |
| 785 | SGRawValueFunctionsIndexed<V>(index, |
| 786 | getter, |
| 787 | setter), |
| 788 | useDefault)) |
| 789 | SG_LOG(SG_GENERAL, SG_DEV_WARN, |
| 790 | "Failed to tie property " << name << " to indexed functions"); |
| 791 | } |
| 792 | |
| 793 | |
| 794 | /** |
| 795 | * Tie a property to a pair of object methods. |
| 796 | * |
| 797 | * Every time the property value is queried, the getter (if any) will |
| 798 | * be invoked; every time the property value is modified, the setter |
| 799 | * (if any) will be invoked. The getter can be 0 to make the property |
| 800 | * unreadable, and the setter can be 0 to make the property |
| 801 | * unmodifiable. |
| 802 | * |
| 803 | * @param name The property name to tie (full path). |
| 804 | * @param obj The object whose methods should be invoked. |
| 805 | * @param getter The object's getter method, or 0 if the value is |
| 806 | * unreadable. |
| 807 | * @param setter The object's setter method, or 0 if the value is |
| 808 | * unmodifiable. |
| 809 | * @param useDefault true if the setter should be invoked with any existing |
| 810 | * property value should be; false if the old value should be |
| 811 | * discarded; defaults to true. |
| 812 | */ |
| 813 | template <class T, class V> |
| 814 | inline void |
| 815 | fgTie (const char * name, T * obj, V (T::*getter)() const, |
| 816 | void (T::*setter)(V) = 0, bool useDefault = true) |
| 817 | { |
| 818 | if (!globals->get_props()->tie(name, |
| 819 | SGRawValueMethods<T,V>(*obj, getter, setter), |
| 820 | useDefault)) |
| 821 | SG_LOG(SG_GENERAL, SG_DEV_WARN, |
| 822 | "Failed to tie property " << name << " to object methods"); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | /** |
| 827 | * Tie a property to a pair of indexed object methods. |
| 828 | * |
| 829 | * Every time the property value is queried, the getter (if any) will |
| 830 | * be invoked with the index provided; every time the property value |
| 831 | * is modified, the setter (if any) will be invoked with the index |
| 832 | * provided. The getter can be 0 to make the property unreadable, and |
| 833 | * the setter can be 0 to make the property unmodifiable. |
| 834 | * |
| 835 | * @param name The property name to tie (full path). |
| 836 | * @param obj The object whose methods should be invoked. |
| 837 | * @param index The integer argument to pass to the getter and |
| 838 | * setter methods. |
| 839 | * @param getter The getter method, or 0 if the value is unreadable. |
| 840 | * @param setter The setter method, or 0 if the value is unmodifiable. |
| 841 | * @param useDefault true if the setter should be invoked with any existing |
| 842 | * property value should be; false if the old value should be |
| 843 | * discarded; defaults to true. |
| 844 | */ |
| 845 | template <class T, class V> |
| 846 | inline void |
| 847 | fgTie (const char * name, T * obj, int index, |
| 848 | V (T::*getter)(int) const, void (T::*setter)(int, V) = 0, |
| 849 | bool useDefault = true) |
| 850 | { |
| 851 | if (!globals->get_props()->tie(name, |
| 852 | SGRawValueMethodsIndexed<T,V>(*obj, |
| 853 | index, |
| 854 | getter, |
| 855 | setter), |
| 856 | useDefault)) |
| 857 | SG_LOG(SG_GENERAL, SG_DEV_WARN, |
| 858 | "Failed to tie property " << name << " to indexed object methods"); |
| 859 | } |
| 860 | |
| 861 | #endif // __FG_PROPS_HXX |
| 862 |