FlightGear next
FGPropertyValue.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileName: FGPropertyValue.cpp
3 * SPDX-FileComment: Stores property values
4 * SPDX-FileCopyrightText: Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org)
5 * SPDX-FileContributor: Copyright (C) 2010 - 2011 Anders Gidenstam (anders(at)gidenstam.org)
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10INCLUDES
11%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
12
13#include <assert.h>
14
15#include "FGPropertyValue.h"
16
17namespace JSBSim {
18
19/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20CLASS IMPLEMENTATION
21%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
22
23FGPropertyValue::FGPropertyValue(const std::string& propName,
24 FGPropertyManager* propertyManager)
25 : PropertyManager(propertyManager), PropertyNode(nullptr),
26 PropertyName(propName), Sign(1.0)
27{
28 if (PropertyName[0] == '-') {
29 PropertyName.erase(0,1);
30 Sign = -1.0;
31 }
32
33 if (PropertyManager->HasNode(PropertyName))
34 PropertyNode = PropertyManager->GetNode(PropertyName);
35}
36
37//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38
40{
41 if (!PropertyNode) {
42 FGPropertyNode* node = PropertyManager->GetNode(PropertyName);
43
44 if (!node)
45 throw(std::string("FGPropertyValue::GetValue() The property " +
46 PropertyName + " does not exist."));
47
48 PropertyNode = node;
49 }
50
51 return PropertyNode;
52}
53
54//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55
56double FGPropertyValue::GetValue(void) const
57{
58 return GetNode()->getDoubleValue()*Sign;
59}
60
61//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62
63void FGPropertyValue::SetValue(double value)
64{
65 // SetValue() ignores the Sign flag. So make sure it is never called with a
66 // negative sign.
67 assert(Sign == 1);
68 GetNode()->setDoubleValue(value);
69}
70
71//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72
73std::string FGPropertyValue::GetName(void) const
74{
75 if (PropertyNode)
76 return PropertyNode->GetName();
77 else
78 return PropertyName;
79}
80
81//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82
83std::string FGPropertyValue::GetNameWithSign(void) const
84{
85 std::string name;
86
87 if (Sign < 0.0) name ="-";
88
89 name += GetName();
90
91 return name;
92}
93
94//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
97{
98 if (PropertyNode)
99 return PropertyNode->GetFullyQualifiedName();
100 else
101 return PropertyName;
102}
103
104//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
107{
108 if (PropertyNode)
109 return PropertyNode->GetPrintableName();
110 else
111 return PropertyName;
112}
113
114}
Class wrapper for property handling.
FGPropertyNode * GetNode(const std::string &path, bool create=false)
Get a property node.
const std::string & GetName(void) const
Get the name of a node.
virtual std::string GetFullyQualifiedName(void) const
double GetValue(void) const override
virtual std::string GetPrintableName(void) const
std::string GetName(void) const override
virtual std::string GetNameWithSign(void) const
FGPropertyNode * GetNode(void) const
void SetValue(double value)
FGPropertyValue(FGPropertyNode *propNode)
const char * name