mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-03 12:24:26 +02:00
Added support for prefixes of non-coherent units
This commit is contained in:
@@ -285,7 +285,7 @@ For example to create a prefixed unit the following may be used:
|
||||
|
||||
```cpp
|
||||
template<typename Child, Prefix P, Unit U>
|
||||
requires requires { U::symbol; } && std::same_as<typename U::prefix_type, typename P::prefix_type>
|
||||
requires requires { U::symbol; }
|
||||
struct prefixed_derived_unit : downcast_helper<Child, unit<typename U::dimension,
|
||||
ratio_multiply<typename P::ratio,
|
||||
typename U::ratio>>> {
|
||||
|
@@ -161,7 +161,7 @@ namespace units {
|
||||
};
|
||||
|
||||
template<typename Child, Prefix P, Unit U>
|
||||
requires requires { U::symbol; } && std::same_as<typename U::prefix_type, typename P::prefix_type>
|
||||
requires requires { U::symbol; }
|
||||
struct prefixed_derived_unit : downcast_helper<Child, unit<typename U::dimension, ratio_multiply<typename P::ratio, typename U::ratio>>> {
|
||||
static constexpr auto symbol = P::symbol + U::symbol;
|
||||
using prefix_type = P::prefix_type;
|
||||
|
@@ -39,14 +39,16 @@ namespace data {
|
||||
struct mebi : units::prefix<mebi, data_prefix, units::ratio<1'048'576>, "Mi"> {};
|
||||
|
||||
struct bit : units::coherent_derived_unit<bit, "b", digital_information, data_prefix> {};
|
||||
struct kilobit : units::prefixed_derived_unit<bit, kibi, bit> {};
|
||||
struct kilobit : units::prefixed_derived_unit<kilobit, kibi, bit> {};
|
||||
struct byte : units::derived_unit<byte, "B", digital_information, units::ratio<8>> {};
|
||||
struct kilobyte : units::prefixed_derived_unit<kilobyte, kibi, byte> {};
|
||||
|
||||
inline namespace literals {
|
||||
|
||||
constexpr auto operator""_b(unsigned long long l) { return units::quantity<bit, std::int64_t>(l); }
|
||||
constexpr auto operator""_Kib(unsigned long long l) { return units::quantity<kilobit, std::int64_t>(l); }
|
||||
constexpr auto operator""_B(unsigned long long l) { return units::quantity<byte, std::int64_t>(l); }
|
||||
constexpr auto operator""_KiB(unsigned long long l) { return units::quantity<kilobyte, std::int64_t>(l); }
|
||||
|
||||
}
|
||||
|
||||
@@ -66,16 +68,22 @@ TEST_CASE("operator<< on a custom quantity", "[text][ostream]")
|
||||
REQUIRE(stream.str() == "64 B");
|
||||
}
|
||||
|
||||
SECTION("prefixed unit")
|
||||
SECTION("prefixed coherent unit")
|
||||
{
|
||||
stream << 256_Kib;
|
||||
// REQUIRE(stream.str() == "256 Kib");
|
||||
REQUIRE(stream.str() == "256 Kib");
|
||||
}
|
||||
|
||||
SECTION("prefixed non-coherent unit")
|
||||
{
|
||||
stream << 1024_KiB;
|
||||
REQUIRE(stream.str() == "1024 KiB");
|
||||
}
|
||||
|
||||
SECTION("other unit matching prefix")
|
||||
{
|
||||
stream << 8_Kib * 8_Kib / 2_b;
|
||||
// REQUIRE(stream.str() == "32 Mib");
|
||||
REQUIRE(stream.str() == "32 Mib");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -37,16 +37,20 @@ namespace {
|
||||
|
||||
struct data_prefix {};
|
||||
|
||||
struct kibi : units::prefix<kibi, data_prefix, units::ratio< 1'024>, "Ki"> {};
|
||||
|
||||
struct bit : units::coherent_derived_unit<bit, "b", digital_information, data_prefix> {};
|
||||
struct kilobit : units::prefixed_derived_unit<kilobit, kibi, bit> {};
|
||||
|
||||
struct byte : units::derived_unit<byte, "B", digital_information, units::ratio<8>> {};
|
||||
struct kilobyte : units::prefixed_derived_unit<kilobyte, kibi, byte> {};
|
||||
|
||||
inline namespace literals {
|
||||
|
||||
constexpr auto operator""_b(unsigned long long l) { return units::quantity<bit, std::int64_t>(l); }
|
||||
constexpr auto operator""_b(long double l) { return units::quantity<bit, long double>(l); }
|
||||
|
||||
constexpr auto operator""_Kib(unsigned long long l) { return units::quantity<kilobit, std::int64_t>(l); }
|
||||
constexpr auto operator""_B(unsigned long long l) { return units::quantity<byte, std::int64_t>(l); }
|
||||
constexpr auto operator""_B(long double l) { return units::quantity<byte, long double>(l); }
|
||||
constexpr auto operator""_KiB(unsigned long long l) { return units::quantity<kilobyte, std::int64_t>(l); }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -54,6 +58,10 @@ namespace {
|
||||
namespace {
|
||||
|
||||
static_assert(1_B == 8_b);
|
||||
static_assert(1024_b == 1_Kib);
|
||||
static_assert(1024_B == 1_KiB);
|
||||
static_assert(8 * 1024_b == 1_KiB);
|
||||
static_assert(8 * 1_Kib == 1_KiB);
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user