Skip to content

Commit

Permalink
Redo the Q_FOREACH loop control without GCC statement expressions
Browse files Browse the repository at this point in the history
It's possible to do without them, which probably makes the number of
supported compilers a lot bigger: they just need to support decltype()
or __typeof__.

That includes the Intel compiler. The old code was also apparently
working, but no one had realized the old workaround for some old version
was still in place.

The loop overhead is more or less the same. I have not done benchmarks,
but inspection of the generated assembly shows more or less the same
number of instructions.

Change-Id: I32d499c84a6ddd19d994b49f17a469acb5c3a3f1
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
  • Loading branch information
thiagomacieira authored and The Qt Project committed Feb 19, 2014
1 parent 9592b98 commit c35a3f5
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/corelib/global/qglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -858,22 +858,32 @@ Q_CORE_EXPORT void qFreeAligned(void *ptr);
# endif
#endif

#if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && !defined(Q_CC_RVCT)
#if (defined(Q_CC_GNU) && !defined(Q_CC_RVCT))
/* make use of typeof-extension */
template <typename T>
class QForeachContainer {
public:
inline QForeachContainer(const T& t) : c(t), brk(0), i(c.begin()), e(c.end()) { }
inline QForeachContainer(const T& t) : c(t), i(c.begin()), e(c.end()), control(1) { }
const T c;
int brk;
typename T::const_iterator i, e;
int control;
};

#define Q_FOREACH(variable, container) \

// Explanation of the control word:
// - it's initialized to 1
// - that means both the inner and outer loops start
// - if there were no breaks, at the end of the inner loop, it's set to 0, which
// causes it to exit (the inner loop is run exactly once)
// - at the end of the outer loop, it's inverted, so it becomes 1 again, allowing
// the outer loop to continue executing
// - if there was a break inside the inner loop, it will exit with control still
// set to 1; in that case, the outer loop will invert it to 0 and will exit too
# define Q_FOREACH(variable, container) \
for (QForeachContainer<__typeof__((container))> _container_((container)); \
!_container_.brk && _container_.i != _container_.e; \
__extension__ ({ ++_container_.brk; ++_container_.i; })) \
for (variable = *_container_.i;; __extension__ ({--_container_.brk; break;}))
_container_.control && _container_.i != _container_.e; \
++_container_.i, _container_.control ^= 1) \
for (variable = *_container_.i; _container_.control; _container_.control = 0)

#else

Expand Down

0 comments on commit c35a3f5

Please sign in to comment.