Create custom transformations

I am trying to create units of measure per meter and kilometer. I want to summarize and convert them later. I know that in the boost :: units library there is already an SI system, but I want to create everything from scratch, because then I need to create my own systems for my projects (so I do this to study). My goal is to declare a variable "Length", which can be changed using units: for example, I want to write

Length xLength1 = 5350 * Meters + 2 Kilometers;

To this end, I created a file length.h containing the definitions of meter and kilometer, and at the end I declare a conversion between the two units:

#ifndef LENGTH_H_
#define LENGTH_H_

#include <boost/units/base_dimension.hpp>
#include <boost/units/base_unit.hpp>
#include <boost/units/scaled_base_unit.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/conversion.hpp>

struct LengthBaseDimension : boost::units::base_dimension<LengthBaseDimension,1>{};

typedef LengthBaseDimension::dimension_type LengthDimension;

struct MeterBaseUnit : boost::units::base_unit<MeterBaseUnit, LengthDimension, 1>{};
template <> struct boost::units::base_unit_info<MeterBaseUnit>
{
  static std::string name()   { return "meter"; }
  static std::string symbol() { return "m";     }
};

struct KilometerBaseUnit : boost::units::base_unit<KilometerBaseUnit, LengthDimension, 2>{};
template <> struct boost::units::base_unit_info<KilometerBaseUnit>
{
  static std::string name()   { return "kilometer"; }
  static std::string symbol() { return "km";        }
};

// I don't want to use scaled_unit because I need this for my real purpose
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(KilometerBaseUnit, MeterBaseUnit, double, 1000.0);

#endif

Then I create a units.h file in which I define my own unit system

#ifndef LIB_UNITS_H_
#define LIB_UNITS_H_

#include "length.h"
#include <boost/units/unit.hpp>
#include <boost/units/static_constant.hpp>
#include <boost/units/make_system.hpp>
#include <boost/units/io.hpp>

typedef boost::units::make_system<MeterBaseUnit>::type UnitsSystem;

typedef boost::units::unit<boost::units::dimensionless_type, UnitsSystem> Dimensionless;
typedef boost::units::unit<LengthDimension                 , UnitsSystem> SystemLength;


BOOST_UNITS_STATIC_CONSTANT(Kilometer  , SystemLength);
BOOST_UNITS_STATIC_CONSTANT(Kilometers , SystemLength);
BOOST_UNITS_STATIC_CONSTANT(Meter      , SystemLength);
BOOST_UNITS_STATIC_CONSTANT(Meters     , SystemLength);

// Typedefs of dimensions
typedef boost::units::quantity<SystemLength> Length;

#endif

,

#include "units.h"
#include <iostream>

int main(void)
{
  Length xLength1 ( 300.0 * Meters);
  Length xLength2 (1500.0 * Kilometer );
  Length xLength3;
  std::cout << xLength2 << std::endl;
  xLength3 = xLength1 + xLength2;

  return 0;
}

, , . xLength2, 1500 1500 1500000 . , 1800 . , , .

?

+5
1

, Boost, . .

! :

class temperature{
   double temp; //always in kelvin
   temperature(double temperature, char unit){
       if(unit=='k')
                temp=temperature
       if(unit=='c')
                temp=temperature+273
    }
 };

, , . , , . SI, , , , , ..

0

All Articles