Converter* ConverterFactory::GetConverter(const std::string& sourceCharset, const std::string& targetCharset) { XR::CSingleLock lock(m_critSect); if (sourceCharset.empty() || sourceCharset.empty()) { LOGERR("One of the charsets missing!"); return nullptr; } std::string ident = sourceCharset + targetCharset; for (unsigned int i = 0; i < m_vConverters.size(); ++i) { ConverterUnit& converterunit = m_vConverters[i]; if (converterunit.m_ident == ident) { converterunit.m_unloadDelayStartTick = XR::SystemClockMillis(); return converterunit.m_converter; } } //Converter not present, sooo.... Let's create one for us; LOGDEBUG("Converter (%s --> %s) not exist, creating new.", sourceCharset.c_str(), targetCharset.c_str()); Converter* pNewConverter = nullptr; lock.Leave(); if (targetCharset == "UTF-8") pNewConverter = CreateConverter(sourceCharset, targetCharset, 4); else pNewConverter = CreateConverter(sourceCharset, targetCharset); if (nullptr == pNewConverter) LOGFATAL("Cannot create new converter! (not enough of memory?)"); lock.Enter(); return pNewConverter; }
void UnitUtil::CreateConverter(derived_unit& unit, UnitConverter& converter) { set<derived_unit_element> unit_refs = unit.derived_unit_element_kind_children(); for (set<derived_unit_element>::const_iterator ci = unit_refs.begin(); ci != unit_refs.end(); ci++) { DimensionRep tmpRep; double exponent = ci->exponent(); named_unit namedUnit = ci->ref(); if (namedUnit != Udm::null) { Uml::Class namedUnitType = namedUnit.type(); if (namedUnitType == si_unit::meta) { DimensionRep tmp; GetDimensionRep(si_unit::Cast(namedUnit), tmp); converter.dimensions+=tmp; } else { CreateConverter(conversion_based_unit::Cast(namedUnit), converter, exponent); } converter.dimensions.MultiplyByN(exponent); } } }
// New Version Stuff void UnitUtil::CreateConverter(unit& cyphyRef, UnitConverter& converter) { Uml::Class unitType = cyphyRef.type(); if (unitType == si_unit::meta) { GetDimensionRep(si_unit::Cast(cyphyRef), converter.dimensions); } else if (unitType == conversion_based_unit::meta) { CreateConverter(conversion_based_unit::Cast(cyphyRef), converter); } else if (unitType == derived_unit::meta) { CreateConverter(derived_unit::Cast(cyphyRef), converter); } }
void UnitUtil::CreateConverter(conversion_based_unit& cunit, UnitConverter& converter, double exponent) { reference_unit refUnit = cunit.reference_unit_child(); if (refUnit) { double conversionFactor = refUnit.conversion_factor(), conversionOffset = refUnit.conversion_offset(); unit namedUnit = refUnit.ref(); if (namedUnit.type() == conversion_based_unit::meta) { //newValue = ConvertValue_To_SIEquivalent(newValue, conversionFactor, conversionOffset, exponent); //newValue = ConvertToSIEquivalent(conversion_based_unit::Cast(namedUnit), unitRep, newValue); converter.conversionVector.push_back(ConversionInfo(exponent, conversionOffset, conversionFactor)); CreateConverter(conversion_based_unit::Cast(namedUnit), converter); } else if (namedUnit.type() == si_unit::meta) { //GetDimensionRep(si_unit::Cast(namedUnit), unitRep); //ConvertValue_To_SIEquivalent(value, conversionFactor, conversionOffset, exponent); DimensionRep tmp; GetDimensionRep(si_unit::Cast(namedUnit), tmp); converter.dimensions+=tmp; converter.conversionVector.push_back(ConversionInfo(exponent, conversionOffset, conversionFactor)); } else // DY 12/9/11: derived unit { //newValue = ConvertToSIEquivalent(derived_unit::Cast(namedUnit), value, unitRep); //newValue = ConvertValue_To_SIEquivalent(newValue, conversionFactor, conversionOffset, 1); CreateConverter(derived_unit::Cast(namedUnit), converter); converter.conversionVector.push_back(ConversionInfo(exponent, conversionOffset, conversionFactor)); } } }
double UnitUtil::ConvertFromSIEquivalent_WLookup(unit& cyphyRef, double value) { if (cyphyRef != Udm::null) { map<CyPhyML::unit, UnitConverter>::iterator i = ConverterMap.find(cyphyRef); if (i != ConverterMap.end()) { return i->second.FromSI(value); } else { // create UnitConverter // add UnitConverter to map UnitConverter converter; CreateConverter(cyphyRef, converter); ConverterMap[cyphyRef] = converter; return converter.FromSI(value); } } else return value; }
double UnitUtil::ConvertToSIEquivalent_WLookup(unit& cyphyRef, double value, DimensionRep& siRep) { if (cyphyRef != Udm::null) { map<CyPhyML::unit, UnitConverter>::iterator i = ConverterMap.find(cyphyRef); if (i != ConverterMap.end()) { siRep = i->second.dimensions; return i->second.ToSI(value); } else { // create UnitConverter // add UnitConverter to map UnitConverter converter; CreateConverter(cyphyRef, converter); ConverterMap[cyphyRef] = converter; siRep = converter.dimensions; return converter.ToSI(value); } } else return value; }
/*---------------------------------------------------------------- CConvFactory::CConvFactory コンストラクタです。 ----------------------------------------------------------------*/ CConvFactory::CConvFactory(CharSetMode nCharSet){ CreateConverter(nCharSet); }
/*---------------------------------------------------------------- CConvFactory::CConvFactory コンストラクタです。 ----------------------------------------------------------------*/ CConvFactory::CConvFactory(){ CreateConverter(CSM_PLATFORM); }