Allow an instantiated template to have the same name as the C++ template name

For example, this is now possible:
  template<typename T> struct X { ... };
  %template(X) X<int>;
Closes #1100.
This commit is contained in:
William S Fulton 2017-09-29 21:18:57 +01:00
commit e27a606335
7 changed files with 325 additions and 4 deletions

View file

@ -0,0 +1,51 @@
%module cpp_template_duplicate_names
// From test-suite/template_class_reuse.i test
%{
namespace Space {
template <bool B> struct Duplicate1 { void f(){}; };
}
%}
// %warnfilter(SWIGWARN_PARSE_REDEFINED) Space::Duplicate1;
namespace Space {
template <bool B> struct Duplicate1 { void f(){}; };
template <bool B> struct Duplicate1 { void f(){}; };
}
// %warnfilter(SWIGWARN_PARSE_REDEFINED) Space::Duplicate2;
%inline %{
namespace Space {
template <int I> struct Duplicate2 { void n(){}; };
}
%}
%template(Duplicate2_0) Space::Duplicate2<0>;
%template(Duplicate2_0) Space::Duplicate2<0>;
// %warnfilter(SWIGWARN_PARSE_REDEFINED) Space::Duplicate3;
%inline %{
namespace Space {
template <int I> struct Duplicate3 { void n(){}; };
}
%}
%template(Duplicate3) Space::Duplicate3<0>;
%template(Duplicate3) Space::Duplicate3<0>;
%{
namespace Space {
template <bool B> struct Duplicate4 { void f(){}; };
}
%}
// %warnfilter(SWIGWARN_PARSE_REDEFINED) Space::Duplicate4;
namespace Space {
template <bool B> struct Duplicate4 { void f(){}; };
template <bool B> struct Duplicate4 { void f(){}; };
}
%template(Duplicate4) Space::Duplicate4<0>;
%template(Duplicate4) Space::Duplicate4<0>;

View file

@ -0,0 +1,14 @@
cpp_template_duplicate_names.i:14: Warning 302: Identifier 'Duplicate1' redefined (ignored),
cpp_template_duplicate_names.i:13: Warning 302: previous definition of 'Duplicate1'.
cpp_template_duplicate_names.i:14: Warning 302: Identifier 'Duplicate1' redefined (ignored),
cpp_template_duplicate_names.i:13: Warning 302: previous definition of 'Duplicate1'.
cpp_template_duplicate_names.i:25: Warning 302: Identifier 'Duplicate2_0' redefined (ignored) (Renamed from 'Duplicate2< 0 >'),
cpp_template_duplicate_names.i:24: Warning 302: previous definition of 'Duplicate2_0' (Renamed from 'Duplicate2< 0 >').
cpp_template_duplicate_names.i:35: Warning 302: Identifier 'Duplicate3' redefined (ignored) (Renamed from 'Duplicate3< 0 >'),
cpp_template_duplicate_names.i:31: Warning 302: previous definition of 'Duplicate3'.
cpp_template_duplicate_names.i:47: Warning 302: Identifier 'Duplicate4' redefined (ignored),
cpp_template_duplicate_names.i:46: Warning 302: previous definition of 'Duplicate4'.
cpp_template_duplicate_names.i:47: Warning 302: Identifier 'Duplicate4' redefined (ignored),
cpp_template_duplicate_names.i:46: Warning 302: previous definition of 'Duplicate4'.
cpp_template_duplicate_names.i:50: Warning 302: Identifier 'Duplicate4' redefined (ignored) (Renamed from 'Duplicate4< 0 >'),
cpp_template_duplicate_names.i:46: Warning 302: previous definition of 'Duplicate4'.

View file

@ -0,0 +1,57 @@
import template_class_reuse_name.*;
public class template_class_reuse_name_runme {
static {
try {
System.loadLibrary("template_class_reuse_name");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
new Bool1().tt();
new Bool1False().ff();
new Bool2().tt();
new Bool2False().ff();
new Bool3().tt();
new Bool3False().ff();
new Bool4().tt();
new Bool4False().ff();
new BoolForward1().tt();
new BoolForward1False().ff();
new BoolForward2().tt();
new BoolForward2False().ff();
new BoolForward3().tt();
new BoolForward3False().ff();
new BoolForward4().tt();
new BoolForward4False().ff();
new IntBool1().tt();
new IntBool1False().ff();
new IntBool2().tt();
new IntBool2False().ff();
new IntBool3().tt();
new IntBool3False().ff();
new IntBool4().tt();
new IntBool4False().ff();
new Duplicate2_0().n();
new Duplicate3().n();
}
}

View file

@ -0,0 +1,42 @@
from template_class_reuse_name import *
Bool1().tt()
Bool1False().ff()
Bool2().tt()
Bool2False().ff()
Bool3().tt()
Bool3False().ff()
Bool4().tt()
Bool4False().ff()
BoolForward1().tt()
BoolForward1False().ff()
BoolForward2().tt()
BoolForward2False().ff()
BoolForward3().tt()
BoolForward3False().ff()
BoolForward4().tt()
BoolForward4False().ff()
IntBool1().tt()
IntBool1False().ff()
IntBool2().tt()
IntBool2False().ff()
IntBool3().tt()
IntBool3False().ff()
IntBool4().tt()
IntBool4False().ff()
Duplicate2_0().n()
Duplicate3().n()

View file

@ -0,0 +1,132 @@
%module template_class_reuse_name
// One parameter templates
%inline %{
namespace Space {
template <bool B> struct Bool1 { void tt(){}; void ff(){}; };
template <bool B = true> struct Bool2 { void tt(){}; void ff(){}; };
template <bool B> struct Bool3 {};
template <> struct Bool3<true> { void tt(){}; };
template <> struct Bool3<false> { void ff(){}; };
template <bool B = true> struct Bool4 { void tt(){}; };
template <> struct Bool4<false> { void ff(){}; };
}
%}
// Instantiated names are the same as C++ template name
%template(Bool1) Space::Bool1<true>;
%template(Bool2) Space::Bool2<true>;
%template(Bool3) Space::Bool3<true>;
%template(Bool4) Space::Bool4<true>;
// Instantiated names are not the same as C++ template name
%template(Bool1False) Space::Bool1<false>;
%template(Bool2False) Space::Bool2<false>;
%template(Bool3False) Space::Bool3<false>;
%template(Bool4False) Space::Bool4<false>;
// Forward declarated templates
%inline %{
namespace Space {
template <bool B> struct BoolForward1;
template <bool B> struct BoolForward2;
template <bool B> struct BoolForward3;
template <bool B> struct BoolForward4;
template <bool B> struct BoolForward1 { void tt(){}; void ff(){}; };
template <bool B = true> struct BoolForward2 { void tt(){}; void ff(){}; };
template <bool B> struct BoolForward3 {};
template <> struct BoolForward3<true> { void tt(){}; };
template <> struct BoolForward3<false> { void ff(){}; };
template <bool B = true> struct BoolForward4 { void tt(){}; };
template <> struct BoolForward4<false> { void ff(){}; };
}
%}
// Instantiated names are the same as C++ template name
%template(BoolForward1) Space::BoolForward1<true>;
%template(BoolForward2) Space::BoolForward2<true>;
%template(BoolForward3) Space::BoolForward3<true>;
%template(BoolForward4) Space::BoolForward4<true>;
// Instantiated names are not the same as C++ template name
%template(BoolForward1False) Space::BoolForward1<false>;
%template(BoolForward2False) Space::BoolForward2<false>;
%template(BoolForward3False) Space::BoolForward3<false>;
%template(BoolForward4False) Space::BoolForward4<false>;
// Two parameter templates
%inline %{
namespace Space {
template <int I, bool B> struct IntBool1 { void tt(){}; void ff(){}; };
template <int I, bool B = true> struct IntBool2 { void tt(){}; void ff(){}; };
template <int I, bool B> struct IntBool3 {};
template <int I> struct IntBool3<I, true> { void tt(){}; };
template <int I> struct IntBool3<I, false> { void ff(){}; };
template <int I, bool B = true> struct IntBool4 { void tt(){}; };
template <int I> struct IntBool4<I, false> { void ff(){}; };
}
%}
// Instantiated names are the same as C++ template name
%template(IntBool1) Space::IntBool1<0, true>;
%template(IntBool2) Space::IntBool2<0, true>;
%template(IntBool3) Space::IntBool3<0, true>;
%template(IntBool4) Space::IntBool4<0, true>;
// Instantiated names are not the same as C++ template name
%template(IntBool1False) Space::IntBool1<0, false>;
%template(IntBool2False) Space::IntBool2<0, false>;
%template(IntBool3False) Space::IntBool3<0, false>;
%template(IntBool4False) Space::IntBool4<0, false>;
%{
namespace Space {
template <bool B> struct Duplicate1 { void ff(){}; };
}
%}
%warnfilter(SWIGWARN_PARSE_REDEFINED) Space::Duplicate1;
namespace Space {
template <bool B> struct Duplicate1 { void ff(){}; };
template <bool B> struct Duplicate1 { void ff(){}; };
}
%warnfilter(SWIGWARN_PARSE_REDEFINED) Space::Duplicate2;
%inline %{
namespace Space {
template <int I> struct Duplicate2 { void n(){}; };
}
%}
%template(Duplicate2_0) Space::Duplicate2<0>;
%template(Duplicate2_0) Space::Duplicate2<0>;
%warnfilter(SWIGWARN_PARSE_REDEFINED) Space::Duplicate3;
%inline %{
namespace Space {
template <int I> struct Duplicate3 { void n(){}; };
}
%}
%template(Duplicate3) Space::Duplicate3<0>;
%template(Duplicate3) Space::Duplicate3<0>;
%{
namespace Space {
template <bool B> struct Duplicate4 { void ff(){}; };
}
%}
%warnfilter(SWIGWARN_PARSE_REDEFINED) Space::Duplicate4;
namespace Space {
template <bool B> struct Duplicate4 { void ff(){}; };
template <bool B> struct Duplicate4 { void ff(){}; };
}
%template(Duplicate4) Space::Duplicate4<0>;
%template(Duplicate4) Space::Duplicate4<0>;