JSBSim Flight Dynamics Model  1.1.11 (13 Feb 2022)
An Open Source Flight Dynamics and Control Software Library in C++
FGPropertyManager.h
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 "simgear/props/propertyObject.hxx"
46 #if !PROPS_STANDALONE
47 # include "simgear/math/SGMath.hxx"
48 #endif
49 
50 #include "FGJSBBase.h"
51 
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 FORWARD DECLARATIONS
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 
56 namespace JSBSim {
57 
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 CLASS DOCUMENTATION
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61 
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 CLASS DECLARATION
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
69 
70 class FGPropertyNode : public SGPropertyNode
71 {
72  public:
74  virtual ~FGPropertyNode(void) {}
75 
84  GetNode (const std::string &path, bool create = false);
85 
87  GetNode (const std::string &relpath, int index, bool create = false);
88 
95  bool HasNode (const std::string &path);
96 
100  const std::string& GetName( void ) const { return getNameString(); }
101 
105  std::string GetPrintableName( void ) const;
106 
111  std::string GetFullyQualifiedName(void) const;
112 
120  std::string GetRelativeName( const std::string &path = "/fdm/jsbsim/" ) const;
121 
136  bool GetBool (const std::string &name, bool defaultValue = false) const;
137 
138 
153  int GetInt (const std::string &name, int defaultValue = 0) const;
154 
155 
170  int GetLong (const std::string &name, long defaultValue = 0L) const;
171 
172 
187  float GetFloat (const std::string &name, float defaultValue = 0.0) const;
188 
189 
204  double GetDouble (const std::string &name, double defaultValue = 0.0) const;
205 
206 
221  std::string GetString (const std::string &name, std::string defaultValue = "") const;
222 
223 
237  bool SetBool (const std::string &name, bool val);
238 
239 
253  bool SetInt (const std::string &name, int val);
254 
255 
269  bool SetLong (const std::string &name, long val);
270 
271 
285  bool SetFloat (const std::string &name, float val);
286 
287 
301  bool SetDouble (const std::string &name, double val);
302 
303 
317  bool SetString (const std::string &name, const std::string &val);
318 
319 
321  // Convenience functions for setting property attributes.
323 
324 
337  void SetArchivable (const std::string &name, bool state = true);
338 
339 
352  void SetReadable (const std::string &name, bool state = true);
353 
354 
367  void SetWritable (const std::string &name, bool state = true);
368 };
369 
370 typedef SGSharedPtr<FGPropertyNode> FGPropertyNode_ptr;
371 typedef SGSharedPtr<const FGPropertyNode> FGConstPropertyNode_ptr;
372 
374 {
375  public:
377  FGPropertyManager(void) { root = new FGPropertyNode; }
378 
380  explicit FGPropertyManager(FGPropertyNode* _root) : root(_root) {};
381 
383  virtual ~FGPropertyManager(void) { Unbind(); }
384 
385  FGPropertyNode* GetNode(void) const { return root; }
386  FGPropertyNode* GetNode(const std::string &path, bool create = false)
387  { return root->GetNode(path, create); }
388  FGPropertyNode* GetNode(const std::string &relpath, int index, bool create = false)
389  { return root->GetNode(relpath, index, create); }
390  bool HasNode(const std::string& path) const
391  {
392  std::string newPath = path;
393  if (newPath[0] == '-') newPath.erase(0,1);
394  return root->HasNode(newPath);
395  }
396 
404  std::string mkPropertyName(std::string name, bool lowercase);
405 
407  // Convenience functions for tying properties, with logging.
409 
410 
419  void Untie (const std::string &name);
420 
429  void Untie (SGPropertyNode* property);
430 
437  void Unbind (void);
438 
448  template <typename T> void
449  Tie (const std::string &name, T *pointer)
450  {
451  SGPropertyNode* property = root->getNode(name.c_str(), true);
452  if (!property) {
453  cerr << "Could not get or create property " << name << endl;
454  return;
455  }
456 
457  if (!property->tie(SGRawValuePointer<T>(pointer), false))
458  cerr << "Failed to tie property " << name << " to a pointer" << endl;
459  else {
460  tied_properties.push_back(property);
461  if (FGJSBBase::debug_lvl & 0x20) cout << name << endl;
462  }
463  }
464 
479  template <typename T> void
480  Tie (const std::string &name, T (*getter)(), void (*setter)(T) = nullptr)
481  {
482  SGPropertyNode* property = root->getNode(name.c_str(), true);
483  if (!property) {
484  std::cerr << "Could not get or create property " << name << std::endl;
485  return;
486  }
487 
488  if (!property->tie(SGRawValueFunctions<T>(getter, setter), false))
489  std::cerr << "Failed to tie property " << name << " to functions"
490  << std::endl;
491  else {
492  if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
493  if (!getter) property->setAttribute(SGPropertyNode::READ, false);
494  tied_properties.push_back(property);
495  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
496  }
497  }
498 
514  template <typename T> void
515  Tie (const std::string &name, int index, T (*getter)(int),
516  void (*setter)(int, T) = nullptr)
517  {
518  SGPropertyNode* property = root->getNode(name.c_str(), true);
519  if (!property) {
520  std::cerr << "Could not get or create property " << name << std::endl;
521  return;
522  }
523 
524  if (!property->tie(SGRawValueFunctionsIndexed<T>(index, getter, setter),
525  false))
526  std::cerr << "Failed to tie property " << name << " to indexed functions"
527  << std::endl;
528  else {
529  if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
530  if (!getter) property->setAttribute(SGPropertyNode::READ, false);
531  tied_properties.push_back(property);
532  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
533  }
534  }
535 
552  template <class T, class V> void
553  Tie (const std::string &name, T * obj, V (T::*getter)() const,
554  void (T::*setter)(V) = nullptr)
555  {
556  SGPropertyNode* property = root->getNode(name.c_str(), true);
557  if (!property) {
558  std::cerr << "Could not get or create property " << name << std::endl;
559  return;
560  }
561 
562  if (!property->tie(SGRawValueMethods<T,V>(*obj, getter, setter), false))
563  std::cerr << "Failed to tie property " << name << " to object methods"
564  << std::endl;
565  else {
566  if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
567  if (!getter) property->setAttribute(SGPropertyNode::READ, false);
568  tied_properties.push_back(property);
569  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
570  }
571  }
572 
589  template <class T, class V> void
590  Tie (const std::string &name, T * obj, int index, V (T::*getter)(int) const,
591  void (T::*setter)(int, V) = nullptr)
592  {
593  SGPropertyNode* property = root->getNode(name.c_str(), true);
594  if (!property) {
595  std::cerr << "Could not get or create property " << name << std::endl;
596  return;
597  }
598 
599  if (!property->tie(SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter),
600  false))
601  std::cerr << "Failed to tie property " << name
602  << " to indexed object methods" << std::endl;
603  else {
604  if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
605  if (!getter) property->setAttribute(SGPropertyNode::READ, false);
606  tied_properties.push_back(property);
607  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
608  }
609  }
610 
611  template <class T> simgear::PropertyObject<T>
612  CreatePropertyObject(const std::string &path)
613  { return simgear::PropertyObject<T>(root->GetNode(path, true)); }
614 
615  private:
616  std::vector<SGPropertyNode_ptr> tied_properties;
617  FGPropertyNode_ptr root;
618 };
619 }
620 #endif // FGPROPERTYMANAGER_H
JSBSim::FGPropertyNode::SetWritable
void SetWritable(const std::string &name, bool state=true)
Set the state of the write attribute for a property.
Definition: FGPropertyManager.cpp:285
JSBSim::FGPropertyNode::GetPrintableName
std::string GetPrintableName(void) const
Get the name of a node without underscores, etc.
Definition: FGPropertyManager.cpp:114
JSBSim::FGPropertyManager::FGPropertyManager
FGPropertyManager(void)
Default constructor.
Definition: FGPropertyManager.h:377
JSBSim::FGPropertyNode::GetName
const std::string & GetName(void) const
Get the name of a node.
Definition: FGPropertyManager.h:100
JSBSim::FGPropertyNode::SetFloat
bool SetFloat(const std::string &name, float val)
Set a float value for a property.
Definition: FGPropertyManager.cpp:238
JSBSim::FGPropertyManager::Tie
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.
Definition: FGPropertyManager.h:553
JSBSim::FGPropertyNode::GetBool
bool GetBool(const std::string &name, bool defaultValue=false) const
Get a bool value for a property.
Definition: FGPropertyManager.cpp:175
JSBSim::FGPropertyNode::GetFloat
float GetFloat(const std::string &name, float defaultValue=0.0) const
Get a float value for a property.
Definition: FGPropertyManager.cpp:196
JSBSim::FGPropertyManager::Tie
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.
Definition: FGPropertyManager.h:590
JSBSim::FGPropertyManager::Tie
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.
Definition: FGPropertyManager.h:515
JSBSim::FGPropertyNode::GetRelativeName
std::string GetRelativeName(const std::string &path="/fdm/jsbsim/") const
Get the qualified name of a node relative to given base path, otherwise the fully qualified name.
Definition: FGPropertyManager.cpp:161
JSBSim::FGPropertyNode::SetInt
bool SetInt(const std::string &name, int val)
Set an int value for a property.
Definition: FGPropertyManager.cpp:224
JSBSim::FGPropertyNode
Class wrapper for property handling.
Definition: FGPropertyManager.h:70
JSBSim::FGPropertyManager::Tie
void Tie(const std::string &name, T(*getter)(), void(*setter)(T)=nullptr)
Tie a property to a pair of simple functions.
Definition: FGPropertyManager.h:480
JSBSim::FGPropertyNode::SetBool
bool SetBool(const std::string &name, bool val)
Set a bool value for a property.
Definition: FGPropertyManager.cpp:217
JSBSim::FGPropertyNode::GetDouble
double GetDouble(const std::string &name, double defaultValue=0.0) const
Get a double value for a property.
Definition: FGPropertyManager.cpp:203
JSBSim::FGPropertyNode::GetInt
int GetInt(const std::string &name, int defaultValue=0) const
Get an int value for a property.
Definition: FGPropertyManager.cpp:182
JSBSim::FGPropertyManager::FGPropertyManager
FGPropertyManager(FGPropertyNode *_root)
Constructor.
Definition: FGPropertyManager.h:380
JSBSim::FGPropertyNode::GetString
std::string GetString(const std::string &name, std::string defaultValue="") const
Get a string value for a property.
Definition: FGPropertyManager.cpp:210
JSBSim::FGPropertyNode::~FGPropertyNode
virtual ~FGPropertyNode(void)
Destructor.
Definition: FGPropertyManager.h:74
JSBSim::FGPropertyManager::Unbind
void Unbind(void)
Unbind all properties bound by this manager to an external data source.
Definition: FGPropertyManager.cpp:54
JSBSim::FGPropertyNode::SetString
bool SetString(const std::string &name, const std::string &val)
Set a string value for a property.
Definition: FGPropertyManager.cpp:252
JSBSim::FGPropertyManager::Untie
void Untie(const std::string &name)
Untie a property from an external data source.
Definition: FGPropertyManager.cpp:298
JSBSim::FGPropertyNode::SetArchivable
void SetArchivable(const std::string &name, bool state=true)
Set the state of the archive attribute for a property.
Definition: FGPropertyManager.cpp:259
JSBSim::FGPropertyManager::mkPropertyName
std::string mkPropertyName(std::string name, bool lowercase)
Property-ify a name replaces spaces with '-' and, optionally, makes name all lower case.
Definition: FGPropertyManager.cpp:64
JSBSim::FGPropertyManager::~FGPropertyManager
virtual ~FGPropertyManager(void)
Destructor.
Definition: FGPropertyManager.h:383
JSBSim::FGPropertyNode::GetLong
int GetLong(const std::string &name, long defaultValue=0L) const
Get a long value for a property.
Definition: FGPropertyManager.cpp:189
JSBSim::FGPropertyNode::SetDouble
bool SetDouble(const std::string &name, double val)
Set a double value for a property.
Definition: FGPropertyManager.cpp:245
JSBSim::FGPropertyNode::SetReadable
void SetReadable(const std::string &name, bool state=true)
Set the state of the read attribute for a property.
Definition: FGPropertyManager.cpp:272
JSBSim::FGPropertyNode::HasNode
bool HasNode(const std::string &path)
Test whether a given node exists.
Definition: FGPropertyManager.cpp:106
JSBSim::FGPropertyNode::GetFullyQualifiedName
std::string GetFullyQualifiedName(void) const
Get the fully qualified name of a node This function is very slow, so is probably useful for debuggin...
Definition: FGPropertyManager.cpp:135
JSBSim::FGPropertyNode::SetLong
bool SetLong(const std::string &name, long val)
Set a long value for a property.
Definition: FGPropertyManager.cpp:231
JSBSim::FGPropertyManager
Definition: FGPropertyManager.h:373
JSBSim::FGPropertyManager::Tie
void Tie(const std::string &name, T *pointer)
Tie a property to an external variable.
Definition: FGPropertyManager.h:449
JSBSim::FGPropertyNode::GetNode
FGPropertyNode * GetNode(const std::string &path, bool create=false)
Get a property node.
Definition: FGPropertyManager.cpp:82