bool Units::can_convert (const symbol from, const symbol to, const double value) const { if (from == to) return true; // Defined? if (!has_unit(from) || !has_unit (to)) { if (!allow_old ()) return false; else return Oldunits::can_convert (from, to, value); } const Unit& from_unit = get_unit (from); const Unit& to_unit = get_unit (to); if (!compatible (from_unit, to_unit)) return false; if (!from_unit.in_native (value)) return false; const double base = from_unit.to_base (value); // We don't have to worry about [cm] and [hPa] as all values are valid. return to_unit.in_base (base); }
bool Units::can_convert (const symbol from, const symbol to) const { if (from == to) return true; // Defined? if (!has_unit(from) || !has_unit (to)) { if (!allow_old ()) return false; else return Oldunits::can_convert (from, to); } const Unit& from_unit = get_unit (from); const Unit& to_unit = get_unit (to); if (compatible (from_unit, to_unit)) return true; if (!allow_old ()) return false; return Oldunits::can_convert (from, to); }
double Units::convert (const symbol from, const symbol to, const double value) const { if (from == to) return value; // Defined? if (!has_unit(from) || !has_unit (to)) { if (allow_old ()) return Oldunits::convert (from, to, value); if (!has_unit (from)) throw "Cannot convert from unknown dimension [" + from + "] to [" + to + "]"; throw "Cannot convert from [" + from + "] to unknown dimension [" + to + "]"; } return Units::unit_convert (get_unit (from), get_unit (to), value); }
bool Units::can_convert (const symbol from, const symbol to, Treelog& msg) const { if (from == to) return true; // Defined? if (!has_unit(from) || !has_unit (to)) { if (!allow_old ()) { if (has_unit (from)) msg.message ("Original dimension [" + from + "] known."); else msg.message ("Original dimension [" + from + "] not known."); if (has_unit (to)) msg.message ("Target dimension [" + to + "] known."); else msg.message ("Target dimension [" + to + "] not known."); return false; } msg.message (std::string ("Trying old conversion of ") + (has_unit (from) ? "" : "unknown ") + "[" + from + "] to " + (has_unit (to) ? "" : "unknown ") + "[" + to + "]." ); return Oldunits::can_convert (from, to); } const Unit& from_unit = get_unit (from); const Unit& to_unit = get_unit (to); if (compatible (from_unit, to_unit)) return true; // Not compatible. std::ostringstream tmp; tmp << "Cannot convert [" << from << "] with base [" << from_unit.base_name () << "] to [" << to << "] with base [" << to_unit.base_name () << "]"; msg.message (tmp.str ()); if (!allow_old ()) return false; msg.message ("Trying old conversion."); return Oldunits::can_convert (from, to); }
const Convert& Units::get_convertion (const symbol from, const symbol to) const { if (from == to) { static struct ConvertIdentity : public Convert { double operator()(const double value) const { return value; } bool valid (const double) const { return true; } } identity; return identity; } const symbol key (from.name () + " -> " + to.name ()); // Already known. convert_map::const_iterator i = this->conversions.find (key); if (i != this->conversions.end ()) return *(*i).second; // Defined? if (!has_unit (from) || !has_unit (to)) { if (allow_old ()) { struct ConvertOld : Convert { const Oldunits::Convert& old; double operator()(const double value) const { return old (value); } bool valid (const double value) const { return old.valid (value); } ConvertOld (const Oldunits::Convert& o) : old (o) { } }; const Oldunits::Convert& old = Oldunits::get_convertion (from.name (), to.name ()); const Convert* convert = new ConvertOld (old); daisy_assert (convert); conversions[key] = convert; return *convert; } if (!has_unit (from)) throw "Cannot get conversion from unknown dimension [" + from + "] to [" + to + "]"; if (!has_unit (to)) throw "Cannot get conversion from [" + from + "] to unknown dimension [" + to + "]"; } const Unit& from_unit = get_unit (from); const Unit& to_unit = get_unit (to); if (!compatible (from_unit, to_unit)) throw "Cannot get conversion from [" + from + "] to dimension [" + to + "]"; const Convert* convert = create_convertion (from_unit, to_unit); daisy_assert (convert); conversions[key] = convert; return *convert; }
#include "catch.hpp" #include "framework/alias.hpp" #include "framework/config/assembler.hpp" #include "framework/routine/system.hpp" TEST_CASE("Assemble SuFlac and SuAlsa", "[rig::flacalsa]") { siocfg::assembler as; auto sys = siortn::system::setup(as, "SuFlacSuAlsa.cfg", 32); REQUIRE(sys->has_unit("flac") == true); REQUIRE(sys->has_unit("alsa") == true); auto flac = sys->get_unit("flac").lock(); auto alsa = sys->get_unit("alsa").lock(); sys->warmup(); auto profile = sys->global_profile(); REQUIRE(profile.channels == 2); std::this_thread::sleep_for(std::chrono::milliseconds(20)); REQUIRE(flac->get_configuration("num_cached") == "5"); sys->start(); flac->set_configuration("kick_start", ""); auto pr = 0u; std::cin >> pr; }