foreach.hpp 2.57 KB
Newer Older
Edwin Carlinet's avatar
Edwin Carlinet committed
1
2
3
#pragma once

#include <boost/preprocessor/punctuation/remove_parens.hpp>
Michaël Roynard's avatar
Michaël Roynard committed
4
#include <mln/core/rangev3/rows.hpp>
Edwin Carlinet's avatar
Edwin Carlinet committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


/// \brief The macro defines mln_foreach(RANGE_DECLARATION, RANGE_EXPRESSION) as equivalent to:
///
/// \code
/// for (auto&& row : mln::ranges::rows(RANGE_EXPRESSION))
///   for (RANGE_DECLARATION : row)
/// \endcode
///
/// with support for *break* statement.
///
/// \note Due to limited support for comma by preprocessor, any RANGE_DECLARATION with comma must be enclosed
/// in parenthesis. For example:
///
/// \code
///   mln_foreach((auto [a,b]), multispan)
/// \endcode
#define mln_foreach_new(PROTECTED_DECL, RNG)                                                                           \
  if (bool __mln_has_been_broken = false; false)                                                                       \
  {                                                                                                                    \
  }                                                                                                                    \
26
27
28
  else if (auto&& __mln_rng = RNG; false)                                                                              \
  {                                                                                                                    \
  }                                                                                                                    \
Edwin Carlinet's avatar
Edwin Carlinet committed
29
  else                                                                                                                 \
30
    for (auto&& __mln_inner_rng : mln::ranges::rows(__mln_rng))                                                        \
Edwin Carlinet's avatar
Edwin Carlinet committed
31
32
33
34
35
36
37
38
      if (__mln_has_been_broken)                                                                                       \
        break;                                                                                                         \
      else                                                                                                             \
        for (BOOST_PP_REMOVE_PARENS(PROTECTED_DECL) : __mln_inner_rng)                                                 \
          if (__mln_has_been_broken)                                                                                   \
            break;                                                                                                     \
          else                                                                                                         \
            for (__mln_has_been_broken = true; __mln_has_been_broken; __mln_has_been_broken = false)