eĿlipsis
a language independent preprocessor
 
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Loading...
Searching...
No Matches
ellipsis-trigger.h File Reference

Trigger a matching closing braces that ends on the same level of nestedness. More...

Go to the source code of this file.

Detailed Description

Trigger a matching closing braces that ends on the same level of nestedness.

As a simple example suppose that we want to mark {} blocks that should always be surrounded by a do ... while(false) construct. This can be achieved

#include_directives<ellipsis-trigger.h> __prefix__(bind NAME STATEMENT)
#define STATEMENT_START do
#define STATEMENT_CLOSE() while (false)

To write a macro SWAP that can be used anywhere a normal statement could be used such as in

if (something) SWAP(a, b);
else SWAP(a, c);

you could use STATEMENT to mark the opening brace:

#define SWAP(X, Y) \
STATEMENT { \
auto tmpY = (Y); \
(Y) = (X); \
(X) = tmpY; \
}

The example from above would then be replaced by something like:

if (something) do { auto tmpY = (b); (b) = (a); (a) = tmpY; } while (false);
else do { auto tmpY = (c); (c) = (a); (a) = tmpY; } while (false);

Note that if hadn't the STATEMENT macro in SWAP (only { ... }) the replacement would have been

if (something) { auto tmpY = (b); (b) = (a); (a) = tmpY; };
else { auto tmpY = (c); (c) = (a); (a) = tmpY; };

which is a syntax error because the else looses its corresponding if.

This replacement work even if the inner part of the macro would contain other nested {}; the do is always put in place of the STATEMENT macro, the while (false) after the closing brace that corresponds to the first opening brace after the STATEMENT macro.

Evidently, this is only a not-so-interesting example to show how the feature works. If you want to see a more sophisticated use please refer to the LAMBDA macro or the ellipsis-lambda.h header file.