Skip to content

Commit a195e17

Browse files
committed
Updates from typemap-colon changes
1 parent 11a9433 commit a195e17

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

Doc/Manual/Typemaps.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,40 @@ <H4><a name="Typemaps_special_macro_typemap">14.4.4.2 $typemap(method, typepatte
24572457
</pre>
24582458
</div>
24592459

2460+
<H4><a name="Typemaps_special_macro_typemap_attribute">14.4.4.3 $typemap(method:attribute, typepattern)</a></H4>
2461+
2462+
<p>
2463+
An enhanced version of <tt>$typemap</tt> provides access to typemap attributes by
2464+
appending a colon and the attribute name after the attribute name. In the example below,
2465+
"cstype" is the typemap method and "out" is the typemap attribute.
2466+
<p>
2467+
2468+
<div class="code">
2469+
<pre>
2470+
%typemap(cstype, out="object") XClass "XClass"
2471+
%typemap(cscode) BarClass %{
2472+
$typemap(cstype:out, XClass) bar()
2473+
{
2474+
return null;
2475+
}
2476+
</pre>
2477+
</div>
2478+
<p>
2479+
which expands to
2480+
</p>
2481+
<div class="code">
2482+
<pre>
2483+
object bar()
2484+
{
2485+
return null;
2486+
}
2487+
</pre>
2488+
</div>
2489+
2490+
<p>
2491+
<b>Compatibility note: </b> Support for typemap attributes in <tt>$typemap</tt>
2492+
was introduced in SWIG-4.1.0.
2493+
</p>
24602494

24612495
<H3><a name="Typemaps_special_variable_attributes">14.4.5 Special variables and typemap attributes</a></H3>
24622496

Examples/test-suite/python/special_variable_macros_runme.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import special_variable_macros
22

3+
cvar = special_variable_macros.cvar
34
name = special_variable_macros.Name()
45
if special_variable_macros.testFred(name) != "none":
56
raise "test failed"
7+
if cvar.accessed_examplekw != 0:
8+
raise "Precondition failed"
69
if special_variable_macros.testJack(name) != "$specialname":
710
raise "test failed"
11+
if cvar.accessed_examplekw != 1:
12+
raise "Postcondition failed"
813
if special_variable_macros.testJill(name) != "jilly":
914
raise "test failed"
1015
if special_variable_macros.testMary(name) != "SWIGTYPE_p_NameWrap":

Examples/test-suite/special_variable_macros.i

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ struct NameWrap {
3939
private:
4040
Name name;
4141
};
42+
43+
// Global variable for testing whether examplekw was touched
44+
int accessed_examplekw = 0;
4245
%}
4346

4447
// check $1 and $input get expanded properly when used from $typemap()
45-
%typemap(in) Name *GENERIC ($*1_type temp)
48+
%typemap(in, examplekw="accessed_examplekw=1;") Name *GENERIC ($*1_type temp)
4649
%{
4750
/*%typemap(in) Name *GENERIC start */
4851
temp = Name("$specialname");
@@ -80,6 +83,7 @@ static const char *nameDescriptor = "$descriptor(Name)";
8083
%typemap(in) Name *jack {
8184
// %typemap(in) Name *jack start
8285
$typemap(in, Name *GENERIC)
86+
$typemap(in:examplekw, Name *GENERIC)
8387
// %typemap(in) Name *jack end
8488
}
8589

Source/Swig/typemap.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,10 @@ int Swig_typemap_apply(ParmList *src, ParmList *dest) {
536536
if (code) {
537537
Replace(nkey, dsig, "", DOH_REPLACE_ANY);
538538
Replace(nkey, "tmap:", "", DOH_REPLACE_ANY);
539-
Setattr(deferred_add, nkey, sm1);
540-
}
541-
Delete(nkey);
542-
}
539+
Setattr(deferred_add, nkey, sm1);
540+
}
541+
}
542+
Delete(nkey);
543543
}
544544
}
545545

@@ -2100,10 +2100,11 @@ static void replace_embedded_typemap(String *s, ParmList *parm_sublist, Wrapper
21002100
Printf(stdout, " Containing: %s\n", dtypemap);
21012101
Delete(dtypemap);
21022102
}
2103-
found_colon = Strstr(tmap_method, ":");
2103+
found_colon = Strchr(tmap_method, ':');
21042104
if (found_colon) {
2105+
/* Substitute from a keyword argument to a typemap. Avoid emitting local variables from the attached typemap by passing NULL for the file. */
21052106
String *temp_tmap_method = NewStringWithSize(Char(tmap_method), found_colon - Char(tmap_method));
2106-
Swig_typemap_attach_parms(temp_tmap_method, to_match_parms, f);
2107+
Swig_typemap_attach_parms(temp_tmap_method, to_match_parms, NULL);
21072108
Delete(temp_tmap_method);
21082109
} else {
21092110
Swig_typemap_attach_parms(tmap_method, to_match_parms, f);

0 commit comments

Comments
 (0)