FlightGear next
FGPropertyManager.h
Go to the documentation of this file.
1/*
2 * SPDX-FileName: FGPropertyManager.h
3 * SPDX-FileComment: Based on work originally by David Megginson
4 * SPDX-FileCopyrightText: Copyright (C) 2002 Tony Peden
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9SENTRY
10%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
11
12#pragma once
13
14/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15INCLUDES
16%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
17
18// This is needed by MSVC9 when included in FlightGear because of
19// the new Vec4d class in props.hxx
20#if defined(HAVE_CONFIG_H)
21#include <config.h>
22#endif
23
24#include <string>
25
26#include "simgear/props/propertyObject.hxx"
27#if !PROPS_STANDALONE
28#include "simgear/math/SGMath.hxx"
29#endif
30
31#include "FGJSBBase.h"
32
33/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34FORWARD DECLARATIONS
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36
37namespace JSBSim {
38
39/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40CLASS DOCUMENTATION
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42
46
47/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48CLASS DECLARATION
49%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51class FGPropertyNode : public SGPropertyNode
52{
53public:
55 virtual ~FGPropertyNode(void) {}
56
65 GetNode(const std::string& path, bool create = false);
66
68 GetNode(const std::string& relpath, int index, bool create = false);
69
76 bool HasNode(const std::string& path);
77
81 const std::string& GetName(void) const { return getNameString(); }
82
86 std::string GetPrintableName(void) const;
87
92 std::string GetFullyQualifiedName(void) const;
93
101 std::string GetRelativeName(const std::string& path = "/fdm/jsbsim/") const;
102
117 bool GetBool(const std::string& name, bool defaultValue = false) const;
118
119
134 int GetInt(const std::string& name, int defaultValue = 0) const;
135
136
151 int GetLong(const std::string& name, long defaultValue = 0L) const;
152
153
168 float GetFloat(const std::string& name, float defaultValue = 0.0) const;
169
170
185 double GetDouble(const std::string& name, double defaultValue = 0.0) const;
186
187
202 std::string GetString(const std::string& name, std::string defaultValue = "") const;
203
204
218 bool SetBool(const std::string& name, bool val);
219
220
234 bool SetInt(const std::string& name, int val);
235
236
250 bool SetLong(const std::string& name, long val);
251
252
266 bool SetFloat(const std::string& name, float val);
267
268
282 bool SetDouble(const std::string& name, double val);
283
284
298 bool SetString(const std::string& name, const std::string& val);
299
300
302 // Convenience functions for setting property attributes.
304
305
318 void SetArchivable(const std::string& name, bool state = true);
319
320
333 void SetReadable(const std::string& name, bool state = true);
334
335
348 void SetWritable(const std::string& name, bool state = true);
349};
350
351typedef SGSharedPtr<FGPropertyNode> FGPropertyNode_ptr;
352typedef SGSharedPtr<const FGPropertyNode> FGConstPropertyNode_ptr;
353
355{
356public:
358 FGPropertyManager(void) { root = new FGPropertyNode; }
359
361 explicit FGPropertyManager(FGPropertyNode* _root) : root(_root){};
362
364 virtual ~FGPropertyManager(void) { Unbind(); }
365
366 FGPropertyNode* GetNode(void) const { return root; }
367 FGPropertyNode* GetNode(const std::string& path, bool create = false)
368 {
369 return root->GetNode(path, create);
370 }
371 FGPropertyNode* GetNode(const std::string& relpath, int index, bool create = false)
372 {
373 return root->GetNode(relpath, index, create);
374 }
375 bool HasNode(const std::string& path) const
376 {
377 std::string newPath = path;
378 if (newPath[0] == '-') newPath.erase(0, 1);
379 return root->HasNode(newPath);
380 }
381
389 std::string mkPropertyName(std::string name, bool lowercase);
390
392 // Convenience functions for tying properties, with logging.
394
395
404 void Untie(const std::string& name);
405
414 void Untie(SGPropertyNode* property);
415
422 void Unbind(void);
423
433 template <typename T>
434 void
435 Tie(const std::string& name, T* pointer)
436 {
437 SGPropertyNode* property = root->getNode(name.c_str(), true);
438 if (!property) {
439 std::cerr << "Could not get or create property " << name << std::endl;
440 return;
441 }
442
443 if (!property->tie(SGRawValuePointer<T>(pointer), false))
444 std::cerr << "Failed to tie property " << name << " to a pointer" << std::endl;
445 else {
446 tied_properties.push_back(property);
447 if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
448 }
449 }
450
464
465 template <typename T>
466 void
467 Tie(const std::string& name, T (*getter)(), void (*setter)(T) = nullptr)
468 {
469 SGPropertyNode* property = root->getNode(name.c_str(), true);
470 if (!property) {
471 std::cerr << "Could not get or create property " << name << std::endl;
472 return;
473 }
474
475 if (!property->tie(SGRawValueFunctions<T>(getter, setter), false))
476 std::cerr << "Failed to tie property " << name << " to functions"
477 << std::endl;
478 else {
479 if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
480 if (!getter) property->setAttribute(SGPropertyNode::READ, false);
481 tied_properties.push_back(property);
482 if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
483 }
484 }
485
501 template <typename T>
502 void
503 Tie(const std::string& name, int index, T (*getter)(int),
504 void (*setter)(int, T) = nullptr)
505 {
506 SGPropertyNode* property = root->getNode(name.c_str(), true);
507 if (!property) {
508 std::cerr << "Could not get or create property " << name << std::endl;
509 return;
510 }
511
512 if (!property->tie(SGRawValueFunctionsIndexed<T>(index, getter, setter),
513 false))
514 std::cerr << "Failed to tie property " << name << " to indexed functions"
515 << std::endl;
516 else {
517 if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
518 if (!getter) property->setAttribute(SGPropertyNode::READ, false);
519 tied_properties.push_back(property);
520 if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
521 }
522 }
523
540 template <class T, class V>
541 void
542 Tie(const std::string& name, T* obj, V (T::*getter)() const,
543 void (T::*setter)(V) = nullptr)
544 {
545 SGPropertyNode* property = root->getNode(name.c_str(), true);
546 if (!property) {
547 std::cerr << "Could not get or create property " << name << std::endl;
548 return;
549 }
550
551 if (!property->tie(SGRawValueMethods<T, V>(*obj, getter, setter), false))
552 std::cerr << "Failed to tie property " << name << " to object methods"
553 << std::endl;
554 else {
555 if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
556 if (!getter) property->setAttribute(SGPropertyNode::READ, false);
557 tied_properties.push_back(property);
558 if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
559 }
560 }
561
578 template <class T, class V>
579 void
580 Tie(const std::string& name, T* obj, int index, V (T::*getter)(int) const,
581 void (T::*setter)(int, V) = nullptr)
582 {
583 SGPropertyNode* property = root->getNode(name.c_str(), true);
584 if (!property) {
585 std::cerr << "Could not get or create property " << name << std::endl;
586 return;
587 }
588
589 if (!property->tie(SGRawValueMethodsIndexed<T, V>(*obj, index, getter, setter),
590 false))
591 std::cerr << "Failed to tie property " << name
592 << " to indexed object methods" << std::endl;
593 else {
594 if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
595 if (!getter) property->setAttribute(SGPropertyNode::READ, false);
596 tied_properties.push_back(property);
597 if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
598 }
599 }
600
601 template <class T>
602 simgear::PropertyObject<T>
603 CreatePropertyObject(const std::string& path)
604 {
605 return simgear::PropertyObject<T>(root->GetNode(path, true));
606 }
607
608private:
609 std::vector<SGPropertyNode_ptr> tied_properties;
611};
612} // namespace JSBSim
static short debug_lvl
Definition FGJSBBase.h:190
void Untie(const std::string &name)
Untie a property from an external data source.
FGPropertyNode * GetNode(void) const
std::string mkPropertyName(std::string name, bool lowercase)
Property-ify a name replaces spaces with '-' and, optionally, makes name all lower case.
void Tie(const std::string &name, T(*getter)(), void(*setter)(T)=nullptr)
Tie a property to a pair of simple functions.
FGPropertyManager(FGPropertyNode *_root)
Constructor.
virtual ~FGPropertyManager(void)
Destructor.
void Tie(const std::string &name, T *pointer)
Tie a property to an external variable.
FGPropertyNode * GetNode(const std::string &path, bool create=false)
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.
FGPropertyNode * GetNode(const std::string &relpath, int index, bool create=false)
bool HasNode(const std::string &path) const
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.
simgear::PropertyObject< T > CreatePropertyObject(const std::string &path)
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.
void Unbind(void)
Unbind all properties bound by this manager to an external data source.
FGPropertyManager(void)
Default constructor.
Class wrapper for property handling.
bool SetLong(const std::string &name, long val)
Set 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...
void SetWritable(const std::string &name, bool state=true)
Set the state of the write attribute for a property.
bool SetBool(const std::string &name, bool val)
Set a bool value for a property.
int GetLong(const std::string &name, long defaultValue=0L) const
Get a long value for a property.
virtual ~FGPropertyNode(void)
Destructor.
bool HasNode(const std::string &path)
Test whether a given node exists.
bool SetFloat(const std::string &name, float val)
Set a float value for a property.
bool SetInt(const std::string &name, int val)
Set an int value for a property.
FGPropertyNode * GetNode(const std::string &relpath, int index, bool create=false)
float GetFloat(const std::string &name, float defaultValue=0.0) const
Get a float value for a property.
void SetArchivable(const std::string &name, bool state=true)
Set the state of the archive attribute for a property.
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.
bool GetBool(const std::string &name, bool defaultValue=false) const
Get a bool value for a property.
FGPropertyNode * GetNode(const std::string &path, bool create=false)
Get a property node.
std::string GetPrintableName(void) const
Get the name of a node without underscores, etc.
const std::string & GetName(void) const
Get the name of a node.
bool SetString(const std::string &name, const std::string &val)
Set a string value for a property.
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 SetReadable(const std::string &name, bool state=true)
Set the state of the read attribute for a property.
bool SetDouble(const std::string &name, double val)
Set a double value for a property.
double GetDouble(const std::string &name, double defaultValue=0.0) const
Get a double value for a property.
const char * name
SGSharedPtr< const FGPropertyNode > FGConstPropertyNode_ptr
SGSharedPtr< FGPropertyNode > FGPropertyNode_ptr