Add X macro for unit conversions.

pull/31/head
weetmuts 2019-05-04 11:10:09 +02:00
rodzic 61cb67942c
commit 801c077ffe
3 zmienionych plików z 26 dodań i 23 usunięć

Wyświetl plik

@ -24,6 +24,13 @@
#include<map>
#include<set>
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<string> ids();

Wyświetl plik

@ -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)
{

Wyświetl plik

@ -23,7 +23,7 @@
enum class Unit
{
Unknown, KWH, GJ
Unknown, KWH, GJ, M3, L
};
bool canConvert(Unit from, Unit to);