From d6d7afb75529f95edee21422a9dba07465b34266 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 24 Jan 2017 08:24:46 +0000 Subject: [PATCH] Enhance %extend to extend a class with template constructors --- CHANGES.current | 15 ++++++++++++ Examples/test-suite/extend_template_method.i | 24 +++++++++++++++++-- .../java/extend_template_method_runme.java | 2 ++ .../python/extend_template_method_runme.py | 8 +++++-- Source/Modules/lang.cxx | 4 +++- 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 68d578915..94907d8ec 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -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 + Foo(int a, T b) { + ... + } + } + }; + %template(Foo) Foo::Foo; + 2017-01-22: wsfulton Issue #876 Enhance %extend to extend a class with template methods, eg: @@ -20,6 +33,8 @@ Version 3.0.12 (in progress) }; %template(do_stuff_inst) Foo::do_stuff; + 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 diff --git a/Examples/test-suite/extend_template_method.i b/Examples/test-suite/extend_template_method.i index 1ba6de80a..8c03d9b51 100644 --- a/Examples/test-suite/extend_template_method.i +++ b/Examples/test-suite/extend_template_method.i @@ -6,6 +6,7 @@ namespace Space { class ExtendMe { public: + ExtendMe() {} template T do_stuff_impl(int a, T b, double d) { return b; @@ -24,7 +25,14 @@ public: return $self->do_stuff_impl(0, b, 4.0); } template - static T static_method(T t) { return t; } + static T static_method(T t) { + return t; + } + template + ExtendMe(T x) { + Space::ExtendMe *em = new Space::ExtendMe(); + return em; + } } %template(do_stuff_double) Space::ExtendMe::do_stuff; %template(do_stuff_string) Space::ExtendMe::do_stuff; @@ -34,11 +42,14 @@ public: %template(static_method) Space::ExtendMe::static_method; +%template(ExtendMe) Space::ExtendMe::ExtendMe; + %inline %{ namespace Space { template class TemplateExtendMe { public: + TemplateExtendMe() {} template T template_stuff_impl(X a, T b, double d) { return b; @@ -57,7 +68,14 @@ public: return $self->template_stuff_impl(0, b, 4.0); } template - static T static_template_method(T t) { return t; } + static T static_template_method(T t) { + return t; + } + template + TemplateExtendMe(T x) { + Space::TemplateExtendMe *em = new Space::TemplateExtendMe(); + return em; + } %template(do_template_stuff_double) do_template_stuff; %template(do_template_stuff_string) do_template_stuff; @@ -66,6 +84,8 @@ public: %template(do_template_overloaded_stuff) do_template_overloaded_stuff; %template(static_template_method) static_template_method; + +%template(TemplateExtendMe) Space::TemplateExtendMe::TemplateExtendMe; } %template(TemplateExtend) Space::TemplateExtendMe; diff --git a/Examples/test-suite/java/extend_template_method_runme.java b/Examples/test-suite/java/extend_template_method_runme.java index d6cdc88df..e957be0bd 100644 --- a/Examples/test-suite/java/extend_template_method_runme.java +++ b/Examples/test-suite/java/extend_template_method_runme.java @@ -34,6 +34,7 @@ public class extend_template_method_runme { } if (ExtendMe.static_method(123) != 123) throw new RuntimeException("static_method failed"); + ExtendMe em2 = new ExtendMe(123); } { TemplateExtend em = new TemplateExtend(); @@ -56,6 +57,7 @@ public class extend_template_method_runme { } if (TemplateExtend.static_template_method(123) != 123) throw new RuntimeException("static_template_method failed"); + TemplateExtend em2 = new TemplateExtend(123); } } } diff --git a/Examples/test-suite/python/extend_template_method_runme.py b/Examples/test-suite/python/extend_template_method_runme.py index 32bd7b070..eb1a82d84 100644 --- a/Examples/test-suite/python/extend_template_method_runme.py +++ b/Examples/test-suite/python/extend_template_method_runme.py @@ -18,7 +18,9 @@ if ret_string != "hello there": 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() @@ -38,4 +40,6 @@ if ret_string != "hello there": 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) diff --git a/Source/Modules/lang.cxx b/Source/Modules/lang.cxx index 07077e446..4dc39e069 100644 --- a/Source/Modules/lang.cxx +++ b/Source/Modules/lang.cxx @@ -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);