Added support for prefixes of non-coherent units

This commit is contained in:
Mateusz Pusz
2019-10-18 22:36:57 +02:00
parent 880528e5ca
commit 987ab86439
4 changed files with 25 additions and 9 deletions

View File

@@ -285,7 +285,7 @@ For example to create a prefixed unit the following may be used:
```cpp ```cpp
template<typename Child, Prefix P, Unit U> 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, struct prefixed_derived_unit : downcast_helper<Child, unit<typename U::dimension,
ratio_multiply<typename P::ratio, ratio_multiply<typename P::ratio,
typename U::ratio>>> { typename U::ratio>>> {

View File

@@ -161,7 +161,7 @@ namespace units {
}; };
template<typename Child, Prefix P, Unit U> 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>>> { 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; static constexpr auto symbol = P::symbol + U::symbol;
using prefix_type = P::prefix_type; using prefix_type = P::prefix_type;

View File

@@ -39,14 +39,16 @@ namespace data {
struct mebi : units::prefix<mebi, data_prefix, units::ratio<1'048'576>, "Mi"> {}; 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 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 byte : units::derived_unit<byte, "B", digital_information, units::ratio<8>> {};
struct kilobyte : units::prefixed_derived_unit<kilobyte, kibi, byte> {};
inline namespace literals { inline namespace literals {
constexpr auto operator""_b(unsigned long long l) { return units::quantity<bit, std::int64_t>(l); } 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""_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(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"); REQUIRE(stream.str() == "64 B");
} }
SECTION("prefixed unit") SECTION("prefixed coherent unit")
{ {
stream << 256_Kib; 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") SECTION("other unit matching prefix")
{ {
stream << 8_Kib * 8_Kib / 2_b; stream << 8_Kib * 8_Kib / 2_b;
// REQUIRE(stream.str() == "32 Mib"); REQUIRE(stream.str() == "32 Mib");
} }
} }
} }

View File

@@ -37,16 +37,20 @@ namespace {
struct data_prefix {}; 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 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 byte : units::derived_unit<byte, "B", digital_information, units::ratio<8>> {};
struct kilobyte : units::prefixed_derived_unit<kilobyte, kibi, byte> {};
inline namespace literals { inline namespace literals {
constexpr auto operator""_b(unsigned long long l) { return units::quantity<bit, std::int64_t>(l); } 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(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 { namespace {
static_assert(1_B == 8_b); 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);
} }