Skip to content

damianorenfer/libquantify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libquantify

Simple C++11 library to handle quantities and units. It is based on the International System of Units concept using the base dimensions (7) and then additional units are built on top of them. There are some great libraries that do the same, but none of them fulfilled my needs. This library uses dynamic (run time) units where the other libs use compile time units.

Features:

  • Dynamic units
  • Conversion between units (of the same dimension)
  • Unit composition
  • Units with offsets (i.e temperature units), at the moment only conversions are available, no composition
  • Should be memory safe as everything is value based, so no new nor malloc in there

Note that this is my first library, first C++11 project and first CMake project. So any suggestions or improvements are welcome :).

Installation

Linux and MinGW users :

git clone ...
cd local_repo
mkdir build && cd build
cmake ..
make
sudo make install

Windows users :

Sorry I have not tested it yet, but it should not be difficult to build and install.

Usage

#include <quantify/quantity.h>
#include <quantify/standardunits.h>

using namespace Quantify;

int main(int argc, char *argv[])
{
   Quantity meters(StandardUnits::LengthUnits::meter, 22.0);
   Quantity feets = meters.convertTo(StandardUnits::LengthUnits::foot); //unit conversion
   Quantity seconds(StandardUnits::TimeUnits::second, 15);

   Quantity speed = meters / seconds; // unit composition
   Quantity speedInKph = speed.convertTo(StandardUnits::SpeedUnits::kilometerPerHour);
   Quantity speedInMph = speed.convertTo(StandardUnits::SpeedUnits::milePerHour);

   Quantity tempInDegreeCelsius(StandardUnits::TemperatureUnits::degreeCelsius, 36.9);
   Quantity tempInDegreeFahrenheit = tempInDegreeCelsius.convertTo(StandardUnits::TemperatureUnits::degreeFahrenheit);

   try
   {
      // will throw UnitUnsupportedOperationException because units with offset do not support composition
      Quantity tempAndTime = tempInDegreeCelsius * seconds;
   }
   catch(std::exception &ex)
   {
      std::cout << ex.what() << std::endl;
   }

   try
   {
      // will throw IncompatibleUnitsException because units are not in the same dimension (not compatible)
      // same would happen with conversions
      Quantity dummySum = speed + meters;
   }
   catch(std::exception &ex)
   {        
      std::cout << ex.what() << std::endl;
   }

   return 0;
}

If a unit is not available in StandardUnits, just create it ;). See src/standardunits.cpp

Credits

License

This project is licensed under the MIT License - see the LICENSE file for details