Add allprotected mode for wrapping protected members when using directors

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10381 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2008-04-20 20:41:01 +00:00
commit 9976dc9d75
17 changed files with 468 additions and 181 deletions

View file

@ -0,0 +1,68 @@
// Tests for the allprotected option
%module(directors="1", allprotected="1") allprotected
%{
#include <string>
%}
%include "std_string.i"
%feature("director") PublicBase;
%feature("director") ProtectedBase;
// protected types not supported (ProtectedEnum, IntegerType). Make sure they can be ignored.
%ignore ProtectedBase::protectedenum;
%ignore ProtectedBase::typedefs;
%inline %{
class Klass {
std::string name;
public:
Klass(const std::string& n) : name(n) {}
std::string getName() { return name; }
};
class PublicBase {
std::string str;
public:
enum AnEnum { EnumVal1, EnumVal2 };
public:
PublicBase(const char* s): str(s) {}
virtual ~PublicBase() { }
virtual std::string virtualMethod() const { return "PublicBase"; }
Klass instanceMethod(Klass k) const { return k; }
static Klass staticMethod(Klass k) { return k; }
int instanceMemberVariable;
static int staticMemberVariable;
static const int staticConstMemberVariable = 20;
AnEnum anEnum;
};
int PublicBase::staticMemberVariable = 10;
class ProtectedBase {
std::string str;
public:
enum AnEnum { EnumVal1, EnumVal2 };
std::string getName() { return str; }
protected:
ProtectedBase(const char* s): str(s) {}
virtual ~ProtectedBase() { }
virtual std::string virtualMethod() const { return "ProtectedBase"; }
Klass instanceMethod(Klass k) const { return k; }
static Klass staticMethod(Klass k) { return k; }
int instanceMemberVariable;
static int staticMemberVariable;
static const int staticConstMemberVariable = 20;
AnEnum anEnum;
// unsupported: types defined with protected access and thus methods/variables which use them
enum ProtectedEnum { ProtEnumVal1, ProtEnumVal2 };
typedef int IntegerType;
ProtectedEnum protectedenum;
IntegerType typedefs(IntegerType it) { return it; }
};
int ProtectedBase::staticMemberVariable = 10;
%}

View file

@ -74,6 +74,7 @@ CPP_TEST_CASES += \
add_link \
aggregate \
allowexcept \
allprotected \
anonymous_bitfield \
apply_signed_char \
apply_strings \

View file

@ -0,0 +1,55 @@
using System;
using allprotectedNamespace;
public class runme
{
static void Main()
{
runme r = new runme();
r.run();
}
void run()
{
MyProtectedBase mpb = new MyProtectedBase("MyProtectedBase");
mpb.accessProtected();
}
}
class MyProtectedBase : ProtectedBase
{
public MyProtectedBase(string name) : base(name) {
}
public void accessProtected() {
string s = virtualMethod();
if (s != "ProtectedBase")
throw new Exception("Failed");
Klass k = instanceMethod(new Klass("xyz"));
if (k.getName() != "xyz")
throw new Exception("Failed");
k = staticMethod(new Klass("abc"));
if (k.getName() != "abc")
throw new Exception("Failed");
instanceMemberVariable = 30;
int i = instanceMemberVariable;
if (i != 30)
throw new Exception("Failed");
staticMemberVariable = 40;
i = staticMemberVariable;
if (i != 40)
throw new Exception("Failed");
i = staticConstMemberVariable;
if (i != 20)
throw new Exception("Failed");
anEnum = ProtectedBase.AnEnum.EnumVal1;
ProtectedBase.AnEnum ae = anEnum;
if (ae != ProtectedBase.AnEnum.EnumVal1)
throw new Exception("Failed");
}
}

View file

@ -0,0 +1,58 @@
import allprotected.*;
public class allprotected_runme {
static {
try {
System.loadLibrary("allprotected");
} 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[])
{
MyProtectedBase mpb = new MyProtectedBase("MyProtectedBase");
mpb.accessProtected();
}
}
class MyProtectedBase extends ProtectedBase
{
MyProtectedBase(String name) {
super(name);
}
void accessProtected() {
String s = virtualMethod();
if (!s.equals("ProtectedBase"))
throw new RuntimeException("Failed");
Klass k = instanceMethod(new Klass("xyz"));
if (!k.getName().equals("xyz"))
throw new RuntimeException("Failed");
k = staticMethod(new Klass("abc"));
if (!k.getName().equals("abc"))
throw new RuntimeException("Failed");
setInstanceMemberVariable(30);
int i = getInstanceMemberVariable();
if (i != 30)
throw new RuntimeException("Failed");
setStaticMemberVariable(40);
i = getStaticMemberVariable();
if (i != 40)
throw new RuntimeException("Failed");
i = staticConstMemberVariable;
if (i != 20)
throw new RuntimeException("Failed");
setAnEnum(ProtectedBase.AnEnum.EnumVal1);
ProtectedBase.AnEnum ae = getAnEnum();
if (ae != ProtectedBase.AnEnum.EnumVal1)
throw new RuntimeException("Failed");
}
}