Units of Measure

The use of a unit of measure is meant to encompass the means by which a measured value is tied explicitly to its unit of measure.

Units of measure can be Base or Derived. Base units are well-defined units which by convention are regarded as dimensionally independent. There is only one Base Unit per type of measure. The instantiation of base units can be seen in the code snippet bellow:

//...
  te::common::UnitOfMeasure* umeter = new te::common::UnitOfMeasure(te::common::UOM_Metre,"metre","m",te::common::Length,"SI standard unit for linear measures");
  te::common::UnitOfMeasure* ukmeter = new te::common::UnitOfMeasure(te::common::UOM_Kilometre,"kilometre","km",te::common::Length,     te::common::UOM_Metre,1000);
 
  te::common::UnitOfMeasure* uradian = new te::common::UnitOfMeasure(te::common::UOM_Radian,"radian","rad",te::common::Angle,"SI standard unit for angular measures");
//...

Derived units are those formed by combining base units according to the algebraic relations linking the corresponding quantities. Derived Units besides have all the attributes of base units plus the identification of the Base Unit they derive of and 4 factors to relate them, using the following formula:

   Y = (AX + B) / (CX + D) 

where Y is the measure in the derived unit and X is measure in base unit.

At the application level, a singleton called UnitsOfMeasureManager maintain a registry of known units of measure. Units, and optionally its alternative names, are registered in the manager, as can be seen in the code snippet bellow:

//...
   UnitsOfMeasureManager::getInstance();
  std::vector<std::string> altnames;
  altnames.push_back("meter");
  altnames.push_back("metro");
  te::common::UnitsOfMeasureManager::getInstance().insert(umeter, altnames);
  te::common::UnitsOfMeasureManager::getInstance().insert(ukmeter);
  te::common::UnitsOfMeasureManager::getInstance().insert(ukradian);
 
//...

Now, if you need a unit you should get it from the UnitOfMeasureManager as shown bellow:

//...
  try {
      // searching by name and displaying information about unit
    te::common::UnitOfMeasure* munit = te::common::UnitsOfMeasureManager::getInstance().find("metre");
    std::cout << "Searching \"metre\": " << munit->getId() << ", " << munit->getSymbol() << ", " << munit->getName() << ", "  << munit->getDescription();
 
    std::vector<std::string> altnames;
    te::common::UnitsOfMeasureManager::getInstance().getAlternativeNames(munit,altnames);
    if (!altnames.empty())
    {
      for (size_t i=0; i<altnames.size(); ++i)
        std::cout << " , " << altnames[i];
      std::cout << std::endl;
    }  
      // converting from units
    std::cout << "1 km = " << te::common::UnitsOfMeasureManager::getInstance().getConversion("kilometre","metre") << " m." << std::endl;
    std::cout << "1  m = " << te::common::UnitsOfMeasureManager::getInstance().getConversion("metre","kilometre") << " km." << std::endl;
 
    std::cout << "1 deg = " << te::common::UnitsOfMeasureManager::getInstance().getConversion("degree","radian") << " rad." << std::endl;
    std::cout << "1 rad = " << te::common::UnitsOfMeasureManager::getInstance().getConversion("radian","degree") << " deg." << std::endl;
 
    std::cout << "1 km = " << te::common::UnitsOfMeasureManager::getInstance().getConversion("kilometre","radian") << "rad." << std::endl;
 
  }
  catch (te::common::Exception& ex) 
  {
    std::cout << ex.what() << std::endl;
  }
 
//...

QR Code
QR Code wiki:designimplementation:common:unitofmeasure (generated for current page)