diff --git a/src/meters_common_implementation.h b/src/meters_common_implementation.h index d38c04b..de2df79 100644 --- a/src/meters_common_implementation.h +++ b/src/meters_common_implementation.h @@ -24,6 +24,13 @@ #include #include +struct Print +{ + string vname; // Value name, like: total current previous + Unit vunit; // Value unit, like: Unit::KWH Unit::GJ Unit::M3 Unit::L + +}; + struct MeterCommonImplementation : public virtual Meter { vector ids(); diff --git a/src/units.cc b/src/units.cc index 3eb9d2e..ec57fb2 100644 --- a/src/units.cc +++ b/src/units.cc @@ -20,38 +20,34 @@ using namespace std; -double from_KWH_to_GJ(double d); -double from_GJ_to_KWH(double d); +#define LIST_OF_CONVERSIONS \ + X(KWH,GJ, {vto=vfrom*0.0036;}) \ + X(GJ,KWH, {vto=vfrom/0.0036;}) \ + X(M3,L, {vto=vfrom*1000.0;}) \ + X(L,M3, {vto=vfrom/1000.0;}) -bool canConvert(Unit from, Unit to) +bool canConvert(Unit ufrom, Unit uto) { - if (from == to) return true; - if (from == Unit::KWH && to == Unit::GJ) return true; - if (from == Unit::GJ && to == Unit::KWH) return true; + if (ufrom == uto) return true; +#define X(from,to,code) if (Unit::from == ufrom && Unit::to == uto) return true; +LIST_OF_CONVERSIONS +#undef X return false; } -double convert(double v, Unit from, Unit to) +double convert(double vfrom, Unit ufrom, Unit uto) { - if (from == Unit::KWH && to == Unit::GJ) { - return from_KWH_to_GJ(v); - } - if (from == Unit::GJ && to == Unit::KWH) { - return from_GJ_to_KWH(v); - } + double vto = -4711.0; + if (ufrom == uto) { { vto = vfrom; } return vto; } + +#define X(from,to,code) if (Unit::from == ufrom && Unit::to == uto) { code return vto; } +LIST_OF_CONVERSIONS +#undef X + error("Cannot convert between units!\n"); return 0; } -double from_KWH_to_GJ(double kwh) -{ - return kwh * 0.0036; -} - -double from_GJ_to_KWH(double gj) -{ - return gj / 0.0036; -} Unit toConversionUnit(string s) { diff --git a/src/units.h b/src/units.h index d7107db..d45ad8d 100644 --- a/src/units.h +++ b/src/units.h @@ -23,7 +23,7 @@ enum class Unit { - Unknown, KWH, GJ + Unknown, KWH, GJ, M3, L }; bool canConvert(Unit from, Unit to);