Enhance %extend to extend a class with static template methods

This commit is contained in:
William S Fulton 2017-01-24 07:40:22 +00:00
commit 481ebfab45
4 changed files with 33 additions and 9 deletions

View file

@ -3,6 +3,7 @@
%include <std_string.i>
%inline %{
namespace Space {
class ExtendMe {
public:
template <typename T>
@ -10,9 +11,10 @@ public:
return b;
}
};
}
%}
%extend ExtendMe {
%extend Space::ExtendMe {
template<typename T>
T do_stuff(int a, T b) {
return $self->do_stuff_impl(a, b, 4.0);
@ -21,15 +23,19 @@ public:
T do_overloaded_stuff(T b) {
return $self->do_stuff_impl(0, b, 4.0);
}
template<typename T>
static T static_method(T t) { return t; }
}
%template(do_stuff_double) ExtendMe::do_stuff<double>;
%template(do_stuff_string) ExtendMe::do_stuff<std::string>;
%template(do_stuff_double) Space::ExtendMe::do_stuff<double>;
%template(do_stuff_string) Space::ExtendMe::do_stuff<std::string>;
%template(do_overloaded_stuff) ExtendMe::do_overloaded_stuff<std::string>;
%template(do_overloaded_stuff) ExtendMe::do_overloaded_stuff<double>;
%template(do_overloaded_stuff) Space::ExtendMe::do_overloaded_stuff<std::string>;
%template(do_overloaded_stuff) Space::ExtendMe::do_overloaded_stuff<double>;
%template(static_method) Space::ExtendMe::static_method<int>;
%inline %{
namespace Space {
template<typename X>
class TemplateExtendMe {
public:
@ -38,9 +44,10 @@ public:
return b;
}
};
}
%}
%extend TemplateExtendMe {
%extend Space::TemplateExtendMe {
template<typename T>
T do_template_stuff(int a, T b) {
return $self->template_stuff_impl(a, b, 4.0);
@ -49,6 +56,8 @@ public:
T do_template_overloaded_stuff(T b) {
return $self->template_stuff_impl(0, b, 4.0);
}
template<typename T>
static T static_template_method(T t) { return t; }
%template(do_template_stuff_double) do_template_stuff<double>;
%template(do_template_stuff_string) do_template_stuff<std::string>;
@ -56,7 +65,8 @@ public:
%template(do_template_overloaded_stuff) do_template_overloaded_stuff<std::string>;
%template(do_template_overloaded_stuff) do_template_overloaded_stuff<double>;
%template(static_template_method) static_template_method<int>;
}
%template(TemplateExtend) TemplateExtendMe<int>;
%template(TemplateExtend) Space::TemplateExtendMe<int>;

View file

@ -32,6 +32,8 @@ public class extend_template_method_runme {
if (!ret_string.equals("hello there"))
throw new RuntimeException("string failed " + ret_string);
}
if (ExtendMe.static_method(123) != 123)
throw new RuntimeException("static_method failed");
}
{
TemplateExtend em = new TemplateExtend();
@ -52,6 +54,8 @@ public class extend_template_method_runme {
if (!ret_string.equals("hello there"))
throw new RuntimeException("string failed " + ret_string);
}
if (TemplateExtend.static_template_method(123) != 123)
throw new RuntimeException("static_template_method failed");
}
}
}

View file

@ -17,6 +17,8 @@ ret_string = em.do_overloaded_stuff("hello there")
if ret_string != "hello there":
raise RuntimeError("string failed " + ret_string)
if ExtendMe.static_method(123) != 123:
raise RuntimeError("static_method failed");
em = TemplateExtend()
@ -34,3 +36,6 @@ if ret_double != 1.1:
ret_string = em.do_template_overloaded_stuff("hello there")
if ret_string != "hello there":
raise RuntimeError("string failed " + ret_string)
if TemplateExtend.static_template_method(123) != 123:
raise RuntimeError("static_template_method failed");

View file

@ -1145,7 +1145,9 @@ int Language::globalfunctionHandler(Node *n) {
Delete(cbname);
}
Setattr(n, "parms", nonvoid_parms(parms));
String *call = Swig_cfunction_call(name, parms);
String *extendname = Getattr(n, "extendname");
String *call = Swig_cfunction_call(extendname ? extendname : name, parms);
String *cres = Swig_cresult(type, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
Delete(cres);
@ -1322,7 +1324,10 @@ int Language::staticmemberfunctionHandler(Node *n) {
if (!defaultargs && code) {
/* Hmmm. An added static member. We have to create a little wrapper for this */
Swig_add_extension_code(n, cname, parms, type, code, CPlusPlus, 0);
String *mangled_cname = Swig_name_mangle(cname);
Swig_add_extension_code(n, mangled_cname, parms, type, code, CPlusPlus, 0);
Setattr(n, "extendname", mangled_cname);
Delete(mangled_cname);
}
}