From 92477b7e5b1017d532cf1a30e3293681f2e8ce80 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sat, 27 Apr 2024 11:52:07 -0400 Subject: [PATCH] xo-ratio: refactor: rename ratio.to<> -> ratio.convert_to<> --- include/xo/ratio/ratio.hpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/include/xo/ratio/ratio.hpp b/include/xo/ratio/ratio.hpp index 1acbb74..2c4b0c5 100644 --- a/include/xo/ratio/ratio.hpp +++ b/include/xo/ratio/ratio.hpp @@ -24,12 +24,17 @@ namespace xo { /** @brief promote value to ratio type **/ template > struct promoter_type; + + /** @brief convert value to different numeric (or ratio) representation **/ + template > + struct converter_type; } /** @class ratio * @brief represent a ratio of two Int values. **/ - template requires std::totally_ordered + template + requires std::totally_ordered struct ratio { public: @@ -255,7 +260,9 @@ namespace xo { * For example: to int or double **/ template - constexpr Repr to() const noexcept { return num_ / static_cast(den_); } + constexpr Repr convert_to() const noexcept { + return detail::converter_type::convert(*this); + } /** @brief convert to short human-friendly flatstring representation * @@ -361,6 +368,23 @@ namespace xo { }; } + namespace detail { + template + struct converter_type; + + template + struct converter_type { + /* to convert to a ratio, can just use built-in conversion */ + static constexpr ToType convert(Ratio x) { return ToType(x.num(), x.den()); } + }; + + template + struct converter_type { + /* to convert to non-ratio, do division */ + static constexpr ToType convert(Ratio x) { return x.num() / static_cast(x.den()); } + }; + } + /** @brief create a ratio in lowest terms from two integers **/ template constexpr auto