Skip to content

Commit

Permalink
Enhance %extend to extend a class with template constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
wsfulton committed Jan 24, 2017
1 parent 481ebfa commit d6d7afb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
15 changes: 15 additions & 0 deletions CHANGES.current
Expand Up @@ -7,6 +7,19 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 3.0.12 (in progress)
============================

2017-01-24: wsfulton
Enhance %extend to extend a class with template constructors, eg:

struct Foo {
%extend {
template<typename T>
Foo(int a, T b) {
...
}
}
};
%template(Foo) Foo::Foo<double>;

2017-01-22: wsfulton
Issue #876 Enhance %extend to extend a class with template methods, eg:

Expand All @@ -20,6 +33,8 @@ Version 3.0.12 (in progress)
};
%template(do_stuff_inst) Foo::do_stuff<double>;

Similarly for static template methods.

2017-01-22: kwwette
[Octave] add support for version 4.2
- The Octave API now uses some C++11 features. It is recommended to use
Expand Down
24 changes: 22 additions & 2 deletions Examples/test-suite/extend_template_method.i
Expand Up @@ -6,6 +6,7 @@
namespace Space {
class ExtendMe {
public:
ExtendMe() {}
template <typename T>
T do_stuff_impl(int a, T b, double d) {
return b;
Expand All @@ -24,7 +25,14 @@ public:
return $self->do_stuff_impl(0, b, 4.0);
}
template<typename T>
static T static_method(T t) { return t; }
static T static_method(T t) {
return t;
}
template<typename T>
ExtendMe(T x) {
Space::ExtendMe *em = new Space::ExtendMe();
return em;
}
}
%template(do_stuff_double) Space::ExtendMe::do_stuff<double>;
%template(do_stuff_string) Space::ExtendMe::do_stuff<std::string>;
Expand All @@ -34,11 +42,14 @@ public:

%template(static_method) Space::ExtendMe::static_method<int>;

%template(ExtendMe) Space::ExtendMe::ExtendMe<int>;

%inline %{
namespace Space {
template<typename X>
class TemplateExtendMe {
public:
TemplateExtendMe() {}
template <typename T>
T template_stuff_impl(X a, T b, double d) {
return b;
Expand All @@ -57,7 +68,14 @@ public:
return $self->template_stuff_impl(0, b, 4.0);
}
template<typename T>
static T static_template_method(T t) { return t; }
static T static_template_method(T t) {
return t;
}
template<typename T>
TemplateExtendMe(T x) {
Space::TemplateExtendMe<X> *em = new Space::TemplateExtendMe<X>();
return em;
}

%template(do_template_stuff_double) do_template_stuff<double>;
%template(do_template_stuff_string) do_template_stuff<std::string>;
Expand All @@ -66,6 +84,8 @@ public:
%template(do_template_overloaded_stuff) do_template_overloaded_stuff<double>;

%template(static_template_method) static_template_method<int>;

%template(TemplateExtendMe) Space::TemplateExtendMe::TemplateExtendMe<int>;
}

%template(TemplateExtend) Space::TemplateExtendMe<int>;
Expand Down
2 changes: 2 additions & 0 deletions Examples/test-suite/java/extend_template_method_runme.java
Expand Up @@ -34,6 +34,7 @@ public static void main(String argv[]) {
}
if (ExtendMe.static_method(123) != 123)
throw new RuntimeException("static_method failed");
ExtendMe em2 = new ExtendMe(123);
}
{
TemplateExtend em = new TemplateExtend();
Expand All @@ -56,6 +57,7 @@ public static void main(String argv[]) {
}
if (TemplateExtend.static_template_method(123) != 123)
throw new RuntimeException("static_template_method failed");
TemplateExtend em2 = new TemplateExtend(123);
}
}
}
8 changes: 6 additions & 2 deletions Examples/test-suite/python/extend_template_method_runme.py
Expand Up @@ -18,7 +18,9 @@
raise RuntimeError("string failed " + ret_string)

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

em2 = ExtendMe(123)

em = TemplateExtend()

Expand All @@ -38,4 +40,6 @@
raise RuntimeError("string failed " + ret_string)

if TemplateExtend.static_template_method(123) != 123:
raise RuntimeError("static_template_method failed");
raise RuntimeError("static_template_method failed")

em2 = TemplateExtend(123)
4 changes: 3 additions & 1 deletion Source/Modules/lang.cxx
Expand Up @@ -2791,7 +2791,9 @@ int Language::constructorHandler(Node *n) {
Setattr(n, "handled_as_constructor", "1");
}

Swig_ConstructorToFunction(n, NSpace, ClassType, none_comparison, director_ctor, CPlusPlus, Getattr(n, "template") ? 0 : Extend, DirectorClassName);
int extendmember = GetFlag(n, "isextendmember") ? Extend : 0;
int flags = Getattr(n, "template") ? extendmember : Extend;
Swig_ConstructorToFunction(n, NSpace, ClassType, none_comparison, director_ctor, CPlusPlus, flags, DirectorClassName);
Setattr(n, "sym:name", mrename);
functionWrapper(n);
Delete(mrename);
Expand Down

0 comments on commit d6d7afb

Please sign in to comment.