本文整理汇总了C++中Quantity::baseUnits方法的典型用法代码示例。如果您正苦于以下问题:C++ Quantity::baseUnits方法的具体用法?C++ Quantity::baseUnits怎么用?C++ Quantity::baseUnits使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Quantity
的用法示例。
在下文中一共展示了Quantity::baseUnits方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: working
boost::optional<Quantity> QuantityConverterSingleton::m_convertToSI(const Quantity &original) const
{
// create a working copy of the original
Quantity working(original);
// Make sure to work unscaled: 10^0
int scaleExponent = working.scale().exponent;
if (working.scale().exponent != 0) {
working.setScale(0);
}
// build a result quantity with SI units and value equal to original
Quantity result(working.value(), UnitSystem(UnitSystem::SI));
// Get the base units of original
std::vector<std::string> baseOfOriginal = original.baseUnits();
// Loop over base units in original and apply conversions found in m_toSImap
std::vector<std::string>::const_iterator it = baseOfOriginal.begin();
std::vector<std::string>::const_iterator end = baseOfOriginal.end();
while( it != end ) {
int baseExponent = working.baseUnitExponent(*it);
// apply conversion factor
BaseUnitConversionMap::const_iterator mapItr = m_toSImap.find(*it);
if (mapItr == m_toSImap.end()) {
LOG(Error,"Cannot convert base unit '" << *it << "' to SI because it is not "
<< "registered with the QuantityConverter.");
return boost::none;
}
baseUnitConversionFactor factor = mapItr->second;
if (factor.offset != 0.0) {
for( int i = 0; i < std::abs(baseExponent); ++i) {
if( baseExponent > 0 ){
result.setValue( (result.value() * factor.factor) + factor.offset);
}else {
result.setValue( (result.value() / factor.factor) + factor.offset);
}
}
}
else {
result.setValue( result.value() * std::pow(factor.factor,baseExponent) );
}
// Parse the conversion string in case the original converts to more than one SI base unit
Unit targetBase = parseUnitString(factor.targetUnit);
std::vector<std::string> targetStrings = targetBase.baseUnits();
std::vector<std::string>::const_iterator targetItr = targetStrings.begin();
std::vector<std::string>::const_iterator targetEnd = targetStrings.end();
while( targetItr != targetEnd ) {
int exp = targetBase.baseUnitExponent( *targetItr );
if( exp != 0 ) {
result.setBaseUnitExponent(*targetItr,result.baseUnitExponent(*targetItr) +
baseExponent*exp);
}
++targetItr;
}//End while( convItr != convEnd )
++it;
}//End while( it != end )
// Set result scale to match original scale
if( scaleExponent != 0 ) {
result.setScale(scaleExponent);
}
// Check if there is a pretty string for the result
std::string pretty = UnitFactory::instance().lookupPrettyString(
result.standardUnitsString(false) );
if( !(pretty.empty()) ) {
result.setPrettyUnitsString( pretty );
}
return result;
}