1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qprotobufoneof.h"
5
6QT_BEGIN_NAMESPACE
7
8namespace QtProtobufPrivate {
9/*!
10 \internal
11 \fn template<typename T> void QProtobufOneof::setValue(const T &value, int fieldNumber)
12
13 Stores the \a value into QProtobufOneof with corresponding \a
14 fieldNumber.
15*/
16
17/*!
18 \internal
19 \fn template<typename T, IsNonMessageProtobufType<T> = 0> T QProtobufOneof::value() const
20
21 Returns the value of non-message protobuf type stored in the
22 QProtobufOneof object.
23*/
24
25/*!
26 \internal
27 \fn template<typename T, IsProtobufMessageType<T> = 0> T &QProtobufOneof::value() const
28
29 Returns the reference to a protobuf message that is stored inside the
30 QProtobufOneof object.
31*/
32
33/*!
34 \internal
35 \fn template<typename T> bool QProtobufOneof::isEqual(const T &value, int fieldNumber) const
36
37 Compares the data stored in QProtobufOneof with the value/fieldNumber
38 pair.
39*/
40
41class QProtobufOneofPrivate final
42{
43public:
44 QVariant value;
45 int fieldNumber = QtProtobuf::InvalidFieldNumber;
46};
47
48/*!
49 \internal
50 \brief The default constructor, constructs uninitialized QProtobufOneof.
51*/
52QProtobufOneof::QProtobufOneof() : d_ptr(new QProtobufOneofPrivate) { }
53
54/*!
55 \internal
56 Constructs a copy of other. This operation takes constant time, because
57 QProtobufOneof is implicitly shared. If a shared instance is modified, it
58 will be copied (copy-on-write).
59*/
60QProtobufOneof::QProtobufOneof(const QProtobufOneof &other)
61 : d_ptr(new QProtobufOneofPrivate(*other.d_ptr))
62{
63}
64
65/*!
66 \internal
67 Assigns other to this QProtobufOneof and returns a reference to this
68 QProtobufOneof.
69*/
70
71QProtobufOneof &QProtobufOneof::operator=(const QProtobufOneof &other)
72{
73 if (this != &other)
74 *d_ptr = *other.d_ptr;
75 return *this;
76}
77
78/*!
79 \internal
80 \fn QProtobufOneof::QProtobufOneof(QProtobufOneof &&other) noexcept
81 Move-constructs a QProtobufOneof instance, making it point at the same
82 object that other was pointing to.
83*/
84
85/*!
86 \internal
87 \fn QProtobufOneof &QProtobufOneof::operator=(QProtobufOneof &&other) noexcept
88 Move-assigns other to this QProtobufOneof instance.
89*/
90
91/*!
92 \internal
93 Checks if values stored in oneof is equal \a other.
94*/
95bool QProtobufOneof::isEqual(const QProtobufOneof &other) const
96{
97 if (d_ptr == other.d_ptr)
98 return true;
99 return d_func()->fieldNumber == other.d_func()->fieldNumber
100 && d_func()->value == other.d_func()->value;
101}
102
103/*!
104 \internal
105 Destroys the oneof.
106*/
107QProtobufOneof::~QProtobufOneof()
108{
109 delete d_ptr;
110}
111
112QVariant &QProtobufOneof::rawValue() const
113{
114 return d_ptr->value;
115}
116
117/*
118 Setting of the QProtobufOneof data makes sense only using
119 value/fieldNumber pair. Instead of non-constant dereferencing of the
120 QSharedDataPointer we simply rewrite it, that keep its copies untouched but
121 creates a new data with the provided values. This avoids redundant data
122 copying when updating the data.
123*/
124void QProtobufOneof::setValue(const QVariant &value, int fieldNumber)
125{
126 d_ptr->value = value;
127 d_ptr->fieldNumber = fieldNumber;
128}
129
130/*!
131 \internal
132 Returns \c true if QProtobufOneof holds the field with \a fieldNumber.
133*/
134bool QProtobufOneof::holdsField(int fieldNumber) const
135{
136 Q_D(const QProtobufOneof);
137 return d->fieldNumber == fieldNumber && fieldNumber != QtProtobuf::InvalidFieldNumber
138 && !d->value.isNull();
139}
140
141/*!
142 \internal
143 Returns the number of a protobuf field that is stored in the object
144*/
145int QProtobufOneof::fieldNumber() const
146{
147 Q_D(const QProtobufOneof);
148 return d->fieldNumber;
149}
150
151} // namespace QtProtobufPrivate
152
153QT_END_NAMESPACE
154

source code of qtgrpc/src/protobuf/qprotobufoneof.cpp