diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-06-13 22:27:39 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-06-20 12:45:19 +0200 |
commit | accab3b040434a72a8e67542981efba6cbe067f4 (patch) | |
tree | 8c5661410395839cc594be948b8a84914aaa819d /src | |
parent | c623f5fa124b7f23d8aa1f3d916ff87c9923e670 (diff) |
Optimize findVoices implementation
Instead of calling QList::removeIf in the recursive template, call
it outside and use the recursive template to check each criteria for the
current voice.
Invert the logic for less brain-twisting: the helper returns true for
voices that match all criteria.
Pick-to: 6.6
Change-Id: I25364919ad0c2d71aefb8b9872752bf726e99334
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/tts/qtexttospeech.h | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/src/tts/qtexttospeech.h b/src/tts/qtexttospeech.h index a9e18b5..db40929 100644 --- a/src/tts/qtexttospeech.h +++ b/src/tts/qtexttospeech.h @@ -143,7 +143,7 @@ public: auto voices = allVoices(plocale); if constexpr (sizeof...(args) > 0) - findVoicesImpl(voices, std::forward<Args>(args)...); + voices.removeIf([&](const QVoice &voice) -> bool { return !voiceMatches(voice, args...); }); return voices; } @@ -205,37 +205,38 @@ private: static constexpr qsizetype value = lastIndexOf(std::make_integer_sequence<qsizetype, sizeof...(Ts)>{}); }; + template <typename Arg0, typename ...Args> - void findVoicesImpl(QList<QVoice> &voices, Arg0 &&arg0, Args &&...args) const - { + bool voiceMatches(const QVoice &voice, Arg0 &&arg0, Args &&...args) const { using ArgType = q20::remove_cvref_t<Arg0>; - voices.removeIf([&](const auto &voice){ + bool matches = [&]{ if constexpr (std::is_same_v<ArgType, QLocale>) { - return voice.locale() != arg0; + return voice.locale() == arg0; } else if constexpr (std::is_same_v<ArgType, QLocale::Language>) { - return voice.locale().language() != arg0; + return voice.locale().language() == arg0; } else if constexpr (std::is_same_v<ArgType, QLocale::Territory>) { - return voice.locale().territory() != arg0; + return voice.locale().territory() == arg0; } else if constexpr (std::is_same_v<ArgType, QVoice::Gender>) { - return voice.gender() != arg0; + return voice.gender() == arg0; } else if constexpr (std::is_same_v<ArgType, QVoice::Age>) { - return voice.age() != arg0; + return voice.age() == arg0; } else if constexpr (std::disjunction_v<std::is_convertible<ArgType, QString>, std::is_convertible<ArgType, QStringView>>) { - return voice.name() != arg0; + return voice.name() == arg0; } else if constexpr (std::is_same_v<ArgType, QRegularExpression>) { - return !arg0.match(voice.name()).hasMatch(); + return arg0.match(voice.name()).hasMatch(); } else { static_assert(QtPrivate::type_dependent_false<Arg0>(), "Type cannot be matched to a QVoice property!"); - return true; + return false; } - }); + }(); if constexpr (sizeof...(args) > 0) { static_assert(LastIndexOf<ArgType, std::tuple<q20::remove_cvref_t<Args>...>>::value == -1, "Using multiple criteria of the same type is not supported"); - findVoicesImpl(voices, std::forward<Args>(args)...); + matches = matches && voiceMatches(voice, args...); } + return matches; } Q_DISABLE_COPY(QTextToSpeech) |