Skip to content

Commit

Permalink
Enhance %extend to extend a class with static template methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wsfulton committed Jan 24, 2017
1 parent 8351913 commit 481ebfa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
24 changes: 17 additions & 7 deletions Examples/test-suite/extend_template_method.i
Expand Up @@ -3,16 +3,18 @@
%include <std_string.i>

%inline %{
namespace Space {
class ExtendMe {
public:
template <typename T>
T do_stuff_impl(int a, T b, double d) {
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);
Expand All @@ -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:
Expand All @@ -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);
Expand All @@ -49,14 +56,17 @@ 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>;

%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>;

4 changes: 4 additions & 0 deletions Examples/test-suite/java/extend_template_method_runme.java
Expand Up @@ -32,6 +32,8 @@ public static void main(String argv[]) {
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();
Expand All @@ -52,6 +54,8 @@ public static void main(String argv[]) {
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");
}
}
}
5 changes: 5 additions & 0 deletions Examples/test-suite/python/extend_template_method_runme.py
Expand Up @@ -17,6 +17,8 @@
if ret_string != "hello there":
raise RuntimeError("string failed " + ret_string)

if ExtendMe.static_method(123) != 123:
raise RuntimeError("static_method failed");

em = TemplateExtend()

Expand All @@ -34,3 +36,6 @@
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");
9 changes: 7 additions & 2 deletions Source/Modules/lang.cxx
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 481ebfa

Please sign in to comment.