Add support for "[not]regexmatch" and "regextarget" to %rename.

"regexmatch" and "notregexmatch" can be used with anonymous %renames in the
same way as "match" and "notmatch" while "regextarget" specifies that the
argument of a non-anonymous %rename should be interpreted as a regular
expression.

Document the new functions.

Also add a new unit test for %regex also testing regexmatch &c and provide
test code for C# and Java verifying that it works as intended.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12174 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Vadim Zeitlin 2010-07-22 17:02:10 +00:00
commit 70c5bb5a47
7 changed files with 216 additions and 8 deletions

View file

@ -285,6 +285,7 @@ CPP_TEST_CASES += \
rename_scope \
rename_strip_encoder \
rename_pcre_encoder \
rename_pcre_enum \
restrict_cplusplus \
return_const_value \
return_value_scope \

View file

@ -0,0 +1,24 @@
using System;
using rename_pcre_enumNamespace;
public class runme {
static void Main() {
Foo foo = Foo.First;
if ( foo == Foo.Second )
throw new Exception("Enum values should be different");
// Check that Foo_Max enum element was ignored.
int numFooEnumElements = Enum.GetValues(typeof(Foo)).Length;
if ( numFooEnumElements != 2 )
throw new Exception(String.Format("Enum should have 2 elements, not {0}",
numFooEnumElements));
BoundaryCondition bc = BoundaryCondition.MaxMax;
if ( (int)bc != 2 )
throw new Exception("Wrong enum value");
Colour c = Colour.red;
if ( c == Colour.blue )
throw new Exception("Enum values should be different");
}
}

View file

@ -0,0 +1,26 @@
import rename_pcre_enum.*;
public class rename_pcre_enum_runme {
static { System.loadLibrary("rename_pcre_enum"); }
public static void main(String argv[])
{
Foo foo = Foo.First;
if ( foo == Foo.Second )
throw new RuntimeException("Enum values should be different");
// Check that Foo_Max enum element was ignored.
int numFooEnumElements = Foo.values().length;
if ( numFooEnumElements != 2 )
throw new RuntimeException(String.format("Enum should have 2 elements, not %d",
numFooEnumElements));
BoundaryCondition bc = BoundaryCondition.MaxMax;
if ( bc.ordinal() != 2 )
throw new RuntimeException("Wrong enum value");
Colour c = Colour.red;
if ( c == Colour.blue )
throw new RuntimeException("Enum values should be different");
}
}

View file

@ -0,0 +1,49 @@
%module rename_pcre_enum
// This file is needed for proper enum support in C#/Java backends
#if defined(SWIGCSHARP) || defined(SWIGJAVA)
%include "enums.swg"
#endif
// Apply a rule for renaming the enum elements to avoid the common prefixes
// redundant in C#/Java
%rename("%(regex:/([A-Z][a-z]+)+_(.*)/\\2/)s",%$isenumitem) "";
// Also don't export special end of enum markers which are often used in C++
// code to just have a symbolic name for the number of enum elements but are
// not needed in target language.
%rename("$ignore", regexmatch$name="([A-Z][a-z]+)+_Max$",%$isenumitem) "";
// Test another way of doing the same thing with regextarget:
%rename("$ignore", %$isenumitem, regextarget=1) "([A-Z][a-z]+)+_Internal$";
// Apply this renaming rule to all enum elements that don't contain more than
// one capital letter.
%rename("%(lower)s", notregexmatch$name="[A-Z]\\w*[A-Z]", %$isenumitem) "";
%inline %{
// Foo_Internal and Foo_Max won't be exported.
enum Foo {
Foo_Internal = -1,
Foo_First,
Foo_Second,
Foo_Max
};
// All elements of this enum will be exported because they do not match the
// excluding regex.
enum BoundaryCondition {
BoundaryCondition_MinMax,
BoundaryCondition_MaxMin,
BoundaryCondition_MaxMax
};
// The elements of this enum will have lower-case names.
enum Colour {
Red,
Blue,
Green
};
%}