SF Patch#268 - Add 'pre', 'post' and 'terminator' attributes to the csdirectorin typemap
"csdirectorin" "pre:" and "post" code attributes in C# module. Without them it is not trivial to marshal strings and smart-pointers back and forth between user callback code and native code. (especially by reference) Also fixes 2 minor issues in director code generation that are difficult to come by until "csdirectorin" attribute is extended. The first is that "ref" types used in directors lead to invalid signature generation (the type array used to match methods possibly overloaded by user). typeof(ref T) is used instead of typeof().MakeByRefType() The second is that ignored director methods are not completely ignored - if there was a %typemap(imtype, "directorinattributes") it is not skipped for ignored method.
This commit is contained in:
parent
f9566ad2df
commit
2b407f4b27
4 changed files with 147 additions and 9 deletions
|
|
@ -5,6 +5,17 @@ using csharp_prepostNamespace;
|
|||
|
||||
public class csharp_prepost_runme {
|
||||
|
||||
class PrePost3_Derived : PrePost3
|
||||
{
|
||||
public PrePost3_Derived(){}
|
||||
public override void method(ref double[] vpre, DoubleVector vpost)
|
||||
{
|
||||
Assert(vpre[0], 1.0);
|
||||
vpre[0] = 2.0;
|
||||
Assert(vpost.Count, 2);
|
||||
vpost.Add(1.0);
|
||||
}
|
||||
}
|
||||
public static void Main() {
|
||||
{
|
||||
double[] v;
|
||||
|
|
@ -37,6 +48,17 @@ public class csharp_prepost_runme {
|
|||
Assert(v[1], 8.8);
|
||||
}
|
||||
|
||||
{
|
||||
PrePost3_Derived p = new PrePost3_Derived();
|
||||
double[] vpre = new double[] { 1.0 };
|
||||
DoubleVector vpost = new DoubleVector();
|
||||
vpost.Add(3.0);
|
||||
vpost.Add(4.0);
|
||||
p.method(ref vpre, vpost);
|
||||
Assert(vpre[0], 2.0);
|
||||
Assert(vpost.Count, 3);
|
||||
}
|
||||
|
||||
// Check attributes are generated for the constructor helper function
|
||||
{
|
||||
CsinAttributes c = new CsinAttributes(5);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
%module csharp_prepost
|
||||
%module (directors="1") csharp_prepost
|
||||
|
||||
// Test the pre, post, terminate and cshin attributes for csin typemaps
|
||||
|
||||
|
|
@ -64,9 +64,11 @@ bool globalfunction2(std::vector<double> & v, std::vector<double> &v2, std::vect
|
|||
struct PrePost2 {
|
||||
PrePost2() {
|
||||
}
|
||||
virtual ~PrePost2() {
|
||||
}
|
||||
PrePost2(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
|
||||
}
|
||||
bool method(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
|
||||
virtual bool method(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
|
||||
return true;
|
||||
}
|
||||
static bool staticmethod(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
|
||||
|
|
@ -75,6 +77,39 @@ struct PrePost2 {
|
|||
};
|
||||
%}
|
||||
|
||||
// Check csdirectorin pre and post attributes
|
||||
// ref param
|
||||
%typemap(csdirectorin,
|
||||
pre=" DoubleVector d$iminput = new DoubleVector($iminput, false);\n"
|
||||
" int count$iminput = d$iminput.Count;\n"
|
||||
" double[] v$iminput = new double[count$iminput];\n"
|
||||
" for (int i=0; i<count$iminput; ++i) {\n"
|
||||
" v$iminput[i] = d$iminput[i];\n"
|
||||
" }\n",
|
||||
post=" foreach (double d in v$iminput) {\n"
|
||||
" d$iminput.Add(d);\n"
|
||||
" }\n"
|
||||
) std::vector<double> &vpre
|
||||
"ref v$iminput"
|
||||
// post only in csdirectorin typemap
|
||||
%typemap(csdirectorin, post=" DoubleVector d$iminput = new DoubleVector($iminput, false);\n"
|
||||
" int size = d$iminput.Count;\n"
|
||||
" for (int i=0; i<size; ++i) {\n"
|
||||
" d$iminput[i] /= 100;\n"
|
||||
" }") std::vector<double> &vpost
|
||||
"new $csclassname($iminput, false)"
|
||||
|
||||
%feature("director") PrePost3;
|
||||
%inline %{
|
||||
struct PrePost3 {
|
||||
PrePost3() {
|
||||
}
|
||||
virtual ~PrePost3(){}
|
||||
virtual void method(std::vector<double> & vpre, std::vector<double> & vpost) {}
|
||||
};
|
||||
%}
|
||||
|
||||
|
||||
%template(DoubleVector) std::vector<double>;
|
||||
|
||||
// Check attributes in the typemaps
|
||||
|
|
|
|||
|
|
@ -21,6 +21,13 @@
|
|||
%ignore ProtectedPureVirtualMethod1;
|
||||
%ignore ProtectedPureVirtualMethod2;
|
||||
|
||||
%typemap(imtype,
|
||||
inattributes="[inattributes should not be used]",
|
||||
outattributes="[outattributes should not be used]",
|
||||
directorinattributes="[directorinattributes should not be used]",
|
||||
directoroutattributes="[directoroutattributes should not be used]"
|
||||
) int& "imtype should not be used"
|
||||
|
||||
%inline %{
|
||||
|
||||
#include <string>
|
||||
|
|
|
|||
|
|
@ -3468,6 +3468,9 @@ public:
|
|||
String *value = Getattr(n, "value");
|
||||
String *decl = Getattr(n, "decl");
|
||||
String *declaration = NewString("");
|
||||
String *pre_code = NewString("");
|
||||
String *post_code = NewString("");
|
||||
String *terminator_code = NewString("");
|
||||
String *tm;
|
||||
Parm *p;
|
||||
int i;
|
||||
|
|
@ -3556,7 +3559,8 @@ public:
|
|||
const String *im_directoroutattributes = Getattr(n, "tmap:imtype:directoroutattributes");
|
||||
if (im_directoroutattributes) {
|
||||
Printf(callback_def, " %s\n", im_directoroutattributes);
|
||||
Printf(director_delegate_definitions, " %s\n", im_directoroutattributes);
|
||||
if (!ignored_method)
|
||||
Printf(director_delegate_definitions, " %s\n", im_directoroutattributes);
|
||||
}
|
||||
|
||||
Printf(callback_def, " private %s SwigDirector%s(", tm, overloaded_name);
|
||||
|
|
@ -3674,6 +3678,33 @@ public:
|
|||
substituteClassname(pt, din);
|
||||
Replaceall(din, "$iminput", ln);
|
||||
|
||||
// :pre and :post attribute support
|
||||
String *pre = Getattr(p, "tmap:csdirectorin:pre");
|
||||
if (pre) {
|
||||
substituteClassname(pt, pre);
|
||||
Replaceall(pre, "$iminput", ln);
|
||||
if (Len(pre_code) > 0)
|
||||
Printf(pre_code, "\n");
|
||||
Printv(pre_code, pre, NIL);
|
||||
}
|
||||
String *post = Getattr(p, "tmap:csdirectorin:post");
|
||||
if (post) {
|
||||
substituteClassname(pt, post);
|
||||
Replaceall(post, "$iminput", ln);
|
||||
if (Len(post_code) > 0)
|
||||
Printf(post_code, "\n");
|
||||
Printv(post_code, post, NIL);
|
||||
}
|
||||
String *terminator = Getattr(p, "tmap:csdirectorin:terminator");
|
||||
if (terminator) {
|
||||
substituteClassname(pt, terminator);
|
||||
Replaceall(terminator, "$iminput", ln);
|
||||
if (Len(terminator_code) > 0)
|
||||
Insert(terminator_code, 0, "\n");
|
||||
Insert(terminator_code, 0, terminator);
|
||||
}
|
||||
// end :pre and :post attribute support
|
||||
|
||||
if (i > 0) {
|
||||
Printf(delegate_parms, ", ");
|
||||
Printf(proxy_method_types, ", ");
|
||||
|
|
@ -3689,7 +3720,15 @@ public:
|
|||
/* Get the C# parameter type */
|
||||
if ((tm = Getattr(p, "tmap:cstype"))) {
|
||||
substituteClassname(pt, tm);
|
||||
Printf(proxy_method_types, "typeof(%s)", tm);
|
||||
if (Strncmp(tm, "ref ", 4) == 0) {
|
||||
DohReplace(tm, "ref ", "", DOH_REPLACE_FIRST);
|
||||
Printf(proxy_method_types, "typeof(%s).MakeByRefType()", tm);
|
||||
} else if (Strncmp(tm, "out ", 4) == 0) {
|
||||
DohReplace(tm, "out ", "", DOH_REPLACE_FIRST);
|
||||
Printf(proxy_method_types, "typeof(%s).MakeByRefType()", tm);
|
||||
} else {
|
||||
Printf(proxy_method_types, "typeof(%s)", tm);
|
||||
}
|
||||
} else {
|
||||
Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(pt, 0));
|
||||
}
|
||||
|
|
@ -3777,13 +3816,45 @@ public:
|
|||
if ((tm = Swig_typemap_lookup("csdirectorout", n, "", 0))) {
|
||||
substituteClassname(returntype, tm);
|
||||
Replaceall(tm, "$cscall", upcall);
|
||||
|
||||
Printf(callback_code, " return %s;\n", tm);
|
||||
// pre: and post: attribute support
|
||||
bool is_pre_code = Len(pre_code) > 0;
|
||||
bool is_post_code = Len(post_code) > 0;
|
||||
bool is_terminator_code = Len(terminator_code) > 0;
|
||||
if (is_pre_code || is_post_code || is_terminator_code) {
|
||||
Insert(tm, 0, " return ");
|
||||
Printf(tm, ";");
|
||||
if (is_post_code) {
|
||||
Insert(tm, 0, "\n try {\n ");
|
||||
Printv(tm, "\n }\n finally {\n", post_code, "\n }", NIL);
|
||||
} else {
|
||||
Insert(tm, 0, "\n ");
|
||||
}
|
||||
if (is_pre_code) {
|
||||
Insert(tm, 0, pre_code);
|
||||
Insert(tm, 0, "\n");
|
||||
}
|
||||
if (is_terminator_code)
|
||||
Printv(tm, "\n", terminator_code, NIL);
|
||||
Printf(callback_code, " %s\n", tm);
|
||||
} else {
|
||||
Printf(callback_code, " return %s;\n", tm);
|
||||
}
|
||||
}
|
||||
|
||||
Delete(tm);
|
||||
} else
|
||||
Printf(callback_code, " %s;\n", upcall);
|
||||
} else {
|
||||
bool is_pre_code = Len(pre_code) > 0;
|
||||
bool is_post_code = Len(post_code) > 0;
|
||||
if (is_pre_code && is_post_code)
|
||||
Printf(callback_code, " %s\n try {\n %s;\n }\n finally {\n %s\n }\n", pre_code, upcall, post_code);
|
||||
else if (is_pre_code)
|
||||
Printf(callback_code, " %s\n %s;\n", pre_code, upcall);
|
||||
else if (is_post_code)
|
||||
Printf(callback_code, " try {\n %s;\n }\n finally {\n %s\n }\n", upcall, post_code);
|
||||
else
|
||||
Printf(callback_code, " %s;\n", upcall);
|
||||
if (Len(terminator_code) > 0)
|
||||
Printv(callback_code, "\n", terminator_code, NIL);
|
||||
}
|
||||
|
||||
Printf(callback_code, " }\n");
|
||||
Delete(upcall);
|
||||
|
|
@ -3886,6 +3957,9 @@ public:
|
|||
Printf(director_connect_parms, "SwigDirector%s%s delegate%s", classname, methid, methid);
|
||||
}
|
||||
|
||||
Delete(pre_code);
|
||||
Delete(post_code);
|
||||
Delete(terminator_code);
|
||||
Delete(qualified_return);
|
||||
Delete(declaration);
|
||||
Delete(callback_typedef_parms);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue