Sfoglia il codice sorgente

Externalize unit enumeration and convenience methods.

Neal Wilson 11 anni fa
parent
commit
7392139651
2 ha cambiato i file con 145 aggiunte e 1 eliminazioni
  1. 3
    1
      src/typica.w
  2. 142
    0
      src/units.w

+ 3
- 1
src/typica.w Vedi File

@@ -5703,7 +5703,9 @@ temperature and the time at which that measurement was taken.
5703 5703
 
5704 5704
 Measurement times are represented as instances of |QTime|.
5705 5705
 
5706
-The declaration of |Measurement| is reasonably straightforward. Measurements
5706
+@i units.w
5707
+
5708
+@ The declaration of |Measurement| is reasonably straightforward. Measurements
5707 5709
 will often be passed around to a number of different objects through the signals
5708 5710
 and slots mechanism of Qt. The simplest way to make this work is to provide a
5709 5711
 copy constructor and an assignment operator.

+ 142
- 0
src/units.w Vedi File

@@ -0,0 +1,142 @@
1
+@* Unit information.
2
+
3
+\noindent As unit information is required in several places, the enumeration
4
+listing these is moved to an external class. This derives from QObject only to
5
+take advantage of the meta-object system. There are no data members, however
6
+there are some convenience methods for working with unit data.
7
+
8
+@(units.h@>=
9
+class Units: public QObject
10
+{
11
+	Q_OBJECT
12
+	Q_ENUMS(Units)
13
+	public:
14
+		enum Units
15
+		{
16
+			Fahrenheit = 10144,
17
+			Celsius = 10143,
18
+			Kelvin = 10325,
19
+			Rankine = 10145
20
+		};
21
+		double convertTemperature(double value, Unit fromUnit, Unit toUnit);
22
+		bool isTemperatureUnit(Unit unit);
23
+}
24
+
25
+@ Methods are implemented in a separate file.
26
+
27
+@(units.cpp@>=
28
+#inculde "units.h"
29
+#include "moc_units.cpp"
30
+
31
+@ The |isTemperatureUnit()| method may seem counter-intuitive while the enum
32
+only contains represenations of temperature measurements, but there are plans
33
+to extend this later to extend hardware support to devices that do not directly
34
+measure temperature values and also to support the measurement of properties
35
+that cannot sensibly be represented as temperatures such as fuel pressure and
36
+control settings.
37
+
38
+@(units.cpp@>=
39
+bool Units::isTemperatureUnit(Unit unit)
40
+{
41
+	if(unit == Fahrenheit ||
42
+	   unit == Celsius ||
43
+	   unit == Kelvin ||
44
+	   unit == Rankine)
45
+	{
46
+		return true;
47
+	}
48
+	return false;
49
+}
50
+
51
+@ Temperature conversions can be performed by the |Units| class and should
52
+eliminate the need to have conversion code in multiple places. This method
53
+takes the measurement value, the unit that value is in, and the unit that we
54
+would like to have the measurement converted into. It returns the value as it
55
+would be expressed after unit conversion. A value of 0 is presently returned
56
+if either or both of the units to convert are not temperature units, however
57
+that behavior may change in the future. Any code activating that branch should
58
+be considered flawed.
59
+
60
+@(units.cpp@>=
61
+double Units::convertTemperature(double value, Unit fromUnit, Unit toUnit)
62
+{
63
+	if(isTemperatureUnit(fromUnit) && isTemperatureUnit(toUnit) == false)
64
+	{
65
+		return 0;
66
+	}
67
+	switch(fromUnit)
68
+	{
69
+		case Fahrenheit:
70
+			switch(toUnit)
71
+			{
72
+				case Fahrenheit:
73
+					return value;
74
+					break;
75
+				case Celsius:
76
+					return (value - 32) * 5 / 9;
77
+					break;
78
+				case Kelvin:
79
+					return (value + 459.67) * 5 / 9;
80
+					break;
81
+				case Rankine:
82
+					return value + 459.67;
83
+					break;
84
+			}
85
+			break;
86
+		case Celsius:
87
+			switch(toUnit)
88
+			{
89
+				case Fahrenheit:
90
+					return value * 9 / 5 + 32;
91
+					break;
92
+				case Celsius:
93
+					return value;
94
+					break;
95
+				case Kelvin:
96
+					return value + 273.15;
97
+					break;
98
+				case Rankine:
99
+					return (value + 273.15) * 9 / 5;
100
+					break;
101
+			}
102
+			break;
103
+		case Kelvin:
104
+			switch(toUnit)
105
+			{
106
+				case Fahrenheit:
107
+					return value * 5 / 9 - 459.67;
108
+					break;
109
+				case Celsius:
110
+					return value - 273.15;
111
+					break;
112
+				case Kelvin:
113
+					return value;
114
+					break;
115
+				case Rankine:
116
+					return value * 9 / 5;
117
+					break;
118
+			}
119
+			break;
120
+		case Rankine:
121
+			switch(toUnit)
122
+			{
123
+				case Fahrenheit:
124
+					return value - 457.67;
125
+					break;
126
+				case Celsius:
127
+					return (value - 491.67) * 5 / 9;
128
+					break;
129
+				case Kelvin:
130
+					return value * 5 / 9;
131
+					break;
132
+				case Rankine:
133
+					return value;
134
+					break;
135
+			}
136
+			break;
137
+		default:
138
+			return 0;
139
+			break;
140
+	}
141
+	return 0;
142
+}

Loading…
Annulla
Salva