diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2021-11-12 12:49:53 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2022-02-15 17:00:03 -0800 |
commit | f1c305cbd2e6b489074360056d9a7820fe5c7ae0 (patch) | |
tree | 8950449b349d6d3f2a37869553305eb376ea0c5f /src/corelib/serialization/qcborvalue_p.h | |
parent | 11ea1721786da2adb47a1d1b31a55e28aa3e0349 (diff) |
QCborMap: merge the operator[] methods into a template
Change-Id: I5e52dc5b093c43a3b678fffd16b6e7a123338178
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/serialization/qcborvalue_p.h')
-rw-r--r-- | src/corelib/serialization/qcborvalue_p.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/corelib/serialization/qcborvalue_p.h b/src/corelib/serialization/qcborvalue_p.h index 55a5dbf871..185b74bc93 100644 --- a/src/corelib/serialization/qcborvalue_p.h +++ b/src/corelib/serialization/qcborvalue_p.h @@ -439,6 +439,30 @@ public: } return QCborValueRef{ this, i + 1 }; } + template <typename KeyType> static QCborValueRef + findOrAddMapKey(QCborContainerPrivate *container, KeyType key) + { + qsizetype size = 0; + qsizetype index = size + 1; + if (container) { + size = container->elements.size(); + index = container->findCborMapKey<KeyType>(key).i; // returns size + 1 if not found + } + Q_ASSERT(index & 1); + Q_ASSERT((size & 1) == 0); + + container = detach(container, qMax(index + 1, size)); + Q_ASSERT(container); + Q_ASSERT((container->elements.size() & 1) == 0); + + if (index >= size) { + container->append(key); + container->append(QCborValue()); + } + Q_ASSERT(index < container->elements.size()); + return { container, index }; + } + template <typename KeyType> static QCborValueRef findOrAddMapKey(QCborMap &map, KeyType key); #if QT_CONFIG(cborstreamreader) void decodeValueFromCbor(QCborStreamReader &reader, int remainingStackDepth); |