summaryrefslogtreecommitdiffstats
path: root/src/protobuf/qprotobufoneof.cpp
blob: 97e5c26c5026d84cea0bfd958373a4423901991d (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtProtobuf/qprotobufoneof.h>

QT_BEGIN_NAMESPACE

namespace QtProtobufPrivate {
/*!
    \internal
    \fn template<typename T, QtProtobuf::if_protobuf_type<T> = true> void QProtobufOneof::setValue(const T &value, int fieldNumber)
    \fn template<typename T, QtProtobuf::if_protobuf_message<T> = true> void setValue(T &&value, int fieldNumber)

    Stores the \a value into QProtobufOneof with corresponding \a
    fieldNumber.
*/

/*!
    \internal
    \fn template <typename T, QtProtobuf::if_protobuf_non_message<T> = true> T QProtobufOneof::value() const

    Returns the value of non-message protobuf type stored in the
    QProtobufOneof object.
*/

/*!
    \internal
    \fn template <typename T, QtProtobuf::if_protobuf_message<T> = true> const T *value() const

    Returns the pointer to a protobuf message that is stored inside the
    QProtobufOneof object.
*/

/*!
    \internal
    \fn template <typename T, QtProtobuf::if_protobuf_message<T> = true> T *value()

    Returns the pointer to a protobuf message that is stored inside the
    QProtobufOneof object. Mutable overload.
*/

/*!
    \internal
    \fn template <typename T, QtProtobuf::if_protobuf_non_message<T> = true> bool isEqual(const T &otherValue, int fieldNumber) const
    Compares the data stored in QProtobufOneof with the value/fieldNumber
    pair.
*/

/*!
    \internal
    \fn template <typename T, QtProtobuf::if_protobuf_message<T> = true> bool isEqual(const T &otherValue, int fieldNumber) const
    Compares the data stored in QProtobufOneof with the value/fieldNumber
    pair.
*/

class QProtobufOneofPrivate final
{
public:
    QVariant value;
    int fieldNumber = QtProtobuf::InvalidFieldNumber;
};

/*!
    \internal
    \brief The default constructor, constructs uninitialized QProtobufOneof.
*/
QProtobufOneof::QProtobufOneof() : d_ptr(new QProtobufOneofPrivate) { }

/*!
    \internal
    Constructs a copy of other. This operation takes constant time, because
    QProtobufOneof is implicitly shared. If a shared instance is modified, it
    will be copied (copy-on-write).
*/
QProtobufOneof::QProtobufOneof(const QProtobufOneof &other)
    : d_ptr(new QProtobufOneofPrivate(*other.d_ptr))
{
}

/*!
    \internal
    Assigns other to this QProtobufOneof and returns a reference to this
    QProtobufOneof.
*/

QProtobufOneof &QProtobufOneof::operator=(const QProtobufOneof &other)
{
    if (this != &other)
        *d_ptr = *other.d_ptr;
    return *this;
}

/*!
    \internal
    \fn QProtobufOneof::QProtobufOneof(QProtobufOneof &&other) noexcept
    Move-constructs a QProtobufOneof instance, making it point at the same
    object that other was pointing to.
*/

/*!
    \internal
    \fn QProtobufOneof &QProtobufOneof::operator=(QProtobufOneof &&other) noexcept
    Move-assigns other to this QProtobufOneof instance.
*/

/*!
    \internal
    Checks if values stored in oneof is equal \a other.
*/
bool comparesEqual(const QProtobufOneof &lhs, const QProtobufOneof &rhs) noexcept
{
    if (lhs.d_ptr == rhs.d_ptr)
        return true;
    return lhs.d_func()->fieldNumber == rhs.d_func()->fieldNumber
        && lhs.d_func()->value == rhs.d_func()->value;
}

/*!
    \internal
    Destroys the oneof.
*/
QProtobufOneof::~QProtobufOneof()
{
    delete d_ptr;
}

const QVariant &QProtobufOneof::rawValue() const
{
    return d_ptr->value;
}

/*
   Setting of the QProtobufOneof data makes sense only using
   value/fieldNumber pair. Instead of non-constant dereferencing of the
   QSharedDataPointer we simply rewrite it, that keep its copies untouched but
   creates a new data with the provided values. This avoids redundant data
   copying when updating the data.
*/
void QProtobufOneof::setValue(const QVariant &value, int fieldNumber)
{
    d_ptr->value = value;
    d_ptr->fieldNumber = fieldNumber;
}

/*!
    \internal
    Returns \c true if QProtobufOneof holds the field with \a fieldNumber.
*/
bool QProtobufOneof::holdsField(int fieldNumber) const
{
    Q_D(const QProtobufOneof);
    return d->fieldNumber == fieldNumber && fieldNumber != QtProtobuf::InvalidFieldNumber
            && !d->value.isNull();
}

/*!
    \internal
    Returns the number of a protobuf field that is stored in the object
*/
int QProtobufOneof::fieldNumber() const
{
    Q_D(const QProtobufOneof);
    return d->fieldNumber;
}

/*!
    \internal
    Ensures that underlying QVariant holds the required QMetaType. Initializes variant with the
    default-constructed QMetaType if QVariant holds the different type.
*/
void QProtobufOneof::ensureRawValue(QMetaType metaType)
{
    Q_D(QProtobufOneof);
    if (metaType != d->value.metaType())
        d->value = QVariant::fromMetaType(metaType);
}
} // namespace QtProtobufPrivate

QT_END_NAMESPACE