(method, typelist) special variable macro fixed/enhanced and made official

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11470 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2009-07-29 20:50:39 +00:00
commit bf0ee4471c
17 changed files with 532 additions and 161 deletions

View file

@ -292,6 +292,7 @@ CPP_TEST_CASES += \
smart_pointer_templatevariables \
smart_pointer_typedef \
special_variables \
special_variable_functions \
static_array_member \
static_const_member \
static_const_member_2 \

View file

@ -0,0 +1,18 @@
using System;
using special_variable_functionsNamespace;
public class runme {
static void Main() {
Name name = new Name();
if (special_variable_functions.testFred(name) != "none")
throw new Exception("test failed");
if (special_variable_functions.testJack(name) != "$specialname")
throw new Exception("test failed");
if (special_variable_functions.testJill(name) != "jilly")
throw new Exception("test failed");
if (special_variable_functions.testMary(name) != "SWIGTYPE_p_NameWrap")
throw new Exception("test failed");
NewName newName = NewName.factory("factoryname");
name = newName.getStoredName();
}
}

View file

@ -25,7 +25,10 @@
"$csclassname.getCPtr(d$csinput)"
// post only in csin typemap
%typemap(csin, post=" int size = $csinput.Count;\n for (int i=0; i<size; ++i) {\n $csinput[i] /= 100;\n }") std::vector<double> &vpost
%typemap(csin, post=" int size = $csinput.Count;\n"
" for (int i=0; i<size; ++i) {\n"
" $csinput[i] /= 100;\n"
" }") std::vector<double> &vpost
"$csclassname.getCPtr($csinput)"
%inline %{

View file

@ -0,0 +1,28 @@
import special_variable_functions.*;
public class special_variable_functions_runme {
static {
try {
System.loadLibrary("special_variable_functions");
} 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[]) {
Name name = new Name();
if (!special_variable_functions.testFred(name).equals("none"))
throw new RuntimeException("test failed");
if (!special_variable_functions.testJack(name).equals("$specialname"))
throw new RuntimeException("test failed");
if (!special_variable_functions.testJill(name).equals("jilly"))
throw new RuntimeException("test failed");
if (!special_variable_functions.testMary(name).equals("SWIGTYPE_p_NameWrap"))
throw new RuntimeException("test failed");
NewName newName = NewName.factory("factoryname");
name = newName.getStoredName();
}
}

View file

@ -0,0 +1,12 @@
import special_variable_functions
name = special_variable_functions.Name()
if special_variable_functions.testFred(name) != "none":
raise "test failed"
if special_variable_functions.testJack(name) != "$specialname":
raise "test failed"
if special_variable_functions.testJill(name) != "jilly":
raise "test failed"
if special_variable_functions.testMary(name) != "SWIGTYPE_p_NameWrap":
raise "test failed"

View file

@ -0,0 +1,121 @@
%module special_variable_functions
// test $typemap() special variable function
// these tests are not typical of how $typemap() should be used, but it checks that it is mostly working
%inline %{
struct Name {
Name(const char *n="none") : name(n) {}
const char *getName() const { return name; };
Name *getNamePtr() { return this; };
private:
const char *name;
};
struct NameWrap {
NameWrap(const char *n="casternone") : name(n) {}
Name *getNamePtr() { return &name; };
private:
Name name;
};
%}
// check $1 and $input get expanded properly when used from $typemap()
%typemap(in) Name *GENERIC ($*1_type temp)
%{
/*%typemap(in) Name *GENERIC start */
temp = Name("$specialname");
(void)$input;
$1 = ($1_ltype) &temp;
/*%typemap(in) Name *GENERIC end */
%}
// This would never be done in real code, it is just a test of what madness can be done.
// Note that the special variable substitutions $*1_type, $descriptor etc are for NameWrap
// even when used within the Name typemap via $typemap. I can't think of any useful use cases
// for this behaviour in the C/C++ typemaps, but it is possible.
%typemap(in) NameWrap *NAMEWRAP ($*1_type temp)
%{
/*%typemap(in) NameWrap *NAMEWRAP start */
temp = $*1_ltype("$descriptor");
(void)$input;
$1 = temp.getNamePtr();
/*%typemap(in) NameWrap *NAMEWRAP end */
%}
//////////////////////////////////////////////////////////////////////////////////////
// This should use Name *GENERIC typemap which ignores passed in Name * and instead uses a newly a newly constructed Name
// held in a typemap variable with name="$specialname"
%typemap(in) Name *jack {
// %typemap(in) Name *jack start
$typemap(in, Name *GENERIC)
// %typemap(in) Name *jack end
}
// as above, but also perform variable substitution
%typemap(in) Name *jill {
// %typemap(in) Name *jill start
$typemap(in, Name *GENERIC, specialname=jilly)
// %typemap(in) Name *jill end
}
%typemap(in) Name *mary {
// %typemap(in) Name *mary start
$typemap(in, NameWrap *NAMEWRAP)
// %typemap(in) Name *mary end
}
%inline %{
const char * testFred(Name *fred) {
return fred->getName();
}
const char * testJack(Name *jack) {
return jack->getName();
}
const char * testJill(Name *jill) {
return jill->getName();
}
const char * testMary(Name *mary) {
return mary->getName();
}
%}
//////////////////////////////////////////////////////////////////////////////////////
// Multi-arg typemap lookup
#warning TODO!!!
//////////////////////////////////////////////////////////////////////////////////////
// A real use case for $typemap
#if defined(SWIGCSHARP)
%typemap(cscode) Space::RenameMe %{
public static NewName factory(String s) {
//below should expand to:
//return new NewName( new Name(s) );
return new $typemap(cstype, Space::RenameMe)( new $typemap(cstype, Name)(s) );
}
%}
#elif defined(SWIGJAVA)
%typemap(javacode) Space::RenameMe %{
public static NewName factory(String s) {
//below should expand to:
//return new NewName( new Name(s) );
return new $typemap(jstype, Space::RenameMe)( new $typemap(jstype, Name)(s) );
}
%}
#endif
%rename(NewName) Space::RenameMe;
%inline %{
namespace Space {
struct RenameMe {
RenameMe(Name n) : storedName(n) {}
Name getStoredName() { return storedName; }
private:
Name storedName;
};
}
%}