File: quantity.h

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (360 lines) | stat: -rw-r--r-- 10,360 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/***************************************************************************
  quantity.h
  -------------------
  Generic quantity class
  -------------------
  Copyright 2001-2008, David Johnson
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***************************************************************************/

#ifndef QUANTITY_H
#define QUANTITY_H

#include <QMap>
#include <QString>

typedef double (*Conversion)(double);

/**
 * A class to encapsulate the idea of a unit of measure.
 * This is a slight variation of the Unit from Fowler's
 * Quantity analysis pattern.
 */
class Unit {
 public:
    // Constructor
    Unit(const QString &name, const QString &symbol);
    // Destructor
    ~Unit();

    // Return the full name of Unit
    QString name() const;
    // Return the symbol of Unit
    QString symbol() const;

    // Add a conversion function for this Unit
    void addConversion(const Unit &to, const Conversion c);
    // Can we convert to Unit?
    bool convertable(const Unit &u) const;
    // Convert amount to Unit
    double convert(double amount, const Unit &u) const;

    // Return pointer to Unit if it exists
    static Unit* unit(const QString &symbol);

    // equivalence operator
    bool operator==(const Unit &u) const;

 private:
    Unit(const Unit&);
    Unit operator=(const Unit&);

 private:
    QString name_;
    QString symbol_;
    QMap<QString,Conversion> conversions_;
    static QMap<QString,Unit*> units_;
};

/**
 * A class to encapsulate the idea of a measurable quantity.
 * This is a slight variation of Fowler's Quantity analysis pattern.
 */
class Quantity {
  public:
    // Constructor
    Quantity(double amount, const Unit &unit);
    // Default constructor
    Quantity();
    // Construct from string
    Quantity(const QString &from, const Unit &defaultunit = generic);
    // Copy constructor
    Quantity(const Quantity &q);
    // Destructor
    virtual ~Quantity();

    // Return quantity amount
    double amount() const;
    // Return converted amount
    double amount(const Unit &u) const;
    // Return quantity unit
    const Unit& unit() const;
    // Convert quantity to unit
    void convert(const Unit &u);
    // output quantity as a string
    QString toString(int prec=2) const;
    // set quantity from string
    void fromQString(const QString &from, const Unit &defaultunit = generic);

    // equivalence operator
    bool operator==(const Quantity &q) const;

    // assignment operators
    Quantity &operator=(const Quantity &q);
    Quantity &operator+=(const Quantity &q);
    Quantity &operator+=(const double d);
    Quantity &operator-=(const Quantity &q);
    Quantity &operator-=(const double d);

    // arithmetic operators
    friend Quantity operator+(const Quantity &q1, const Quantity &q2);
    friend Quantity operator+(const Quantity &q, const double d);
    friend Quantity operator+(const double d, const Quantity &q);

    friend Quantity operator-(const Quantity &q1, const Quantity &q2);
    friend Quantity operator-(const Quantity &q, const double d);
    friend Quantity operator-(const double d, const Quantity &q);

    friend Quantity operator-(const Quantity &q);

 public:
    static Unit generic;

 protected:
    double amount_;
    Unit const* unit_;
};

class Weight : public Quantity
{
 public:
    // Constructor
    Weight(double amount, const Unit &unit);
    /// Default constructor
    Weight();
    /// Construct from string
    Weight(const QString &from, const Unit &defaultunit = pound);
    // Copy constructor
    Weight(const Weight &w);
    // Conversion constructor
    Weight(const Quantity &q);
    // Destructor
    ~Weight();

 public:
    static Unit gram;
    static Unit kilogram;
    static Unit ounce;
    static Unit pound;

 private:
    // initialize the weight units
    static void initialize();

 private:
    static bool initialized_;
};

class Volume : public Quantity
{
 public:
    // Constructor
    Volume(double amount, const Unit &unit);
    /// Default constructor
    Volume();
    /// Construct from string
    Volume(const QString &from, const Unit &defaultunit = gallon);
    // Copy constructor
    Volume(const Volume &v);
    // Conversion constructor
    Volume(const Quantity &q);
    // destructor
    ~Volume();

 public:
    static Unit barrel;
    static Unit fluidounce;
    static Unit gallon;
    static Unit hectoliter;
    static Unit liter;
    static Unit milliliter;

 private:
    // initialize the weight units
    static void initialize();

 private:
    static bool initialized_;
};

class Temperature : public Quantity
{
 public:
    // Constructor
    Temperature(double amount, const Unit &unit);
    /// Default constructor
    Temperature();
    /// Construct from string
    Temperature(const QString &from, const Unit &defaultunit = fahrenheit);
    // Copy constructor
    Temperature(const Temperature &t);
    // Conversion constructor
    Temperature(const Quantity &q);
    // destructor
    ~Temperature();

 public:
    static Unit fahrenheit;
    static Unit celsius;

 private:
    // initialize the temperature units
    static void initialize();

 private:
    static bool initialized_;
};

// Conversion Prototypes ////////////////////////////////////////////////////

double gram2kilogram(double value);
double gram2ounce(double value);
double gram2pound(double value);
double kilogram2gram(double value);
double kilogram2ounce(double value);
double kilogram2pound(double value);
double ounce2gram(double value);
double ounce2kilogram(double value);
double ounce2pound(double value);
double pound2gram(double value);
double pound2kilogram(double value);
double pound2ounce(double value);

double barrel2fluidounce(double value);
double barrel2gallon(double value);
double barrel2hectoliter(double value);
double barrel2liter(double value);
double barrel2milliliter(double value);
double fluidounce2barrel(double value);
double fluidounce2gallon(double value);
double fluidounce2hectoliter(double value);
double fluidounce2liter(double value);
double fluidounce2milliliter(double value);
double gallon2barrel(double value);
double gallon2fluidounce(double value);
double gallon2hectoliter(double value);
double gallon2liter(double value);
double gallon2milliliter(double value);
double hectoliter2barrel(double value);
double hectoliter2fluidounce(double value);
double hectoliter2gallon(double value);
double hectoliter2liter(double value);
double hectoliter2milliliter(double value);
double liter2barrel(double value);
double liter2fluidounce(double value);
double liter2gallon(double value);
double liter2hectoliter(double value);
double liter2milliliter(double value);
double milliliter2barrel(double value);
double milliliter2fluidounce(double value);
double milliliter2gallon(double value);
double milliliter2hectoliter(double value);
double milliliter2liter(double value);

double fahrenheit2celsius(double value);
double celsius2fahrenheit(double value);

// Inlined Methods //////////////////////////////////////////////////////////

inline QString Unit::name() const
    { return name_; }

inline QString Unit::symbol() const
    { return symbol_; }

inline bool Unit::convertable(const Unit &u) const
    { return (conversions_.contains(u.name())); }

inline Quantity::Quantity(double amount, const Unit &unit)
    : amount_(amount), unit_(&unit) { ; }

inline Quantity::Quantity()
    : amount_(0.0), unit_(&generic) { ; }

inline Quantity::Quantity(const QString &from, const Unit &defaultunit)
    : amount_(0.0), unit_(&defaultunit) { fromQString(from, defaultunit); }

inline Quantity::Quantity(const Quantity &q)
    : amount_(q.amount_), unit_(q.unit_) { ; }

inline Quantity::~Quantity()
    { ; }

inline Quantity &Quantity::operator+=(const double d)
    { amount_ += d; return *this; }

inline Quantity operator+(const double d, const Quantity &q)
    { return (q + d); }

inline Quantity &Quantity::operator-=(const double d)
    { amount_ -= d;  return *this; }

inline double Quantity::amount() const
    { return amount_; }

inline double Quantity::amount(const Unit &u) const
    { return (unit_->convert(amount_, u)); }

inline const Unit& Quantity::unit() const
    { return *unit_; }

inline Weight::Weight(const Weight &w)
    : Quantity(w) { ; }

inline Weight::Weight()
    : Quantity(0.0, pound) { ; }

inline Weight::Weight(const QString &from, const Unit &defaultunit)
    : Quantity(from, defaultunit) { ; }

inline Weight::~Weight()
    { ; }

inline Volume::Volume(const Volume &v)
    : Quantity(v) { ; }

inline Volume::Volume()
    : Quantity(0.0, gallon) { ; }

inline Volume::Volume(const QString &from, const Unit &defaultunit)
    : Quantity(from, defaultunit) { ; }

inline Volume::~Volume()
    { ; }

inline Temperature::Temperature(const Temperature &t)
    : Quantity(t) { ; }

inline Temperature::Temperature()
    : Quantity(0.0, fahrenheit) { ; }

inline Temperature::Temperature(const QString &from, const Unit &defaultunit)
    : Quantity(from, defaultunit) { ; }

inline Temperature::~Temperature()
    { ; }

#endif // QUANTITY_H