kpmcore/src/util/capacity.h

82 lines
2.2 KiB
C
Raw Normal View History

/*
SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
SPDX-FileCopyrightText: 2012-2019 Andrius Štikonas <andrius@stikonas.eu>
SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
SPDX-License-Identifier: GPL-3.0-or-later
*/
2018-04-11 22:47:40 +01:00
#ifndef KPMCORE_CAPACITY_H
#define KPMCORE_CAPACITY_H
2018-04-11 22:47:40 +01:00
#include "util/libpartitionmanagerexport.h"
class Partition;
class Device;
2016-04-18 17:14:31 +01:00
#include <QtGlobal>
/** Represent any kind of capacity.
2015-07-13 15:16:36 +01:00
Any kind of capacity that can be expressed in units of Byte, KiB, MiB and so on. Also prints
capacities in nicely formatted ways.
2015-07-13 15:16:36 +01:00
@author Volker Lanz <vl@fidra.de>
*/
class LIBKPMCORE_EXPORT Capacity
{
2015-07-13 15:16:36 +01:00
public:
/** Units we can deal with */
enum class Unit : uint8_t { Byte, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB };
2015-07-13 15:16:36 +01:00
/** Type of capacity to print */
enum class Type : uint8_t { Used, Available, Total };
2015-07-13 15:16:36 +01:00
/** Flags for printing */
enum class Flag : uint8_t { NoFlags = 0, AppendUnit = 1, AppendBytes = 2 };
2015-07-13 15:16:36 +01:00
Q_DECLARE_FLAGS(Flags, Flag)
2015-07-13 15:16:36 +01:00
public:
explicit Capacity(qint64 size);
2018-04-09 15:14:34 +01:00
explicit Capacity(const Partition& p, Type t = Type::Total);
2015-07-13 15:16:36 +01:00
Capacity(const Device& d);
2015-07-13 15:16:36 +01:00
public:
bool operator==(const Capacity& other) const {
return other.m_Size == m_Size;
}
bool operator!=(const Capacity& other) const {
return other.m_Size != m_Size;
}
bool operator>(const Capacity& other) const {
return other.m_Size > m_Size;
}
bool operator<(const Capacity& other) const {
return other.m_Size < m_Size;
}
bool operator>=(const Capacity& other) const {
return other.m_Size >= m_Size;
}
bool operator<=(const Capacity& other) const {
return other.m_Size <= m_Size;
}
2015-07-13 15:16:36 +01:00
qint64 toInt(Unit u) const;
double toDouble(Unit u) const;
2015-07-13 15:16:36 +01:00
bool isValid() const;
2015-07-13 15:16:36 +01:00
static QString formatByteSize(double size, int precision = 2);
static const QString& invalidString() {
return m_InvalidString; /**< @return string representing an invalid capacity */
}
static QString unitName(Unit u, qint64 val = 1);
static qint64 unitFactor(Unit from, Unit to);
2015-07-13 15:16:36 +01:00
private:
qint64 m_Size;
static const QString m_InvalidString;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Capacity::Flags)
#endif