Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qcbormap.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 Intel Corporation.
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 "qcbormap.h"
5#include "qcborvalue_p.h"
6
8
9using namespace QtCbor;
10
170 : d(nullptr)
171{
172}
173
178 : d(other.d)
179{
180}
181
202{
203}
204
210{
211 d = other.d;
212 return *this;
213}
214
248qsizetype QCborMap::size() const noexcept
249{
250 return d ? d->elements.size() / 2 : 0;
251}
252
259{
260 d.reset();
261}
262
269{
271 if (d) {
272 result.reserve(size());
273 for (qsizetype i = 0; i < d->elements.size(); i += 2)
274 result << d->valueAt(i);
275 }
276 return result;
277}
278
392{
394}
395
516{
518}
519
639QCborValueRef QCborMap::operator[](const QString & key)
640{
642}
643
760{
761 return QCborContainerPrivate::findOrAddMapKey<const QCborValue &>(*this, key);
762}
763
764template <typename KeyType> inline QCborValueRef
766{
767 QCborValueRef result = findOrAddMapKey<KeyType>(map.d.data(), key);
768 map.d = result.d;
769 return result;
770}
771
792{
793 detach();
794 auto it = constFind(key);
795 return { d.data(), it.item.i };
796}
797
817{
818 detach();
819 auto it = constFind(key);
820 return { d.data(), it.item.i };
821}
822
842{
843 detach();
844 auto it = constFind(key);
845 return { d.data(), it.item.i };
846}
847
867{
868 detach();
869 auto it = constFind(key);
870 return { d.data(), it.item.i };
871}
872
890{
891 return d ? d->findCborMapKey(key) : constEnd();
892}
893
911{
912 return d ? d->findCborMapKey(key) : constEnd();
913}
914
932{
934}
935
953{
954 return d ? d->findCborMapKey<const QCborValue &>(key) : constEnd();
955}
956
1045{
1046 detach();
1047
1048 // remove both key and value
1049 // ### optimize?
1050 d->removeAt(it.item.i - 1);
1051 d->removeAt(it.item.i - 1);
1052 return it;
1053}
1054
1065{
1066 detach();
1067 QCborValue v = d->extractAt(it.item.i);
1068 // remove both key and value
1069 // ### optimize?
1070 d->removeAt(it.item.i - 1);
1071 d->removeAt(it.item.i - 1);
1072
1073 return v;
1074}
1075
1164void QCborMap::detach(qsizetype reserved)
1165{
1166 d = QCborContainerPrivate::detach(d.data(), reserved ? reserved : size() * 2);
1167}
1168
1681size_t qHash(const QCborMap &map, size_t seed)
1682{
1683 return qHashRange(map.begin(), map.end(), seed);
1684}
1685
1686#if !defined(QT_NO_DEBUG_STREAM)
1688{
1689 QDebugStateSaver saver(dbg);
1690 dbg.nospace() << "QCborMap{";
1691 const char *open = "{";
1692 for (auto pair : m) {
1693 dbg << open << pair.first << ", " << pair.second << '}';
1694 open = ", {";
1695 }
1696 return dbg << '}';
1697}
1698#endif
1699
1700#ifndef QT_NO_DATASTREAM
1701#if QT_CONFIG(cborstreamwriter)
1703{
1704 stream << value.toCborValue().toCbor();
1705 return stream;
1706}
1707#endif
1708
1710{
1712 stream >> buffer;
1713 QCborParserError parseError{};
1714 value = QCborValue::fromCbor(buffer, &parseError).toMap();
1715 if (parseError.error)
1717 return stream;
1718}
1719#endif
1720
\inmodule QtCore
Definition qbytearray.h:57
static QCborContainerPrivate * detach(QCborContainerPrivate *d, qsizetype reserved)
void removeAt(qsizetype idx)
QCborValueConstRef findCborMapKey(KeyType key)
QList< QtCbor::Element > elements
QCborValue extractAt(qsizetype idx)
static QCborValueRef findOrAddMapKey(QCborContainerPrivate *container, KeyType key)
QCborValue valueAt(qsizetype idx) const
\inmodule QtCore
Definition qcbormap.h:86
\inmodule QtCore\reentrant
Definition qcbormap.h:29
\inmodule QtCore\reentrant
Definition qcbormap.h:21
QList< QCborValue > keys() const
Returns a list of all keys in this map.
Definition qcbormap.cpp:268
iterator erase(iterator it)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const_iterator constFind(qint64 key) const
Returns a map iterator to the key-value pair whose key is key, if the map contains such a pair.
Definition qcbormap.cpp:889
QCborValue extract(iterator it)
QCborMap & operator=(const QCborMap &other) noexcept
Replaces the contents of this object with a copy of other, then returns a reference to this object.
Definition qcbormap.cpp:209
void clear()
Empties this map.
Definition qcbormap.cpp:258
QCborMap() noexcept
Constructs an empty CBOR Map object.
Definition qcbormap.cpp:169
iterator find(qint64 key)
Definition qcbormap.cpp:791
~QCborMap()
Destroys this QCborMap object and frees any associated resources it owns.
Definition qcbormap.cpp:201
const QCborValue operator[](qint64 key) const
Returns the QCborValue element in this map that corresponds to key key, if there is one.
Definition qcbormap.h:181
const_iterator constEnd() const
Returns a map iterator representing an element just past the last element in the map.
Definition qcbormap.h:248
qsizetype size() const noexcept Q_DECL_PURE_FUNCTION
Returns the number of elements in this map.
Definition qcbormap.cpp:248
\inmodule QtCore\reentrant
Definition qcborvalue.h:50
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore
void reset(T *ptr=nullptr) noexcept
T * data() const noexcept
Returns a pointer to the shared data object.
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
iterator begin()
Definition qmap.h:597
iterator end()
Definition qmap.h:601
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QMap< QString, QString > map
[6]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
QDebug operator<<(QDebug dbg, const QCborMap &m)
size_t qHash(const QCborMap &map, size_t seed)
QDataStream & operator>>(QDataStream &stream, QCborMap &value)
EGLStreamKHR stream
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
size_t qHashRange(InputIterator first, InputIterator last, size_t seed=0) noexcept(noexcept(qHash(*first)))
GLsizei const GLfloat * v
[13]
const GLfloat * m
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint buffer
GLuint64EXT * result
[6]
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
ptrdiff_t qsizetype
Definition qtypes.h:70
long long qint64
Definition qtypes.h:55
file open(QIODevice::ReadOnly)
QSharedPointer< T > other(t)
[5]
\inmodule QtCore\reentrant
Definition qcborvalue.h:40