Fix %import and %fragment forced inclusion to not generate code.
This commit is contained in:
parent
857a62425c
commit
57a89f987d
8 changed files with 186 additions and 7 deletions
|
|
@ -3904,9 +3904,9 @@ A fragment can use one or more additional fragments, for example:
|
|||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%fragment("<limits.h>", "header") {
|
||||
%#include <limits.h>
|
||||
}
|
||||
%fragment("<limits.h>", "header") %{
|
||||
#include <limits.h>
|
||||
%}
|
||||
|
||||
|
||||
%fragment("AsMyClass", "header", fragment="<limits.h>") {
|
||||
|
|
@ -3989,8 +3989,91 @@ Finally, you can force the inclusion of a fragment at any point in the generated
|
|||
</div>
|
||||
|
||||
<p>
|
||||
which is very useful inside a template class, for example.
|
||||
which, for example, is very useful inside a template class.
|
||||
Another useful case is when using <tt>%extend</tt> inside a class
|
||||
where the additional code in the <tt>%extend</tt> block depends on the contents of the fragment.
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%fragment("<limits.h>", "header") %{
|
||||
#include <limits.h>
|
||||
%}
|
||||
|
||||
struct X {
|
||||
...
|
||||
%extend {
|
||||
%fragment("<limits.h>");
|
||||
bool check(short val) {
|
||||
if (val < SHRT_MIN /*defined in <limits.h>*/) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
Forced inclusion of fragments can be used as a replacement for <a href="SWIG.html#SWIG_nn42">code insertion block</a>, ensuring the
|
||||
code block is only generated once.
|
||||
Consider the contents of FileA.i below which first uses a code insertion block and then a forced fragment inclusion to generate code:
|
||||
<p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
// FileA.i
|
||||
%{
|
||||
#include <stdio.h>
|
||||
%}
|
||||
%fragment("<limits.h>");
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
and another file including the above:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
// FileB.i
|
||||
%include "FileA.i"
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
The resulting code in the wrappers for FileB.i is:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <limits.h>
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
A note of caution must be mentioned when using <tt>%fragment</tt> forced inclusion or code insertion blocks with <tt>%import</tt>.
|
||||
If <tt>%import</tt> is used instead:
|
||||
<p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
// FileC.i
|
||||
%import "FileA.i"
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
then nothing is generated in the resulting code in the wrappers for FileC.i.
|
||||
This is because <tt>%import</tt> is for collecting type information and does not result in any code
|
||||
being generated, see <a href="Preprocessor.html#Preprocessor_nn3">File Imports</a>.
|
||||
</p>
|
||||
|
||||
|
||||
</ol>
|
||||
|
||||
<p>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue