From 2f911bc10995085baf7993612748c901a5d26107 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 1 Apr 2024 18:52:36 -0400 Subject: [PATCH] xo-unit: adopt dim-checking unit impl from xo-observable --- include/xo/unit/dim_util.hpp | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 include/xo/unit/dim_util.hpp diff --git a/include/xo/unit/dim_util.hpp b/include/xo/unit/dim_util.hpp new file mode 100644 index 00000000..222f9c9e --- /dev/null +++ b/include/xo/unit/dim_util.hpp @@ -0,0 +1,59 @@ +/* @file dim_util.hpp */ + +#pragma once + +#include "stringliteral.hpp" + +namespace xo { + namespace obs { + enum class dim { + /** weight. native unit = 1 gram **/ + mass, + /** distance. native unit = 1 meter **/ + distance, + /** time. native unit = 1 second **/ + time, + /** a currency amount. native unit depends on actual currency. + * For USD: one US dollar. + * + * NOTE: unit system isn't suitable for multicurrency work: + * (1usd + 1eur) is well-defined, but (1sec + 1m) is not. + **/ + currency, + /** a screen price. dimensionless **/ + price, + }; + + enum class native_unit_id { + gram, + meter, + second, + currency, + price + }; + + template + struct native_unit_for; + + template <> + struct native_unit_for { static constexpr auto value = native_unit_id::gram; }; + + template <> + struct native_unit_for { static constexpr auto value = native_unit_id::meter; }; + + template <> + struct native_unit_for { static constexpr auto value = native_unit_id::second; }; + + template <> + struct native_unit_for { static constexpr auto value = native_unit_id::currency; }; + + template <> + struct native_unit_for { static constexpr auto value = native_unit_id::price; }; + + template + constexpr auto native_unit_for_v = native_unit_for::value; + } /*namespace obs*/ +} /*namespace xo*/ + + +/* end dim_util.hpp */