| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGParameterValue.h |
| 4 | Author: Bertrand Coconnier |
| 5 | Date started: December 09 2018 |
| 6 | |
| 7 | --------- Copyright (C) 2018 B. Coconnier ([email protected]) ----------- |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify it under |
| 10 | the terms of the GNU Lesser General Public License as published by the Free |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 17 | details. |
| 18 | |
| 19 | You should have received a copy of the GNU Lesser General Public License along |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on the world wide web at http://www.gnu.org. |
| 25 | |
| 26 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 27 | SENTRY |
| 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 29 | |
| 30 | #ifndef FGPARAMETERVALUE_H |
| 31 | #define FGPARAMETERVALUE_H |
| 32 | |
| 33 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 34 | INCLUDES |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 36 | |
| 37 | #include "math/FGRealValue.h" |
| 38 | #include "math/FGPropertyValue.h" |
| 39 | #include "input_output/FGXMLElement.h" |
| 40 | |
| 41 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 42 | FORWARD DECLARATIONS |
| 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 44 | |
| 45 | namespace JSBSim { |
| 46 | |
| 47 | class FGPropertyManager; |
| 48 | |
| 49 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 50 | CLASS DOCUMENTATION |
| 51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 52 | |
| 53 | /** Represents a either a real value or a property value |
| 54 | @author Bertrand Coconnier |
| 55 | */ |
| 56 | |
| 57 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 58 | DECLARATION: FGParameterValue |
| 59 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 60 | |
| 61 | class JSBSIM_API FGParameterValue : public FGParameter |
| 62 | { |
| 63 | public: |
| 64 | FGParameterValue(Element* el, FGPropertyManager* pm) { |
| 65 | std::string value = el->GetDataLine(); |
| 66 | |
| 67 | if (el->GetNumDataLines() != 1 || value.empty()) { |
| 68 | std::cerr << el->ReadFrom() |
| 69 | << "The element <" << el->GetName() |
| 70 | << "> must either contain a value number or a property name." |
| 71 | << std::endl; |
| 72 | throw BaseException("FGParameterValue: Illegal argument defining: " + el->GetName()); |
| 73 | } |
| 74 | |
| 75 | Construct(value, pm); |
| 76 | } |
| 77 | |
| 78 | FGParameterValue(const std::string& value, FGPropertyManager* pm) { |
| 79 | Construct(value, pm); |
| 80 | } |
| 81 | |
| 82 | double GetValue(void) const override { return param->GetValue(); } |
| 83 | bool IsConstant(void) const override { return param->IsConstant(); } |
| 84 | |
| 85 | std::string GetName(void) const override { |
| 86 | FGPropertyValue* v = dynamic_cast<FGPropertyValue*>(param.ptr()); |
| 87 | if (v) |
| 88 | return v->GetNameWithSign(); |
| 89 | else |
| 90 | return param->GetName(); |
| 91 | } |
| 92 | |
| 93 | bool IsLateBound(void) const { |
| 94 | FGPropertyValue* v = dynamic_cast<FGPropertyValue*>(param.ptr()); |
| 95 | return v != nullptr && v->IsLateBound(); |
| 96 | } |
| 97 | private: |
| 98 | FGParameter_ptr param; |
| 99 | |
| 100 | void Construct(const std::string& value, FGPropertyManager* pm) { |
| 101 | if (is_number(value)) { |
| 102 | param = new FGRealValue(atof(value.c_str())); |
| 103 | } else { |
| 104 | // "value" must be a property if execution passes to here. |
| 105 | param = new FGPropertyValue(value, pm); |
| 106 | } |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | typedef SGSharedPtr<FGParameterValue> FGParameterValue_ptr; |
| 111 | |
| 112 | } // namespace JSBSim |
| 113 | |
| 114 | #endif |