From eeee5a2a3675f7bde64f19d1abab887b0ff928e5 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 29 Apr 2024 23:33:17 -0500 Subject: [PATCH 1/9] xo-unit: builds + runs on darwin --- docs/CMakeLists.txt | 2 +- include/xo/unit/quantity2.hpp | 27 +++++++++++++++++++++++---- utest/scaled_unit.test.cpp | 12 ++++++------ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 403cc2af..43df570c 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -7,4 +7,4 @@ xo_docdir_sphinx_config( quantity-reference.rst quantity-class.rst quantity-factoryfunctions.rst quantity-unitvars.rst unit-reference.rst unit-concept.rst unit-quantities.rst ) -xo_utest_coverage_config2() +#xo_utest_coverage_config2() diff --git a/include/xo/unit/quantity2.hpp b/include/xo/unit/quantity2.hpp index efd2b75f..8da5a914 100644 --- a/include/xo/unit/quantity2.hpp +++ b/include/xo/unit/quantity2.hpp @@ -1,4 +1,4 @@ -/** @file quantity2.hpp +/** @file quantity.hpp * * Author: Roland Conybeare **/ @@ -14,15 +14,34 @@ namespace xo { * * Constexpr implementation, can compute units at compile time **/ - template - class quantity2 { + template NaturalUnit> + class quantity { public: using repr_type = Repr; + using unit_type = natural_unit; + + public: + constexpr quantity(Repr scale) + : scale_{scale} {} + + constexpr const repr_type & scale() const { return scale_; } + constexpr unit_type unit() const { return NaturalUnit; } + + constexpr bool is_dimensionless() const { return s_unit.is_dimensionless(); } + + constexpr quantity unit_qty() { return quantity(1); } private: + /** @brief unit (established at compile time) for this quantity **/ + static NaturalUnit s_unit = NaturalUnit; + + /** @brief quantity represents this multiple of unit amount **/ + Repr scale_ = Repr(); }; } /*namespace qty*/ } /*namespace xo*/ -/** end quantity2.hpp **/ +/** end quantity.hpp **/ diff --git a/utest/scaled_unit.test.cpp b/utest/scaled_unit.test.cpp index d9b1d3d6..74c29918 100644 --- a/utest/scaled_unit.test.cpp +++ b/utest/scaled_unit.test.cpp @@ -20,14 +20,14 @@ namespace xo { constexpr su64_type su_reciprocal = su.reciprocal(); TEST_CASE("scaled_unit", "[scaled_unit]") { - static_assert(su_reciprocal.natural_unit_ == nu::gram.reciprocal()); - REQUIRE(su_reciprocal.natural_unit_ == nu::gram.reciprocal()); + static_assert(su_reciprocal.natural_unit_ == nu::gram.reciprocal()); + REQUIRE(su_reciprocal.natural_unit_ == nu::gram.reciprocal()); - static_assert(su_reciprocal.outer_scale_factor_ == 1); - REQUIRE(su_reciprocal.outer_scale_factor_ == 1); + static_assert(su_reciprocal.outer_scale_factor_ == 1); + REQUIRE(su_reciprocal.outer_scale_factor_ == 1); - static_assert(su_reciprocal.outer_scale_sq_ == 1.0); - REQUIRE(su_reciprocal.outer_scale_sq_ == 1.0); + static_assert(su_reciprocal.outer_scale_sq_ == 1.0); + REQUIRE(su_reciprocal.outer_scale_sq_ == 1.0); } /*TEST_CASE(scaled_unit)*/ TEST_CASE("su_product", "[scaled_unit]") { From e57ee85a7d02a2b4f7ff77839076f263c0e0edda Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 30 Apr 2024 08:40:08 -0500 Subject: [PATCH 2/9] xo-unit: rename files to avoid collisions on osx filesystem --- include/xo/unit/{quantity2.hpp => quantity2_old.hpp} | 0 include/xo/unit/{quantity.hpp => quantity_.hpp} | 0 utest/{Quantity.test.cpp => Quantity2.test.cpp} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename include/xo/unit/{quantity2.hpp => quantity2_old.hpp} (100%) rename include/xo/unit/{quantity.hpp => quantity_.hpp} (100%) rename utest/{Quantity.test.cpp => Quantity2.test.cpp} (100%) diff --git a/include/xo/unit/quantity2.hpp b/include/xo/unit/quantity2_old.hpp similarity index 100% rename from include/xo/unit/quantity2.hpp rename to include/xo/unit/quantity2_old.hpp diff --git a/include/xo/unit/quantity.hpp b/include/xo/unit/quantity_.hpp similarity index 100% rename from include/xo/unit/quantity.hpp rename to include/xo/unit/quantity_.hpp diff --git a/utest/Quantity.test.cpp b/utest/Quantity2.test.cpp similarity index 100% rename from utest/Quantity.test.cpp rename to utest/Quantity2.test.cpp From f92371cd9b1b32cc6412bfb36a49ddc22fcbf4e3 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 30 Apr 2024 09:34:05 -0500 Subject: [PATCH 3/9] xo-unit: rename to avoid conflicts on case-insensitive fs --- include/xo/unit/{Quantity.hpp => Quantity2.hpp} | 0 .../xo/unit/{Quantity_iostream.hpp => Quantity2_iostream.hpp} | 0 include/xo/unit/{quantity_.hpp => quantity.hpp} | 0 utest/CMakeLists.txt | 2 +- utest/Quantity2.test.cpp | 4 ++-- 5 files changed, 3 insertions(+), 3 deletions(-) rename include/xo/unit/{Quantity.hpp => Quantity2.hpp} (100%) rename include/xo/unit/{Quantity_iostream.hpp => Quantity2_iostream.hpp} (100%) rename include/xo/unit/{quantity_.hpp => quantity.hpp} (100%) diff --git a/include/xo/unit/Quantity.hpp b/include/xo/unit/Quantity2.hpp similarity index 100% rename from include/xo/unit/Quantity.hpp rename to include/xo/unit/Quantity2.hpp diff --git a/include/xo/unit/Quantity_iostream.hpp b/include/xo/unit/Quantity2_iostream.hpp similarity index 100% rename from include/xo/unit/Quantity_iostream.hpp rename to include/xo/unit/Quantity2_iostream.hpp diff --git a/include/xo/unit/quantity_.hpp b/include/xo/unit/quantity.hpp similarity index 100% rename from include/xo/unit/quantity_.hpp rename to include/xo/unit/quantity.hpp diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt index 09cfd8ff..9584b632 100644 --- a/utest/CMakeLists.txt +++ b/utest/CMakeLists.txt @@ -3,7 +3,7 @@ set(SELF_EXE utest.unit) set(SELF_SRCS unit_utest_main.cpp #mpl_unit.test.cpp - Quantity.test.cpp + Quantity2.test.cpp quantity.test.cpp bpu.test.cpp basis_unit.test.cpp diff --git a/utest/Quantity2.test.cpp b/utest/Quantity2.test.cpp index 62a5b3d1..66647ef2 100644 --- a/utest/Quantity2.test.cpp +++ b/utest/Quantity2.test.cpp @@ -1,7 +1,7 @@ /* @file Quantity.test.cpp */ -#include "Quantity.hpp" -#include "Quantity_iostream.hpp" +#include "Quantity2.hpp" +#include "Quantity2_iostream.hpp" #include "xo/randomgen/random_seed.hpp" #include "xo/randomgen/xoshiro256.hpp" #include "xo/indentlog/scope.hpp" From 9b8702e26d3bffd2e09ceeed19854c745034642b Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 30 Apr 2024 09:37:21 -0500 Subject: [PATCH 4/9] xo-unit: rename: Quantity2 -> xquantity filenames only for now --- include/xo/unit/{Quantity2.hpp => xquantity.hpp} | 0 .../unit/{Quantity2_iostream.hpp => xquantity_iostream.hpp} | 0 utest/CMakeLists.txt | 2 +- utest/{Quantity2.test.cpp => xquantity.test.cpp} | 4 ++-- 4 files changed, 3 insertions(+), 3 deletions(-) rename include/xo/unit/{Quantity2.hpp => xquantity.hpp} (100%) rename include/xo/unit/{Quantity2_iostream.hpp => xquantity_iostream.hpp} (100%) rename utest/{Quantity2.test.cpp => xquantity.test.cpp} (99%) diff --git a/include/xo/unit/Quantity2.hpp b/include/xo/unit/xquantity.hpp similarity index 100% rename from include/xo/unit/Quantity2.hpp rename to include/xo/unit/xquantity.hpp diff --git a/include/xo/unit/Quantity2_iostream.hpp b/include/xo/unit/xquantity_iostream.hpp similarity index 100% rename from include/xo/unit/Quantity2_iostream.hpp rename to include/xo/unit/xquantity_iostream.hpp diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt index 9584b632..5a063bc0 100644 --- a/utest/CMakeLists.txt +++ b/utest/CMakeLists.txt @@ -3,7 +3,7 @@ set(SELF_EXE utest.unit) set(SELF_SRCS unit_utest_main.cpp #mpl_unit.test.cpp - Quantity2.test.cpp + xquantity.test.cpp quantity.test.cpp bpu.test.cpp basis_unit.test.cpp diff --git a/utest/Quantity2.test.cpp b/utest/xquantity.test.cpp similarity index 99% rename from utest/Quantity2.test.cpp rename to utest/xquantity.test.cpp index 66647ef2..319edcd6 100644 --- a/utest/Quantity2.test.cpp +++ b/utest/xquantity.test.cpp @@ -1,7 +1,7 @@ /* @file Quantity.test.cpp */ -#include "Quantity2.hpp" -#include "Quantity2_iostream.hpp" +#include "xquantity.hpp" +#include "xquantity_iostream.hpp" #include "xo/randomgen/random_seed.hpp" #include "xo/randomgen/xoshiro256.hpp" #include "xo/indentlog/scope.hpp" From bb90a37460216f42e5c7480af5191ab138ba2efe Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 1 May 2024 10:25:10 -0500 Subject: [PATCH 5/9] xo-unit: streamline xquantity printing, match quantity printing --- include/xo/unit/xquantity_iostream.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/xo/unit/xquantity_iostream.hpp b/include/xo/unit/xquantity_iostream.hpp index 402c77d8..0eb6ce95 100644 --- a/include/xo/unit/xquantity_iostream.hpp +++ b/include/xo/unit/xquantity_iostream.hpp @@ -17,10 +17,14 @@ namespace xo { operator<< (std::ostream & os, const Quantity & x) { + os << x.scale() << x.abbrev(); + +#ifdef NOT_USING os << ""; +#endif return os; } From a91c90fad4088db26a218e1f28a070875661d5ad Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 1 May 2024 10:25:44 -0500 Subject: [PATCH 6/9] xo-unit: operator+ overloads for (xquantity, double) --- include/xo/unit/xquantity.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/xo/unit/xquantity.hpp b/include/xo/unit/xquantity.hpp index 84807aa4..92e31a89 100644 --- a/include/xo/unit/xquantity.hpp +++ b/include/xo/unit/xquantity.hpp @@ -311,6 +311,26 @@ namespace xo { return Quantity::add(x, y); } + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) + **/ + template + requires quantity2_concept + constexpr auto + operator+ (const Quantity & x, double y) + { + return x + Quantity(y, nu::dimensionless); + } + + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) + **/ + template + requires quantity2_concept + constexpr auto + operator+ (double x, const Quantity & y) + { + return Quantity(x, nu::dimensionless) + y; + } + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) **/ template From 9438456e94078d3af521b88a46fc2fa6824f6ce7 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 1 May 2024 10:32:43 -0500 Subject: [PATCH 7/9] xo-unit: + (xquantity, double) overloads for operator- --- include/xo/unit/xquantity.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/xo/unit/xquantity.hpp b/include/xo/unit/xquantity.hpp index 92e31a89..3aec78c2 100644 --- a/include/xo/unit/xquantity.hpp +++ b/include/xo/unit/xquantity.hpp @@ -341,6 +341,26 @@ namespace xo { return Quantity::subtract(x, y); } + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) + **/ + template + requires quantity2_concept + constexpr auto + operator- (const Quantity & x, double y) + { + return x - Quantity(y, nu::dimensionless); + } + + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) + **/ + template + requires quantity2_concept + constexpr auto + operator- (double x, const Quantity & y) + { + return Quantity(x, nu::dimensionless) - y; + } + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) **/ template From 022bef09d0df18c1e54edae6e8c7ca23745c1304 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 1 May 2024 10:42:35 -0500 Subject: [PATCH 8/9] xquantity: + (xquantity, double) overloads for operator== --- include/xo/unit/xquantity.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/xo/unit/xquantity.hpp b/include/xo/unit/xquantity.hpp index 3aec78c2..c03de0ec 100644 --- a/include/xo/unit/xquantity.hpp +++ b/include/xo/unit/xquantity.hpp @@ -371,6 +371,26 @@ namespace xo { return (Quantity::compare(x, y) == 0); } + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) + **/ + template + requires quantity2_concept + constexpr auto + operator== (const Quantity & x, double y) + { + return (x == Quantity(y, nu::dimensionless)); + } + + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) + **/ + template + requires quantity2_concept + constexpr auto + operator== (double x, const Quantity & y) + { + return (Quantity(x, nu::dimensionless) == y); + } + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) **/ template From 6a949d16c53bf83a825c340883842b54b2fb61ee Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 1 May 2024 14:30:51 -0400 Subject: [PATCH 9/9] xo-unit: operator<=> for (xquantity, double) pairs --- include/xo/unit/xquantity.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/xo/unit/xquantity.hpp b/include/xo/unit/xquantity.hpp index c03de0ec..ca2bb2e2 100644 --- a/include/xo/unit/xquantity.hpp +++ b/include/xo/unit/xquantity.hpp @@ -401,6 +401,26 @@ namespace xo { return Quantity::compare(x, y); } + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) + **/ + template + requires quantity2_concept + constexpr auto + operator<=> (const Quantity & x, double y) + { + return Quantity::compare(x, Quantity(y, nu::dimensionless)); + } + + /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) + **/ + template + requires quantity2_concept + constexpr auto + operator<=> (double x, const Quantity & y) + { + return Quantity::compare(Quantity(x, nu::dimensionless), y); + } + namespace unit { constexpr auto nanogram = natural_unit_qty(nu::nanogram); }