diff --git a/CHANGES.current b/CHANGES.current index 23455b289..43df38b2c 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,30 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.0.0 (in progress) =========================== +2019-02-28: wsfulton + [Java] std::vector improvements for types that do not have a default constructor. + + The std::vector wrappers have been changed to work by default for elements that are + not default insertable, i.e. have no default constructor. This has been achieved by + not wrapping: + + vector(size_type n); + + Previously the above had to be ignored via %ignore. + + If the above constructor is still required it can be added back in again via %extend: + + %extend std::vector { + vector(size_type count) { return new std::vector< T >(count); } + } + + Alternatively, the following wrapped constructor could be used as it provides near-enough + equivalent functionality: + + vector(jint count, const value_type& value); + + *** POTENTIAL INCOMPATIBILITY *** + 2019-02-25: wsfulton [Python] Fix compile errors wrapping overloaded functions/constructors where a vararg function is declared after a non-vararg function. diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 3d4edd47b..60f6a9522 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -407,6 +407,7 @@ CPP_TEST_CASES += \ static_array_member \ static_const_member \ static_const_member_2 \ + stl_no_default_constructor \ string_constants \ struct_initialization_cpp \ struct_value \ diff --git a/Examples/test-suite/java/li_std_list_runme.java b/Examples/test-suite/java/li_std_list_runme.java index 94bcada02..96412fdaf 100644 --- a/Examples/test-suite/java/li_std_list_runme.java +++ b/Examples/test-suite/java/li_std_list_runme.java @@ -53,7 +53,7 @@ public class li_std_list_runme { if (v1.size() != 0) throw new RuntimeException("v1 test (16) failed"); if (v1.remove(Integer.valueOf(456))) throw new RuntimeException("v1 test (17) failed"); - if (new IntList(3).size() != 3) throw new RuntimeException("constructor initial size test failed"); + if (new IntList(3, 0).size() != 3) throw new RuntimeException("constructor initial size test failed"); for (int n : new IntList(10, 999)) if (n != 999) throw new RuntimeException("constructor initialization with value failed"); for (int n : new IntList(new IntList(10, 999))) diff --git a/Examples/test-suite/java/li_std_vector_runme.java b/Examples/test-suite/java/li_std_vector_runme.java index ba0648562..fc8ba0f6f 100644 --- a/Examples/test-suite/java/li_std_vector_runme.java +++ b/Examples/test-suite/java/li_std_vector_runme.java @@ -54,7 +54,7 @@ public class li_std_vector_runme { if (v1.size() != 0) throw new RuntimeException("v1 test (16) failed"); if (v1.remove(Integer.valueOf(456))) throw new RuntimeException("v1 test (17) failed"); - if (new IntVector(3).size() != 3) throw new RuntimeException("constructor initial size test failed"); + if (new IntVector(3, 0).size() != 3) throw new RuntimeException("constructor initial size test failed"); for (int n : new IntVector(10, 999)) if (n != 999) throw new RuntimeException("constructor initialization with value failed"); for (int n : new IntVector(new IntVector(10, 999))) diff --git a/Examples/test-suite/stl_no_default_constructor.i b/Examples/test-suite/stl_no_default_constructor.i new file mode 100644 index 000000000..164710bd5 --- /dev/null +++ b/Examples/test-suite/stl_no_default_constructor.i @@ -0,0 +1,19 @@ +%module stl_no_default_constructor + +%include + +%inline %{ +struct NoDefaultCtor { + int value; + NoDefaultCtor(int i) : value(i) {} +}; +%} + +#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGD) +%template(VectorNoDefaultCtor) std::vector; +#endif + +#if defined(SWIGJAVA) +%include +%template(ListNoDefaultCtor) std::list; +#endif diff --git a/Lib/java/std_list.i b/Lib/java/std_list.i index 82ccde38b..1077bd0a0 100644 --- a/Lib/java/std_list.i +++ b/Lib/java/std_list.i @@ -198,11 +198,6 @@ namespace std { %extend { %fragment("SWIG_ListSize"); - list(jint count) throw (std::out_of_range) { - if (count < 0) - throw std::out_of_range("list count must be positive"); - return new std::list(static_cast::size_type>(count)); - } list(jint count, const T &value) throw (std::out_of_range) { if (count < 0) diff --git a/Lib/java/std_vector.i b/Lib/java/std_vector.i index f621e8992..88de46f46 100644 --- a/Lib/java/std_vector.i +++ b/Lib/java/std_vector.i @@ -94,11 +94,6 @@ SWIGINTERN jint SWIG_VectorSize(size_t size) { void clear(); %extend { %fragment("SWIG_VectorSize"); - vector(jint count) throw (std::out_of_range) { - if (count < 0) - throw std::out_of_range("vector count must be positive"); - return new std::vector< CTYPE >(static_cast::size_type>(count)); - } vector(jint count, const CTYPE &value) throw (std::out_of_range) { if (count < 0)