source: trunk/src/core/RpNumber.cc @ 829

Last change on this file since 829 was 135, checked in by dkearney, 18 years ago

1) fixed children function in c++'s library module so users can now
search for children by type.
2) adjusted bindings dictionary module for storing lib's to allow caller
to set the key of the value being stored.
3) removed old targets for rappture_interface.o and rappture_fortran.o
from makefile
4) renamed matlab and octave binding functions names to match the module
they came from.
5) adjusted matlab/octave example in examples/app_fermi/matlab
6) added matlab and octave search paths environment variables to
gui/apps/rappture

File size: 5.3 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture 2.0 Number Object Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick Kearney, Purdue University
7 *  Copyright (c) 2004-2005  Purdue Research Foundation
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13
14#include "RpNumber.h"
15
16/**********************************************************************/
17// METHOD: setDefaultValue()
18/// set the default value of a RpNumber object.
19/**
20 */
21
22RpNumber&
23RpNumber::setDefaultValue(double newDefaultVal) {
24
25    double* def = NULL;
26
27    def = (double*) RpVariable::getDefaultValue();
28
29    if (!def) {
30        RpVariable::setDefaultValue(new double (newDefaultVal));
31    }
32    else {
33        *def = newDefaultVal;
34    }
35
36    return *this;
37}
38
39/**********************************************************************/
40// METHOD: setCurrentValue()
41/// Set the current value of a RpNumber object.
42/**
43 */
44
45RpNumber&
46RpNumber::setCurrentValue(double newCurrentVal) {
47
48    double* cur = (double*) RpVariable::getCurrentValue();
49    double* def = (double*) RpVariable::getDefaultValue();
50
51    if (cur == def) {
52        RpVariable::setCurrentValue(new double (newCurrentVal));
53    }
54    else {
55        *cur = newCurrentVal;
56    }
57
58    return *this;
59}
60
61
62/**********************************************************************/
63// METHOD: setMin()
64/// Set the min value of a RpNumber object.
65/**
66 */
67
68RpNumber&
69RpNumber::setMin(double newMin) {
70
71    min = newMin;
72    return *this;
73}
74
75/**********************************************************************/
76// METHOD: setMax()
77/// set the min value of a RpNumber object
78/**
79 */
80
81RpNumber&
82RpNumber::setMax(double newMax) {
83
84    max = newMax;
85    return *this;
86}
87
88/**********************************************************************/
89// METHOD: getUnits()
90/// Report the units of the object.
91/**
92 */
93
94std::string
95RpNumber::getUnits() const {
96
97    return units->getUnitsName();
98}
99
100
101/**********************************************************************/
102// METHOD: getDefaultValue()
103/// Report the default value of the object.
104/**
105 */
106
107double
108RpNumber::getDefaultValue(void* null_val) const {
109
110    return *((double*) RpVariable::getDefaultValue());
111}
112
113/**********************************************************************/
114// METHOD: getCurrentValue()
115/// Report the current value of the object.
116/**
117 */
118
119double
120RpNumber::getCurrentValue(void* null_val) const {
121
122    return *((double*) RpVariable::getCurrentValue());
123}
124
125/**********************************************************************/
126// METHOD: getMin()
127/// Report the min of the object.
128/**
129 */
130
131double
132RpNumber::getMin() const {
133
134    return min;
135}
136
137/**********************************************************************/
138// METHOD: getMax()
139/// report the max of the object.
140/**
141 */
142
143double
144RpNumber::getMax() const {
145
146    return max;
147}
148
149/**********************************************************************/
150// METHOD: convert()
151/// Convert the number object to another unit from string
152/**
153 * Store the result as the currentValue.
154 */
155
156double
157RpNumber::convert(std::string toUnitStr, int storeResult, int* result) {
158
159    const RpUnits* toUnit = NULL;
160    double retVal = 0;
161    int my_result = 0;
162
163    toUnit = RpUnits::find(toUnitStr);
164
165    // set the result to the default value if it exists
166    if (result) {
167        *result = my_result;
168    }
169
170    if (!toUnit) {
171        // should raise error!
172        // conversion not defined because unit does not exist
173        return retVal;
174    }
175
176    // perform the conversion
177    retVal = convert(toUnit,&my_result);
178
179    // check the result of the conversion and store if necessary
180    if (my_result) {
181        if (result) {
182            *result = my_result;
183        }
184
185        // check if we should store the value and change units
186        if (storeResult) {
187            // no need to deallocate old units,
188            // because they are stored in static dictionary
189            units = toUnit;
190            RpNumber::setCurrentValue(retVal);
191        }
192    }
193
194    return retVal;
195}
196
197/**********************************************************************/
198// METHOD: convert()
199/// Convert the number object to another unit from string.
200/**
201 */
202
203double
204RpNumber::convert(std::string toUnitStr, int* result) {
205
206    const RpUnits* toUnit = NULL;
207    toUnit = RpUnits::find(toUnitStr);
208    if (!toUnit) {
209        // should raise error!
210        // conversion not defined because unit does not exist
211        if (result) {
212            *result = -1;
213        }
214        return 0.0;
215    }
216    return convert(toUnit, result);
217}
218
219/**********************************************************************/
220// METHOD: convert()
221/// Convert the number object to another unit from RpUnits object.
222/**
223 */
224
225double
226RpNumber::convert(const RpUnits* toUnit, int *result) {
227
228    return units->convert(toUnit,getCurrentValue(), result);
229}
230
231/**********************************************************************/
232// METHOD: put()
233/// Store the information of this Rappture Object into the xml
234/**
235 */
236//
237//RpNumber&
238//RpNumber::put() const {
239//
240//   
241//    return *this;
242//}
243
244
245// -------------------------------------------------------------------- //
246
Note: See TracBrowser for help on using the repository browser.