| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGPropertyManager.h |
| 4 | Author: Tony Peden |
| 5 | Based on work originally by David Megginson |
| 6 | Date: 2/2002 |
| 7 | |
| 8 | ------------- Copyright (C) 2002 ------------- |
| 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 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 28 | SENTRY |
| 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 30 | |
| 31 | #ifndef FGPROPERTYMANAGER_H |
| 32 | #define FGPROPERTYMANAGER_H |
| 33 | |
| 34 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | INCLUDES |
| 36 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 37 | |
| 38 | // This is needed by MSVC9 when included in FlightGear because of |
| 39 | // the new Vec4d class in props.hxx |
| 40 | #if defined( HAVE_CONFIG_H ) |
| 41 | # include <config.h> |
| 42 | #endif |
| 43 | |
| 44 | #include <string> |
| 45 | #include <list> |
| 46 | #include "simgear/props/propertyObject.hxx" |
| 47 | #if !PROPS_STANDALONE |
| 48 | # include "simgear/math/SGMath.hxx" |
| 49 | #endif |
| 50 | |
| 51 | #include "FGJSBBase.h" |
| 52 | |
| 53 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 54 | FORWARD DECLARATIONS |
| 55 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 56 | |
| 57 | namespace JSBSim { |
| 58 | |
| 59 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 60 | CLASS DOCUMENTATION |
| 61 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 62 | |
| 63 | /** Class wrapper for property handling. |
| 64 | @author David Megginson, Tony Peden |
| 65 | */ |
| 66 | |
| 67 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 68 | CLASS DECLARATION |
| 69 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 70 | |
| 71 | class JSBSIM_API FGPropertyNode : public SGPropertyNode |
| 72 | { |
| 73 | public: |
| 74 | /// Destructor |
| 75 | virtual ~FGPropertyNode(void) {} |
| 76 | |
| 77 | /** |
| 78 | * Get a property node. |
| 79 | * |
| 80 | * @param path The path of the node, relative to root. |
| 81 | * @param create true to create the node if it doesn't exist. |
| 82 | * @return The node, or 0 if none exists and none was created. |
| 83 | */ |
| 84 | FGPropertyNode* |
| 85 | GetNode (const std::string &path, bool create = false); |
| 86 | |
| 87 | FGPropertyNode* |
| 88 | GetNode (const std::string &relpath, int index, bool create = false); |
| 89 | |
| 90 | /** |
| 91 | * Test whether a given node exists. |
| 92 | * |
| 93 | * @param path The path of the node, relative to root. |
| 94 | * @return true if the node exists, false otherwise. |
| 95 | */ |
| 96 | bool HasNode (const std::string &path); |
| 97 | |
| 98 | /** |
| 99 | * Get the name of a node |
| 100 | */ |
| 101 | const std::string& GetName( void ) const { return getNameString(); } |
| 102 | |
| 103 | /** |
| 104 | * Get the name of a node without underscores, etc. |
| 105 | */ |
| 106 | std::string GetPrintableName( void ) const; |
| 107 | |
| 108 | /** |
| 109 | * Get the fully qualified name of a node |
| 110 | * This function is very slow, so is probably useful for debugging only. |
| 111 | */ |
| 112 | std::string GetFullyQualifiedName(void) const; |
| 113 | |
| 114 | /** |
| 115 | * Get the qualified name of a node relative to given base path, |
| 116 | * otherwise the fully qualified name. |
| 117 | * This function is very slow, so is probably useful for debugging only. |
| 118 | * |
| 119 | * @param path The path to strip off, if found. |
| 120 | */ |
| 121 | std::string GetRelativeName( const std::string &path = "/fdm/jsbsim/" ) const; |
| 122 | |
| 123 | /** |
| 124 | * Get a bool value for a property. |
| 125 | * |
| 126 | * This method is convenient but inefficient. It should be used |
| 127 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 128 | * not in the main loop. If you need to get a value frequently, |
| 129 | * it is better to look up the node itself using GetNode and then |
| 130 | * use the node's getBoolValue() method, to avoid the lookup overhead. |
| 131 | * |
| 132 | * @param name The property name. |
| 133 | * @param defaultValue The default value to return if the property |
| 134 | * does not exist. |
| 135 | * @return The property's value as a bool, or the default value provided. |
| 136 | */ |
| 137 | bool GetBool (const std::string &name, bool defaultValue = false) const; |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * Get an int value for a property. |
| 142 | * |
| 143 | * This method is convenient but inefficient. It should be used |
| 144 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 145 | * not in the main loop. If you need to get a value frequently, |
| 146 | * it is better to look up the node itself using GetNode and then |
| 147 | * use the node's getIntValue() method, to avoid the lookup overhead. |
| 148 | * |
| 149 | * @param name The property name. |
| 150 | * @param defaultValue The default value to return if the property |
| 151 | * does not exist. |
| 152 | * @return The property's value as an int, or the default value provided. |
| 153 | */ |
| 154 | int GetInt (const std::string &name, int defaultValue = 0) const; |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Get a long value for a property. |
| 159 | * |
| 160 | * This method is convenient but inefficient. It should be used |
| 161 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 162 | * not in the main loop. If you need to get a value frequently, |
| 163 | * it is better to look up the node itself using GetNode and then |
| 164 | * use the node's getLongValue() method, to avoid the lookup overhead. |
| 165 | * |
| 166 | * @param name The property name. |
| 167 | * @param defaultValue The default value to return if the property |
| 168 | * does not exist. |
| 169 | * @return The property's value as a long, or the default value provided. |
| 170 | */ |
| 171 | int GetLong (const std::string &name, long defaultValue = 0L) const; |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * Get a float value for a property. |
| 176 | * |
| 177 | * This method is convenient but inefficient. It should be used |
| 178 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 179 | * not in the main loop. If you need to get a value frequently, |
| 180 | * it is better to look up the node itself using GetNode and then |
| 181 | * use the node's getFloatValue() method, to avoid the lookup overhead. |
| 182 | * |
| 183 | * @param name The property name. |
| 184 | * @param defaultValue The default value to return if the property |
| 185 | * does not exist. |
| 186 | * @return The property's value as a float, or the default value provided. |
| 187 | */ |
| 188 | float GetFloat (const std::string &name, float defaultValue = 0.0) const; |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Get a double value for a property. |
| 193 | * |
| 194 | * This method is convenient but inefficient. It should be used |
| 195 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 196 | * not in the main loop. If you need to get a value frequently, |
| 197 | * it is better to look up the node itself using GetNode and then |
| 198 | * use the node's getDoubleValue() method, to avoid the lookup overhead. |
| 199 | * |
| 200 | * @param name The property name. |
| 201 | * @param defaultValue The default value to return if the property |
| 202 | * does not exist. |
| 203 | * @return The property's value as a double, or the default value provided. |
| 204 | */ |
| 205 | double GetDouble (const std::string &name, double defaultValue = 0.0) const; |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * Get a string value for a property. |
| 210 | * |
| 211 | * This method is convenient but inefficient. It should be used |
| 212 | * infrequently (i.e. for initializing, loading, saving, etc.), |
| 213 | * not in the main loop. If you need to get a value frequently, |
| 214 | * it is better to look up the node itself using GetNode and then |
| 215 | * use the node's getStringValue() method, to avoid the lookup overhead. |
| 216 | * |
| 217 | * @param name The property name. |
| 218 | * @param defaultValue The default value to return if the property |
| 219 | * does not exist. |
| 220 | * @return The property's value as a string, or the default value provided. |
| 221 | */ |
| 222 | std::string GetString (const std::string &name, std::string defaultValue = "") const; |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * Set a bool value for a property. |
| 227 | * |
| 228 | * Assign a bool value to a property. If the property does not |
| 229 | * yet exist, it will be created and its type will be set to |
| 230 | * BOOL; if it has a type of UNKNOWN, the type will also be set to |
| 231 | * BOOL; otherwise, the value type will be converted to the property's |
| 232 | * type. |
| 233 | * |
| 234 | * @param name The property name. |
| 235 | * @param val The new value for the property. |
| 236 | * @return true if the assignment succeeded, false otherwise. |
| 237 | */ |
| 238 | bool SetBool (const std::string &name, bool val); |
| 239 | |
| 240 | |
| 241 | /** |
| 242 | * Set an int value for a property. |
| 243 | * |
| 244 | * Assign an int value to a property. If the property does not |
| 245 | * yet exist, it will be created and its type will be set to |
| 246 | * INT; if it has a type of UNKNOWN, the type will also be set to |
| 247 | * INT; otherwise, the value type will be converted to the property's |
| 248 | * type. |
| 249 | * |
| 250 | * @param name The property name. |
| 251 | * @param val The new value for the property. |
| 252 | * @return true if the assignment succeeded, false otherwise. |
| 253 | */ |
| 254 | bool SetInt (const std::string &name, int val); |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * Set a long value for a property. |
| 259 | * |
| 260 | * Assign a long value to a property. If the property does not |
| 261 | * yet exist, it will be created and its type will be set to |
| 262 | * LONG; if it has a type of UNKNOWN, the type will also be set to |
| 263 | * LONG; otherwise, the value type will be converted to the property's |
| 264 | * type. |
| 265 | * |
| 266 | * @param name The property name. |
| 267 | * @param val The new value for the property. |
| 268 | * @return true if the assignment succeeded, false otherwise. |
| 269 | */ |
| 270 | bool SetLong (const std::string &name, long val); |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * Set a float value for a property. |
| 275 | * |
| 276 | * Assign a float value to a property. If the property does not |
| 277 | * yet exist, it will be created and its type will be set to |
| 278 | * FLOAT; if it has a type of UNKNOWN, the type will also be set to |
| 279 | * FLOAT; otherwise, the value type will be converted to the property's |
| 280 | * type. |
| 281 | * |
| 282 | * @param name The property name. |
| 283 | * @param val The new value for the property. |
| 284 | * @return true if the assignment succeeded, false otherwise. |
| 285 | */ |
| 286 | bool SetFloat (const std::string &name, float val); |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * Set a double value for a property. |
| 291 | * |
| 292 | * Assign a double value to a property. If the property does not |
| 293 | * yet exist, it will be created and its type will be set to |
| 294 | * DOUBLE; if it has a type of UNKNOWN, the type will also be set to |
| 295 | * DOUBLE; otherwise, the double value will be converted to the property's |
| 296 | * type. |
| 297 | * |
| 298 | * @param name The property name. |
| 299 | * @param val The new value for the property. |
| 300 | * @return true if the assignment succeeded, false otherwise. |
| 301 | */ |
| 302 | bool SetDouble (const std::string &name, double val); |
| 303 | |
| 304 | |
| 305 | /** |
| 306 | * Set a string value for a property. |
| 307 | * |
| 308 | * Assign a string value to a property. If the property does not |
| 309 | * yet exist, it will be created and its type will be set to |
| 310 | * STRING; if it has a type of UNKNOWN, the type will also be set to |
| 311 | * STRING; otherwise, the string value will be converted to the property's |
| 312 | * type. |
| 313 | * |
| 314 | * @param name The property name. |
| 315 | * @param val The new value for the property. |
| 316 | * @return true if the assignment succeeded, false otherwise. |
| 317 | */ |
| 318 | bool SetString (const std::string &name, const std::string &val); |
| 319 | |
| 320 | |
| 321 | //////////////////////////////////////////////////////////////////////// |
| 322 | // Convenience functions for setting property attributes. |
| 323 | //////////////////////////////////////////////////////////////////////// |
| 324 | |
| 325 | |
| 326 | /** |
| 327 | * Set the state of the archive attribute for a property. |
| 328 | * |
| 329 | * If the archive attribute is true, the property will be written |
| 330 | * when a flight is saved; if it is false, the property will be |
| 331 | * skipped. |
| 332 | * |
| 333 | * A warning message will be printed if the property does not exist. |
| 334 | * |
| 335 | * @param name The property name. |
| 336 | * @param state The state of the archive attribute (defaults to true). |
| 337 | */ |
| 338 | void SetArchivable (const std::string &name, bool state = true); |
| 339 | |
| 340 | |
| 341 | /** |
| 342 | * Set the state of the read attribute for a property. |
| 343 | * |
| 344 | * If the read attribute is true, the property value will be readable; |
| 345 | * if it is false, the property value will always be the default value |
| 346 | * for its type. |
| 347 | * |
| 348 | * A warning message will be printed if the property does not exist. |
| 349 | * |
| 350 | * @param name The property name. |
| 351 | * @param state The state of the read attribute (defaults to true). |
| 352 | */ |
| 353 | void SetReadable (const std::string &name, bool state = true); |
| 354 | |
| 355 | |
| 356 | /** |
| 357 | * Set the state of the write attribute for a property. |
| 358 | * |
| 359 | * If the write attribute is true, the property value may be modified |
| 360 | * (depending on how it is tied); if the write attribute is false, the |
| 361 | * property value may not be modified. |
| 362 | * |
| 363 | * A warning message will be printed if the property does not exist. |
| 364 | * |
| 365 | * @param name The property name. |
| 366 | * @param state The state of the write attribute (defaults to true). |
| 367 | */ |
| 368 | void SetWritable (const std::string &name, bool state = true); |
| 369 | }; |
| 370 | |
| 371 | typedef SGSharedPtr<FGPropertyNode> FGPropertyNode_ptr; |
| 372 | typedef SGSharedPtr<const FGPropertyNode> FGConstPropertyNode_ptr; |
| 373 | |
| 374 | class JSBSIM_API FGPropertyManager |
| 375 | { |
| 376 | public: |
| 377 | /// Default constructor |
| 378 | FGPropertyManager(void) { root = new FGPropertyNode; } |
| 379 | |
| 380 | /// Constructor |
| 381 | explicit FGPropertyManager(FGPropertyNode* _root) : root(_root) {}; |
| 382 | |
| 383 | /// Destructor |
| 384 | virtual ~FGPropertyManager(void) { Unbind(); } |
| 385 | |
| 386 | FGPropertyNode* GetNode(void) const { return root; } |
| 387 | FGPropertyNode* GetNode(const std::string &path, bool create = false) |
| 388 | { return root->GetNode(path, create); } |
| 389 | FGPropertyNode* GetNode(const std::string &relpath, int index, bool create = false) |
| 390 | { return root->GetNode(relpath, index, create); } |
| 391 | bool HasNode(const std::string& path) const |
| 392 | { |
| 393 | std::string newPath = path; |
| 394 | if (newPath[0] == '-') newPath.erase(0,1); |
| 395 | return root->HasNode(newPath); |
| 396 | } |
| 397 | |
| 398 | /** Property-ify a name |
| 399 | * replaces spaces with '-' and, optionally, makes name all lower case |
| 400 | * @param name string to change |
| 401 | * @param lowercase true to change all upper case chars to lower |
| 402 | * NOTE: this function changes its argument and thus relies |
| 403 | * on pass by value |
| 404 | */ |
| 405 | std::string mkPropertyName(std::string name, bool lowercase); |
| 406 | |
| 407 | //////////////////////////////////////////////////////////////////////// |
| 408 | // Convenience functions for tying properties, with logging. |
| 409 | //////////////////////////////////////////////////////////////////////// |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * Untie a property from an external data source. |
| 414 | * |
| 415 | * Classes should use this function to release control of any |
| 416 | * properties they are managing. |
| 417 | * |
| 418 | * @param name The property name to untie (full path). |
| 419 | */ |
| 420 | void Untie (const std::string &name); |
| 421 | |
| 422 | /** |
| 423 | * Untie a property from an external data source. |
| 424 | * |
| 425 | * Classes should use this function to release control of any |
| 426 | * properties they are managing. |
| 427 | * |
| 428 | * @param property A pointer to the property to untie. |
| 429 | */ |
| 430 | void Untie (SGPropertyNode* property); |
| 431 | |
| 432 | /// Unbind all properties bound by this manager to an external data source. |
| 433 | void Unbind (void); |
| 434 | |
| 435 | /** |
| 436 | * Unbind all properties bound by this manager to an instance. |
| 437 | * |
| 438 | * Classes should use this function to release control of any |
| 439 | * properties they have bound using this property manager. |
| 440 | * @param instance The instance which properties shall be unbound. |
| 441 | */ |
| 442 | void Unbind(const void* instance); |
| 443 | |
| 444 | /** |
| 445 | * Tie a property to an external variable. |
| 446 | * |
| 447 | * The property's value will automatically mirror the variable's |
| 448 | * value, and vice-versa, until the property is untied. |
| 449 | * |
| 450 | * @param name The property name to tie (full path). |
| 451 | * @param pointer A pointer to the variable. |
| 452 | */ |
| 453 | template <typename T> void |
| 454 | Tie (const std::string &name, T *pointer) |
| 455 | { |
| 456 | SGPropertyNode* property = root->getNode(name.c_str(), true); |
| 457 | if (!property) { |
| 458 | std::cerr << "Could not get or create property " << name << std::endl; |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | if (!property->tie(SGRawValuePointer<T>(pointer), false)) |
| 463 | std::cerr << "Failed to tie property " << name << " to a pointer" << std::endl; |
| 464 | else { |
| 465 | tied_properties.push_back(PropertyState(property, nullptr)); |
| 466 | if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Tie a property to a pair of simple functions. |
| 472 | * |
| 473 | * Every time the property value is queried, the getter (if any) will |
| 474 | * be invoked; every time the property value is modified, the setter |
| 475 | * (if any) will be invoked. The getter can be 0 to make the property |
| 476 | * unreadable, and the setter can be 0 to make the property |
| 477 | * unmodifiable. |
| 478 | * |
| 479 | * @param name The property name to tie (full path). |
| 480 | * @param getter The getter function, or 0 if the value is unreadable. |
| 481 | * @param setter The setter function, or 0 if the value is unmodifiable. |
| 482 | */ |
| 483 | |
| 484 | template <typename T> void |
| 485 | Tie (const std::string &name, T (*getter)(), void (*setter)(T) = nullptr) |
| 486 | { |
| 487 | SGPropertyNode* property = root->getNode(name.c_str(), true); |
| 488 | if (!property) { |
| 489 | std::cerr << "Could not get or create property " << name << std::endl; |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | if (!property->tie(SGRawValueFunctions<T>(getter, setter), false)) |
| 494 | std::cerr << "Failed to tie property " << name << " to functions" |
| 495 | << std::endl; |
| 496 | else { |
| 497 | tied_properties.push_back(PropertyState(property, nullptr)); |
| 498 | if (!setter) property->setAttribute(SGPropertyNode::WRITE, false); |
| 499 | if (!getter) property->setAttribute(SGPropertyNode::READ, false); |
| 500 | if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Tie a property to a pair of indexed functions. |
| 506 | * |
| 507 | * Every time the property value is queried, the getter (if any) will |
| 508 | * be invoked with the index provided; every time the property value |
| 509 | * is modified, the setter (if any) will be invoked with the index |
| 510 | * provided. The getter can be 0 to make the property unreadable, and |
| 511 | * the setter can be 0 to make the property unmodifiable. |
| 512 | * |
| 513 | * @param name The property name to tie (full path). |
| 514 | * @param index The integer argument to pass to the getter and |
| 515 | * setter functions. |
| 516 | * @param getter The getter function, or 0 if the value is unreadable. |
| 517 | * @param setter The setter function, or 0 if the value is unmodifiable. |
| 518 | */ |
| 519 | template <typename T> void |
| 520 | Tie (const std::string &name, int index, T (*getter)(int), |
| 521 | void (*setter)(int, T) = nullptr) |
| 522 | { |
| 523 | SGPropertyNode* property = root->getNode(name.c_str(), true); |
| 524 | if (!property) { |
| 525 | std::cerr << "Could not get or create property " << name << std::endl; |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | if (!property->tie(SGRawValueFunctionsIndexed<T>(index, getter, setter), |
| 530 | false)) |
| 531 | std::cerr << "Failed to tie property " << name << " to indexed functions" |
| 532 | << std::endl; |
| 533 | else { |
| 534 | tied_properties.push_back(PropertyState(property, nullptr)); |
| 535 | if (!setter) property->setAttribute(SGPropertyNode::WRITE, false); |
| 536 | if (!getter) property->setAttribute(SGPropertyNode::READ, false); |
| 537 | if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Tie a property to a pair of object methods. |
| 543 | * |
| 544 | * Every time the property value is queried, the getter (if any) will |
| 545 | * be invoked; every time the property value is modified, the setter |
| 546 | * (if any) will be invoked. The getter can be 0 to make the property |
| 547 | * unreadable, and the setter can be 0 to make the property |
| 548 | * unmodifiable. |
| 549 | * |
| 550 | * @param name The property name to tie (full path). |
| 551 | * @param obj The object whose methods should be invoked. |
| 552 | * @param getter The object's getter method, or 0 if the value is |
| 553 | * unreadable. |
| 554 | * @param setter The object's setter method, or 0 if the value is |
| 555 | * unmodifiable. |
| 556 | */ |
| 557 | template <class T, class V> void |
| 558 | Tie (const std::string &name, T * obj, V (T::*getter)() const, |
| 559 | void (T::*setter)(V) = nullptr) |
| 560 | { |
| 561 | SGPropertyNode* property = root->getNode(name.c_str(), true); |
| 562 | if (!property) { |
| 563 | std::cerr << "Could not get or create property " << name << std::endl; |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | if (!property->tie(SGRawValueMethods<T,V>(*obj, getter, setter), false)) |
| 568 | std::cerr << "Failed to tie property " << name << " to object methods" |
| 569 | << std::endl; |
| 570 | else { |
| 571 | tied_properties.push_back(PropertyState(property, obj)); |
| 572 | if (!setter) property->setAttribute(SGPropertyNode::WRITE, false); |
| 573 | if (!getter) property->setAttribute(SGPropertyNode::READ, false); |
| 574 | if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Tie a property to a pair of indexed object methods. |
| 580 | * |
| 581 | * Every time the property value is queried, the getter (if any) will |
| 582 | * be invoked with the index provided; every time the property value |
| 583 | * is modified, the setter (if any) will be invoked with the index |
| 584 | * provided. The getter can be 0 to make the property unreadable, and |
| 585 | * the setter can be 0 to make the property unmodifiable. |
| 586 | * |
| 587 | * @param name The property name to tie (full path). |
| 588 | * @param obj The object whose methods should be invoked. |
| 589 | * @param index The integer argument to pass to the getter and |
| 590 | * setter methods. |
| 591 | * @param getter The getter method, or 0 if the value is unreadable. |
| 592 | * @param setter The setter method, or 0 if the value is unmodifiable. |
| 593 | */ |
| 594 | template <class T, class V> void |
| 595 | Tie (const std::string &name, T * obj, int index, V (T::*getter)(int) const, |
| 596 | void (T::*setter)(int, V) = nullptr) |
| 597 | { |
| 598 | SGPropertyNode* property = root->getNode(name.c_str(), true); |
| 599 | if (!property) { |
| 600 | std::cerr << "Could not get or create property " << name << std::endl; |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | if (!property->tie(SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter), |
| 605 | false)) |
| 606 | std::cerr << "Failed to tie property " << name |
| 607 | << " to indexed object methods" << std::endl; |
| 608 | else { |
| 609 | tied_properties.push_back(PropertyState(property, obj)); |
| 610 | if (!setter) property->setAttribute(SGPropertyNode::WRITE, false); |
| 611 | if (!getter) property->setAttribute(SGPropertyNode::READ, false); |
| 612 | if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | template <class T> simgear::PropertyObject<T> |
| 617 | CreatePropertyObject(const std::string &path) |
| 618 | { return simgear::PropertyObject<T>(root->GetNode(path, true)); } |
| 619 | |
| 620 | private: |
| 621 | struct PropertyState { |
| 622 | SGPropertyNode_ptr node; |
| 623 | const void* BindingInstance = nullptr; |
| 624 | bool WriteAttribute = true; |
| 625 | bool ReadAttribute = true; |
| 626 | PropertyState(SGPropertyNode* property, const void* instance) |
| 627 | : node(property), BindingInstance(instance) { |
| 628 | WriteAttribute = node->getAttribute(SGPropertyNode::WRITE); |
| 629 | ReadAttribute = node->getAttribute(SGPropertyNode::READ); |
| 630 | } |
| 631 | void untie(void) { |
| 632 | node->setAttribute(SGPropertyNode::WRITE, WriteAttribute); |
| 633 | node->setAttribute(SGPropertyNode::READ, ReadAttribute); |
| 634 | node->untie(); |
| 635 | } |
| 636 | }; |
| 637 | std::list<PropertyState> tied_properties; |
| 638 | FGPropertyNode_ptr root; |
| 639 | }; |
| 640 | } |
| 641 | #endif // FGPROPERTYMANAGER_H |