// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include #include #include template struct SampleStrings { static constexpr char key[] = "hello"; }; template <> struct SampleStrings { static constexpr char16_t key[] = u"hello"; }; template <> struct SampleStrings { static const QChar *const key; }; const QChar *const SampleStrings::key = reinterpret_cast(SampleStrings::key); template constexpr bool hasValueType = false; template constexpr bool hasValueType> = true; class tst_QCborValue : public QObject { Q_OBJECT private: template void doKeyLookup(); template void doConstruct(); private slots: void keyLookupLatin1() { doKeyLookup(); } void keyLookupString() { doKeyLookup(); } void keyLookupConstCharPtr() { doKeyLookup(); }; void constructLatin1() { doConstruct(); } void constructString() { doConstruct(); } void constructStringView() { doConstruct(); } void constructConstCharPtr() { doConstruct(); } }; template void tst_QCborValue::doKeyLookup() { const QCborMap m{{"hello", "world"}, {1, 2}}; const QCborValue v = m; if constexpr (hasValueType) { using Char = std::remove_cv_t; using Strings = SampleStrings; const Type s(Strings::key); QBENCHMARK { [[maybe_unused]] const QCborValue r = v[s]; } } else { QBENCHMARK { [[maybe_unused]] const QCborValue r = v[SampleStrings::key]; } } } template void tst_QCborValue::doConstruct() { if constexpr (hasValueType) { using Char = std::remove_cv_t; using Strings = SampleStrings; const Type s(Strings::key); QBENCHMARK { [[maybe_unused]] const auto v = QCborValue{s}; } } else { QBENCHMARK { [[maybe_unused]] const auto v = QCborValue{SampleStrings::key}; } } } QTEST_MAIN(tst_QCborValue) #include "tst_bench_qcborvalue.moc"