aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/size_literals.h
blob: b21971727a2e747e89ba56b5443ea364e39ae6f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once

#include "yexception.h"
#include <util/system/types.h>
#include <limits>

// Unsigned literals

constexpr ui64 operator"" _B(unsigned long long value) noexcept {
    return value;
}

constexpr ui64 operator"" _KB(unsigned long long value) noexcept {
    return value * 1024;
}

constexpr ui64 operator"" _MB(unsigned long long value) noexcept {
    return value * 1024_KB;
}

constexpr ui64 operator"" _GB(unsigned long long value) noexcept {
    return value * 1024_MB;
}

constexpr ui64 operator"" _TB(unsigned long long value) noexcept {
    return value * 1024_GB;
}

constexpr ui64 operator"" _PB(unsigned long long value) noexcept {
    return value * 1024_TB;
}

constexpr ui64 operator"" _EB(unsigned long long value) noexcept {
    return value * 1024_PB;
}

// Signed literals

namespace NPrivate {
    constexpr i64 SignedCast(ui64 value) {
        return value <= static_cast<ui64>(std::numeric_limits<i64>::max())
                   ? static_cast<i64>(value)
                   : ythrow yexception() << "The resulting value " << value << " does not fit into the i64 type";
    }
} // namespace NPrivate

constexpr i64 operator"" _KBs(const unsigned long long value) noexcept {
    return ::NPrivate::SignedCast(value * 1024);
}

constexpr i64 operator"" _MBs(unsigned long long value) noexcept {
    return ::NPrivate::SignedCast(value * 1024_KB);
}

constexpr i64 operator"" _GBs(unsigned long long value) noexcept {
    return ::NPrivate::SignedCast(value * 1024_MB);
}

constexpr i64 operator"" _TBs(unsigned long long value) noexcept {
    return ::NPrivate::SignedCast(value * 1024_GB);
}

constexpr i64 operator"" _PBs(unsigned long long value) noexcept {
    return ::NPrivate::SignedCast(value * 1024_TB);
}

constexpr i64 operator"" _EBs(unsigned long long value) noexcept {
    return ::NPrivate::SignedCast(value * 1024_PB);
}