File indexing completed on 2024-05-12 17:22:42

0001 // This file is part of the SpeedCrunch project
0002 // Copyright (C) 2015 Pol Welter <polwelter@gmail.com>
0003 // Copyright (C) 2016 @heldercorreia
0004 //
0005 // This program is free software; you can redistribute it and/or
0006 // modify it under the terms of the GNU General Public License
0007 // as published by the Free Software Foundation; either version 2
0008 // of the License, or (at your option) any later version.
0009 //
0010 // This program is distributed in the hope that it will be useful,
0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 // GNU General Public License for more details.
0014 //
0015 // You should have received a copy of the GNU General Public License
0016 // along with this program; see the file COPYING.  If not, write to
0017 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018 // Boston, MA 02110-1301, USA.
0019 
0020 #ifndef UNITS_H
0021 #define UNITS_H
0022 
0023 #include <QHash>
0024 #include <QMap>
0025 
0026 #include "quantity.h"
0027 class Rational;
0028 
0029 struct Unit {
0030     QString name;
0031     Quantity value;
0032     Unit(QString name, Quantity value)
0033         : name(name)
0034         , value(value)
0035     { }
0036     Unit()
0037         : name("")
0038         , value(1)
0039     { }
0040 };
0041 
0042 class Units {
0043     static void pushUnit(Quantity q, QString name);
0044     static QHash<QMap<QString, Rational>, Unit> m_matchLookup;
0045     static QMap<QString, Quantity> m_cache;
0046     static void initTable();
0047 
0048 public:
0049     static void findUnit(Quantity& q);
0050     static void clearCache() { m_cache.clear(); }
0051     static const QList<Unit> getList();
0052 
0053     // Base SI units.
0054     static const Quantity meter();
0055     static const Quantity second();
0056     static const Quantity kilogram();
0057     static const Quantity ampere();
0058     static const Quantity mole();
0059     static const Quantity kelvin();
0060     static const Quantity candela();
0061 
0062     // Base non-SI units.
0063     static const Quantity bit();
0064 
0065     // SI prefixes.
0066     static const Quantity yocto();
0067     static const Quantity zepto();
0068     static const Quantity atto();
0069     static const Quantity femto();
0070     static const Quantity pico();
0071     static const Quantity nano();
0072     static const Quantity micro();
0073     static const Quantity milli();
0074     static const Quantity centi();
0075     static const Quantity deci();
0076 
0077     static const Quantity deca();
0078     static const Quantity hecto();
0079     static const Quantity kilo();
0080     static const Quantity mega();
0081     static const Quantity giga();
0082     static const Quantity tera();
0083     static const Quantity peta();
0084     static const Quantity exa();
0085     static const Quantity zetta();
0086     static const Quantity yotta();
0087 
0088     // Binary prefixes.
0089     static const Quantity kibi();
0090     static const Quantity mebi();
0091     static const Quantity gibi();
0092     static const Quantity tebi();
0093     static const Quantity pebi();
0094     static const Quantity exbi();
0095     static const Quantity zebi();
0096     static const Quantity yobi();
0097 
0098     // Derived SI units.
0099     static const Quantity sqmeter();
0100     static const Quantity cbmeter();
0101     static const Quantity newton();
0102     static const Quantity hertz();
0103     static const Quantity steradian();
0104     static const Quantity pascal();
0105     static const Quantity joule();
0106     static const Quantity watt();
0107     static const Quantity coulomb();
0108     static const Quantity volt();
0109     static const Quantity farad();
0110     static const Quantity ohm();
0111     static const Quantity siemens();
0112     static const Quantity weber();
0113     static const Quantity tesla();
0114     static const Quantity henry();
0115     static const Quantity lumen();
0116     static const Quantity lux();
0117     static const Quantity becquerel();
0118     static const Quantity gray();
0119     static const Quantity sievert();
0120     static const Quantity katal();
0121 
0122     // Derived from SI units.
0123 
0124     // Mass.
0125     static const Quantity metric_ton();
0126     static const Quantity short_ton();
0127     static const Quantity long_ton();
0128     static const Quantity pound();
0129     static const Quantity ounce();
0130     static const Quantity grain();
0131     static const Quantity gram();
0132     static const Quantity atomic_mass_unit();
0133     static const Quantity carat();
0134 
0135 
0136     // Distance/length.
0137     static const Quantity micron();
0138     static const Quantity angstrom();
0139     static const Quantity astronomical_unit();
0140     static const Quantity lightyear();
0141     static const Quantity lightsecond();
0142     static const Quantity lightminute();
0143     static const Quantity parsec();
0144     // US measures.
0145     static const Quantity inch();
0146     static const Quantity foot();
0147     static const Quantity yard();
0148     static const Quantity mile();
0149     static const Quantity rod();
0150     static const Quantity furlong();
0151     // Nautical (US).
0152     static const Quantity fathom();
0153     static const Quantity nautical_mile();
0154     static const Quantity cable();
0155 
0156     // Area.
0157     static const Quantity are();
0158     static const Quantity hectare();
0159     static const Quantity acre();
0160 
0161     // Volume.
0162     static const Quantity US_gallon();
0163     static const Quantity UK_gallon();
0164     static const Quantity US_quart();
0165     static const Quantity UK_quart();
0166     static const Quantity US_pint();
0167     static const Quantity UK_pint();
0168     static const Quantity US_fluid_ounce();
0169     static const Quantity UK_fluid_ounce();
0170     static const Quantity liter();
0171 
0172     // Time.
0173     static const Quantity minute();
0174     static const Quantity hour();
0175     static const Quantity day();
0176     static const Quantity week();
0177     static const Quantity julian_year();
0178     static const Quantity tropical_year();
0179     static const Quantity sidereal_year();
0180 
0181     // Concentration.
0182     static const Quantity percent();
0183     static const Quantity ppm();
0184     static const Quantity ppb();
0185     static const Quantity karat();
0186 
0187     // Pressure.
0188     static const Quantity bar();
0189     static const Quantity atmosphere();
0190     static const Quantity torr();
0191     static const Quantity pounds_per_sqinch();
0192 
0193     // Energy.
0194     static const Quantity electron_volt();
0195     static const Quantity calorie();
0196     static const Quantity british_thermal_unit();
0197 
0198 
0199     // Information.
0200     static const Quantity nat();
0201     static const Quantity hartley();
0202     static const Quantity byte();
0203 
0204     static const Quantity exabyte();
0205     static const Quantity terabyte();
0206     static const Quantity gigabyte();
0207     static const Quantity megabyte();
0208     static const Quantity kilobyte();
0209 
0210     static const Quantity exbibyte();
0211     static const Quantity tebibyte();
0212     static const Quantity gibibyte();
0213     static const Quantity mebibyte();
0214     static const Quantity kibibyte();
0215 
0216     static const Quantity bps();
0217     static const Quantity tbps();
0218     static const Quantity gbps();
0219     static const Quantity mbps();
0220     static const Quantity kbps();
0221 
0222 
0223     // Cooking.
0224     // Note: these again differ from US to UK, Australia, Japan, ...
0225     // Since for cooking generally not that high a precision is
0226     // required, let's just stick with the so called 'legal' variant.
0227     static const Quantity tablespoon();
0228     static const Quantity teaspoon();
0229     static const Quantity cup();
0230 
0231     // Various others.
0232     // TODO: Some of these are constants that should be moved once constants are also accessible via builtin names.
0233     static const Quantity gravity();
0234     static const Quantity speed_of_light();
0235     static const Quantity elementary_charge();
0236     static const Quantity speed_of_sound_STP();
0237     static const Quantity knot();
0238     static const Quantity horsepower();
0239 };
0240 
0241 #endif // UNITS_H