JSBSim Flight Dynamics Model  1.0 (02 March 2017)
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 DEFINITIONS
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 
56 #define ID_PROPERTYMANAGER "$Id: FGPropertyManager.h,v 1.30 2016/05/05 15:32:42 bcoconni Exp $"
57 
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 FORWARD DECLARATIONS
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61 
62 namespace JSBSim {
63 
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 CLASS DOCUMENTATION
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 
72 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 CLASS DECLARATION
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 
76 class FGPropertyNode : public SGPropertyNode
77 {
78  public:
80  virtual ~FGPropertyNode(void) {}
81 
90  GetNode (const std::string &path, bool create = false);
91 
93  GetNode (const std::string &relpath, int index, bool create = false);
94 
101  bool HasNode (const std::string &path);
102 
106  std::string GetName( void ) const;
107 
111  std::string GetPrintableName( void ) const;
112 
117  std::string GetFullyQualifiedName(void) const;
118 
126  std::string GetRelativeName( const std::string &path = "/fdm/jsbsim/" ) const;
127 
142  bool GetBool (const std::string &name, bool defaultValue = false) const;
143 
144 
159  int GetInt (const std::string &name, int defaultValue = 0) const;
160 
161 
176  int GetLong (const std::string &name, long defaultValue = 0L) const;
177 
178 
193  float GetFloat (const std::string &name, float defaultValue = 0.0) const;
194 
195 
210  double GetDouble (const std::string &name, double defaultValue = 0.0) const;
211 
212 
227  std::string GetString (const std::string &name, std::string defaultValue = "") const;
228 
229 
243  bool SetBool (const std::string &name, bool val);
244 
245 
259  bool SetInt (const std::string &name, int val);
260 
261 
275  bool SetLong (const std::string &name, long val);
276 
277 
291  bool SetFloat (const std::string &name, float val);
292 
293 
307  bool SetDouble (const std::string &name, double val);
308 
309 
323  bool SetString (const std::string &name, const std::string &val);
324 
325 
327  // Convenience functions for setting property attributes.
329 
330 
343  void SetArchivable (const std::string &name, bool state = true);
344 
345 
358  void SetReadable (const std::string &name, bool state = true);
359 
360 
373  void SetWritable (const std::string &name, bool state = true);
374 };
375 
376 typedef SGSharedPtr<FGPropertyNode> FGPropertyNode_ptr;
377 typedef SGSharedPtr<const FGPropertyNode> FGConstPropertyNode_ptr;
378 
380 {
381  public:
383  FGPropertyManager(void) { root = new FGPropertyNode; }
384 
386  explicit FGPropertyManager(FGPropertyNode* _root) : root(_root) {};
387 
389  virtual ~FGPropertyManager(void) { Unbind(); }
390 
391  FGPropertyNode* GetNode(void) const { return root; }
392  FGPropertyNode* GetNode(const std::string &path, bool create = false)
393  { return root->GetNode(path, create); }
394  FGPropertyNode* GetNode(const std::string &relpath, int index, bool create = false)
395  { return root->GetNode(relpath, index, create); }
396  bool HasNode(const std::string& path) const
397  {
398  std::string newPath = path;
399  if (newPath[0] == '-') newPath.erase(0,1);
400  return root->HasNode(newPath);
401  }
402 
410  std::string mkPropertyName(std::string name, bool lowercase);
411 
413  // Convenience functions for tying properties, with logging.
415 
416 
423  void Untie (const std::string &name);
424 
431  void Unbind (void);
432 
433  // Templates cause ambiguity here
434 
447  void
448  Tie (const std::string &name, bool *pointer, bool useDefault = true);
449 
450 
463  void
464  Tie (const std::string &name, int *pointer, bool useDefault = true);
465 
466 
479  void
480  Tie (const std::string &name, long *pointer, bool useDefault = true);
481 
482 
495  void
496  Tie (const std::string &name, float *pointer, bool useDefault = true);
497 
510  void
511  Tie (const std::string &name, double *pointer, bool useDefault = true);
512 
513 //============================================================================
514 //
515 // All of the following functions *must* be inlined, otherwise linker
516 // errors will result
517 //
518 //============================================================================
519 
520  /* template <class V> void
521  Tie (const std::string &name, V (*getter)(), void (*setter)(V) = 0,
522  bool useDefault = true);
523 
524  template <class V> void
525  Tie (const std::string &name, int index, V (*getter)(int),
526  void (*setter)(int, V) = 0, bool useDefault = true);
527 
528  template <class T, class V> void
529  Tie (const std::string &name, T * obj, V (T::*getter)() const,
530  void (T::*setter)(V) = 0, bool useDefault = true);
531 
532  template <class T, class V> void
533  Tie (const std::string &name, T * obj, int index,
534  V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
535  bool useDefault = true); */
536 
554  template <class V> inline void
555  Tie (const std::string &name, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true)
556  {
557  SGPropertyNode* property = root->getNode(name.c_str(), true);
558  if (!property) {
559  std::cerr << "Could not get or create property " << name << std::endl;
560  return;
561  }
562 
563  if (!property->tie(SGRawValueFunctions<V>(getter, setter), useDefault))
564  std::cerr << "Failed to tie property " << name << " to functions" << std::endl;
565  else {
566  if (setter == 0) property->setAttribute(SGPropertyNode::WRITE, false);
567  if (getter == 0) 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 
573 
592  template <class V> inline void Tie (const std::string &name, int index, V (*getter)(int),
593  void (*setter)(int, V) = 0, bool useDefault = true)
594  {
595  SGPropertyNode* property = root->getNode(name.c_str(), true);
596  if (!property) {
597  std::cerr << "Could not get or create property " << name << std::endl;
598  return;
599  }
600 
601  if (!property->tie(SGRawValueFunctionsIndexed<V>(index, getter, setter), useDefault))
602  std::cerr << "Failed to tie property " << name << " to indexed functions" << std::endl;
603  else {
604  if (setter == 0) property->setAttribute(SGPropertyNode::WRITE, false);
605  if (getter == 0) 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 
631  template <class T, class V> inline void
632  Tie (const std::string &name, T * obj, V (T::*getter)() const,
633  void (T::*setter)(V) = 0, bool useDefault = true)
634  {
635  SGPropertyNode* property = root->getNode(name.c_str(), true);
636  if (!property) {
637  std::cerr << "Could not get or create property " << name << std::endl;
638  return;
639  }
640 
641  if (!property->tie(SGRawValueMethods<T,V>(*obj, getter, setter), useDefault))
642  std::cerr << "Failed to tie property " << name << " to object methods" << std::endl;
643  else {
644  if (setter == 0) property->setAttribute(SGPropertyNode::WRITE, false);
645  if (getter == 0) property->setAttribute(SGPropertyNode::READ, false);
646  tied_properties.push_back(property);
647  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
648  }
649  }
650 
670  template <class T, class V> inline void
671  Tie (const std::string &name, T * obj, int index, V (T::*getter)(int) const,
672  void (T::*setter)(int, V) = 0, bool useDefault = true)
673  {
674  SGPropertyNode* property = root->getNode(name.c_str(), true);
675  if (!property) {
676  std::cerr << "Could not get or create property " << name << std::endl;
677  return;
678  }
679 
680  if (!property->tie(SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter), useDefault))
681  std::cerr << "Failed to tie property " << name << " to indexed object methods" << std::endl;
682  else {
683  if (setter == 0) property->setAttribute(SGPropertyNode::WRITE, false);
684  if (getter == 0) property->setAttribute(SGPropertyNode::READ, false);
685  tied_properties.push_back(property);
686  if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
687  }
688  }
689 
690  template <class T> simgear::PropertyObject<T>
691  CreatePropertyObject(const std::string &path)
692  { return simgear::PropertyObject<T>(root->GetNode(path, true)); }
693 
694  private:
695  std::vector<SGPropertyNode_ptr> tied_properties;
696  FGPropertyNode_ptr root;
697 };
698 }
699 #endif // FGPROPERTYMANAGER_H
700 
int GetInt(const std::string &name, int defaultValue=0) const
Get an int value for a property.
std::string GetString(const std::string &name, std::string defaultValue="") const
Get a string value for a property.
FGPropertyManager(FGPropertyNode *_root)
Constructor.
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...
void Tie(const std::string &name, T *obj, V(T::*getter)() const, void(T::*setter)(V)=0, bool useDefault=true)
Tie a property to a pair of object methods.
bool HasNode(const std::string &path)
Test whether a given node exists.
bool SetString(const std::string &name, const std::string &val)
Set a string value for a property.
void SetArchivable(const std::string &name, bool state=true)
Set the state of the archive attribute for a property.
virtual ~FGPropertyManager(void)
Destructor.
std::string GetName(void) const
Get the name of a node.
FGPropertyNode * GetNode(const std::string &path, bool create=false)
Get a property node.
Class wrapper for property handling.
bool SetDouble(const std::string &name, double val)
Set a double value for a property.
float GetFloat(const std::string &name, float defaultValue=0.0) const
Get a float value for a property.
int GetLong(const std::string &name, long defaultValue=0L) const
Get a long value for a property.
std::string GetFullyQualifiedName(void) const
Get the fully qualified name of a node This function is very slow, so is probably useful for debuggin...
bool SetLong(const std::string &name, long val)
Set a long value for a property.
void SetReadable(const std::string &name, bool state=true)
Set the state of the read attribute for a property.
virtual ~FGPropertyNode(void)
Destructor.
void Tie(const std::string &name, V(*getter)(), void(*setter)(V)=0, bool useDefault=true)
Tie a property to a pair of simple functions.
void SetWritable(const std::string &name, bool state=true)
Set the state of the write attribute for a property.
std::string GetPrintableName(void) const
Get the name of a node without underscores, etc.
bool GetBool(const std::string &name, bool defaultValue=false) const
Get a bool value for a property.
bool SetFloat(const std::string &name, float val)
Set a float value for a property.
void Tie(const std::string &name, T *obj, int index, V(T::*getter)(int) const, void(T::*setter)(int, V)=0, bool useDefault=true)
Tie a property to a pair of indexed object methods.
double GetDouble(const std::string &name, double defaultValue=0.0) const
Get a double value for a property.
FGPropertyManager(void)
Default constructor.
void Tie(const std::string &name, int index, V(*getter)(int), void(*setter)(int, V)=0, bool useDefault=true)
Tie a property to a pair of indexed functions.
bool SetInt(const std::string &name, int val)
Set an int value for a property.
bool SetBool(const std::string &name, bool val)
Set a bool value for a property.