FlightGear next
FGParameterValue.h
Go to the documentation of this file.
1/*
2 * SPDX-FileName: FGParameterValue.h
3 * SPDX-FileComment: Author: Bertrand Coconnier, Date started: December 09 2018
4 * SPDX-FileCopyrightText: Copyright (C) 2018 B. Coconnier (bcoconni@users.sf.net)
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9 SENTRY
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
11
12#pragma once
13
14/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 INCLUDES
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
17
18#include <stdexcept>
19
22#include "math/FGRealValue.h"
23
24/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25 FORWARD DECLARATIONS
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
27
28namespace JSBSim {
29
31
32/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 CLASS DOCUMENTATION
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
35
39
40/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 DECLARATION: FGParameterValue
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43
45{
46public:
48 {
49 std::string value = el->GetDataLine();
50
51 if (el->GetNumDataLines() != 1 || value.empty()) {
52 std::cerr << el->ReadFrom()
53 << "The element <" << el->GetName()
54 << "> must either contain a value number or a property name."
55 << std::endl;
56 throw std::invalid_argument("FGParameterValue: Illegal argument defining: " + el->GetName());
57 }
58
59 Construct(value, pm);
60 }
61
62 FGParameterValue(const std::string& value, FGPropertyManager* pm)
63 {
64 Construct(value, pm);
65 }
66
67 double GetValue(void) const override { return param->GetValue(); }
68 bool IsConstant(void) const override { return param->IsConstant(); }
69
70 std::string GetName(void) const override
71 {
72 FGPropertyValue* v = dynamic_cast<FGPropertyValue*>(param.ptr());
73 if (v)
74 return v->GetNameWithSign();
75 else
76 return std::to_string(param->GetValue());
77 }
78
79 bool IsLateBound(void) const
80 {
81 FGPropertyValue* v = dynamic_cast<FGPropertyValue*>(param.ptr());
82 return v != nullptr && v->IsLateBound();
83 }
84
85private:
86 FGParameter_ptr param;
87
88 void Construct(const std::string& value, FGPropertyManager* pm)
89 {
90 if (is_number(value)) {
91 param = new FGRealValue(atof(value.c_str()));
92 } else {
93 // "value" must be a property if execution passes to here.
94 param = new FGPropertyValue(value, pm);
95 }
96 }
97};
98
99typedef SGSharedPtr<FGParameterValue> FGParameterValue_ptr;
100
101} // namespace JSBSim
const std::string & GetName(void) const
Retrieves the element name.
std::string ReadFrom(void) const
Return a string that contains a description of the location where the current XML element was read fr...
unsigned int GetNumDataLines(void)
Returns the number of lines of data stored.
std::string GetDataLine(unsigned int i=0)
Gets a line of data belonging to an element.
double GetValue(void) const override
bool IsLateBound(void) const
std::string GetName(void) const override
bool IsConstant(void) const override
FGParameterValue(Element *el, FGPropertyManager *pm)
FGParameterValue(const std::string &value, FGPropertyManager *pm)
Represents various types of parameters.
Definition FGParameter.h:59
Represents a property value which can use late binding.
bool IsLateBound(void) const
virtual std::string GetNameWithSign(void) const
Represents a real value.
Definition FGRealValue.h:58
SGSharedPtr< FGParameter > FGParameter_ptr
Definition FGParameter.h:70
SGSharedPtr< FGParameterValue > FGParameterValue_ptr
static double atof(const string &str)
Definition options.cxx:107
bool is_number(const std::string &str)