Add new terminator attribute for the csin typemap for C#

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10808 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2008-09-02 20:56:52 +00:00
commit 044895ef6e
4 changed files with 210 additions and 9 deletions

View file

@ -1,6 +1,18 @@
Version 1.3.37 (in progress)
=============================
2008-09-02: wsfulton
[C#] Support for 'using' and 'fixed' blocks in the 'csin' typemap is now
possible through the use of the pre attribute and the new terminator attribute, eg
%typemap(csin,
pre=" using (CDate temp$csinput = new CDate($csinput)) {",
terminator=" } // terminate temp$csinput using block",
) const CDate &
"$csclassname.getCPtr(temp$csinput)"
See CSharp.html for more info.
2008-09-01: wsfulton
[CFFI] Commit patch #2079381 submitted by Boris Smilga - constant exprs put into
no-eval context in DEFCENUM

View file

@ -1567,6 +1567,7 @@ Let's assume the code being wrapped is as follows:
<pre>
class CDate {
public:
CDate();
CDate(int year, int month, int day);
int getYear();
int getMonth();
@ -1649,8 +1650,8 @@ The typemaps to achieve this are shown below.
<pre>
%typemap(cstype) const CDate&amp; "System.DateTime"
%typemap(csin,
pre=" CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day);")
const CDate &amp;
pre=" CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day);"
) const CDate &amp;
"$csclassname.getCPtr(temp$csinput)"
%typemap(cstype) CDate&amp; "out System.DateTime"
@ -1658,7 +1659,8 @@ The typemaps to achieve this are shown below.
pre=" CDate temp$csinput = new CDate();",
post=" $csinput = new System.DateTime(temp$csinput.getYear(),"
" temp$csinput.getMonth(), temp$csinput.getDay(), 0, 0, 0);",
cshin="out $csinput") CDate &amp;
cshin="out $csinput"
) CDate &amp;
"$csclassname.getCPtr(temp$csinput)"
</pre>
@ -1763,7 +1765,8 @@ will be possible with the following <tt>CDate *</tt> typemaps
pre=" CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day);",
post=" $csinput = new System.DateTime(temp$csinput.getYear(),"
" temp$csinput.getMonth(), temp$csinput.getDay(), 0, 0, 0);",
cshin="ref $csinput") CDate *
cshin="ref $csinput"
) CDate *
"$csclassname.getCPtr(temp$csinput)"
</pre>
</div>
@ -1788,6 +1791,50 @@ public class example {
</pre>
</div>
<p>
The following typemap is the same as the previous but demonstrates how a using block can be used for the temporary variable.
The only change to the previous typemap is the introduction of the 'terminator' attribute to terminate the <tt>using</tt> block.
The <tt>subtractYears</tt> method is nearly identical to the above <tt>addYears</tt> method.
</p>
<div class="code">
<pre>
%typemap(csin,
pre=" using (CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day)) {",
post=" $csinput = new System.DateTime(temp$csinput.getYear(),"
" temp$csinput.getMonth(), temp$csinput.getDay(), 0, 0, 0);",
terminator=" } // terminate temp$csinput using block",
cshin="ref $csinput"
) CDate *
"$csclassname.getCPtr(temp$csinput)"
void subtractYears(CDate *pDate, int years) {
*pDate = CDate(pDate-&gt;getYear() - years, pDate-&gt;getMonth(), pDate-&gt;getDay());
}
</pre>
</div>
<p>
The resulting generated code shows the termination of the <tt>using</tt> block:
</p>
<div class="code">
<pre>
public class example {
public static void subtractYears(ref System.DateTime pDate, int years) {
using (CDate temppDate = new CDate(pDate.Year, pDate.Month, pDate.Day)) {
try {
examplePINVOKE.subtractYears(CDate.getCPtr(temppDate), years);
} finally {
pDate = new System.DateTime(temppDate.getYear(), temppDate.getMonth(), temppDate.getDay(), 0, 0, 0);
}
} // terminate temppDate using block
}
...
}
</pre>
</div>
<H3><a name="csharp_date_properties"></a>17.5.4 A date example demonstrating marshalling of C# properties</H3>
@ -1827,7 +1874,8 @@ The typemap type required is thus <tt>CDate *</tt>. Given that the previous sect
pre=" CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day);",
post=" $csinput = new System.DateTime(temp$csinput.getYear(),"
" temp$csinput.getMonth(), temp$csinput.getDay(), 0, 0, 0);",
cshin="ref $csinput") CDate *
cshin="ref $csinput"
) CDate *
"$csclassname.getCPtr(temp$csinput)"
%typemap(csvarin, excode=SWIGEXCODE2) CDate * %{

View file

@ -1,6 +1,6 @@
%module csharp_prepost
// Test the pre, post and cshin attributes for csin typemaps
// Test the pre, post, terminate and cshin attributes for csin typemaps
%include "std_vector.i"
@ -88,3 +88,102 @@ public:
};
%}
// test Date marshalling with pre post and terminate typemap attributes (Documented in CSharp.html)
%typemap(cstype) const CDate& "System.DateTime"
%typemap(csin,
pre=" CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day);"
) const CDate &
"$csclassname.getCPtr(temp$csinput)"
%typemap(cstype) CDate& "out System.DateTime"
%typemap(csin,
pre=" CDate temp$csinput = new CDate();",
post=" $csinput = new System.DateTime(temp$csinput.getYear(),"
" temp$csinput.getMonth(), temp$csinput.getDay(), 0, 0, 0);",
cshin="out $csinput"
) CDate &
"$csclassname.getCPtr(temp$csinput)"
%inline %{
class CDate {
public:
CDate();
CDate(int year, int month, int day);
int getYear();
int getMonth();
int getDay();
private:
int m_year;
int m_month;
int m_day;
};
struct Action {
int doSomething(const CDate &dateIn, CDate &dateOut);
Action(const CDate &dateIn, CDate& dateOut);
};
%}
%{
Action::Action(const CDate &dateIn, CDate& dateOut) {dateOut = dateIn;}
int Action::doSomething(const CDate &dateIn, CDate &dateOut) { dateOut = dateIn; return 0; }
CDate::CDate() : m_year(0), m_month(0), m_day(0) {}
CDate::CDate(int year, int month, int day) : m_year(year), m_month(month), m_day(day) {}
int CDate::getYear() { return m_year; }
int CDate::getMonth() { return m_month; }
int CDate::getDay() { return m_day; }
%}
%typemap(cstype, out="System.DateTime") CDate * "ref System.DateTime"
%typemap(csin,
pre=" CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day);",
post=" $csinput = new System.DateTime(temp$csinput.getYear(),"
" temp$csinput.getMonth(), temp$csinput.getDay(), 0, 0, 0);",
cshin="ref $csinput"
) CDate *
"$csclassname.getCPtr(temp$csinput)"
%inline %{
void addYears(CDate *pDate, int years) {
*pDate = CDate(pDate->getYear() + years, pDate->getMonth(), pDate->getDay());
}
%}
%typemap(csin,
pre=" using (CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day)) {",
post=" $csinput = new System.DateTime(temp$csinput.getYear(),"
" temp$csinput.getMonth(), temp$csinput.getDay(), 0, 0, 0);",
terminator=" } // terminate temp$csinput using block",
cshin="ref $csinput"
) CDate *
"$csclassname.getCPtr(temp$csinput)"
%inline %{
void subtractYears(CDate *pDate, int years) {
*pDate = CDate(pDate->getYear() - years, pDate->getMonth(), pDate->getDay());
}
%}
%typemap(csvarin, excode=SWIGEXCODE2) CDate * %{
/* csvarin typemap code */
set {
CDate temp$csinput = new CDate($csinput.Year, $csinput.Month, $csinput.Day);
$imcall;$excode
} %}
%typemap(csvarout, excode=SWIGEXCODE2) CDate * %{
/* csvarout typemap code */
get {
IntPtr cPtr = $imcall;
CDate tempDate = (cPtr == IntPtr.Zero) ? null : new CDate(cPtr, $owner);$excode
return new System.DateTime(tempDate.getYear(), tempDate.getMonth(), tempDate.getDay(),
0, 0, 0);
} %}
%inline %{
CDate ImportantDate = CDate(1999, 12, 31);
%}

View file

@ -1887,6 +1887,7 @@ public:
bool setter_flag = false;
String *pre_code = NewString("");
String *post_code = NewString("");
String *terminator_code = NewString("");
if (!proxy_flag)
return;
@ -2035,6 +2036,14 @@ public:
Printf(post_code, "\n");
Printv(post_code, post, NIL);
}
String *terminator = Getattr(p, "tmap:csin:terminator");
if (terminator) {
substituteClassname(pt, terminator);
Replaceall(terminator, "$csinput", arg);
if (Len(terminator_code) > 0)
Insert(terminator_code, 0, "\n");
Insert(terminator_code, 0, terminator);
}
Printv(imcall, tm, NIL);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSIN_UNDEF, input_file, line_number, "No csin typemap defined for %s\n", SwigType_str(pt, 0));
@ -2060,7 +2069,8 @@ public:
excodeSubstitute(n, tm, "csout", n);
bool is_pre_code = Len(pre_code) > 0;
bool is_post_code = Len(post_code) > 0;
if (is_pre_code || is_post_code) {
bool is_terminator_code = Len(terminator_code) > 0;
if (is_pre_code || is_post_code || is_terminator_code) {
Replaceall(tm, "\n ", "\n "); // add extra indentation to code in typemap
if (is_post_code) {
Insert(tm, 0, "\n try ");
@ -2072,6 +2082,9 @@ public:
Insert(tm, 0, pre_code);
Insert(tm, 0, "\n");
}
if (is_terminator_code) {
Printv(tm, "\n", terminator_code, NIL);
}
Insert(tm, 0, "{");
Printf(tm, "\n }");
}
@ -2171,6 +2184,7 @@ public:
Delete(pre_code);
Delete(post_code);
Delete(terminator_code);
Delete(function_code);
Delete(return_type);
Delete(imcall);
@ -2191,6 +2205,7 @@ public:
String *helper_args = NewString("");
String *pre_code = NewString("");
String *post_code = NewString("");
String *terminator_code = NewString("");
String *im_return_type = NewString("");
bool feature_director = (parentNode(n) && Swig_directorclass(n));
@ -2286,6 +2301,14 @@ public:
Printf(post_code, "\n");
Printv(post_code, post, NIL);
}
String *terminator = Getattr(p, "tmap:csin:terminator");
if (terminator) {
substituteClassname(pt, terminator);
Replaceall(terminator, "$csinput", arg);
if (Len(terminator_code) > 0)
Insert(terminator_code, 0, "\n");
Insert(terminator_code, 0, terminator);
}
cshin = Getattr(p, "tmap:csin:cshin");
if (cshin)
Replaceall(cshin, "$csinput", arg);
@ -2342,7 +2365,8 @@ public:
bool is_pre_code = Len(pre_code) > 0;
bool is_post_code = Len(post_code) > 0;
if (is_pre_code || is_post_code) {
bool is_terminator_code = Len(terminator_code) > 0;
if (is_pre_code || is_post_code || is_terminator_code) {
Printf(helper_code, " {\n");
if (is_pre_code) {
Printv(helper_code, pre_code, "\n", NIL);
@ -2354,6 +2378,9 @@ public:
} else {
Printv(helper_code, " return ", imcall, ";", NIL);
}
if (is_terminator_code) {
Printv(helper_code, "\n", terminator_code, NIL);
}
Printf(helper_code, "\n }\n");
String *helper_name = NewStringf("%s.SwigConstruct%s(%s)", proxy_class_name, proxy_class_name, helper_args);
String *im_outattributes = Getattr(n, "tmap:imtype:outattributes");
@ -2372,6 +2399,7 @@ public:
Delete(im_return_type);
Delete(pre_code);
Delete(post_code);
Delete(terminator_code);
Delete(construct_tm);
Delete(attributes);
Delete(overloaded_name);
@ -2489,6 +2517,7 @@ public:
bool setter_flag = false;
String *pre_code = NewString("");
String *post_code = NewString("");
String *terminator_code = NewString("");
if (l) {
if (SwigType_type(Getattr(l, "type")) == T_VOID) {
@ -2593,6 +2622,14 @@ public:
Printf(post_code, "\n");
Printv(post_code, post, NIL);
}
String *terminator = Getattr(p, "tmap:csin:terminator");
if (terminator) {
substituteClassname(pt, terminator);
Replaceall(terminator, "$csinput", arg);
if (Len(terminator_code) > 0)
Insert(terminator_code, 0, "\n");
Insert(terminator_code, 0, terminator);
}
Printv(imcall, tm, NIL);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSIN_UNDEF, input_file, line_number, "No csin typemap defined for %s\n", SwigType_str(pt, 0));
@ -2617,7 +2654,8 @@ public:
excodeSubstitute(n, tm, "csout", n);
bool is_pre_code = Len(pre_code) > 0;
bool is_post_code = Len(post_code) > 0;
if (is_pre_code || is_post_code) {
bool is_terminator_code = Len(terminator_code) > 0;
if (is_pre_code || is_post_code || is_terminator_code) {
Replaceall(tm, "\n ", "\n "); // add extra indentation to code in typemap
if (is_post_code) {
Insert(tm, 0, "\n try ");
@ -2629,6 +2667,9 @@ public:
Insert(tm, 0, pre_code);
Insert(tm, 0, "\n");
}
if (is_terminator_code) {
Printv(tm, "\n", terminator_code, NIL);
}
Insert(tm, 0, "{");
Printf(tm, "\n }");
}
@ -2704,6 +2745,7 @@ public:
Delete(pre_code);
Delete(post_code);
Delete(terminator_code);
Delete(function_code);
Delete(return_type);
Delete(imcall);