mirror of
https://github.com/boostorg/preprocessor.git
synced 2025-07-23 17:17:12 +02:00
set -> seq
[SVN r16151]
This commit is contained in:
@ -558,10 +558,10 @@ for (int i = start(1); i <= finish(1); ++i) {
|
|||||||
<div>
|
<div>
|
||||||
The library also provides macros to access values in dimensions <i>relative</i>
|
The library also provides macros to access values in dimensions <i>relative</i>
|
||||||
to the current dimension (e.g. the <i>previous</i> dimension). These
|
to the current dimension (e.g. the <i>previous</i> dimension). These
|
||||||
macros take an argument that is interpreted as an offseq from the current
|
macros take an argument that is interpreted as an offset from the current
|
||||||
frame. For example, <b>BOOST_PP_RELATIVE_ITERATION</b>(<i>1</i>) always
|
frame. For example, <b>BOOST_PP_RELATIVE_ITERATION</b>(<i>1</i>) always
|
||||||
refers to the outer dimension immediately previous to the current
|
refers to the outer dimension immediately previous to the current
|
||||||
dimension. An argument of <i>0</i> is interpreted as an offseq of <i>0</i>
|
dimension. An argument of <i>0</i> is interpreted as an offset of <i>0</i>
|
||||||
which causes <b>BOOST_PP_RELATIVE_ITERATION</b>(<i>0</i>) to be equivalent to <b>BOOST_PP_ITERATION</b>().
|
which causes <b>BOOST_PP_RELATIVE_ITERATION</b>(<i>0</i>) to be equivalent to <b>BOOST_PP_ITERATION</b>().
|
||||||
<b>BOOST_PP_RELATIVE_ITERATION</b>(<i>2</i>) refers to the iteration value of
|
<b>BOOST_PP_RELATIVE_ITERATION</b>(<i>2</i>) refers to the iteration value of
|
||||||
the dimension immediately preceding the dimension that precedes the current
|
the dimension immediately preceding the dimension that precedes the current
|
||||||
|
@ -4,40 +4,48 @@
|
|||||||
<link rel="stylesheet" type="text/css" href="../styles.css">
|
<link rel="stylesheet" type="text/css" href="../styles.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h4>Reentrancy</h4>
|
<h4>
|
||||||
|
Reentrancy
|
||||||
|
</h4>
|
||||||
<div>
|
<div>
|
||||||
Macro expansion in the preprocessor is entirely functional.
|
Macro expansion in the preprocessor is entirely functional. Therefore,
|
||||||
Therefore, there is no iteration.
|
there is no iteration. Unfortunately, the preprocessor also disallows
|
||||||
Unfortunately, the preprocessor also disallows recursion.
|
recursion. This means that the library must fake iteration or recursion
|
||||||
This means that the library must fake iteration or recursion by
|
by defining seqs of macros that are implemented similarly.
|
||||||
defining sets of macros that are implemented similarly.
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
To illustrate, here is a simple concatenation macro:
|
To illustrate, here is a simple concatenation macro:
|
||||||
</div>
|
</div>
|
||||||
<div class="code"><pre>
|
<div class="code">
|
||||||
|
<pre>
|
||||||
#define CONCAT(a, b) CONCAT_D(a, b)
|
#define CONCAT(a, b) CONCAT_D(a, b)
|
||||||
#define CONCAT_D(a, b) a ## b
|
#define CONCAT_D(a, b) a ## b
|
||||||
|
|
||||||
CONCAT(a, CONCAT(b, c)) // abc
|
CONCAT(a, CONCAT(b, c)) // abc
|
||||||
</pre></div>
|
</pre>
|
||||||
<div>
|
|
||||||
This is fine for a simple case like the above, but what happens in a scenario like the following:
|
|
||||||
</div>
|
</div>
|
||||||
<div class="code"><pre>
|
<div>
|
||||||
|
This is fine for a simple case like the above, but what happens in a scenario
|
||||||
|
like the following:
|
||||||
|
</div>
|
||||||
|
<div class="code">
|
||||||
|
<pre>
|
||||||
#define AB(x, y) CONCAT(x, y)
|
#define AB(x, y) CONCAT(x, y)
|
||||||
|
|
||||||
CONCAT(A, B(p, q)) // CONCAT(p, q)
|
CONCAT(A, B(p, q)) // CONCAT(p, q)
|
||||||
</pre></div>
|
</pre>
|
||||||
<div>
|
|
||||||
Because there is no recursion, the example above expands to <code>CONCAT(p, q)</code> rather than <code>pq</code>.
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
There are only two ways to "fix" the above.
|
Because there is no recursion, the example above expands to <code>CONCAT(p, q)</code>
|
||||||
First, it can be documented that <code>AB</code> uses <code>CONCAT</code> and disallow usage similar to the above.
|
rather than <code>pq</code>.
|
||||||
Second, multiple concatenation macros can be provided....
|
|
||||||
</div>
|
</div>
|
||||||
<div class="code"><pre>
|
<div>
|
||||||
|
There are only two ways to "fix" the above. First, it can be documented
|
||||||
|
that <code>AB</code> uses <code>CONCAT</code> and disallow usage similar to the
|
||||||
|
above. Second, multiple concatenation macros can be provided....
|
||||||
|
</div>
|
||||||
|
<div class="code">
|
||||||
|
<pre>
|
||||||
#define CONCAT_1(a, b) CONCAT_1_D(a, b)
|
#define CONCAT_1(a, b) CONCAT_1_D(a, b)
|
||||||
#define CONCAT_1_D(a, b) a ## b
|
#define CONCAT_1_D(a, b) a ## b
|
||||||
|
|
||||||
@ -47,68 +55,80 @@ CONCAT(A, B(p, q)) // CONCAT(p, q)
|
|||||||
#define AB(x, y) CONCAT_2(x, y)
|
#define AB(x, y) CONCAT_2(x, y)
|
||||||
|
|
||||||
CONCAT_1(A, B(p, q)) // pq
|
CONCAT_1(A, B(p, q)) // pq
|
||||||
</pre></div>
|
</pre>
|
||||||
|
</div>
|
||||||
<div>
|
<div>
|
||||||
This solves the problem.
|
This solves the problem. However, it is now necessary to know that <code>AB</code>
|
||||||
However, it is now necessary to know that <code>AB</code> uses, not only <i>a</i> concatenation macro,
|
uses, not only <i>a</i> concatenation macro, but <code>CONCAT_2</code> specifically.
|
||||||
but <code>CONCAT_2</code> specifically.
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
A better solution is to abstract <i>which</i> concatenation macro is used....
|
A better solution is to abstract <i>which</i> concatenation macro is used....
|
||||||
</div>
|
</div>
|
||||||
<div class="code"><pre>
|
<div class="code">
|
||||||
|
<pre>
|
||||||
#define AB(c, x, y) CONCAT_ ## c(x, y)
|
#define AB(c, x, y) CONCAT_ ## c(x, y)
|
||||||
|
|
||||||
CONCAT_1(A, B(2, p, q)) // pq
|
CONCAT_1(A, B(2, p, q)) // pq
|
||||||
</pre></div>
|
</pre>
|
||||||
<div>
|
|
||||||
This is an example of <i>generic reentrance</i>, in this case, into a fictional set of concatenation macros.
|
|
||||||
The <code>c</code> parameter represents the "state" of the concatenation construct,
|
|
||||||
and as long as the user keeps track of this state, <code>AB</code> can be used inside of a concatenation macro.
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
The library has the same choices.
|
This is an example of <i>generic reentrance</i>, in this case, into a fictional
|
||||||
It either has to disallow a construct being inside itself or provide multiple, equivalent definitions of a construct
|
seq of concatenation macros. The <code>c</code> parameter represents the
|
||||||
and provide a uniform way to <i>reenter</i> that construct.
|
"state" of the concatenation construct, and as long as the user keeps track of
|
||||||
There are several contructs that <i>require</i> recursion (such as <b>BOOST_PP_WHILE</b>).
|
this state, <code>AB</code> can be used inside of a concatenation macro.
|
||||||
Consequently, the library chooses to provide several sets of macros with mechanisms to reenter the set at a macro
|
|
||||||
that has not already been used.
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
In particular, the library must provide reentrance for <b>BOOST_PP_FOR</b>, <b>BOOST_PP_REPEAT</b>, and <b>BOOST_PP_WHILE</b>.
|
The library has the same choices. It either has to disallow a construct
|
||||||
There are two mechanisms that are used to accomplish this: state parameters (like the above concatenation example) and <i>automatic recursion</i>.
|
being inside itself or provide multiple, equivalent definitions of a construct
|
||||||
</div>
|
and provide a uniform way to <i>reenter</i> that construct. There are
|
||||||
<h4>State Parameters</h4>
|
several contructs that <i>require</i> recursion (such as <b>BOOST_PP_WHILE</b>).
|
||||||
<div>
|
Consequently, the library chooses to provide several seqs of macros with
|
||||||
Each of the above constructs (<b>BOOST_PP_FOR</b>, <b>BOOST_PP_REPEAT</b>, and <b>BOOST_PP_WHILE</b>) has an associated state.
|
mechanisms to reenter the seq at a macro that has not already been used.
|
||||||
This state provides the means to reenter the respective construct.
|
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
Several user-defined macros are passed to each of these constructs (for use as predicates, operations, etc.).
|
In particular, the library must provide reentrance for <b>BOOST_PP_FOR</b>, <b>BOOST_PP_REPEAT</b>,
|
||||||
Every time a user-defined macro is invoked, it is passed the current state of the construct that invoked it so that the macro can reenter
|
and <b>BOOST_PP_WHILE</b>. There are two mechanisms that are used to
|
||||||
the respective set if necessary.
|
accomplish this: state parameters (like the above concatenation example)
|
||||||
|
and <i>automatic recursion</i>.
|
||||||
|
</div>
|
||||||
|
<h4>
|
||||||
|
State Parameters
|
||||||
|
</h4>
|
||||||
|
<div>
|
||||||
|
Each of the above constructs (<b>BOOST_PP_FOR</b>, <b>BOOST_PP_REPEAT</b>, and <b>BOOST_PP_WHILE</b>)
|
||||||
|
has an associated state. This state provides the means to reenter the
|
||||||
|
respective construct.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
These states are used in one of two ways--either by concatenating to or passing to another macro.
|
Several user-defined macros are passed to each of these constructs (for use as
|
||||||
|
predicates, operations, etc.). Every time a user-defined macro is
|
||||||
|
invoked, it is passed the current state of the construct that invoked it so
|
||||||
|
that the macro can reenter the respective seq if necessary.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
There are three types of macros that use these state parameters.
|
These states are used in one of two ways--either by concatenating to or passing
|
||||||
First, the set itself which is reentered through concatenation.
|
to another macro.
|
||||||
Second, corresponding sets that act like they are a part of the the primary set.
|
</div>
|
||||||
These are also reentered through concatenation.
|
<div>
|
||||||
And third, macros that internally use the first or second type of macro.
|
There are three types of macros that use these state parameters. First,
|
||||||
These macros take the state as an additional argument.
|
the seq itself which is reentered through concatenation. Second,
|
||||||
|
corresponding seqs that act like they are a part of the the primary seq.
|
||||||
|
These are also reentered through concatenation. And third, macros that
|
||||||
|
internally use the first or second type of macro. These macros take the
|
||||||
|
state as an additional argument.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
The state of <b>BOOST_PP_WHILE</b> is symbolized by the letter <i>D</i>.
|
The state of <b>BOOST_PP_WHILE</b> is symbolized by the letter <i>D</i>.
|
||||||
Two user-defined macros are passed to <b>BOOST_PP_WHILE</b>--a predicate and an operation.
|
Two user-defined macros are passed to <b>BOOST_PP_WHILE</b>--a predicate and an
|
||||||
When <b>BOOST_PP_WHILE</b> expands these macros, it passes along its state so that these macros
|
operation. When <b>BOOST_PP_WHILE</b> expands these macros, it passes
|
||||||
can reenter the <b>BOOST_PP_WHILE</b> set.
|
along its state so that these macros can reenter the <b>BOOST_PP_WHILE</b> seq.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
Consider the following multiplication implementation that illustrates this technique:
|
Consider the following multiplication implementation that illustrates this
|
||||||
|
technique:
|
||||||
</div>
|
</div>
|
||||||
<div class="code"><pre>
|
<div class="code">
|
||||||
|
<pre>
|
||||||
// The addition interface macro.
|
// The addition interface macro.
|
||||||
// The _D signifies that it reenters
|
// The _D signifies that it reenters
|
||||||
// BOOST_PP_WHILE with concatenation.
|
// BOOST_PP_WHILE with concatenation.
|
||||||
@ -175,79 +195,96 @@ CONCAT_1(A, B(2, p, q)) // pq
|
|||||||
/**/
|
/**/
|
||||||
|
|
||||||
MUL(3, 2) // expands to 6
|
MUL(3, 2) // expands to 6
|
||||||
</pre></div>
|
</pre>
|
||||||
|
</div>
|
||||||
<div>
|
<div>
|
||||||
There are a couple things to note in the above implementation.
|
There are a couple things to note in the above implementation. First,
|
||||||
First, note how <code>ADD_D</code> reenters <b>BOOST_PP_WHILE</b> using the <i>d</i> state parameter.
|
note how <code>ADD_D</code> reenters <b>BOOST_PP_WHILE</b> using the <i>d</i> state
|
||||||
Second, note how <code>MUL</code>'s operation, which is expanded by <b>BOOST_PP_WHILE</b>, passes the state
|
parameter. Second, note how <code>MUL</code>'s operation, which is
|
||||||
on to <code>ADD_D</code>.
|
expanded by <b>BOOST_PP_WHILE</b>, passes the state on to <code>ADD_D</code>.
|
||||||
This illustrates state reentrance by both argument and concatenation.
|
This illustrates state reentrance by both argument and concatenation.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
For every macro in the library that uses <b>BOOST_PP_WHILE</b>,
|
For every macro in the library that uses <b>BOOST_PP_WHILE</b>, there is a
|
||||||
there is a state reentrant variant.
|
state reentrant variant. If that variant uses an argument rather than
|
||||||
If that variant uses an argument rather than concatenation, it is suffixed by <code>_D</code> to symbolize its
|
concatenation, it is suffixed by <code>_D</code> to symbolize its method of
|
||||||
method of reentrance.
|
reentrance. Examples or this include the library's own <b>BOOST_PP_ADD_D</b>
|
||||||
Examples or this include the library's own <b>BOOST_PP_ADD_D</b> and <b>BOOST_PP_MUL_D</b>.
|
and <b>BOOST_PP_MUL_D</b>. If the variant uses concatenation, it is
|
||||||
If the variant uses concatenation, it is suffixed by an underscore.
|
suffixed by an underscore. It is completed by concatenation of the
|
||||||
It is completed by concatenation of the state.
|
state. This includes <b>BOOST_PP_WHILE</b> itself with <b>BOOST_PP_WHILE_</b>
|
||||||
This includes <b>BOOST_PP_WHILE</b> itself with <b>BOOST_PP_WHILE_</b> ## <i>d</i> and, for example,
|
## <i>d</i> and, for example, <b>BOOST_PP_LIST_FOLD_LEFT</b> with <b>BOOST_PP_LIST_FOLD_LEFT_</b>
|
||||||
<b>BOOST_PP_LIST_FOLD_LEFT</b> with <b>BOOST_PP_LIST_FOLD_LEFT_</b> ## <i>d</i>.
|
## <i>d</i>.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
The same set of conventions are used for <b>BOOST_PP_FOR</b> and <b>BOOST_PP_REPEAT</b>, but with the letters
|
The same seq of conventions are used for <b>BOOST_PP_FOR</b> and <b>BOOST_PP_REPEAT</b>,
|
||||||
<i>R</i> and <i>Z</i>, respectively, to symbolize their states.
|
but with the letters <i>R</i> and <i>Z</i>, respectively, to symbolize their
|
||||||
|
states.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
Also note that the above <code>MUL</code> implementation, though not immediately obvious, is using <i>all three</i>
|
Also note that the above <code>MUL</code> implementation, though not
|
||||||
types of reentrance.
|
immediately obvious, is using <i>all three</i> types of reentrance. Not
|
||||||
Not only is it using both types of <i>state</i> reentrance, it is also using <i>automatic recursion</i>....
|
only is it using both types of <i>state</i> reentrance, it is also using <i>automatic
|
||||||
|
recursion</i>....
|
||||||
</div>
|
</div>
|
||||||
<h4>Automatic Recursion</h4>
|
<h4>
|
||||||
|
Automatic Recursion
|
||||||
|
</h4>
|
||||||
<div>
|
<div>
|
||||||
Automatic recursion is a technique that vastly simplifies the use of reentrant constructs.
|
Automatic recursion is a technique that vastly simplifies the use of reentrant
|
||||||
It is used by simply <i>not</i> using any state parameters at all.
|
constructs. It is used by simply <i>not</i> using any state parameters at
|
||||||
|
all.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
The <code>MUL</code> example above uses automatic recursion when it uses <b>BOOST_PP_WHILE</b> by itself.
|
The <code>MUL</code> example above uses automatic recursion when it uses <b>BOOST_PP_WHILE</b>
|
||||||
In other words, <code>MUL</code> can <i>still</i> be used inside <b>BOOST_PP_WHILE</b> even though it doesn't
|
by itself. In other words, <code>MUL</code> can <i>still</i> be used
|
||||||
reenter <b>BOOST_PP_WHILE</b> by concatenating the state to <b>BOOST_PP_WHILE_</b>.
|
inside <b>BOOST_PP_WHILE</b> even though it doesn't reenter <b>BOOST_PP_WHILE</b>
|
||||||
|
by concatenating the state to <b>BOOST_PP_WHILE_</b>.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
To accomplish this, the library uses a "trick."
|
To accomplish this, the library uses a "trick." Despite what it looks
|
||||||
Despite what it looks like, the macro <b>BOOST_PP_WHILE</b> does not take three arguments.
|
like, the macro <b>BOOST_PP_WHILE</b> does not take three arguments. In
|
||||||
In fact, it takes no arguments at all.
|
fact, it takes no arguments at all. Instead, the <b>BOOST_PP_WHILE</b> macro
|
||||||
Instead, the <b>BOOST_PP_WHILE</b> macro expands <i>to</i> a macro that takes three arguments.
|
expands <i>to</i> a macro that takes three arguments. It simply detects
|
||||||
It simply detects what the next available <b>BOOST_PP_WHILE_</b> ## <i>d</i> macro is and returns it.
|
what the next available <b>BOOST_PP_WHILE_</b> ## <i>d</i> macro is and returns
|
||||||
This detection process is somewhat involved, so I won't go into <i>how</i> it works here,
|
it. This detection process is somewhat involved, so I won't go into <i>how</i>
|
||||||
but suffice to say it <i>does</i> work.
|
it works here, but suffice to say it <i>does</i> work.
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
Using automatic recursion to reenter various sets of macros is obviously much simpler.
|
Using automatic recursion to reenter various seqs of macros is obviously much
|
||||||
It completely hides the underlying implementation details.
|
simpler. It completely hides the underlying implementation details.
|
||||||
So, if it is so much easier to use, why do the state parameters still exist?
|
So, if it is so much easier to use, why do the state parameters still
|
||||||
The reason is simple as well.
|
exist? The reason is simple as well. When state parameters are
|
||||||
When state parameters are used, the state is <i>known</i> at all times.
|
used, the state is <i>known</i> at all times. This is not the case when
|
||||||
This is not the case when automatic recursion is used.
|
automatic recursion is used. The automatic recursion mechanism has to <i>deduce</i>
|
||||||
The automatic recursion mechanism has to <i>deduce</i> the state at each point that it is used.
|
the state at each point that it is used. This implies a cost in macro
|
||||||
This implies a cost in macro complexity that in some situations--notably at deep macro depths--will slow
|
complexity that in some situations--notably at deep macro depths--will slow
|
||||||
some preprocessors to a crawl.
|
some preprocessors to a crawl.
|
||||||
</div>
|
</div>
|
||||||
<h4>Conclusion</h4>
|
<h4>
|
||||||
|
Conclusion
|
||||||
|
</h4>
|
||||||
<div>
|
<div>
|
||||||
It is really a tradeoff whether to use state parameters or automatic recursion for reentrancy.
|
It is really a tradeoff whether to use state parameters or automatic recursion
|
||||||
The strengths of automatic recursion are ease of use and implementation encapsulation.
|
for reentrancy. The strengths of automatic recursion are ease of use and
|
||||||
These come at a performance cost on some preprocessors in some situations.
|
implementation encapsulation. These come at a performance cost on some
|
||||||
The primary strength of state parameters, on the other hand, is efficiency.
|
preprocessors in some situations. The primary strength of state
|
||||||
Use of the state parameters is the only way to achieve <i>maximum</i> efficiency.
|
parameters, on the other hand, is efficiency. Use of the state parameters
|
||||||
This efficiency comes at the cost of both code complexity and exposition of implementation.
|
is the only way to achieve <i>maximum</i> efficiency. This efficiency
|
||||||
|
comes at the cost of both code complexity and exposition of implementation.
|
||||||
</div>
|
</div>
|
||||||
<h4>See Also</h4>
|
<h4>
|
||||||
|
See Also
|
||||||
|
</h4>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="../ref/for.html">BOOST_PP_FOR</a></li>
|
<li>
|
||||||
<li><a href="../ref/repeat.html">BOOST_PP_REPEAT</a></li>
|
<a href="../ref/for.html">BOOST_PP_FOR</a></li>
|
||||||
<li><a href="../ref/while.html">BOOST_PP_WHILE</a></li>
|
<li>
|
||||||
|
<a href="../ref/repeat.html">BOOST_PP_REPEAT</a></li>
|
||||||
|
<li>
|
||||||
|
<a href="../ref/while.html">BOOST_PP_WHILE</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<div class="sig">- Paul Mensonides</div>
|
<div class="sig">
|
||||||
|
- Paul Mensonides
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Reference in New Issue
Block a user