[115] | 1 | /* |
---|
| 2 | * ====================================================================== |
---|
| 3 | * Copyright (c) 2004-2005 Purdue Research Foundation |
---|
| 4 | * |
---|
| 5 | * See the file "license.terms" for information on usage and |
---|
| 6 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 7 | * ====================================================================== |
---|
| 8 | */ |
---|
[38] | 9 | #include <iostream> |
---|
| 10 | #include <string> |
---|
| 11 | #include <sstream> |
---|
| 12 | #include <stdlib.h> |
---|
| 13 | #include <errno.h> |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | // #include "RpLibrary.h" |
---|
| 17 | |
---|
| 18 | #ifndef _RpVARIABLE_H |
---|
| 19 | #include "RpVariable.h" |
---|
| 20 | #endif |
---|
| 21 | |
---|
| 22 | #ifndef _RpUNITS_H |
---|
| 23 | #include "RpUnits.h" |
---|
| 24 | #endif |
---|
| 25 | |
---|
| 26 | #ifndef _RpNUMBER_H |
---|
| 27 | #define _RpNUMBER_H |
---|
| 28 | |
---|
| 29 | class RpNumber : public RpVariable |
---|
| 30 | { |
---|
| 31 | public: |
---|
[104] | 32 | |
---|
[38] | 33 | // users member fxns |
---|
| 34 | |
---|
| 35 | /* |
---|
| 36 | virtual RpNumber& setUnits(std::string newUnits); |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | virtual RpNumber& setDefaultValue(double newDefaultVal); |
---|
| 40 | virtual RpNumber& setCurrentValue(double newCurrentVal); |
---|
| 41 | virtual RpNumber& setMin(double newMin); |
---|
| 42 | virtual RpNumber& setMax(double newMax); |
---|
| 43 | |
---|
| 44 | std::string getUnits() const; |
---|
| 45 | // changed from "Value" to "Val" because base class makes |
---|
| 46 | // these functions virtual and derived class has a different |
---|
| 47 | // return type. compiler doesnt like this. need to find another |
---|
| 48 | // way around this |
---|
| 49 | // |
---|
| 50 | // if we keep the null_val=NULL will that give us undefined behavior? |
---|
| 51 | // |
---|
| 52 | double getDefaultValue(void* null_val=NULL) const; |
---|
| 53 | double getCurrentValue(void* null_val=NULL) const; |
---|
| 54 | double getMin() const; |
---|
| 55 | double getMax() const; |
---|
| 56 | |
---|
| 57 | double convert( std::string toUnitStr, |
---|
| 58 | int storeResult, |
---|
| 59 | int* result = NULL ); |
---|
| 60 | double convert(std::string toUnitStr, int* result = NULL); |
---|
[104] | 61 | double convert(const RpUnits* toUnit, int* result = NULL); |
---|
| 62 | |
---|
[38] | 63 | // place the information from this object into the xml library 'lib' |
---|
| 64 | // virtual RpNumber& put(RpLibrary lib); |
---|
[135] | 65 | RpNumber& put() const; |
---|
| 66 | RpNumber& put(double currentVal); |
---|
[38] | 67 | |
---|
| 68 | |
---|
[104] | 69 | |
---|
| 70 | /* |
---|
[38] | 71 | * user provides a 'path' for where this object lives in xml file |
---|
| 72 | * user calls define with standard hints as described in Number docs |
---|
| 73 | * 'units' - string providing units of the number |
---|
| 74 | * 'defaultVal' - numeric value to be used if user provides no value |
---|
| 75 | * 'min' - (optional) minimum allowed value |
---|
| 76 | * 'max' - (optional) maximum allowed value |
---|
| 77 | * 'label' - (optional) name of the input for a gui field |
---|
| 78 | * 'desc' - (optional) description of the input for the gui |
---|
| 79 | * |
---|
| 80 | * Notes: |
---|
| 81 | * if the user sets a min or max value the following applies: |
---|
| 82 | * if the value of min is greater than the value of default, |
---|
| 83 | * then min will be set to default |
---|
| 84 | * |
---|
| 85 | * if the value of max is less than the value of default, |
---|
| 86 | * then max will be set to default. |
---|
| 87 | * |
---|
| 88 | * |
---|
[104] | 89 | * |
---|
[38] | 90 | */ |
---|
[104] | 91 | |
---|
[38] | 92 | /* what about unit-less numbers */ |
---|
| 93 | RpNumber ( |
---|
| 94 | std::string path, |
---|
| 95 | std::string units, |
---|
| 96 | double defaultVal |
---|
| 97 | ) |
---|
| 98 | : RpVariable (path, new double (defaultVal) ), |
---|
| 99 | // units (units), |
---|
| 100 | min (0), |
---|
| 101 | max (0), |
---|
| 102 | minmaxSet (0) |
---|
| 103 | { |
---|
| 104 | this->units = RpUnits::find(units); |
---|
| 105 | if (! this->units) { |
---|
| 106 | this->units = RpUnits::define(units,NULL); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | RpNumber ( |
---|
| 111 | std::string path, |
---|
| 112 | std::string units, |
---|
| 113 | double defaultVal, |
---|
| 114 | double min, |
---|
| 115 | double max, |
---|
| 116 | std::string label, |
---|
| 117 | std::string desc |
---|
| 118 | ) |
---|
| 119 | : RpVariable (path, new double (defaultVal), label, desc), |
---|
| 120 | // units (units), |
---|
| 121 | min (min), |
---|
| 122 | max (max), |
---|
| 123 | minmaxSet (0) |
---|
| 124 | { |
---|
| 125 | |
---|
| 126 | this->units = RpUnits::find(units); |
---|
| 127 | if (! this->units) { |
---|
| 128 | this->units = RpUnits::define(units,NULL); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if ((min == 0) && (max == 0)) { |
---|
| 132 | minmaxSet = 0; |
---|
| 133 | } |
---|
| 134 | else { |
---|
| 135 | |
---|
| 136 | if (min > defaultVal) { |
---|
| 137 | this->min = defaultVal; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | if (max < defaultVal) { |
---|
| 141 | this->max = defaultVal; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | } |
---|
[104] | 146 | |
---|
[38] | 147 | // copy constructor |
---|
| 148 | RpNumber ( const RpNumber& myRpNumber ) |
---|
| 149 | : RpVariable(myRpNumber), |
---|
| 150 | units (myRpNumber.units), |
---|
| 151 | min (myRpNumber.min), |
---|
| 152 | max (myRpNumber.max), |
---|
| 153 | minmaxSet (myRpNumber.minmaxSet) |
---|
| 154 | {} |
---|
| 155 | |
---|
| 156 | // default destructor |
---|
| 157 | // virtual ~RpNumber () |
---|
| 158 | virtual ~RpNumber () |
---|
| 159 | { |
---|
| 160 | // clean up dynamic memory |
---|
| 161 | |
---|
| 162 | } |
---|
| 163 | private: |
---|
| 164 | |
---|
| 165 | // std::string units; |
---|
[104] | 166 | const RpUnits* units; |
---|
[38] | 167 | double min; |
---|
| 168 | double max; |
---|
| 169 | |
---|
| 170 | // flag to tell if the user specified the min and max values |
---|
| 171 | int minmaxSet; |
---|
| 172 | |
---|
| 173 | }; |
---|
| 174 | |
---|
| 175 | /*--------------------------------------------------------------------------*/ |
---|
| 176 | /*--------------------------------------------------------------------------*/ |
---|
| 177 | |
---|
| 178 | #endif |
---|