Skip to content

Commit

Permalink
Merge branch 'alexey-pelykh-cpp11_strongly_typed_enums__direct_inject…
Browse files Browse the repository at this point in the history
…_in_java'

* alexey-pelykh-cpp11_strongly_typed_enums__direct_inject_in_java:
  Enhance cpp11_strongly_typed_enumerations testcase and turn it on
  Simplify/improve strongly typed enum implementation for Java
  Rewrite some Java director nested class support code for strongly typed enums
  Expand director_nested_class test to test more than one level of nesting
  Add director_nested_class testcase
  Removed useless code (it does not affect output, at least on our testcases)
  Java/Fix: swig_connect_director used not-fully-qualified classname (proper)
  Java/Fix: swig_connect_director used not-fully-qualified classname
  Java: fix generation of ProxyName when JNI descriptor is requested - for inner classes '$' should be used as separator instead of '/'
  Java: fix invalid director 'self' variable type name (wasn't fully qualified)
  Clean-up test suite and fix issue with nspace, as well as keep the fix for Class::Struct::EnumClass being JNI-referenced as Struct_EnumClass
  C++11 strongly-typed enums fix for Java only (proper)
  Revert "C++11 strongly-typed enums fix for Java only"
  Additional test cases for C++11 strongly-typed enums
  C++11 strongly-typed enums fix for Java only
  • Loading branch information
wsfulton committed Nov 21, 2014
2 parents ac1f067 + f72b9f9 commit 296d45a
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Examples/test-suite/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ CPP11_TEST_CASES = \
cpp11_rvalue_reference3 \
cpp11_sizeof_object \
cpp11_static_assert \
cpp11_strongly_typed_enumerations \
cpp11_thread_local \
cpp11_template_double_brackets \
cpp11_template_explicit \
Expand All @@ -539,7 +540,6 @@ CPP11_TEST_CASES = \
# Broken C++11 test cases.
CPP11_TEST_BROKEN = \
# cpp11_hash_tables \ # not fully implemented yet
# cpp11_strongly_typed_enumerations \ # SWIG not quite getting this right yet in all langs
# cpp11_variadic_templates \ # Broken for some languages (such as Java)
# cpp11_reference_wrapper \ # No typemaps

Expand Down
132 changes: 119 additions & 13 deletions Examples/test-suite/cpp11_strongly_typed_enumerations.i
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
strongly typed enums. Enums with the same type are comparable. Enum classes
require support for nested classes. */
%module cpp11_strongly_typed_enumerations
%warnfilter(302) Val1;
%warnfilter(302) Val2;
%warnfilter(302) Val3;
%warnfilter(302) Val4;

/* Forward declarations (illegally accepted by SWIG - oh well!) */
enum Enum1 : short;
Expand All @@ -17,14 +13,14 @@ enum : unsigned short;
enum class Enum1 {
Val1,
Val2,
Val3 = 100,
Val4 /* = 101 */
Val3 = 13,
Val4
};

enum class Enum2 : short {
Val1,
Val2,
Val3 = 100,
Val3 = 23,
Val4
};
%}
Expand All @@ -39,32 +35,32 @@ enum class Enum5; // Legal in C++11, because enum class declarati
enum class Enum6 : unsigned int; // Legal C++11.

enum Enum4 : unsigned int {
Val1, Val2, Val3 = 100, Val4
Val1, Val2, Val3 = 43, Val4
};

enum class Enum5 {
Val1, Val2, Val3 = 100, Val4
Val1, Val2, Val3 = 53, Val4
};

enum class Enum6 : unsigned int {
Val1, Val2, Val3 = 300, Val4
Val1, Val2, Val3 = 63, Val4
};

typedef enum class Enum7 : unsigned int {
Val1, Val2, Val3 = 300, Val4
Val1, Val2, Val3 = 73, Val4
} Enum7td;

// enum inherits from non-primitive type
enum class Enum8 : size_t {
Val1, Val2, Val3 = 300, Val4
Val1, Val2, Val3 = 83, Val4
};

template <typename T> struct TType {
typedef T type_name;
};

enum class Enum10 : TType<int>::type_name {
Val1, Val2, Val3 = 300, Val4
Val1, Val2, Val3 = 103, Val4
};

// forward declaration, no definition of enum
Expand All @@ -73,6 +69,116 @@ struct UseEnum11 {
Enum11 myenum11;
};

class Class1
{
public:
enum class Enum12
{
Val1 = 1121,
Val2 = 1122,
Val3,
Val4
};

enum Enum13
{
Val1 = 1131,
Val2 = 1132,
Val3,
Val4
};

enum class Enum14
{
Val1 = 1141,
Val2 = 1142,
Val3,
Val4
};

struct Struct1
{
enum class Enum12
{
Val1 = 3121,
Val2 = 3122,
Val3,
Val4
};

enum Enum13
{
Val1 = 3131,
Val2 = 3132,
Val3,
Val4
};

enum class Enum14
{
Val1 = 3141,
Val2 = 3142,
Val3,
Val4
};
};
};

class Class2
{
public:
enum class Enum12
{
Val1 = 2121,
Val2 = 2122,
Val3,
Val4
};

enum Enum13
{
Val1 = 2131,
Val2 = 2132,
Val3,
Val4
};

enum class Enum14
{
Val1 = 2141,
Val2 = 2142,
Val3,
Val4
};

struct Struct1
{
enum class Enum12
{
Val1 = 4121,
Val2 = 4122,
Val3,
Val4
};

enum Enum13
{
Val1 = 4131,
Val2 = 4132,
Val3,
Val4
};

enum class Enum14
{
Val1 = 4141,
Val2 = 4142,
Val3,
Val4
};
};
};

/*
TODO
enum class MyClass {AAA, BBB, CCC};
Expand Down
25 changes: 25 additions & 0 deletions Examples/test-suite/director_nested_class.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%module(directors="1") director_nested_class


%feature("director") DirectorOuter::DirectorInner;
%feature("director") DirectorOuter::DirectorInner::DirectorInnerInner;

%inline %{
struct DirectorOuter {
struct DirectorInner {
virtual ~DirectorInner() {}
virtual int vmethod(int input) const = 0;
struct DirectorInnerInner {
DirectorInnerInner(DirectorInner *din = 0) {}
virtual ~DirectorInnerInner() {}
virtual int innervmethod(int input) const = 0;
};
};
static int callMethod(const DirectorInner &di, int value) {
return di.vmethod(value);
}
static int callInnerInnerMethod(const DirectorInner::DirectorInnerInner &di, int value) {
return di.innervmethod(value);
}
};
%}
41 changes: 41 additions & 0 deletions Examples/test-suite/java/director_nested_class_runme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import director_nested_class.*;

public class director_nested_class_runme {

static {
try {
System.loadLibrary("director_nested_class");
} 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[]) {

director_nested_class_Derived d = new director_nested_class_Derived();

if (DirectorOuter.callMethod(d, 999) != 9990) {
throw new RuntimeException("callMethod(999) failed");
}

director_nested_class_DerivedInnerInner dinner = new director_nested_class_DerivedInnerInner();

if (DirectorOuter.callInnerInnerMethod(dinner, 999) != 999000) {
throw new RuntimeException("callMethod(999) failed");
}
}
}

class director_nested_class_Derived extends DirectorOuter.DirectorInner {
public int vmethod(int input) {
return input * 10;
}
}

class director_nested_class_DerivedInnerInner extends DirectorOuter.DirectorInner.DirectorInnerInner {
public int innervmethod(int input) {
return input * 1000;
}
}
37 changes: 18 additions & 19 deletions Source/Modules/java.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class JAVA:public Language {
String *symname = Copy(Getattr(n, "sym:name"));
if (symname && !GetFlag(n, "feature:flatnested")) {
for (Node *outer_class = Getattr(n, "nested:outer"); outer_class; outer_class = Getattr(outer_class, "nested:outer")) {
Push(symname, ".");
Push(symname, jnidescriptor ? "$" : ".");
Push(symname, Getattr(outer_class, "sym:name"));
}
}
Expand Down Expand Up @@ -838,7 +838,7 @@ class JAVA:public Language {
bool is_destructor = (Cmp(Getattr(n, "nodeType"), "destructor") == 0);

if (!Getattr(n, "sym:overloaded")) {
if (!addSymbol(Getattr(n, "sym:name"), n, imclass_name))
if (!addSymbol(symname, n, imclass_name))
return SWIG_ERROR;
}

Expand Down Expand Up @@ -2959,6 +2959,16 @@ class JAVA:public Language {
// Use the C syntax to make a true Java constant and hope that it compiles as Java code
value = Getattr(n, "enumvalue") ? Copy(Getattr(n, "enumvalue")) : Copy(Getattr(n, "enumvalueex"));
} else {
String *newsymname = 0;
if (!getCurrentClass() || !proxy_flag) {
String *enumClassPrefix = getEnumClassPrefix();
if (enumClassPrefix) {
// A global scoped enum
newsymname = Swig_name_member(0, enumClassPrefix, symname);
symname = newsymname;
}
}

// Get the enumvalue from a JNI call
if (!getCurrentClass() || !cparse_cplusplus || !proxy_flag) {
// Strange hack to change the name
Expand All @@ -2967,8 +2977,9 @@ class JAVA:public Language {
value = NewStringf("%s.%s()", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), symname));
} else {
memberconstantHandler(n);
value = NewStringf("%s.%s()", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), Swig_name_member(0, proxy_class_name, symname)));
value = NewStringf("%s.%s()", full_imclass_name ? full_imclass_name : imclass_name, Swig_name_get(getNSpace(), Swig_name_member(0, getEnumClassPrefix(), symname)));
}
Delete(newsymname);
}
}
return value;
Expand Down Expand Up @@ -3632,7 +3643,6 @@ class JAVA:public Language {
* --------------------------------------------------------------- */

int classDirectorMethod(Node *n, Node *parent, String *super) {
String *classname = Getattr(parent, "sym:name");
String *c_classname = Getattr(parent, "name");
String *name = Getattr(n, "name");
String *symname = Getattr(n, "sym:name");
Expand Down Expand Up @@ -3666,14 +3676,7 @@ class JAVA:public Language {
String *imcall_args = NewString("");
int classmeth_off = curr_class_dmethod - first_class_dmethod;
bool ignored_method = GetFlag(n, "feature:ignore") ? true : false;
String *qualified_classname = Copy(classname);
String *nspace = getNSpace();

if (nspace && package)
Insert(qualified_classname, 0, NewStringf("%s.%s.", package, nspace));
else if(nspace)
Insert(qualified_classname, 0, NewStringf("%s.", nspace));

String *qualified_classname = getProxyName(getClassName());

// Kludge Alert: functionWrapper sets sym:overload properly, but it
// isn't at this point, so we have to manufacture it ourselves. At least
Expand Down Expand Up @@ -3796,7 +3799,6 @@ class JAVA:public Language {
}

Delete(adjustedreturntypeparm);
Delete(qualified_classname);

Swig_director_parms_fixup(l);

Expand Down Expand Up @@ -4437,18 +4439,15 @@ class JAVA:public Language {
* ------------------------------------------------------------ */

int classDirectorEnd(Node *n) {
String *classname = Getattr(n, "sym:name");
String *full_classname = Getattr(n, "name");
String *classname = getProxyName(full_classname, true);
String *director_classname = directorClassName(n);
String *internal_classname;

Wrapper *w = NewWrapper();

if (Len(package_path) > 0 && Len(getNSpace()) > 0)
internal_classname = NewStringf("%s/%s/%s", package_path, getNSpace(), classname);
else if (Len(package_path) > 0)
if (Len(package_path) > 0)
internal_classname = NewStringf("%s/%s", package_path, classname);
else if (Len(getNSpace()) > 0)
internal_classname = NewStringf("%s/%s", getNSpace(), classname);
else
internal_classname = NewStringf("%s", classname);

Expand Down
Loading

0 comments on commit 296d45a

Please sign in to comment.