File indexing completed on 2024-04-28 05:45:57

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2012-2019 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
0005 
0006     SPDX-License-Identifier: GPL-3.0-or-later
0007 */
0008 
0009 #ifndef KPMCORE_CAPACITY_H
0010 #define KPMCORE_CAPACITY_H
0011 
0012 #include "util/libpartitionmanagerexport.h"
0013 
0014 class Partition;
0015 class Device;
0016 
0017 #include <QtGlobal>
0018 
0019 /** Represent any kind of capacity.
0020 
0021     Any kind of capacity that can be expressed in units of Byte, KiB, MiB and so on. Also prints
0022     capacities in nicely formatted ways.
0023 
0024     @author Volker Lanz <vl@fidra.de>
0025 */
0026 class LIBKPMCORE_EXPORT Capacity
0027 {
0028 public:
0029     /** Units we can deal with */
0030     enum class Unit : uint8_t { Byte, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB };
0031     /** Type of capacity to print */
0032     enum class Type : uint8_t { Used, Available, Total };
0033     /** Flags for printing */
0034     enum class Flag : uint8_t { NoFlags = 0, AppendUnit = 1, AppendBytes = 2 };
0035     Q_DECLARE_FLAGS(Flags, Flag)
0036 
0037 public:
0038     explicit Capacity(qint64 size);
0039     explicit Capacity(const Partition& p, Type t = Type::Total);
0040     Capacity(const Device& d);
0041 
0042 public:
0043     bool operator==(const Capacity& other) const {
0044         return other.m_Size == m_Size;
0045     }
0046     bool operator!=(const Capacity& other) const {
0047         return other.m_Size != m_Size;
0048     }
0049     bool operator>(const Capacity& other) const {
0050         return other.m_Size > m_Size;
0051     }
0052     bool operator<(const Capacity& other) const {
0053         return other.m_Size < m_Size;
0054     }
0055     bool operator>=(const Capacity& other) const {
0056         return other.m_Size >= m_Size;
0057     }
0058     bool operator<=(const Capacity& other) const {
0059         return other.m_Size <= m_Size;
0060     }
0061 
0062     qint64 toInt(Unit u) const;
0063     double toDouble(Unit u) const;
0064 
0065     bool isValid() const;
0066 
0067     static QString formatByteSize(double size, int precision = 2);
0068     static const QString& invalidString() {
0069         return m_InvalidString;    /**< @return string representing an invalid capacity */
0070     }
0071     static QString unitName(Unit u, qint64 val = 1);
0072     static qint64 unitFactor(Unit from, Unit to);
0073 
0074 private:
0075     qint64 m_Size;
0076     static const QString m_InvalidString;
0077 };
0078 
0079 Q_DECLARE_OPERATORS_FOR_FLAGS(Capacity::Flags)
0080 
0081 #endif