Nested class improvements - Fixed inconsistency in handling C++ nested classes - sometimes they were treated as forward declarations, other times as if C nested struct was parsed. Added the nestedworkaround feature for C++ nested class handling. Document improved nested class handling. Numerous C and C++ nested struct/class/union test cases added.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11734 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
a0ff0a86d0
commit
ebed6508e4
12 changed files with 300 additions and 82 deletions
|
|
@ -244,6 +244,7 @@ CPP_TEST_CASES += \
|
|||
naturalvar \
|
||||
nested_class \
|
||||
nested_comment \
|
||||
nested_workaround \
|
||||
newobject1 \
|
||||
null_pointer \
|
||||
operator_overload \
|
||||
|
|
|
|||
|
|
@ -12,6 +12,27 @@ struct TestStruct {
|
|||
int a;
|
||||
};
|
||||
|
||||
struct OuterStructNamed {
|
||||
struct InnerStructNamed {
|
||||
double dd;
|
||||
} inner_struct_named;
|
||||
union InnerUnionNamed {
|
||||
double ee;
|
||||
int ff;
|
||||
} inner_union_named;
|
||||
};
|
||||
|
||||
struct OuterStructUnnamed {
|
||||
struct {
|
||||
double xx;
|
||||
} inner_struct_unnamed;
|
||||
union {
|
||||
double yy;
|
||||
int zz;
|
||||
} inner_union_unnamed;
|
||||
};
|
||||
|
||||
|
||||
typedef struct OuterStruct {
|
||||
union {
|
||||
|
||||
|
|
|
|||
|
|
@ -4,52 +4,94 @@
|
|||
|
||||
%inline %{
|
||||
struct Outer {
|
||||
typedef int Integer;
|
||||
///////////////////////////////////////////
|
||||
struct InnerStruct1 {
|
||||
int x;
|
||||
Integer x;
|
||||
};
|
||||
|
||||
class InnerClass1 {
|
||||
public:
|
||||
int x;
|
||||
Integer x;
|
||||
};
|
||||
|
||||
union InnerUnion1 {
|
||||
int x;
|
||||
Integer x;
|
||||
double y;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////
|
||||
class {
|
||||
public:
|
||||
int a;
|
||||
Integer a;
|
||||
};
|
||||
|
||||
struct {
|
||||
int b;
|
||||
Integer b;
|
||||
};
|
||||
|
||||
union {
|
||||
int c;
|
||||
Integer c;
|
||||
double d;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////
|
||||
class InnerClass2 {
|
||||
public:
|
||||
int x;
|
||||
Integer x;
|
||||
} InnerClass2Name;
|
||||
|
||||
struct InnerStruct2 {
|
||||
int x;
|
||||
Integer x;
|
||||
} InnerStruct2Name;
|
||||
|
||||
union InnerUnion2 {
|
||||
int x;
|
||||
Integer x;
|
||||
double y;
|
||||
} InnerUnion2Name;
|
||||
|
||||
///////////////////////////////////////////
|
||||
class {
|
||||
public:
|
||||
Integer x;
|
||||
} InnerClass3Name;
|
||||
|
||||
struct {
|
||||
Integer x;
|
||||
} InnerStruct3Name;
|
||||
|
||||
union {
|
||||
Integer x;
|
||||
double y;
|
||||
} InnerUnion3Name;
|
||||
|
||||
///////////////////////////////////////////
|
||||
typedef class {
|
||||
public:
|
||||
Integer x;
|
||||
} InnerClass4;
|
||||
|
||||
typedef struct {
|
||||
Integer x;
|
||||
} InnerStruct4;
|
||||
|
||||
typedef union {
|
||||
Integer x;
|
||||
double y;
|
||||
} InnerUnion4;
|
||||
|
||||
// bug #909387 - inner declared types are treated as forward declarations
|
||||
InnerStruct1* getInnerStruct1() { return 0; }
|
||||
InnerClass1* getInnerClass1() { return 0; }
|
||||
InnerUnion1* getInnerUnion1() { return 0; }
|
||||
|
||||
InnerStruct2* getInnerStruct2() { return 0; }
|
||||
InnerClass2* getInnerClass2() { return 0; }
|
||||
InnerUnion2* getInnerUnion2() { return 0; }
|
||||
|
||||
InnerStruct4* getInnerStruct4() { return 0; }
|
||||
InnerClass4* getInnerClass4() { return 0; }
|
||||
InnerUnion4* getInnerUnion4() { return 0; }
|
||||
};
|
||||
|
||||
%}
|
||||
|
|
|
|||
38
Examples/test-suite/nested_workaround.i
Normal file
38
Examples/test-suite/nested_workaround.i
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
%module nested_workaround
|
||||
// Similar to "Nested classes" documentation example.
|
||||
|
||||
class Inner {
|
||||
int val;
|
||||
public:
|
||||
Inner(int v = 0) : val(v) {}
|
||||
void setValue(int v) { val = v; }
|
||||
int getValue() const { return val; }
|
||||
};
|
||||
%nestedworkaround Outer::Inner;
|
||||
|
||||
%inline %{
|
||||
class Outer {
|
||||
public:
|
||||
class Inner {
|
||||
int val;
|
||||
public:
|
||||
Inner(int v = 0) : val(v) {}
|
||||
void setValue(int v) { val = v; }
|
||||
int getValue() const { return val; }
|
||||
};
|
||||
Inner createInner(int v) const { return Inner(v); }
|
||||
int getInnerValue(const Inner& i) const { return i.getValue(); }
|
||||
Inner doubleInnerValue(Inner inner) {
|
||||
inner.setValue(inner.getValue() * 2);
|
||||
return inner;
|
||||
}
|
||||
};
|
||||
%}
|
||||
|
||||
// We've fooled SWIG into thinking that Inner is a global class, so now we need
|
||||
// to trick the C++ compiler into understanding this apparent global type.
|
||||
%{
|
||||
typedef Outer::Inner Inner;
|
||||
%}
|
||||
|
||||
|
||||
13
Examples/test-suite/python/nested_workaround_runme.py
Normal file
13
Examples/test-suite/python/nested_workaround_runme.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from nested_workaround import *
|
||||
|
||||
inner = Inner(5)
|
||||
outer = Outer()
|
||||
newInner = outer.doubleInnerValue(inner)
|
||||
if newInner.getValue() != 10:
|
||||
raise RuntimeError
|
||||
|
||||
outer = Outer()
|
||||
inner = outer.createInner(3)
|
||||
newInner = outer.doubleInnerValue(inner)
|
||||
if outer.getInnerValue(newInner) != 6:
|
||||
raise RuntimeError
|
||||
Loading…
Add table
Add a link
Reference in a new issue