aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/ApiExtractor/parser/enumvalue.cpp
blob: 3749e16a8b31b9868281be8cd4f65e95afac2e91 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "enumvalue.h"

#include <QtCore/QDebug>
#include <QtCore/QString>
#include <QtCore/QTextStream>

using namespace Qt::StringLiterals;

QString EnumValue::toString() const
{
    return m_type == EnumValue::Signed
        ? QString::number(m_value) : QString::number(m_unsignedValue);
}

QString EnumValue::toHex(int fieldWidth) const
{
    QString result;
    QTextStream str(&result);
    // Note: Qt goofes up formatting of negative padded hex numbers, it ends up
    // with "0x00-1". Write '-' before.
    if (isNegative())
        str << '-';
    str << "0x" << Qt::hex;
    if (fieldWidth) {
        str.setFieldWidth(fieldWidth);
        str.setPadChar(u'0');
    }
    if (m_type == EnumValue::Signed)
        str << qAbs(m_value);
    else
        str << m_unsignedValue;
    return result;
}

void EnumValue::setValue(qint64 v)
{
    m_value = v;
    m_type = Signed;
}

void EnumValue::setUnsignedValue(quint64 v)
{
    m_unsignedValue = v;
    m_type = Unsigned;
}

EnumValue EnumValue::toUnsigned() const
{
    if (m_type == Unsigned)
        return *this;
    EnumValue result;
    result.setUnsignedValue(m_value < 0 ? quint64(-m_value) : quint64(m_value));
    return result;
}

bool comparesEqual(const EnumValue &lhs, const EnumValue &rhs) noexcept
{
    if (lhs.m_type != rhs.m_type)
        return false;
    return lhs.m_type == EnumValue::Signed
        ? lhs.m_value == rhs.m_value : lhs.m_unsignedValue == rhs.m_unsignedValue;
}

void EnumValue::formatDebugHex(QDebug &d) const
{
    d << "0x" << Qt::hex;
    formatDebug(d);
    d << Qt::dec;
}

void EnumValue::formatDebug(QDebug &d) const
{

    if (m_type == EnumValue::Signed)
        d << m_value;
    else
        d << m_unsignedValue << 'u';
}

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d,const EnumValue &v)
{
    QDebugStateSaver saver(d);
    d.nospace();
    d.noquote();
    d << "EnumValue(";
    v.formatDebug(d);
    d << ')';
    return d;
}
#endif // !QT_NO_DEBUG_STREAM

QTextStream &operator<<(QTextStream &s, const EnumValue &v)
{
    if (v.m_type == EnumValue::Signed)
        s << v.m_value;
    else
        s << v.m_unsignedValue;
    return s;
}