JSBSim Flight Dynamics Model  1.2.0 (05 Nov 2023)
An Open Source Flight Dynamics and Control Software Library in C++
FGPropertyManager Class Reference

Detailed Description

Definition at line 375 of file FGPropertyManager.h.

Public Member Functions

 FGPropertyManager (FGPropertyNode *_root)
 Constructor.
 
 FGPropertyManager (void)
 Default constructor.
 
virtual ~FGPropertyManager (void)
 Destructor.
 
FGPropertyNodeGetNode (const std::string &path, bool create=false)
 
FGPropertyNodeGetNode (const std::string &relpath, int index, bool create=false)
 
FGPropertyNodeGetNode (void) const
 
bool HasNode (const std::string &path) const
 
std::string mkPropertyName (std::string name, bool lowercase)
 Property-ify a name replaces spaces with '-' and, optionally, makes name all lower case. More...
 
template<typename T >
void Tie (const std::string &name, int index, T(*getter)(int), void(*setter)(int, T)=nullptr)
 Tie a property to a pair of indexed functions. More...
 
template<class T , class V >
void Tie (const std::string &name, T *obj, int index, V(T::*getter)(int) const, void(T::*setter)(int, V)=nullptr)
 Tie a property to a pair of indexed object methods. More...
 
template<class T , class V >
void Tie (const std::string &name, T *obj, V(T::*getter)() const, void(T::*setter)(V)=nullptr)
 Tie a property to a pair of object methods. More...
 
template<typename T >
void Tie (const std::string &name, T *pointer)
 Tie a property to an external variable. More...
 
template<typename T >
void Tie (const std::string &name, T(*getter)(), void(*setter)(T)=nullptr)
 Tie a property to a pair of simple functions. More...
 
template<typename T >
void Unbind (const std::shared_ptr< T > &instance)
 Unbind all properties bound by this manager to an instance. More...
 
void Unbind (const void *instance)
 Unbind all properties bound by this manager to an instance. More...
 
void Unbind (void)
 Unbind all properties bound by this manager to an external data source.
 
void Untie (const std::string &name)
 Untie a property from an external data source. More...
 
void Untie (SGPropertyNode *property)
 Untie a property from an external data source. More...
 

Member Function Documentation

◆ mkPropertyName()

string mkPropertyName ( std::string  name,
bool  lowercase 
)

Property-ify a name replaces spaces with '-' and, optionally, makes name all lower case.

Parameters
namestring to change
lowercasetrue to change all upper case chars to lower NOTE: this function changes its argument and thus relies on pass by value

Definition at line 79 of file FGPropertyManager.cpp.

79  {
80 
81  /* do this two pass to avoid problems with characters getting skipped
82  because the index changed */
83  unsigned i;
84  for(i=0;i<name.length();i++) {
85  if( lowercase && isupper(name[i]) )
86  name[i]=tolower(name[i]);
87  else if( isspace(name[i]) )
88  name[i]='-';
89  }
90 
91  return name;
92 }

◆ Tie() [1/5]

void Tie ( const std::string &  name,
int  index,
T(*)(int)  getter,
void(*)(int, T)  setter = nullptr 
)
inline

Tie a property to a pair of indexed functions.

Every time the property value is queried, the getter (if any) will be invoked with the index provided; every time the property value is modified, the setter (if any) will be invoked with the index provided. The getter can be 0 to make the property unreadable, and the setter can be 0 to make the property unmodifiable.

Parameters
nameThe property name to tie (full path).
indexThe integer argument to pass to the getter and setter functions.
getterThe getter function, or 0 if the value is unreadable.
setterThe setter function, or 0 if the value is unmodifiable.

Definition at line 533 of file FGPropertyManager.h.

535  {
536  SGPropertyNode* property = root->getNode(name.c_str(), true);
537  if (!property) {
538  std::cerr << "Could not get or create property " << name << std::endl;
539  return;
540  }
541 
542  if (!property->tie(SGRawValueFunctionsIndexed<T>(index, getter, setter),
543  false))
544  std::cerr << "Failed to tie property " << name << " to indexed functions"
545  << std::endl;
546  else {
547  tied_properties.push_back(PropertyState(property, nullptr));
548  if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
549  if (!getter) property->setAttribute(SGPropertyNode::READ, false);
550  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
551  }
552  }

◆ Tie() [2/5]

void Tie ( const std::string &  name,
T *  obj,
int  index,
V(T::*)(int) const  getter,
void(T::*)(int, V)  setter = nullptr 
)
inline

Tie a property to a pair of indexed object methods.

Every time the property value is queried, the getter (if any) will be invoked with the index provided; every time the property value is modified, the setter (if any) will be invoked with the index provided. The getter can be 0 to make the property unreadable, and the setter can be 0 to make the property unmodifiable.

Parameters
nameThe property name to tie (full path).
objThe object whose methods should be invoked.
indexThe integer argument to pass to the getter and setter methods.
getterThe getter method, or 0 if the value is unreadable.
setterThe setter method, or 0 if the value is unmodifiable.

Definition at line 608 of file FGPropertyManager.h.

610  {
611  SGPropertyNode* property = root->getNode(name.c_str(), true);
612  if (!property) {
613  std::cerr << "Could not get or create property " << name << std::endl;
614  return;
615  }
616 
617  if (!property->tie(SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter),
618  false))
619  std::cerr << "Failed to tie property " << name
620  << " to indexed object methods" << std::endl;
621  else {
622  tied_properties.push_back(PropertyState(property, obj));
623  if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
624  if (!getter) property->setAttribute(SGPropertyNode::READ, false);
625  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
626  }
627  }

◆ Tie() [3/5]

void Tie ( const std::string &  name,
T *  obj,
V(T::*)() const  getter,
void(T::*)(V)  setter = nullptr 
)
inline

Tie a property to a pair of object methods.

Every time the property value is queried, the getter (if any) will be invoked; every time the property value is modified, the setter (if any) will be invoked. The getter can be 0 to make the property unreadable, and the setter can be 0 to make the property unmodifiable.

Parameters
nameThe property name to tie (full path).
objThe object whose methods should be invoked.
getterThe object's getter method, or 0 if the value is unreadable.
setterThe object's setter method, or 0 if the value is unmodifiable.

Definition at line 571 of file FGPropertyManager.h.

573  {
574  SGPropertyNode* property = root->getNode(name.c_str(), true);
575  if (!property) {
576  std::cerr << "Could not get or create property " << name << std::endl;
577  return;
578  }
579 
580  if (!property->tie(SGRawValueMethods<T,V>(*obj, getter, setter), false))
581  std::cerr << "Failed to tie property " << name << " to object methods"
582  << std::endl;
583  else {
584  tied_properties.push_back(PropertyState(property, obj));
585  if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
586  if (!getter) property->setAttribute(SGPropertyNode::READ, false);
587  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
588  }
589  }

◆ Tie() [4/5]

void Tie ( const std::string &  name,
T *  pointer 
)
inline

Tie a property to an external variable.

The property's value will automatically mirror the variable's value, and vice-versa, until the property is untied.

Parameters
nameThe property name to tie (full path).
pointerA pointer to the variable.

Definition at line 467 of file FGPropertyManager.h.

468  {
469  SGPropertyNode* property = root->getNode(name.c_str(), true);
470  if (!property) {
471  cerr << "Could not get or create property " << name << endl;
472  return;
473  }
474 
475  if (!property->tie(SGRawValuePointer<T>(pointer), false))
476  cerr << "Failed to tie property " << name << " to a pointer" << endl;
477  else {
478  tied_properties.push_back(PropertyState(property, nullptr));
479  if (FGJSBBase::debug_lvl & 0x20) cout << name << endl;
480  }
481  }

◆ Tie() [5/5]

void Tie ( const std::string &  name,
T(*)()  getter,
void(*)(T)  setter = nullptr 
)
inline

Tie a property to a pair of simple functions.

Every time the property value is queried, the getter (if any) will be invoked; every time the property value is modified, the setter (if any) will be invoked. The getter can be 0 to make the property unreadable, and the setter can be 0 to make the property unmodifiable.

Parameters
nameThe property name to tie (full path).
getterThe getter function, or 0 if the value is unreadable.
setterThe setter function, or 0 if the value is unmodifiable.

Definition at line 498 of file FGPropertyManager.h.

499  {
500  SGPropertyNode* property = root->getNode(name.c_str(), true);
501  if (!property) {
502  std::cerr << "Could not get or create property " << name << std::endl;
503  return;
504  }
505 
506  if (!property->tie(SGRawValueFunctions<T>(getter, setter), false))
507  std::cerr << "Failed to tie property " << name << " to functions"
508  << std::endl;
509  else {
510  tied_properties.push_back(PropertyState(property, nullptr));
511  if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
512  if (!getter) property->setAttribute(SGPropertyNode::READ, false);
513  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
514  }
515  }

◆ Unbind() [1/2]

void Unbind ( const std::shared_ptr< T > &  instance)
inline

Unbind all properties bound by this manager to an instance.

Classes should use this function to release control of any properties they have bound using this property manager. Helper function for shared_ptr

See also
Unbind(const void*)

Definition at line 453 of file FGPropertyManager.h.

453  {
454  Unbind(instance.get());
455  }
void Unbind(void)
Unbind all properties bound by this manager to an external data source.

◆ Unbind() [2/2]

void Unbind ( const void *  instance)

Unbind all properties bound by this manager to an instance.

Classes should use this function to release control of any properties they have bound using this property manager.

Parameters
instanceThe instance which properties shall be unbound.

Definition at line 64 of file FGPropertyManager.cpp.

65 {
66  auto it = tied_properties.begin();
67 
68  while(it != tied_properties.end()) {
69  auto property = it++;
70  if (property->BindingInstance == instance) {
71  property->untie();
72  tied_properties.erase(property);
73  }
74  }
75 }

◆ Untie() [1/2]

void Untie ( const std::string &  name)

Untie a property from an external data source.

Classes should use this function to release control of any properties they are managing.

Parameters
nameThe property name to untie (full path).

Definition at line 303 of file FGPropertyManager.cpp.

304 {
305  SGPropertyNode* property = root->getNode(name.c_str());
306  if (!property) {
307  cerr << "Attempt to untie a non-existant property." << name << endl;
308  return;
309  }
310 
311  Untie(property);
312 }
void Untie(const std::string &name)
Untie a property from an external data source.

◆ Untie() [2/2]

void Untie ( SGPropertyNode *  property)

Untie a property from an external data source.

Classes should use this function to release control of any properties they are managing.

Parameters
propertyA pointer to the property to untie.

Definition at line 316 of file FGPropertyManager.cpp.

317 {
318  const string& name = property->getNameString();
319 
320  assert(property->isTied());
321 
322  for (auto it = tied_properties.begin(); it != tied_properties.end(); ++it) {
323  if (it->node.ptr() == property) {
324  it->untie();
325  tied_properties.erase(it);
326  if (FGJSBBase::debug_lvl & 0x20) cout << "Untied " << name << endl;
327  return;
328  }
329  }
330 
331  cerr << "Failed to untie property " << name << endl
332  << "JSBSim is not the owner of this property." << endl;
333 }

The documentation for this class was generated from the following files: