| 1 | /*************************************************************************** |
| 2 | quantity.h |
| 3 | ------------------- |
| 4 | Generic quantity class |
| 5 | ------------------- |
| 6 | Copyright 2001-2008, David Johnson |
| 7 | All rights reserved. |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without |
| 10 | modification, are permitted provided that the following conditions |
| 11 | are met: |
| 12 | |
| 13 | 1. Redistributions of source code must retain the above copyright |
| 14 | notice, this list of conditions and the following disclaimer. |
| 15 | |
| 16 | 2. Redistributions in binary form must reproduce the above copyright |
| 17 | notice, this list of conditions and the following disclaimer in the |
| 18 | documentation and/or other materials provided with the distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | ***************************************************************************/ |
| 32 | |
| 33 | #ifndef QUANTITY_H |
| 34 | #define QUANTITY_H |
| 35 | |
| 36 | #include <QMap> |
| 37 | #include <QString> |
| 38 | |
| 39 | typedef double (*Conversion)(double); |
| 40 | |
| 41 | /** |
| 42 | * A class to encapsulate the idea of a unit of measure. |
| 43 | * This is a slight variation of the Unit from Fowler's |
| 44 | * Quantity analysis pattern. |
| 45 | */ |
| 46 | class Unit { |
| 47 | public: |
| 48 | // Constructor |
| 49 | Unit(const QString &name, const QString &symbol); |
| 50 | // Destructor |
| 51 | ~Unit(); |
| 52 | |
| 53 | // Return the full name of Unit |
| 54 | QString name() const; |
| 55 | // Return the symbol of Unit |
| 56 | QString symbol() const; |
| 57 | |
| 58 | // Add a conversion function for this Unit |
| 59 | void addConversion(const Unit &to, const Conversion c); |
| 60 | // Can we convert to Unit? |
| 61 | bool convertable(const Unit &u) const; |
| 62 | // Convert amount to Unit |
| 63 | double convert(double amount, const Unit &u) const; |
| 64 | |
| 65 | // Return pointer to Unit if it exists |
| 66 | static Unit* unit(const QString &symbol); |
| 67 | |
| 68 | // equivalence operator |
| 69 | bool operator==(const Unit &u) const; |
| 70 | |
| 71 | private: |
| 72 | Unit(const Unit&); |
| 73 | Unit operator=(const Unit&); |
| 74 | |
| 75 | private: |
| 76 | QString name_; |
| 77 | QString symbol_; |
| 78 | QMap<QString,Conversion> conversions_; |
| 79 | static QMap<QString,Unit*> units_; |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * A class to encapsulate the idea of a measurable quantity. |
| 84 | * This is a slight variation of Fowler's Quantity analysis pattern. |
| 85 | */ |
| 86 | class Quantity { |
| 87 | public: |
| 88 | // Constructor |
| 89 | Quantity(double amount, const Unit &unit); |
| 90 | // Default constructor |
| 91 | Quantity(); |
| 92 | // Construct from string |
| 93 | Quantity(const QString &from, const Unit &defaultunit = generic); |
| 94 | // Copy constructor |
| 95 | Quantity(const Quantity &q); |
| 96 | // Destructor |
| 97 | virtual ~Quantity(); |
| 98 | |
| 99 | // Return quantity amount |
| 100 | double amount() const; |
| 101 | // Return converted amount |
| 102 | double amount(const Unit &u) const; |
| 103 | // Return quantity unit |
| 104 | const Unit& unit() const; |
| 105 | // Convert quantity to unit |
| 106 | void convert(const Unit &u); |
| 107 | // output quantity as a string |
| 108 | QString toString(int prec=2) const; |
| 109 | // set quantity from string |
| 110 | void fromQString(const QString &from, const Unit &defaultunit = generic); |
| 111 | |
| 112 | // equivalence operator |
| 113 | bool operator==(const Quantity &q) const; |
| 114 | |
| 115 | // assignment operators |
| 116 | Quantity &operator=(const Quantity &q); |
| 117 | Quantity &operator+=(const Quantity &q); |
| 118 | Quantity &operator+=(const double d); |
| 119 | Quantity &operator-=(const Quantity &q); |
| 120 | Quantity &operator-=(const double d); |
| 121 | |
| 122 | // arithmetic operators |
| 123 | friend Quantity operator+(const Quantity &q1, const Quantity &q2); |
| 124 | friend Quantity operator+(const Quantity &q, const double d); |
| 125 | friend Quantity operator+(const double d, const Quantity &q); |
| 126 | |
| 127 | friend Quantity operator-(const Quantity &q1, const Quantity &q2); |
| 128 | friend Quantity operator-(const Quantity &q, const double d); |
| 129 | friend Quantity operator-(const double d, const Quantity &q); |
| 130 | |
| 131 | friend Quantity operator-(const Quantity &q); |
| 132 | |
| 133 | public: |
| 134 | static Unit generic; |
| 135 | |
| 136 | protected: |
| 137 | double amount_; |
| 138 | Unit const* unit_; |
| 139 | }; |
| 140 | |
| 141 | class Weight : public Quantity |
| 142 | { |
| 143 | public: |
| 144 | // Constructor |
| 145 | Weight(double amount, const Unit &unit); |
| 146 | /// Default constructor |
| 147 | Weight(); |
| 148 | /// Construct from string |
| 149 | Weight(const QString &from, const Unit &defaultunit = pound); |
| 150 | // Copy constructor |
| 151 | Weight(const Weight &w); |
| 152 | // Conversion constructor |
| 153 | Weight(const Quantity &q); |
| 154 | // Destructor |
| 155 | ~Weight(); |
| 156 | |
| 157 | public: |
| 158 | static Unit gram; |
| 159 | static Unit kilogram; |
| 160 | static Unit ounce; |
| 161 | static Unit pound; |
| 162 | |
| 163 | private: |
| 164 | // initialize the weight units |
| 165 | static void initialize(); |
| 166 | |
| 167 | private: |
| 168 | static bool initialized_; |
| 169 | }; |
| 170 | |
| 171 | class Volume : public Quantity |
| 172 | { |
| 173 | public: |
| 174 | // Constructor |
| 175 | Volume(double amount, const Unit &unit); |
| 176 | /// Default constructor |
| 177 | Volume(); |
| 178 | /// Construct from string |
| 179 | Volume(const QString &from, const Unit &defaultunit = gallon); |
| 180 | // Copy constructor |
| 181 | Volume(const Volume &v); |
| 182 | // Conversion constructor |
| 183 | Volume(const Quantity &q); |
| 184 | // destructor |
| 185 | ~Volume(); |
| 186 | |
| 187 | public: |
| 188 | static Unit barrel; |
| 189 | static Unit fluidounce; |
| 190 | static Unit gallon; |
| 191 | static Unit hectoliter; |
| 192 | static Unit liter; |
| 193 | static Unit milliliter; |
| 194 | |
| 195 | private: |
| 196 | // initialize the weight units |
| 197 | static void initialize(); |
| 198 | |
| 199 | private: |
| 200 | static bool initialized_; |
| 201 | }; |
| 202 | |
| 203 | class Temperature : public Quantity |
| 204 | { |
| 205 | public: |
| 206 | // Constructor |
| 207 | Temperature(double amount, const Unit &unit); |
| 208 | /// Default constructor |
| 209 | Temperature(); |
| 210 | /// Construct from string |
| 211 | Temperature(const QString &from, const Unit &defaultunit = fahrenheit); |
| 212 | // Copy constructor |
| 213 | Temperature(const Temperature &t); |
| 214 | // Conversion constructor |
| 215 | Temperature(const Quantity &q); |
| 216 | // destructor |
| 217 | ~Temperature(); |
| 218 | |
| 219 | public: |
| 220 | static Unit fahrenheit; |
| 221 | static Unit celsius; |
| 222 | |
| 223 | private: |
| 224 | // initialize the temperature units |
| 225 | static void initialize(); |
| 226 | |
| 227 | private: |
| 228 | static bool initialized_; |
| 229 | }; |
| 230 | |
| 231 | // Conversion Prototypes //////////////////////////////////////////////////// |
| 232 | |
| 233 | double gram2kilogram(double value); |
| 234 | double gram2ounce(double value); |
| 235 | double gram2pound(double value); |
| 236 | double kilogram2gram(double value); |
| 237 | double kilogram2ounce(double value); |
| 238 | double kilogram2pound(double value); |
| 239 | double ounce2gram(double value); |
| 240 | double ounce2kilogram(double value); |
| 241 | double ounce2pound(double value); |
| 242 | double pound2gram(double value); |
| 243 | double pound2kilogram(double value); |
| 244 | double pound2ounce(double value); |
| 245 | |
| 246 | double barrel2fluidounce(double value); |
| 247 | double barrel2gallon(double value); |
| 248 | double barrel2hectoliter(double value); |
| 249 | double barrel2liter(double value); |
| 250 | double barrel2milliliter(double value); |
| 251 | double fluidounce2barrel(double value); |
| 252 | double fluidounce2gallon(double value); |
| 253 | double fluidounce2hectoliter(double value); |
| 254 | double fluidounce2liter(double value); |
| 255 | double fluidounce2milliliter(double value); |
| 256 | double gallon2barrel(double value); |
| 257 | double gallon2fluidounce(double value); |
| 258 | double gallon2hectoliter(double value); |
| 259 | double gallon2liter(double value); |
| 260 | double gallon2milliliter(double value); |
| 261 | double hectoliter2barrel(double value); |
| 262 | double hectoliter2fluidounce(double value); |
| 263 | double hectoliter2gallon(double value); |
| 264 | double hectoliter2liter(double value); |
| 265 | double hectoliter2milliliter(double value); |
| 266 | double liter2barrel(double value); |
| 267 | double liter2fluidounce(double value); |
| 268 | double liter2gallon(double value); |
| 269 | double liter2hectoliter(double value); |
| 270 | double liter2milliliter(double value); |
| 271 | double milliliter2barrel(double value); |
| 272 | double milliliter2fluidounce(double value); |
| 273 | double milliliter2gallon(double value); |
| 274 | double milliliter2hectoliter(double value); |
| 275 | double milliliter2liter(double value); |
| 276 | |
| 277 | double fahrenheit2celsius(double value); |
| 278 | double celsius2fahrenheit(double value); |
| 279 | |
| 280 | // Inlined Methods ////////////////////////////////////////////////////////// |
| 281 | |
| 282 | inline QString Unit::name() const |
| 283 | { return name_; } |
| 284 | |
| 285 | inline QString Unit::symbol() const |
| 286 | { return symbol_; } |
| 287 | |
| 288 | inline bool Unit::convertable(const Unit &u) const |
| 289 | { return (conversions_.contains(u.name())); } |
| 290 | |
| 291 | inline Quantity::Quantity(double amount, const Unit &unit) |
| 292 | : amount_(amount), unit_(&unit) { ; } |
| 293 | |
| 294 | inline Quantity::Quantity() |
| 295 | : amount_(0.0), unit_(&generic) { ; } |
| 296 | |
| 297 | inline Quantity::Quantity(const QString &from, const Unit &defaultunit) |
| 298 | : amount_(0.0), unit_(&defaultunit) { fromQString(from, defaultunit); } |
| 299 | |
| 300 | inline Quantity::Quantity(const Quantity &q) |
| 301 | : amount_(q.amount_), unit_(q.unit_) { ; } |
| 302 | |
| 303 | inline Quantity::~Quantity() |
| 304 | { ; } |
| 305 | |
| 306 | inline Quantity &Quantity::operator+=(const double d) |
| 307 | { amount_ += d; return *this; } |
| 308 | |
| 309 | inline Quantity operator+(const double d, const Quantity &q) |
| 310 | { return (q + d); } |
| 311 | |
| 312 | inline Quantity &Quantity::operator-=(const double d) |
| 313 | { amount_ -= d; return *this; } |
| 314 | |
| 315 | inline double Quantity::amount() const |
| 316 | { return amount_; } |
| 317 | |
| 318 | inline double Quantity::amount(const Unit &u) const |
| 319 | { return (unit_->convert(amount_, u)); } |
| 320 | |
| 321 | inline const Unit& Quantity::unit() const |
| 322 | { return *unit_; } |
| 323 | |
| 324 | inline Weight::Weight(const Weight &w) |
| 325 | : Quantity(w) { ; } |
| 326 | |
| 327 | inline Weight::Weight() |
| 328 | : Quantity(0.0, pound) { ; } |
| 329 | |
| 330 | inline Weight::Weight(const QString &from, const Unit &defaultunit) |
| 331 | : Quantity(from, defaultunit) { ; } |
| 332 | |
| 333 | inline Weight::~Weight() |
| 334 | { ; } |
| 335 | |
| 336 | inline Volume::Volume(const Volume &v) |
| 337 | : Quantity(v) { ; } |
| 338 | |
| 339 | inline Volume::Volume() |
| 340 | : Quantity(0.0, gallon) { ; } |
| 341 | |
| 342 | inline Volume::Volume(const QString &from, const Unit &defaultunit) |
| 343 | : Quantity(from, defaultunit) { ; } |
| 344 | |
| 345 | inline Volume::~Volume() |
| 346 | { ; } |
| 347 | |
| 348 | inline Temperature::Temperature(const Temperature &t) |
| 349 | : Quantity(t) { ; } |
| 350 | |
| 351 | inline Temperature::Temperature() |
| 352 | : Quantity(0.0, fahrenheit) { ; } |
| 353 | |
| 354 | inline Temperature::Temperature(const QString &from, const Unit &defaultunit) |
| 355 | : Quantity(from, defaultunit) { ; } |
| 356 | |
| 357 | inline Temperature::~Temperature() |
| 358 | { ; } |
| 359 | |
| 360 | #endif // QUANTITY_H |