From 93a68c3d5f140805d993e06c019b46da260b4588 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 13 May 2019 13:32:14 +0200 Subject: [PATCH] [libcalamares] Add convenience method to check for unit-comparability - Not all kinds of units are comparable. Introduce a method in PartitionSize to check for comparability (this could also be a free method, but seems more tidy here because it is specifically about comparing in the context of partition sizes). --- src/libcalamares/partition/PartitionSize.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libcalamares/partition/PartitionSize.h b/src/libcalamares/partition/PartitionSize.h index 13ffa5c70..975ebd887 100644 --- a/src/libcalamares/partition/PartitionSize.h +++ b/src/libcalamares/partition/PartitionSize.h @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2019, Collabora Ltd + * Copyright 2019, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -96,6 +97,20 @@ public: * @return the size in bytes, or -1 if it cannot be calculated. */ qint64 toBytes() const; + + /** @brief Are the units comparable? + * + * None units cannot be compared with anything. Percentages can + * be compared with each other, and all the explicit sizes (KiB, ...) + * can be compared with each other. + */ + static constexpr bool unitsComparable( const SizeUnit u1, const SizeUnit u2 ) + { + return !( ( u1 == SizeUnit::None || u2 == SizeUnit::None ) || + ( u1 == SizeUnit::Percent && u2 != SizeUnit::Percent ) || + ( u1 != SizeUnit::Percent && u2 == SizeUnit::Percent ) ); + } + }; } // namespace Calamares