Skip to content

Commit aee4f97

Browse files
marcmutzthiagomacieira
authored andcommitted
tst_Encoder: port away from Q_FOREACH
Qt is defaulting to QT_NO_FOREACH these days, so make sure we integrate nicely with downstream and fix the single Q_FOREACH/foreach user, in tst_encoder.cpp. Unfortunately, the container's initialization code doesn't exactly lend itself to making the container const (not even IILE (Immediately-Invoked Lambda Expression) would help here, due to the interdependency with `len`), so the idiomatic solution would be to use std::as_const()/qAsConst(). The former is available from C++17, which we don't require, yet, and the latter is not available under QT_NO_AS_CONST (the default for Qt these days), so grab the nettle and implement a t17::as_const() that switches between a manual implementation of std::as_const and the real thing, depending on __cpp_lib_as_const. The `t17` here mimicks the qNN (q20::remove_cvref_t/q23::forward_like/etc) mechanism used in Qt itself for backports, with s/q/t/ because ... _T_inyCbor. The t17 implementation is local to tst_encoder.cpp, but can easily be extracted into a separate header once more users emerge.
1 parent 0b2e66d commit aee4f97

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

tests/encoder/encoder.pro

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ SOURCES += tst_encoder.cpp
33
CONFIG += testcase parallel_test c++11
44
QT = core testlib
55

6+
DEFINES += QT_NO_FOREACH QT_NO_AS_CONST
7+
68
INCLUDEPATH += ../../src
79
msvc: POST_TARGETDEPS = ../../lib/tinycbor.lib
810
else: POST_TARGETDEPS += ../../lib/libtinycbor.a

tests/encoder/tst_encoder.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@
2929
#include <qfloat16.h>
3030
#endif
3131

32+
#include <utility>
33+
namespace t17 {
34+
#ifdef __cpp_lib_as_const
35+
using std::as_const;
36+
#else
37+
template <typename T>
38+
constexpr typename std::add_const<T>::type &as_const(T &t) noexcept { return t; }
39+
// prevent rvalue arguments:
40+
template <typename T>
41+
void as_const(const T &&) = delete;
42+
#endif // __cpp_lib_as_const
43+
} // namespace t17
44+
3245
Q_DECLARE_METATYPE(CborError)
3346
namespace QTest {
3447
template<> char *toString<CborError>(const CborError &err)
@@ -153,7 +166,7 @@ CborError encodeVariant(CborEncoder *encoder, const QVariant &v)
153166
CborError err = cbor_encoder_create_array(encoder, &sub, len);
154167
if (err && !isOomError(err))
155168
return err;
156-
foreach (const QVariant &v2, list) {
169+
for (const QVariant &v2 : t17::as_const(list)) {
157170
err = static_cast<CborError>(err | encodeVariant(&sub, v2));
158171
if (err && !isOomError(err))
159172
return err;

0 commit comments

Comments
 (0)