Use std::optional to return results

This commit is contained in:
Chocobo1 2021-02-09 12:55:22 +08:00
parent fffe5e7003
commit 4a0b36a50b
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
2 changed files with 21 additions and 15 deletions

View file

@ -28,6 +28,8 @@
#include "misc.h" #include "misc.h"
#include <optional>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h> #include <windows.h>
#include <powrprof.h> #include <powrprof.h>
@ -80,21 +82,26 @@ namespace
// see http://en.wikipedia.org/wiki/Kilobyte // see http://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes // value must be given in bytes
// to send numbers instead of strings with suffixes // to send numbers instead of strings with suffixes
bool splitToFriendlyUnit(const qint64 sizeInBytes, qreal &val, Utils::Misc::SizeUnit &unit) struct SplitToFriendlyUnitResult
{ {
if (sizeInBytes < 0) return false; qreal value;
Utils::Misc::SizeUnit unit;
};
std::optional<SplitToFriendlyUnitResult> splitToFriendlyUnit(const qint64 bytes)
{
if (bytes < 0)
return std::nullopt;
int i = 0; int i = 0;
val = static_cast<qreal>(sizeInBytes); auto value = static_cast<qreal>(bytes);
while ((val >= 1024.) && (i < static_cast<int>(Utils::Misc::SizeUnit::ExbiByte))) while ((value >= 1024) && (i < static_cast<int>(Utils::Misc::SizeUnit::ExbiByte)))
{ {
val /= 1024.; value /= 1024;
++i; ++i;
} }
return {{value, static_cast<Utils::Misc::SizeUnit>(i)}};
unit = static_cast<Utils::Misc::SizeUnit>(i);
return true;
} }
} }
@ -249,15 +256,14 @@ QString Utils::Misc::unitString(const SizeUnit unit, const bool isSpeed)
return ret; return ret;
} }
QString Utils::Misc::friendlyUnit(const qint64 bytesValue, const bool isSpeed) QString Utils::Misc::friendlyUnit(const qint64 bytes, const bool isSpeed)
{ {
SizeUnit unit; const std::optional<SplitToFriendlyUnitResult> result = splitToFriendlyUnit(bytes);
qreal friendlyVal; if (!result)
if (!splitToFriendlyUnit(bytesValue, friendlyVal, unit))
return QCoreApplication::translate("misc", "Unknown", "Unknown (size)"); return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
return Utils::String::fromDouble(friendlyVal, friendlyUnitPrecision(unit)) return Utils::String::fromDouble(result->value, friendlyUnitPrecision(result->unit))
+ QString::fromUtf8(C_NON_BREAKING_SPACE) + QString::fromUtf8(C_NON_BREAKING_SPACE)
+ unitString(unit, isSpeed); + unitString(result->unit, isSpeed);
} }
int Utils::Misc::friendlyUnitPrecision(const SizeUnit unit) int Utils::Misc::friendlyUnitPrecision(const SizeUnit unit)

View file

@ -73,7 +73,7 @@ namespace Utils::Misc
// return the best user friendly storage unit (B, KiB, MiB, GiB, TiB) // return the best user friendly storage unit (B, KiB, MiB, GiB, TiB)
// value must be given in bytes // value must be given in bytes
QString friendlyUnit(qint64 bytesValue, bool isSpeed = false); QString friendlyUnit(qint64 bytes, bool isSpeed = false);
int friendlyUnitPrecision(SizeUnit unit); int friendlyUnitPrecision(SizeUnit unit);
qint64 sizeInBytes(qreal size, SizeUnit unit); qint64 sizeInBytes(qreal size, SizeUnit unit);