From e37340f6c297b1e2a20b24e3de00db0acdc07447 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Fri, 3 May 2024 21:54:11 -0400 Subject: [PATCH] xo-unit: use auto to elide a template arg to quantity --- include/xo/unit/quantity.hpp | 105 +++++++++++++------------- include/xo/unit/quantity_iostream.hpp | 6 +- utest/quantity.test.cpp | 4 + 3 files changed, 61 insertions(+), 54 deletions(-) diff --git a/include/xo/unit/quantity.hpp b/include/xo/unit/quantity.hpp index fe74c809..c266a9d4 100644 --- a/include/xo/unit/quantity.hpp +++ b/include/xo/unit/quantity.hpp @@ -19,14 +19,13 @@ namespace xo { **/ template < typename Repr = double, - typename Int = std::int64_t, - natural_unit NaturalUnit = natural_unit(), - typename Int2x = detail::width2x_t > + auto /*natural_unit*/ NaturalUnit = natural_unit(), + typename Int2x = detail::width2x_t > class quantity { public: using repr_type = Repr; - using unit_type = natural_unit; - using ratio_int_type = Int; + using unit_type = decltype(NaturalUnit); + using ratio_int_type = decltype(NaturalUnit)::ratio_int_type; using ratio_int2x_type = Int2x; public: @@ -47,7 +46,9 @@ namespace xo { template constexpr auto with_repr() const { - return quantity(scale_); + return quantity(scale_); } /* parallel implementation to Quantity::rescale(), @@ -55,7 +56,7 @@ namespace xo { * * NOTE: constexpr as long as no fractional units involved. */ - template NaturalUnit2> + template NaturalUnit2> constexpr auto rescale() const { /* conversion factor from .unit -> unit2*/ @@ -68,13 +69,13 @@ namespace xo { : ::sqrt(rr.outer_scale_sq_)) * rr.outer_scale_factor_.template convert_to() * this->scale_); - return quantity(r_scale); + return quantity(r_scale); } else { - return quantity(std::numeric_limits::quiet_NaN()); + return quantity(std::numeric_limits::quiet_NaN()); } } - template ScaledUnit2> + template ScaledUnit2> constexpr auto rescale_ext() const { /* conversion factor from .unit -> unit2*/ @@ -92,9 +93,9 @@ namespace xo { * rr.outer_scale_factor_.template convert_to() * this->scale_ / ScaledUnit2.outer_scale_factor_.template convert_to()); - return quantity(r_scale); + return quantity(r_scale); } else { - return quantity(std::numeric_limits::quiet_NaN()); + return quantity(std::numeric_limits::quiet_NaN()); } } @@ -152,14 +153,14 @@ namespace xo { } public: /* need public members so that a quantity instance can be a non-type template parameter (is a structural type) */ - static constexpr natural_unit s_unit = NaturalUnit; + static constexpr natural_unit s_unit = NaturalUnit; Repr scale_ = Repr{}; }; template NaturalUnit = natural_unit()> - using stdquantity = quantity; + using stdquantity = quantity; template constexpr auto @@ -195,7 +196,6 @@ namespace xo { * static_cast(y.scale())); return quantity(r_scale); @@ -224,7 +224,6 @@ namespace xo { / static_cast(y.scale())); return quantity(r_scale); @@ -281,63 +280,63 @@ namespace xo { namespace qty { // ----- mass ----- - inline constexpr auto picograms(double x) { return quantity(x); } - inline constexpr auto nanograms(double x) { return quantity(x); } - inline constexpr auto micrograms(double x) { return quantity(x); } - inline constexpr auto milligrams(double x) { return quantity(x); } - inline constexpr auto grams(double x) { return quantity(x); } - inline constexpr auto kilograms(double x) { return quantity(x); } - inline constexpr auto tonnes(double x) { return quantity(x); } - inline constexpr auto kilotonnes(double x) { return quantity(x); } - inline constexpr auto megatonnes(double x) { return quantity(x); } - inline constexpr auto gigatonnes(double x) { return quantity(x); } + inline constexpr auto picograms(double x) { return quantity(x); } + inline constexpr auto nanograms(double x) { return quantity(x); } + inline constexpr auto micrograms(double x) { return quantity(x); } + inline constexpr auto milligrams(double x) { return quantity(x); } + inline constexpr auto grams(double x) { return quantity(x); } + inline constexpr auto kilograms(double x) { return quantity(x); } + inline constexpr auto tonnes(double x) { return quantity(x); } + inline constexpr auto kilotonnes(double x) { return quantity(x); } + inline constexpr auto megatonnes(double x) { return quantity(x); } + inline constexpr auto gigatonnes(double x) { return quantity(x); } // ----- distance ----- - inline constexpr auto picometers(double x) { return quantity(x); } - inline constexpr auto nanometers(double x) { return quantity(x); } - inline constexpr auto micrometers(double x) { return quantity(x); } - inline constexpr auto millimeters(double x) { return quantity(x); } - inline constexpr auto meters(double x) { return quantity(x); } - inline constexpr auto kilometers(double x) { return quantity(x); } - inline constexpr auto megameters(double x) { return quantity(x); } - inline constexpr auto gigameters(double x) { return quantity(x); } + inline constexpr auto picometers(double x) { return quantity(x); } + inline constexpr auto nanometers(double x) { return quantity(x); } + inline constexpr auto micrometers(double x) { return quantity(x); } + inline constexpr auto millimeters(double x) { return quantity(x); } + inline constexpr auto meters(double x) { return quantity(x); } + inline constexpr auto kilometers(double x) { return quantity(x); } + inline constexpr auto megameters(double x) { return quantity(x); } + inline constexpr auto gigameters(double x) { return quantity(x); } - inline constexpr auto lightseconds(double x) { return quantity(x); } - inline constexpr auto astronomicalunits(double x) { return quantity(x); } + inline constexpr auto lightseconds(double x) { return quantity(x); } + inline constexpr auto astronomicalunits(double x) { return quantity(x); } static constexpr auto meter = meters(1); // ----- time ----- - inline constexpr auto picoseconds(double x) { return quantity(x); } - inline constexpr auto nanoseconds(double x) { return quantity(x); } - inline constexpr auto microseconds(double x) { return quantity(x); } - inline constexpr auto milliseconds(double x) { return quantity(x); } + inline constexpr auto picoseconds(double x) { return quantity(x); } + inline constexpr auto nanoseconds(double x) { return quantity(x); } + inline constexpr auto microseconds(double x) { return quantity(x); } + inline constexpr auto milliseconds(double x) { return quantity(x); } template - inline constexpr auto seconds(Repr x) { return quantity(x); } + inline constexpr auto seconds(Repr x) { return quantity(x); } template - inline constexpr auto minutes(Repr x) { return quantity(x); } + inline constexpr auto minutes(Repr x) { return quantity(x); } - inline constexpr auto hours(double x) { return quantity(x); } - inline constexpr auto days(double x) { return quantity(x); } - inline constexpr auto weeks(double x) { return quantity(x); } - inline constexpr auto months(double x) { return quantity(x); } - inline constexpr auto years(double x) { return quantity(x); } - inline constexpr auto year250s(double x) { return quantity(x); } - inline constexpr auto year360s(double x) { return quantity(x); } - inline constexpr auto year365s(double x) { return quantity(x); } - //inline constexpr auto year366s(double x) { return quantity(x); } + inline constexpr auto hours(double x) { return quantity(x); } + inline constexpr auto days(double x) { return quantity(x); } + inline constexpr auto weeks(double x) { return quantity(x); } + inline constexpr auto months(double x) { return quantity(x); } + inline constexpr auto years(double x) { return quantity(x); } + inline constexpr auto year250s(double x) { return quantity(x); } + inline constexpr auto year360s(double x) { return quantity(x); } + inline constexpr auto year365s(double x) { return quantity(x); } + //inline constexpr auto year366s(double x) { return quantity(x); } static constexpr auto second = seconds(1); // ----- volatility ----- /* volatility in units of 1/yr */ - inline constexpr auto volatility_250d(double x) { return quantity(x); } - inline constexpr auto volatility_360d(double x) { return quantity(x); } + inline constexpr auto volatility_250d(double x) { return quantity(x); } + inline constexpr auto volatility_360d(double x) { return quantity(x); } } /* reminder: see [quantity_ops.hpp] for operator* etc */ diff --git a/include/xo/unit/quantity_iostream.hpp b/include/xo/unit/quantity_iostream.hpp index 29302816..d2ddd7f9 100644 --- a/include/xo/unit/quantity_iostream.hpp +++ b/include/xo/unit/quantity_iostream.hpp @@ -18,7 +18,11 @@ namespace xo { > inline std::ostream & operator<< (std::ostream & os, - const quantity & x) + const quantity & x) { os << x.scale() << x.abbrev(); return os; diff --git a/utest/quantity.test.cpp b/utest/quantity.test.cpp index 2dd8f3f7..6d327eaa 100644 --- a/utest/quantity.test.cpp +++ b/utest/quantity.test.cpp @@ -388,7 +388,9 @@ namespace xo { static_assert(rr.natural_unit_.n_bpu() == 1); constexpr auto q1 = quantity(ms.scale() * ms.scale()); @@ -441,7 +443,9 @@ namespace xo { static_assert(rr.natural_unit_.n_bpu() == 0); constexpr auto q1 = quantity(ms.scale() * ms.scale());