Browse Source

Use std::optional to return results

adaptive-webui-19844
Chocobo1 4 years ago
parent
commit
4a0b36a50b
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 34
      src/base/utils/misc.cpp
  2. 2
      src/base/utils/misc.h

34
src/base/utils/misc.cpp

@ -28,6 +28,8 @@ @@ -28,6 +28,8 @@
#include "misc.h"
#include <optional>
#ifdef Q_OS_WIN
#include <windows.h>
#include <powrprof.h>
@ -80,21 +82,26 @@ namespace @@ -80,21 +82,26 @@ namespace
// see http://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes
// 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;
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;
}
unit = static_cast<Utils::Misc::SizeUnit>(i);
return true;
return {{value, static_cast<Utils::Misc::SizeUnit>(i)}};
}
}
@ -249,15 +256,14 @@ QString Utils::Misc::unitString(const SizeUnit unit, const bool isSpeed) @@ -249,15 +256,14 @@ QString Utils::Misc::unitString(const SizeUnit unit, const bool isSpeed)
return ret;
}
QString Utils::Misc::friendlyUnit(const qint64 bytesValue, const bool isSpeed)
QString Utils::Misc::friendlyUnit(const qint64 bytes, const bool isSpeed)
{
SizeUnit unit;
qreal friendlyVal;
if (!splitToFriendlyUnit(bytesValue, friendlyVal, unit))
const std::optional<SplitToFriendlyUnitResult> result = splitToFriendlyUnit(bytes);
if (!result)
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)
+ unitString(unit, isSpeed);
+ unitString(result->unit, isSpeed);
}
int Utils::Misc::friendlyUnitPrecision(const SizeUnit unit)

2
src/base/utils/misc.h

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

Loading…
Cancel
Save