1
General discussions / Re: SFML Game Development -- A book on SFML
« on: December 20, 2013, 05:36:06 am »
I'm in the middle of reading the book and I decided to look at the source code to try to get a better understanding of they are teaching. However, I'm having trouble understanding the FOREACH statement
I understand what it is being used for and how it is implemented, but I can't get how it actually works. I'm hoping somebody could help me out, that would be appreciated.
// Preprocessor trick to allow nested loops
#define BOOK_PP_CAT_IMPL(a, b) a ## b
#define BOOK_PP_CAT(a, b) BOOK_PP_CAT_IMPL(a, b)
#define BOOK_ID(identifier) BOOK_PP_CAT(auroraDetail_, identifier)
#define BOOK_LINE_ID(identifier) BOOK_PP_CAT(BOOK_ID(identifier), __LINE__)
// Macro to emulate C++11 range-based for loop
// Instead of for (decl : range) you write FOREACH(decl, range) as in the following example
//
// std::vector<int> v = ...;
// FOREACH(int& i, v)
// {
// i += 2;
// }
#define FOREACH(declaration, container) \
if (bool BOOK_LINE_ID(broken) = false) {} else \
for (auto BOOK_LINE_ID(itr) = (container).begin(); BOOK_LINE_ID(itr) != (container).end() && !BOOK_LINE_ID(broken); ++BOOK_LINE_ID(itr)) \
if (bool BOOK_LINE_ID(passed) = false) {} else \
if (BOOK_LINE_ID(broken) = true, false) {} else \
for (declaration = *BOOK_LINE_ID(itr); !BOOK_LINE_ID(passed); BOOK_LINE_ID(passed) = true, BOOK_LINE_ID(broken) = false)
#define BOOK_PP_CAT_IMPL(a, b) a ## b
#define BOOK_PP_CAT(a, b) BOOK_PP_CAT_IMPL(a, b)
#define BOOK_ID(identifier) BOOK_PP_CAT(auroraDetail_, identifier)
#define BOOK_LINE_ID(identifier) BOOK_PP_CAT(BOOK_ID(identifier), __LINE__)
// Macro to emulate C++11 range-based for loop
// Instead of for (decl : range) you write FOREACH(decl, range) as in the following example
//
// std::vector<int> v = ...;
// FOREACH(int& i, v)
// {
// i += 2;
// }
#define FOREACH(declaration, container) \
if (bool BOOK_LINE_ID(broken) = false) {} else \
for (auto BOOK_LINE_ID(itr) = (container).begin(); BOOK_LINE_ID(itr) != (container).end() && !BOOK_LINE_ID(broken); ++BOOK_LINE_ID(itr)) \
if (bool BOOK_LINE_ID(passed) = false) {} else \
if (BOOK_LINE_ID(broken) = true, false) {} else \
for (declaration = *BOOK_LINE_ID(itr); !BOOK_LINE_ID(passed); BOOK_LINE_ID(passed) = true, BOOK_LINE_ID(broken) = false)
I understand what it is being used for and how it is implemented, but I can't get how it actually works. I'm hoping somebody could help me out, that would be appreciated.