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

The equivalent change to std::list has also been made (std::list
wrappers were not in the previous release [3.0.12] though).
This commit is contained in:
William S Fulton 2019-03-01 18:01:14 +00:00
commit be491506a4
7 changed files with 46 additions and 12 deletions

View file

@ -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 \

View file

@ -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)))

View file

@ -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)))

View file

@ -0,0 +1,19 @@
%module stl_no_default_constructor
%include <stl.i>
%inline %{
struct NoDefaultCtor {
int value;
NoDefaultCtor(int i) : value(i) {}
};
%}
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGD)
%template(VectorNoDefaultCtor) std::vector<NoDefaultCtor>;
#endif
#if defined(SWIGJAVA)
%include <std_list.i>
%template(ListNoDefaultCtor) std::list<NoDefaultCtor>;
#endif