The great merge
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
5fcae5eb66
commit
12a43edc2d
1508 changed files with 125983 additions and 44037 deletions
23
Examples/python/check.list
Normal file
23
Examples/python/check.list
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# see top-level Makefile.in
|
||||
class
|
||||
constants
|
||||
enum
|
||||
exceptshadow
|
||||
funcattr
|
||||
funcptr
|
||||
funcptr2
|
||||
functor
|
||||
import
|
||||
import
|
||||
template
|
||||
mpointer
|
||||
multimap
|
||||
operator
|
||||
pointer
|
||||
reference
|
||||
shadow
|
||||
simple
|
||||
smartptr
|
||||
template
|
||||
value
|
||||
variables
|
||||
9
Examples/python/class/.cvsignore
Normal file
9
Examples/python/class/.cvsignore
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -14,6 +14,7 @@ static::
|
|||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
|
|
@ -11,18 +11,18 @@ void Shape::move(double dx, double dy) {
|
|||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area() {
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter() {
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area() {
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter() {
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
|
|
|||
152
Examples/python/class/example.dsp
Normal file
152
Examples/python/class/example.dsp
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
# Microsoft Developer Studio Project File - Name="example" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=example - Win32 Release
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "example.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "example.mak" CFG="example - Win32 Release"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "example - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "example - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "example - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "$(PYTHON_INCLUDE)" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "_DEBUG"
|
||||
# ADD RSC /l 0x809 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib "$(PYTHON_LIB)" /nologo /dll /debug /machine:I386 /out:"_example.dll" /pdbtype:sept
|
||||
|
||||
!ELSEIF "$(CFG)" == "example - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
|
||||
# ADD CPP /nologo /MT /W3 /GX /O2 /I "$(PYTHON_INCLUDE)" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "NDEBUG"
|
||||
# ADD RSC /l 0x809 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib "$(PYTHON_LIB)" /nologo /dll /machine:I386 /out:"_example.dll"
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "example - Win32 Debug"
|
||||
# Name "example - Win32 Release"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.cxx
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example_wrap.cxx
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.h
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.i
|
||||
|
||||
!IF "$(CFG)" == "example - Win32 Debug"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\example.i
|
||||
InputName=example
|
||||
|
||||
"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
echo In order to function correctly, please ensure the following environment variables are correctly set:
|
||||
echo PYTHON_INCLUDE: %PYTHON_INCLUDE%
|
||||
echo PYTHON_LIB: %PYTHON_LIB%
|
||||
echo on
|
||||
..\..\..\swig -c++ -python $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ELSEIF "$(CFG)" == "example - Win32 Release"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\example.i
|
||||
InputName=example
|
||||
|
||||
"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
echo In order to function correctly, please ensure the following environment variables are correctly set:
|
||||
echo PYTHON_INCLUDE: %PYTHON_INCLUDE%
|
||||
echo PYTHON_LIB: %PYTHON_LIB%
|
||||
echo on
|
||||
..\..\..\swig -c++ -python $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# End Target
|
||||
# End Project
|
||||
|
|
@ -10,8 +10,8 @@ public:
|
|||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area() = 0;
|
||||
virtual double perimeter() = 0;
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
|
|
@ -20,17 +20,17 @@ private:
|
|||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area();
|
||||
virtual double perimeter();
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area();
|
||||
virtual double perimeter();
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,67 +0,0 @@
|
|||
# file: example.py
|
||||
|
||||
# This file illustrates the low-level C++ interface
|
||||
# created by SWIG. In this case, all of our C++ classes
|
||||
# get converted into function calls.
|
||||
|
||||
import example
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:"
|
||||
c = example.new_Circle(10)
|
||||
print " Created circle", c
|
||||
s = example.new_Square(10)
|
||||
print " Created square", s
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
print "\nA total of", example.cvar.Shape_nshapes,"shapes were created"
|
||||
|
||||
# ----- Member data access -----
|
||||
|
||||
# Set the location of the object
|
||||
|
||||
|
||||
example.Shape_x_set(c, 20)
|
||||
example.Shape_y_set(c, 30)
|
||||
example.Shape_x_set(s,-10)
|
||||
example.Shape_y_set(s,5)
|
||||
|
||||
print "\nHere is their current position:"
|
||||
print " Circle = (%f, %f)" % (example.Shape_x_get(c), example.Shape_y_get(c))
|
||||
print " Square = (%f, %f)" % (example.Shape_x_get(s), example.Shape_y_get(s))
|
||||
|
||||
# ----- Call some methods -----
|
||||
|
||||
print "\nHere are some properties of the shapes:"
|
||||
for o in [c,s]:
|
||||
print " ", o
|
||||
print " area = ", example.Shape_area(o)
|
||||
print " perimeter = ", example.Shape_perimeter(o)
|
||||
|
||||
# Notice how the Shape_area() and Shape_perimeter() functions really
|
||||
# invoke the appropriate virtual method on each object.
|
||||
|
||||
# ----- Try to cause a type error -----
|
||||
|
||||
print "\nI'm going to try and break the type system"
|
||||
try:
|
||||
# Bad script!
|
||||
Square_area(c) # Try to invoke Square method on a Circle
|
||||
print " Bad bad SWIG!"
|
||||
except:
|
||||
print " Well, it didn't work. Good SWIG."
|
||||
|
||||
|
||||
# ----- Delete everything -----
|
||||
|
||||
print "\nGuess I'll clean up now"
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
example.delete_Shape(c)
|
||||
example.delete_Shape(s)
|
||||
|
||||
print example.cvar.Shape_nshapes,"shapes remain"
|
||||
print "Goodbye"
|
||||
|
||||
|
|
@ -109,14 +109,14 @@ For example:
|
|||
|
||||
<blockquote>
|
||||
<pre>
|
||||
example.Circle_x_set(c,15) # Set member data
|
||||
example.Shape_x_set(c,15) # Set member data
|
||||
x = example.Shape_x_get(c) # Get member data
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Note: when accessing member data, the name of the base class or the derived class can be
|
||||
used in the function name as shown above. Of course, it would probably be more
|
||||
proper to just use the base class version such as <tt>Shape_x_get()</tt>
|
||||
Note: when accessing member data, the name of the class in which
|
||||
the member data was must be used. In this case, <tt>Shape_x_get()</tt>
|
||||
and <tt>Shape_x_set()</tt> are used since 'x' was defined in Shape.
|
||||
|
||||
<p>
|
||||
<li>To invoke a member function, you simply do this
|
||||
|
|
@ -164,13 +164,14 @@ example.cvar.Shapes_nshapes = 13 # Set a static data member
|
|||
<h2>General Comments</h2>
|
||||
|
||||
<ul>
|
||||
<li>This low-level interface is not the only way to handle C++ code. Shadow classes
|
||||
provide a much higher-level interface.
|
||||
<li>This low-level interface is not the only way to handle C++ code.
|
||||
Shadow classes provide a much higher-level interface.
|
||||
|
||||
<p>
|
||||
<li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
|
||||
hierarchy (including multiple inheritance). Therefore it is perfectly safe to pass
|
||||
an object of a derived class to any function involving a base class.
|
||||
<li>SWIG *does* know how to properly perform upcasting of objects in
|
||||
an inheritance hierarchy (including multiple inheritance). Therefore
|
||||
it is perfectly safe to pass an object of a derived class to any
|
||||
function involving a base class.
|
||||
|
||||
<p>
|
||||
<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
|
||||
|
|
@ -206,28 +207,6 @@ to write a helper function. For example:
|
|||
<p>
|
||||
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
|
||||
|
||||
<p>
|
||||
<li>Templates. Not supported at all. SWIG throws out anything that looks like a template.
|
||||
You can work around the problem by aliasing a template class behind a typedef however.
|
||||
For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%{
|
||||
typedef vector<int> IntVector;
|
||||
%}
|
||||
|
||||
class IntVector {
|
||||
public:
|
||||
... methods ...
|
||||
};
|
||||
</pre>
|
||||
</blockquote>
|
||||
</ul>
|
||||
<p>
|
||||
<li>There is no guarantee that an extremely complex C++ application will be able to compile
|
||||
as a Python extension. Sorry.
|
||||
|
||||
<p>
|
||||
<li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to
|
||||
use C++ in moderation.
|
||||
|
|
|
|||
51
Examples/python/class/runme.py
Normal file
51
Examples/python/class/runme.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
# file: runme.py
|
||||
|
||||
# This file illustrates the shadow-class C++ interface generated
|
||||
# by SWIG.
|
||||
|
||||
import example
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:"
|
||||
c = example.Circle(10)
|
||||
print " Created circle", c
|
||||
s = example.Square(10)
|
||||
print " Created square", s
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
print "\nA total of", example.cvar.Shape_nshapes,"shapes were created"
|
||||
|
||||
# ----- Member data access -----
|
||||
|
||||
# Set the location of the object
|
||||
|
||||
c.x = 20
|
||||
c.y = 30
|
||||
|
||||
s.x = -10
|
||||
s.y = 5
|
||||
|
||||
print "\nHere is their current position:"
|
||||
print " Circle = (%f, %f)" % (c.x,c.y)
|
||||
print " Square = (%f, %f)" % (s.x,s.y)
|
||||
|
||||
# ----- Call some methods -----
|
||||
|
||||
print "\nHere are some properties of the shapes:"
|
||||
for o in [c,s]:
|
||||
print " ", o
|
||||
print " area = ", o.area()
|
||||
print " perimeter = ", o.perimeter()
|
||||
|
||||
print "\nGuess I'll clean up now"
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
del c
|
||||
del s
|
||||
|
||||
s = 3
|
||||
print example.cvar.Shape_nshapes,"shapes remain"
|
||||
print "Goodbye"
|
||||
|
||||
9
Examples/python/constants/.cvsignore
Normal file
9
Examples/python/constants/.cvsignore
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -13,6 +13,7 @@ static::
|
|||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -19,19 +19,9 @@
|
|||
/* Neither should this (BAR isn't defined) */
|
||||
#define FOO (ICONST + BAR)
|
||||
|
||||
/* The following statements also produce constants */
|
||||
const int iconst = 37;
|
||||
const double fconst = 3.14;
|
||||
|
||||
%constant(double) PI = 3.14159;
|
||||
|
||||
/* Test address of operator */
|
||||
|
||||
%{
|
||||
static double blah = 1.2345;
|
||||
%}
|
||||
|
||||
%constant(double *) blahptr = &blah;
|
||||
/* The following directives also produce constants */
|
||||
|
||||
%constant int iconst = 37;
|
||||
%constant double fconst = 3.14;
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ print "SCONST2 =", example.SCONST2, "(should be '\"Hello World\"')"
|
|||
print "EXPR =", example.EXPR, "(should be 48.5484)"
|
||||
print "iconst =", example.iconst, "(should be 37)"
|
||||
print "fconst =", example.fconst, "(should be 3.14)"
|
||||
print "PI =", example.PI, "(should be 3.14159)"
|
||||
|
||||
try:
|
||||
print "EXTERN = ", example.EXTERN, "(Arg! This shouldn't print anything)"
|
||||
except AttributeError:
|
||||
9
Examples/python/enum/.cvsignore
Normal file
9
Examples/python/enum/.cvsignore
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -14,6 +14,7 @@ static::
|
|||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
# file: example.py
|
||||
|
||||
import example
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
# Print out the value of some enums
|
||||
print "*** color ***"
|
||||
print " RED =", example.RED
|
||||
print " BLUE =", example.BLUE
|
||||
print " GREEN =", example.GREEN
|
||||
|
||||
print "\n*** Foo::speed ***"
|
||||
print " Foo_IMPULSE =", example.Foo_IMPULSE
|
||||
print " Foo_WARP =", example.Foo_WARP
|
||||
print " Foo_LUDICROUS =", example.Foo_LUDICROUS
|
||||
|
||||
print "\nTesting use of enums with functions\n"
|
||||
|
||||
example.enum_test(example.RED, example.Foo_IMPULSE)
|
||||
example.enum_test(example.BLUE, example.Foo_WARP)
|
||||
example.enum_test(example.GREEN, example.Foo_LUDICROUS)
|
||||
example.enum_test(1234,5678)
|
||||
|
||||
print "\nTesting use of enum with class method"
|
||||
f = example.new_Foo()
|
||||
|
||||
example.Foo_enum_test(f,example.Foo_IMPULSE)
|
||||
example.Foo_enum_test(f,example.Foo_WARP)
|
||||
example.Foo_enum_test(f,example.Foo_LUDICROUS)
|
||||
31
Examples/python/enum/runme.py
Normal file
31
Examples/python/enum/runme.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# file: example.py
|
||||
|
||||
import example
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
# Print out the value of some enums
|
||||
print "*** color ***"
|
||||
print " RED =", example.RED
|
||||
print " BLUE =", example.BLUE
|
||||
print " GREEN =", example.GREEN
|
||||
|
||||
print "\n*** Foo::speed ***"
|
||||
print " Foo_IMPULSE =", example.Foo.IMPULSE
|
||||
print " Foo_WARP =", example.Foo.WARP
|
||||
print " Foo_LUDICROUS =", example.Foo.LUDICROUS
|
||||
|
||||
print "\nTesting use of enums with functions\n"
|
||||
|
||||
example.enum_test(example.RED, example.Foo.IMPULSE)
|
||||
example.enum_test(example.BLUE, example.Foo.WARP)
|
||||
example.enum_test(example.GREEN, example.Foo.LUDICROUS)
|
||||
example.enum_test(1234,5678)
|
||||
|
||||
print "\nTesting use of enum with class method"
|
||||
f = example.Foo()
|
||||
|
||||
f.enum_test(example.Foo.IMPULSE)
|
||||
f.enum_test(example.Foo.WARP)
|
||||
f.enum_test(example.Foo.LUDICROUS)
|
||||
|
||||
20
Examples/python/exception/Makefile
Normal file
20
Examples/python/exception/Makefile
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
37
Examples/python/exception/example.h
Normal file
37
Examples/python/exception/example.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/* File : example.h */
|
||||
|
||||
#include <string>
|
||||
|
||||
class Exc {
|
||||
public:
|
||||
Exc(int c, const char *m) {
|
||||
code = c;
|
||||
strncpy(msg,m,256);
|
||||
}
|
||||
int code;
|
||||
char msg[256];
|
||||
};
|
||||
|
||||
class Test {
|
||||
public:
|
||||
int simple() throw(int) {
|
||||
throw(37);
|
||||
}
|
||||
int message() throw(const char *) {
|
||||
throw("I died.");
|
||||
}
|
||||
int hosed() throw(Exc) {
|
||||
throw(Exc(42,"Hosed"));
|
||||
}
|
||||
int multi(int x) throw(int, const char *, Exc) {
|
||||
if (x == 1) throw(37);
|
||||
if (x == 2) throw("Bleah!");
|
||||
if (x == 3) throw(Exc(42,"No-go-diggy-die"));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
12
Examples/python/exception/example.i
Normal file
12
Examples/python/exception/example.i
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "std_string.i"
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
29
Examples/python/exception/runme.py
Normal file
29
Examples/python/exception/runme.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# file: runme.py
|
||||
|
||||
# Throw a lot of exceptions
|
||||
|
||||
import example
|
||||
|
||||
t = example.Test()
|
||||
try:
|
||||
t.simple()
|
||||
except RuntimeError,e:
|
||||
print e.args[0]
|
||||
|
||||
try:
|
||||
t.message()
|
||||
except RuntimeError,e:
|
||||
print e.args[0]
|
||||
|
||||
try:
|
||||
t.hosed()
|
||||
except example.Exc,e:
|
||||
print e.code, e.msg
|
||||
|
||||
for i in range(1,4):
|
||||
try:
|
||||
t.multi(i)
|
||||
except RuntimeError,e:
|
||||
print e.args[0]
|
||||
except example.Exc,e:
|
||||
print e.code, e.msg
|
||||
10
Examples/python/exceptshadow/.cvsignore
Normal file
10
Examples/python/exceptshadow/.cvsignore
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
example.py
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
21
Examples/python/exceptshadow/Makefile
Normal file
21
Examples/python/exceptshadow/Makefile
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
51
Examples/python/exceptshadow/example.h
Normal file
51
Examples/python/exceptshadow/example.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* File : example.h */
|
||||
|
||||
// A simple exception
|
||||
class EmptyError { };
|
||||
class FullError {
|
||||
public:
|
||||
int maxsize;
|
||||
FullError(int m) : maxsize(m) { }
|
||||
};
|
||||
|
||||
template<typename T> class Queue {
|
||||
int maxsize;
|
||||
T *items;
|
||||
int nitems;
|
||||
int last;
|
||||
public:
|
||||
Queue(int size) {
|
||||
maxsize = size;
|
||||
items = new T[size];
|
||||
nitems = 0;
|
||||
last = 0;
|
||||
}
|
||||
~Queue() {
|
||||
delete [] items;
|
||||
}
|
||||
void enqueue(T x) throw(FullError) {
|
||||
if (nitems == maxsize) {
|
||||
throw FullError(maxsize);
|
||||
}
|
||||
items[last] = x;
|
||||
last = (last + 1) % maxsize;
|
||||
nitems++;
|
||||
}
|
||||
T dequeue() throw(EmptyError) {
|
||||
T x;
|
||||
if (nitems == 0) throw EmptyError();
|
||||
x = items[(last + maxsize - nitems) % maxsize];
|
||||
nitems--;
|
||||
return x;
|
||||
}
|
||||
int length() {
|
||||
return nitems;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
83
Examples/python/exceptshadow/example.i
Normal file
83
Examples/python/exceptshadow/example.i
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/* This is a rather sophisticated example that illustrates exception handling,
|
||||
templates, and shadow classes.
|
||||
|
||||
(i) The %exception directive is used to attach exception handlers
|
||||
to specific methods.
|
||||
|
||||
(ii) Exception classes are automatically converted to shadow class
|
||||
objects.
|
||||
|
||||
(iii) The %template directive is used to expand the templates
|
||||
*/
|
||||
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Define some exception handlers for specific methods. In
|
||||
the header file, the enqueue method throws FullError and
|
||||
the dequeue method throws EmptyError. Since we don't
|
||||
want to define an exception handler for everything, we
|
||||
simply write a handler each method individually.
|
||||
|
||||
Note: the *::enqueue syntax means that we simply define
|
||||
the handler for any class with this method defined.
|
||||
*/
|
||||
|
||||
%exception *::enqueue {
|
||||
try {
|
||||
$action
|
||||
} catch(FullError e) {
|
||||
FullError *ecopy = new FullError(e);
|
||||
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_FullError, 1);
|
||||
PyErr_SetObject((PyObject *) SWIGTYPE_p_FullError->clientdata, err);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Some notes about the code above:
|
||||
|
||||
(0) $action gets replaced with the actual method call.
|
||||
|
||||
(1) We are going to return a copy of the exception object (FullError)
|
||||
to pass back to the Python interpreter. This is why the copy
|
||||
constructor is being called.
|
||||
|
||||
(2) The SWIG_NewPointerObj() call automatically wraps the exception object
|
||||
into a shadow class. The SWIGTYPE_p_FullError is the type-descriptor
|
||||
used for type checking. The "1" indicates that Python will have
|
||||
ownership of the resulting object.
|
||||
|
||||
(3) The PyErr_SetObject call sets the Python exception. However,
|
||||
the SWIGTYPE_p_FullError->clientdata reference may not be
|
||||
obvious. This is actually the Python shadow class object
|
||||
for FullError. Recall that in Python, exceptions are defined
|
||||
as classes. Therefore, this works perfectly as the argument to
|
||||
PyErr_SetObject()! A neat trick perhaps.
|
||||
*/
|
||||
|
||||
%exception *::dequeue {
|
||||
try {
|
||||
$action
|
||||
} catch(EmptyError e) {
|
||||
EmptyError *ecopy = new EmptyError(e);
|
||||
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_EmptyError, 1);
|
||||
PyErr_SetObject((PyObject *) SWIGTYPE_p_EmptyError->clientdata, err);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Grab the original header file */
|
||||
%include "example.h"
|
||||
|
||||
/* Instantiate a few templates */
|
||||
|
||||
%template(intQueue) Queue<int>;
|
||||
%template(doubleQueue) Queue<double>;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
45
Examples/python/exceptshadow/runme.py
Normal file
45
Examples/python/exceptshadow/runme.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# file: runme.py
|
||||
import example
|
||||
|
||||
q = example.intQueue(10)
|
||||
|
||||
print "Inserting items into intQueue"
|
||||
|
||||
try:
|
||||
for i in range(0,100):
|
||||
q.enqueue(i)
|
||||
except example.FullError,e:
|
||||
print "Maxsize is", e.maxsize
|
||||
|
||||
print "Removing items"
|
||||
|
||||
try:
|
||||
while 1:
|
||||
q.dequeue()
|
||||
except example.EmptyError,e:
|
||||
pass
|
||||
|
||||
|
||||
q = example.doubleQueue(1000)
|
||||
|
||||
print "Inserting items into doubleQueue"
|
||||
|
||||
try:
|
||||
for i in range(0,10000):
|
||||
q.enqueue(i*1.5)
|
||||
except example.FullError,e:
|
||||
print "Maxsize is", e.maxsize
|
||||
|
||||
print "Removing items"
|
||||
|
||||
try:
|
||||
while 1:
|
||||
q.dequeue()
|
||||
except example.EmptyError,e:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
10
Examples/python/funcattr/.cvsignore
Normal file
10
Examples/python/funcattr/.cvsignore
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
example.py
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
21
Examples/python/funcattr/Makefile
Normal file
21
Examples/python/funcattr/Makefile
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
17
Examples/python/funcattr/example.i
Normal file
17
Examples/python/funcattr/example.i
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* Turn all data attributes into a pair of accessor functions.
|
||||
In this case, a data attribute foo is accessed using
|
||||
foo() and changed using set_foo(). The format strings
|
||||
in %attributefunc() can be used to precisely determine
|
||||
the format of the get/set functions */
|
||||
|
||||
%attributefunc(%s,set_%s)
|
||||
|
||||
%inline %{
|
||||
class Vector {
|
||||
public:
|
||||
double x,y,z;
|
||||
};
|
||||
%}
|
||||
22
Examples/python/funcattr/index.html
Normal file
22
Examples/python/funcattr/index.html
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:python:funcattr</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/python/funcattr/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Turning Attributes into Functions</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example shows how you can turn data attributes into member functions.
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
17
Examples/python/funcattr/runme.py
Normal file
17
Examples/python/funcattr/runme.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# file: runme.py
|
||||
|
||||
import example
|
||||
|
||||
# Create an object
|
||||
v = example.Vector()
|
||||
|
||||
print "Setting some values"
|
||||
# Set some values (using functions instead of attributes)
|
||||
v.set_x(3.5)
|
||||
v.set_y(4.0)
|
||||
v.set_z(10.5)
|
||||
|
||||
# Get some values
|
||||
print v
|
||||
print v.x(), v.y(), v.z()
|
||||
|
||||
9
Examples/python/funcptr/.cvsignore
Normal file
9
Examples/python/funcptr/.cvsignore
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -13,6 +13,7 @@ static::
|
|||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -15,3 +15,5 @@ int sub(int a, int b) {
|
|||
int mul(int a, int b) {
|
||||
return a*b;
|
||||
}
|
||||
|
||||
int (*funcvar)(int,int) = add;
|
||||
|
|
|
|||
|
|
@ -8,8 +8,9 @@
|
|||
extern int do_op(int a, int b, int (*op)(int, int));
|
||||
|
||||
/* Now install a bunch of "ops" as constants */
|
||||
%constant(int (*)(int,int)) ADD = add;
|
||||
%constant(int (*)(int,int)) SUB = sub;
|
||||
%constant(int (*)(int,int)) MUL = mul;
|
||||
%constant int (*ADD)(int,int) = add;
|
||||
%constant int (*SUB)(int,int) = sub;
|
||||
%constant int (*MUL)(int,int) = mul;
|
||||
|
||||
extern int (*funcvar)(int,int);
|
||||
|
||||
|
|
|
|||
9
Examples/python/funcptr2/.cvsignore
Normal file
9
Examples/python/funcptr2/.cvsignore
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
19
Examples/python/funcptr2/Makefile
Normal file
19
Examples/python/funcptr2/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
19
Examples/python/funcptr2/example.c
Normal file
19
Examples/python/funcptr2/example.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* File : example.c */
|
||||
|
||||
int do_op(int a, int b, int (*op)(int,int)) {
|
||||
return (*op)(a,b);
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
int sub(int a, int b) {
|
||||
return a-b;
|
||||
}
|
||||
|
||||
int mul(int a, int b) {
|
||||
return a*b;
|
||||
}
|
||||
|
||||
int (*funcvar)(int,int) = add;
|
||||
7
Examples/python/funcptr2/example.h
Normal file
7
Examples/python/funcptr2/example.h
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/* file: example.h */
|
||||
|
||||
extern int do_op(int,int, int (*op)(int,int));
|
||||
extern int add(int,int);
|
||||
extern int sub(int,int);
|
||||
extern int mul(int,int);
|
||||
|
||||
18
Examples/python/funcptr2/example.i
Normal file
18
Examples/python/funcptr2/example.i
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Wrap a function taking a pointer to a function */
|
||||
extern int do_op(int a, int b, int (*op)(int, int));
|
||||
|
||||
/* Now install a bunch of "ops" as constants */
|
||||
%callback("%(upper)s")
|
||||
int add(int, int);
|
||||
int sub(int, int);
|
||||
int mul(int, int);
|
||||
%nocallback
|
||||
|
||||
extern int (*funcvar)(int,int);
|
||||
|
||||
20
Examples/python/funcptr2/runme.py
Normal file
20
Examples/python/funcptr2/runme.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# file: example.py
|
||||
|
||||
import example
|
||||
|
||||
a = 37
|
||||
b = 42
|
||||
|
||||
# Now call our C function with a bunch of callbacks
|
||||
|
||||
print "Trying some C callback functions"
|
||||
print " a =", a
|
||||
print " b =", b
|
||||
print " ADD(a,b) =", example.do_op(a,b,example.ADD)
|
||||
print " SUB(a,b) =", example.do_op(a,b,example.SUB)
|
||||
print " MUL(a,b) =", example.do_op(a,b,example.MUL)
|
||||
|
||||
print "Here is what the C callback function objects look like in Python"
|
||||
print " ADD =", example.ADD
|
||||
print " SUB =", example.SUB
|
||||
print " MUL =", example.MUL
|
||||
10
Examples/python/functor/.cvsignore
Normal file
10
Examples/python/functor/.cvsignore
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
example.py
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
21
Examples/python/functor/Makefile
Normal file
21
Examples/python/functor/Makefile
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
29
Examples/python/functor/example.i
Normal file
29
Examples/python/functor/example.i
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
|
||||
%inline %{
|
||||
// From B. Strousjoup, "The C++ Programming Language, Third Edition", p. 514
|
||||
template<class T> class Sum {
|
||||
T res;
|
||||
public:
|
||||
Sum(T i = 0) : res(i) { }
|
||||
void operator() (T x) { res += x; }
|
||||
T result() const { return res; }
|
||||
};
|
||||
|
||||
%}
|
||||
|
||||
// Rename the application operator to __call__ for python.
|
||||
// Note: this is normally automatic, but if you had to do it yourself
|
||||
// you would use this directive:
|
||||
//
|
||||
// %rename(__call__) *::operator();
|
||||
|
||||
// Instantiate a few versions
|
||||
%template(intSum) Sum<int>;
|
||||
%template(doubleSum) Sum<double>;
|
||||
|
||||
|
||||
|
||||
|
||||
17
Examples/python/functor/runme.py
Normal file
17
Examples/python/functor/runme.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# Operator overloading example
|
||||
import example
|
||||
import math
|
||||
|
||||
a = example.intSum(0)
|
||||
b = example.doubleSum(100.0)
|
||||
|
||||
# Use the objects. They should be callable just like a normal
|
||||
# python function.
|
||||
|
||||
for i in range(0,100):
|
||||
a(i) # Note: function call
|
||||
b(math.sqrt(i)) # Note: function call
|
||||
|
||||
print a.result()
|
||||
print b.result()
|
||||
|
||||
13
Examples/python/import/.cvsignore
Normal file
13
Examples/python/import/.cvsignore
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
bar.py
|
||||
base.py
|
||||
foo.py
|
||||
spam.py
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
22
Examples/python/import/Makefile
Normal file
22
Examples/python/import/Makefile
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SWIGOPT = -c
|
||||
#If your system requires linking with the runtime libraries then set the directory location here
|
||||
RUNTIMEDIR =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='base' INTERFACE='base.i' python_multi_cpp
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='foo' INTERFACE='foo.i' python_multi_cpp
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='bar' INTERFACE='bar.i' python_multi_cpp
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='spam' INTERFACE='spam.i' python_multi_cpp
|
||||
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
@rm -f foo.py bar.py spam.py base.py
|
||||
|
||||
check: all
|
||||
28
Examples/python/import/README
Normal file
28
Examples/python/import/README
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
This example tests the SWIG run-time libraries and use of the
|
||||
%import directive to work with multiple modules.
|
||||
|
||||
Use 'python runme.py' to run a test.
|
||||
|
||||
Overview:
|
||||
---------
|
||||
|
||||
The example defines 4 different extension modules--each wrapping
|
||||
a separate C++ class.
|
||||
|
||||
base.i - Base class
|
||||
foo.i - Foo class derived from Base
|
||||
bar.i - Bar class derived from Base
|
||||
spam.i - Spam class derived from Bar
|
||||
|
||||
Each module used %import to refer to another module. For
|
||||
example, the 'foo.i' module uses '%import base.i' to get
|
||||
definitions for its base class.
|
||||
|
||||
If everything is working correctly, all of the modules will load
|
||||
correctly and type checking will work correctly. The
|
||||
example requires the use of the SWIG run-time libraries
|
||||
which must be built and properly installed.
|
||||
|
||||
|
||||
|
||||
|
||||
22
Examples/python/import/bar.h
Normal file
22
Examples/python/import/bar.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include "base.h"
|
||||
|
||||
class Bar : public Base {
|
||||
public:
|
||||
Bar() { }
|
||||
~Bar() { }
|
||||
virtual void A() {
|
||||
printf("I'm Bar::A\n");
|
||||
}
|
||||
void B() {
|
||||
printf("I'm Bar::B\n");
|
||||
}
|
||||
virtual Base *toBase() {
|
||||
return static_cast<Base *>(this);
|
||||
}
|
||||
static Bar *fromBase(Base *b) {
|
||||
return dynamic_cast<Bar *>(b);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
9
Examples/python/import/bar.i
Normal file
9
Examples/python/import/bar.i
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
%module bar
|
||||
%{
|
||||
#include "bar.h"
|
||||
%}
|
||||
|
||||
%import base.i
|
||||
%include "bar.h"
|
||||
|
||||
|
||||
18
Examples/python/import/base.h
Normal file
18
Examples/python/import/base.h
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
|
||||
class Base {
|
||||
public:
|
||||
Base() { };
|
||||
~Base() { };
|
||||
virtual void A() {
|
||||
printf("I'm Base::A\n");
|
||||
}
|
||||
void B() {
|
||||
printf("I'm Base::B\n");
|
||||
}
|
||||
virtual Base *toBase() {
|
||||
return static_cast<Base *>(this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
6
Examples/python/import/base.i
Normal file
6
Examples/python/import/base.i
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
%module base
|
||||
%{
|
||||
#include "base.h"
|
||||
%}
|
||||
|
||||
%include base.h
|
||||
21
Examples/python/import/foo.h
Normal file
21
Examples/python/import/foo.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include "base.h"
|
||||
|
||||
class Foo : public Base {
|
||||
public:
|
||||
Foo() { }
|
||||
~Foo() { }
|
||||
virtual void A() {
|
||||
printf("I'm Foo::A\n");
|
||||
}
|
||||
void B() {
|
||||
printf("I'm Foo::B\n");
|
||||
}
|
||||
virtual Base *toBase() {
|
||||
return static_cast<Base *>(this);
|
||||
}
|
||||
static Foo *fromBase(Base *b) {
|
||||
return dynamic_cast<Foo *>(b);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
8
Examples/python/import/foo.i
Normal file
8
Examples/python/import/foo.i
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
%module foo
|
||||
%{
|
||||
#include "foo.h"
|
||||
%}
|
||||
|
||||
%import base.i
|
||||
%include "foo.h"
|
||||
|
||||
111
Examples/python/import/runme.py
Normal file
111
Examples/python/import/runme.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
# file: runme.py
|
||||
# Test various properties of classes defined in separate modules
|
||||
|
||||
print "Testing the %import directive"
|
||||
import base
|
||||
import foo
|
||||
import bar
|
||||
import spam
|
||||
|
||||
# Create some objects
|
||||
|
||||
print "Creating some objects"
|
||||
|
||||
a = base.Base()
|
||||
b = foo.Foo()
|
||||
c = bar.Bar()
|
||||
d = spam.Spam()
|
||||
|
||||
# Try calling some methods
|
||||
print "Testing some methods"
|
||||
print "",
|
||||
print "Should see 'Base::A' ---> ",
|
||||
a.A()
|
||||
print "Should see 'Base::B' ---> ",
|
||||
a.B()
|
||||
|
||||
print "Should see 'Foo::A' ---> ",
|
||||
b.A()
|
||||
print "Should see 'Foo::B' ---> ",
|
||||
b.B()
|
||||
|
||||
print "Should see 'Bar::A' ---> ",
|
||||
c.A()
|
||||
print "Should see 'Bar::B' ---> ",
|
||||
c.B()
|
||||
|
||||
print "Should see 'Spam::A' ---> ",
|
||||
d.A()
|
||||
print "Should see 'Spam::B' ---> ",
|
||||
d.B()
|
||||
|
||||
# Try some casts
|
||||
|
||||
print "\nTesting some casts\n"
|
||||
print "",
|
||||
|
||||
x = a.toBase()
|
||||
print "Should see 'Base::A' ---> ",
|
||||
x.A()
|
||||
print "Should see 'Base::B' ---> ",
|
||||
x.B()
|
||||
|
||||
x = b.toBase()
|
||||
print "Should see 'Foo::A' ---> ",
|
||||
x.A()
|
||||
|
||||
print "Should see 'Base::B' ---> ",
|
||||
x.B()
|
||||
|
||||
x = c.toBase()
|
||||
print "Should see 'Bar::A' ---> ",
|
||||
x.A()
|
||||
|
||||
print "Should see 'Base::B' ---> ",
|
||||
x.B()
|
||||
|
||||
x = d.toBase()
|
||||
print "Should see 'Spam::A' ---> ",
|
||||
x.A()
|
||||
|
||||
print "Should see 'Base::B' ---> ",
|
||||
x.B()
|
||||
|
||||
x = d.toBar()
|
||||
print "Should see 'Bar::B' ---> ",
|
||||
x.B()
|
||||
|
||||
print "\nTesting some dynamic casts\n"
|
||||
x = d.toBase()
|
||||
|
||||
print " Spam -> Base -> Foo : ",
|
||||
y = foo.Foo_fromBase(x)
|
||||
if y:
|
||||
print "bad swig"
|
||||
else:
|
||||
print "good swig"
|
||||
|
||||
print " Spam -> Base -> Bar : ",
|
||||
y = bar.Bar_fromBase(x)
|
||||
if y:
|
||||
print "good swig"
|
||||
else:
|
||||
print "bad swig"
|
||||
|
||||
print " Spam -> Base -> Spam : ",
|
||||
y = spam.Spam_fromBase(x)
|
||||
if y:
|
||||
print "good swig"
|
||||
else:
|
||||
print "bad swig"
|
||||
|
||||
print " Foo -> Spam : ",
|
||||
y = spam.Spam_fromBase(b)
|
||||
if y:
|
||||
print "bad swig"
|
||||
else:
|
||||
print "good swig"
|
||||
|
||||
|
||||
|
||||
|
||||
24
Examples/python/import/spam.h
Normal file
24
Examples/python/import/spam.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include "bar.h"
|
||||
|
||||
class Spam : public Bar {
|
||||
public:
|
||||
Spam() { }
|
||||
~Spam() { }
|
||||
virtual void A() {
|
||||
printf("I'm Spam::A\n");
|
||||
}
|
||||
void B() {
|
||||
printf("I'm Spam::B\n");
|
||||
}
|
||||
virtual Base *toBase() {
|
||||
return static_cast<Base *>(this);
|
||||
}
|
||||
virtual Bar *toBar() {
|
||||
return static_cast<Bar *>(this);
|
||||
}
|
||||
static Spam *fromBase(Base *b) {
|
||||
return dynamic_cast<Spam *>(b);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
9
Examples/python/import/spam.i
Normal file
9
Examples/python/import/spam.i
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
%module spam
|
||||
%{
|
||||
#include "spam.h"
|
||||
%}
|
||||
|
||||
%import bar.i
|
||||
%include "spam.h"
|
||||
|
||||
|
||||
10
Examples/python/import_template/.cvsignore
Normal file
10
Examples/python/import_template/.cvsignore
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
base.py
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
22
Examples/python/import_template/Makefile
Normal file
22
Examples/python/import_template/Makefile
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SWIGOPT = -c
|
||||
#If your system requires linking with the runtime libraries then set the directory location here
|
||||
RUNTIMEDIR =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='base' INTERFACE='base.i' python_multi_cpp
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='foo' INTERFACE='foo.i' python_multi_cpp
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='bar' INTERFACE='bar.i' python_multi_cpp
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='spam' INTERFACE='spam.i' python_multi_cpp
|
||||
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
@rm -f foo.py bar.py spam.py base.py
|
||||
|
||||
check: all
|
||||
30
Examples/python/import_template/README
Normal file
30
Examples/python/import_template/README
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
This example tests the SWIG run-time libraries and use of the
|
||||
%import directive to work with multiple modules. However,
|
||||
unlike the import example, this uses templates to really
|
||||
stress test the type-system.
|
||||
|
||||
Use 'python runme.py' to run a test.
|
||||
|
||||
Overview:
|
||||
---------
|
||||
|
||||
The example defines 4 different extension modules--each wrapping
|
||||
a separate C++ class.
|
||||
|
||||
base.i - Base class
|
||||
foo.i - Foo class derived from Base
|
||||
bar.i - Bar class derived from Base
|
||||
spam.i - Spam class derived from Bar
|
||||
|
||||
Each module used %import to refer to another module. For
|
||||
example, the 'foo.i' module uses '%import base.i' to get
|
||||
definitions for its base class.
|
||||
|
||||
If everything is working correctly, all of the modules will load
|
||||
correctly and type checking will work correctly. The
|
||||
example requires the use of the SWIG run-time libraries
|
||||
which must be built and properly installed.
|
||||
|
||||
|
||||
|
||||
|
||||
22
Examples/python/import_template/bar.h
Normal file
22
Examples/python/import_template/bar.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include "base.h"
|
||||
|
||||
template<class T> class Bar : public Base<T> {
|
||||
public:
|
||||
Bar() { }
|
||||
~Bar() { }
|
||||
virtual void A() {
|
||||
printf("I'm Bar::A\n");
|
||||
}
|
||||
void B() {
|
||||
printf("I'm Bar::B\n");
|
||||
}
|
||||
virtual Base<T> *toBase() {
|
||||
return static_cast<Base<T> *>(this);
|
||||
}
|
||||
static Bar<T> *fromBase(Base<T> *b) {
|
||||
return dynamic_cast<Bar<T> *>(b);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
11
Examples/python/import_template/bar.i
Normal file
11
Examples/python/import_template/bar.i
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
%module bar
|
||||
%{
|
||||
#include "bar.h"
|
||||
%}
|
||||
|
||||
%import base.i
|
||||
%include "bar.h"
|
||||
|
||||
%template(intBar) Bar<int>;
|
||||
|
||||
|
||||
18
Examples/python/import_template/base.h
Normal file
18
Examples/python/import_template/base.h
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
|
||||
template<class T> class Base {
|
||||
public:
|
||||
Base() { };
|
||||
~Base() { };
|
||||
virtual void A() {
|
||||
printf("I'm Base::A\n");
|
||||
}
|
||||
void B() {
|
||||
printf("I'm Base::B\n");
|
||||
}
|
||||
virtual Base<T> *toBase() {
|
||||
return static_cast<Base<T> *>(this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
7
Examples/python/import_template/base.i
Normal file
7
Examples/python/import_template/base.i
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
%module base
|
||||
%{
|
||||
#include "base.h"
|
||||
%}
|
||||
|
||||
%include base.h
|
||||
%template(intBase) Base<int>;
|
||||
21
Examples/python/import_template/foo.h
Normal file
21
Examples/python/import_template/foo.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include "base.h"
|
||||
|
||||
template<class T> class Foo : public Base<T> {
|
||||
public:
|
||||
Foo() { }
|
||||
~Foo() { }
|
||||
virtual void A() {
|
||||
printf("I'm Foo::A\n");
|
||||
}
|
||||
void B() {
|
||||
printf("I'm Foo::B\n");
|
||||
}
|
||||
virtual Base<T> *toBase() {
|
||||
return static_cast<Base<T> *>(this);
|
||||
}
|
||||
static Foo<T> *fromBase(Base<T> *b) {
|
||||
return dynamic_cast<Foo<T> *>(b);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
10
Examples/python/import_template/foo.i
Normal file
10
Examples/python/import_template/foo.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
%module foo
|
||||
%{
|
||||
#include "foo.h"
|
||||
%}
|
||||
|
||||
%import base.i
|
||||
%include "foo.h"
|
||||
|
||||
%template(intFoo) Foo<int>;
|
||||
|
||||
111
Examples/python/import_template/runme.py
Normal file
111
Examples/python/import_template/runme.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
# file: runme.py
|
||||
# Test various properties of classes defined in separate modules
|
||||
|
||||
print "Testing the %import directive with templates"
|
||||
import base
|
||||
import foo
|
||||
import bar
|
||||
import spam
|
||||
|
||||
# Create some objects
|
||||
|
||||
print "Creating some objects"
|
||||
|
||||
a = base.intBase()
|
||||
b = foo.intFoo()
|
||||
c = bar.intBar()
|
||||
d = spam.intSpam()
|
||||
|
||||
# Try calling some methods
|
||||
print "Testing some methods"
|
||||
print "",
|
||||
print "Should see 'Base::A' ---> ",
|
||||
a.A()
|
||||
print "Should see 'Base::B' ---> ",
|
||||
a.B()
|
||||
|
||||
print "Should see 'Foo::A' ---> ",
|
||||
b.A()
|
||||
print "Should see 'Foo::B' ---> ",
|
||||
b.B()
|
||||
|
||||
print "Should see 'Bar::A' ---> ",
|
||||
c.A()
|
||||
print "Should see 'Bar::B' ---> ",
|
||||
c.B()
|
||||
|
||||
print "Should see 'Spam::A' ---> ",
|
||||
d.A()
|
||||
print "Should see 'Spam::B' ---> ",
|
||||
d.B()
|
||||
|
||||
# Try some casts
|
||||
|
||||
print "\nTesting some casts\n"
|
||||
print "",
|
||||
|
||||
x = a.toBase()
|
||||
print "Should see 'Base::A' ---> ",
|
||||
x.A()
|
||||
print "Should see 'Base::B' ---> ",
|
||||
x.B()
|
||||
|
||||
x = b.toBase()
|
||||
print "Should see 'Foo::A' ---> ",
|
||||
x.A()
|
||||
|
||||
print "Should see 'Base::B' ---> ",
|
||||
x.B()
|
||||
|
||||
x = c.toBase()
|
||||
print "Should see 'Bar::A' ---> ",
|
||||
x.A()
|
||||
|
||||
print "Should see 'Base::B' ---> ",
|
||||
x.B()
|
||||
|
||||
x = d.toBase()
|
||||
print "Should see 'Spam::A' ---> ",
|
||||
x.A()
|
||||
|
||||
print "Should see 'Base::B' ---> ",
|
||||
x.B()
|
||||
|
||||
x = d.toBar()
|
||||
print "Should see 'Bar::B' ---> ",
|
||||
x.B()
|
||||
|
||||
print "\nTesting some dynamic casts\n"
|
||||
x = d.toBase()
|
||||
|
||||
print " Spam -> Base -> Foo : ",
|
||||
y = foo.intFoo_fromBase(x)
|
||||
if y:
|
||||
print "bad swig"
|
||||
else:
|
||||
print "good swig"
|
||||
|
||||
print " Spam -> Base -> Bar : ",
|
||||
y = bar.intBar_fromBase(x)
|
||||
if y:
|
||||
print "good swig"
|
||||
else:
|
||||
print "bad swig"
|
||||
|
||||
print " Spam -> Base -> Spam : ",
|
||||
y = spam.intSpam_fromBase(x)
|
||||
if y:
|
||||
print "good swig"
|
||||
else:
|
||||
print "bad swig"
|
||||
|
||||
print " Foo -> Spam : ",
|
||||
y = spam.intSpam_fromBase(b)
|
||||
if y:
|
||||
print "bad swig"
|
||||
else:
|
||||
print "good swig"
|
||||
|
||||
|
||||
|
||||
|
||||
24
Examples/python/import_template/spam.h
Normal file
24
Examples/python/import_template/spam.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include "bar.h"
|
||||
|
||||
template<class T> class Spam : public Bar<T> {
|
||||
public:
|
||||
Spam() { }
|
||||
~Spam() { }
|
||||
virtual void A() {
|
||||
printf("I'm Spam::A\n");
|
||||
}
|
||||
void B() {
|
||||
printf("I'm Spam::B\n");
|
||||
}
|
||||
virtual Base<T> *toBase() {
|
||||
return static_cast<Base<T> *>(this);
|
||||
}
|
||||
virtual Bar<T> *toBar() {
|
||||
return static_cast<Bar<T> *>(this);
|
||||
}
|
||||
static Spam<T> *fromBase(Base<T> *b) {
|
||||
return dynamic_cast<Spam<T> *>(b);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
10
Examples/python/import_template/spam.i
Normal file
10
Examples/python/import_template/spam.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
%module spam
|
||||
%{
|
||||
#include "spam.h"
|
||||
%}
|
||||
|
||||
%import bar.i
|
||||
%include "spam.h"
|
||||
|
||||
%template(intSpam) Spam<int>;
|
||||
|
||||
|
|
@ -35,7 +35,11 @@ certain C declarations are turned into constants.
|
|||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<li>The compilation of examples is done using the file <tt>Example/Makefile</tt>. This
|
||||
<li>
|
||||
Please see the <a href="../../Doc/Manual/Windows.html">Windows</a> page in the main manual for information on using the examples on Windows. <p>
|
||||
</li>
|
||||
|
||||
<li>On Unix the compilation of examples is done using the file <tt>Example/Makefile</tt>. This
|
||||
makefile performs a manual module compilation which is platform specific. Typically,
|
||||
the steps look like this (Linux):
|
||||
|
||||
|
|
|
|||
19
Examples/python/libffi/Makefile
Normal file
19
Examples/python/libffi/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' LIBS='-L/usr/local/lib -lffi' python
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
176
Examples/python/libffi/example.i
Normal file
176
Examples/python/libffi/example.i
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include <unistd.h>
|
||||
#include <ffi.h>
|
||||
%}
|
||||
|
||||
/* A wrapper for execlp() using libffi to handle an arbitrary
|
||||
number of arguments */
|
||||
|
||||
%typemap(in) (...) {
|
||||
char **argv;
|
||||
int argc;
|
||||
int i;
|
||||
|
||||
argc = PyTuple_Size(varargs);
|
||||
argv = (char **) malloc(sizeof(char *)*(argc+1));
|
||||
for (i = 0; i < argc; i++) {
|
||||
PyObject *o = PyTuple_GetItem(varargs,i);
|
||||
if (!PyString_Check(o)) {
|
||||
PyErr_SetString(PyExc_ValueError,"Expected a string");
|
||||
return NULL;
|
||||
}
|
||||
argv[i] = PyString_AsString(o);
|
||||
}
|
||||
argv[i] = NULL;
|
||||
$1 = (void *) argv;
|
||||
}
|
||||
|
||||
/* Rewrite the function call, using libffi */
|
||||
%feature("action") execlp {
|
||||
int i, vc;
|
||||
ffi_cif cif;
|
||||
ffi_type **types;
|
||||
void **values;
|
||||
char **args;
|
||||
|
||||
vc = PyTuple_Size(varargs);
|
||||
types = (ffi_type **) malloc((vc+3)*sizeof(ffi_type *));
|
||||
values = (void **) malloc((vc+3)*sizeof(void *));
|
||||
args = (char **) arg3;
|
||||
|
||||
/* Set up path parameter */
|
||||
types[0] = &ffi_type_pointer;
|
||||
values[0] = &arg1;
|
||||
|
||||
/* Set up first argument */
|
||||
types[1] = &ffi_type_pointer;
|
||||
values[1] = &arg2;
|
||||
|
||||
/* Set up rest of parameters */
|
||||
for (i = 0; i <= vc; i++) {
|
||||
types[2+i] = &ffi_type_pointer;
|
||||
values[2+i] = &args[i];
|
||||
}
|
||||
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, vc+3,
|
||||
&ffi_type_uint, types) == FFI_OK) {
|
||||
ffi_call(&cif, (void (*)()) execlp, &result, values);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Whoa!!!!!");
|
||||
free(types);
|
||||
free(values);
|
||||
free(arg3);
|
||||
return NULL;
|
||||
}
|
||||
free(types);
|
||||
free(values);
|
||||
free(arg3);
|
||||
}
|
||||
|
||||
int execlp(const char *path, const char *arg1, ...);
|
||||
|
||||
|
||||
/* A wrapper for printf() using libffi */
|
||||
|
||||
%{
|
||||
typedef struct {
|
||||
int type;
|
||||
union {
|
||||
int ivalue;
|
||||
double dvalue;
|
||||
void *pvalue;
|
||||
} val;
|
||||
} vtype;
|
||||
enum { VT_INT, VT_DOUBLE, VT_POINTER };
|
||||
%}
|
||||
|
||||
%typemap(in) (const char *fmt, ...) {
|
||||
vtype *argv;
|
||||
int argc;
|
||||
int i;
|
||||
|
||||
$1 = PyString_AsString($input);
|
||||
|
||||
argc = PyTuple_Size(varargs);
|
||||
argv = (vtype *) malloc(argc*sizeof(vtype));
|
||||
for (i = 0; i < argc; i++) {
|
||||
PyObject *o = PyTuple_GetItem(varargs,i);
|
||||
if (PyInt_Check(o)) {
|
||||
argv[i].type = VT_INT;
|
||||
argv[i].val.ivalue = PyInt_AsLong(o);
|
||||
} else if (PyFloat_Check(o)) {
|
||||
argv[i].type = VT_DOUBLE;
|
||||
argv[i].val.dvalue = PyFloat_AsDouble(o);
|
||||
} else if (PyString_Check(o)) {
|
||||
argv[i].type = VT_POINTER;
|
||||
argv[i].val.pvalue = (void *) PyString_AsString(o);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_ValueError,"Unsupported argument type");
|
||||
free(argv);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
$2 = (void *) argv;
|
||||
}
|
||||
|
||||
/* Rewrite the function call, using libffi */
|
||||
%feature("action") printf {
|
||||
int i, vc;
|
||||
ffi_cif cif;
|
||||
ffi_type **types;
|
||||
void **values;
|
||||
vtype *args;
|
||||
|
||||
vc = PyTuple_Size(varargs);
|
||||
types = (ffi_type **) malloc((vc+1)*sizeof(ffi_type *));
|
||||
values = (void **) malloc((vc+1)*sizeof(void *));
|
||||
args = (vtype *) arg2;
|
||||
|
||||
/* Set up fmt parameter */
|
||||
types[0] = &ffi_type_pointer;
|
||||
values[0] = &arg1;
|
||||
|
||||
/* Set up rest of parameters */
|
||||
for (i = 0; i < vc; i++) {
|
||||
switch(args[i].type) {
|
||||
case VT_INT:
|
||||
types[1+i] = &ffi_type_uint;
|
||||
values[1+i] = &args[i].val.ivalue;
|
||||
break;
|
||||
case VT_DOUBLE:
|
||||
types[1+i] = &ffi_type_double;
|
||||
values[1+i] = &args[i].val.dvalue;
|
||||
break;
|
||||
case VT_POINTER:
|
||||
types[1+i] = &ffi_type_pointer;
|
||||
values[1+i] = &args[i].val.pvalue;
|
||||
break;
|
||||
default:
|
||||
abort(); /* Whoa! We're seriously hosed */
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, vc+1,
|
||||
&ffi_type_uint, types) == FFI_OK) {
|
||||
ffi_call(&cif, (void (*)()) printf, &result, values);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Whoa!!!!!");
|
||||
free(types);
|
||||
free(values);
|
||||
free(args);
|
||||
return NULL;
|
||||
}
|
||||
free(types);
|
||||
free(values);
|
||||
free(args);
|
||||
}
|
||||
|
||||
int printf(const char *fmt, ...);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
10
Examples/python/mpointer/.cvsignore
Normal file
10
Examples/python/mpointer/.cvsignore
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
example.py
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
21
Examples/python/mpointer/Makefile
Normal file
21
Examples/python/mpointer/Makefile
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
45
Examples/python/mpointer/example.cxx
Normal file
45
Examples/python/mpointer/example.cxx
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
double do_op(Shape *s, double (Shape::*m)(void)) {
|
||||
return (s->*m)();
|
||||
}
|
||||
|
||||
double (Shape::*areapt())(void) {
|
||||
return &Shape::area;
|
||||
}
|
||||
|
||||
double (Shape::*perimeterpt())(void) {
|
||||
return &Shape::perimeter;
|
||||
}
|
||||
|
||||
/* Member pointer variables */
|
||||
double (Shape::*areavar)(void) = &Shape::area;
|
||||
double (Shape::*perimetervar)(void) = &Shape::perimeter;
|
||||
|
||||
50
Examples/python/mpointer/example.h
Normal file
50
Examples/python/mpointer/example.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
double *z;
|
||||
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
extern double do_op(Shape *s, double (Shape::*m)(void));
|
||||
|
||||
/* Functions that return member pointers */
|
||||
|
||||
extern double (Shape::*areapt())(void);
|
||||
extern double (Shape::*perimeterpt())(void);
|
||||
|
||||
/* Global variables that are member pointers */
|
||||
extern double (Shape::*areavar)(void);
|
||||
extern double (Shape::*perimetervar)(void);
|
||||
|
||||
|
||||
|
||||
16
Examples/python/mpointer/example.i
Normal file
16
Examples/python/mpointer/example.i
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
/* Some constants */
|
||||
|
||||
%constant double (Shape::*AREAPT)(void) = &Shape::area;
|
||||
%constant double (Shape::*PERIMPT)(void) = &Shape::perimeter;
|
||||
%constant double (Shape::*NULLPT)(void) = 0;
|
||||
|
||||
51
Examples/python/mpointer/runme.py
Normal file
51
Examples/python/mpointer/runme.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
# Example using pointers to member functions
|
||||
|
||||
import example
|
||||
|
||||
# Get the pointers
|
||||
|
||||
area_pt = example.areapt()
|
||||
perim_pt = example.perimeterpt()
|
||||
|
||||
print "area_pt =", area_pt
|
||||
print "perim_pt = ", perim_pt
|
||||
|
||||
# Create some objects
|
||||
|
||||
c = example.Circle(4)
|
||||
s = example.Square(10)
|
||||
|
||||
# Do some calculations
|
||||
|
||||
print "Circle area = ", example.do_op(c,area_pt)
|
||||
print "Circle perim = ", example.do_op(c,perim_pt)
|
||||
print "Square area = ", example.do_op(s,area_pt)
|
||||
print "Square perim = ", example.do_op(s,perim_pt)
|
||||
|
||||
print "cvar.areavar =", example.cvar.areavar
|
||||
print "cvar.perimetervar =", example.cvar.perimetervar
|
||||
|
||||
# Try the variables
|
||||
print "Circle area = ", example.do_op(c,example.cvar.areavar)
|
||||
print "Circle perim = ", example.do_op(c,example.cvar.perimetervar)
|
||||
print "Square area = ", example.do_op(s,example.cvar.areavar)
|
||||
print "Square perim = ", example.do_op(s,example.cvar.perimetervar)
|
||||
|
||||
# Modify one of the variables
|
||||
example.cvar.areavar = perim_pt
|
||||
|
||||
print "Circle perimeter = ", example.do_op(c,example.cvar.areavar)
|
||||
|
||||
# Try the constants
|
||||
|
||||
print "example.AREAPT =", example.AREAPT
|
||||
print "example.PERIMPT=", example.PERIMPT
|
||||
print "example.NULLPT =", example.NULLPT
|
||||
|
||||
print "Circle area = ", example.do_op(c,example.AREAPT)
|
||||
print "Circle perim = ", example.do_op(c,example.PERIMPT)
|
||||
print "Square area = ", example.do_op(s,example.AREAPT)
|
||||
print "Square perim = ", example.do_op(s,example.PERIMPT)
|
||||
|
||||
|
||||
|
||||
9
Examples/python/multimap/.cvsignore
Normal file
9
Examples/python/multimap/.cvsignore
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
19
Examples/python/multimap/Makefile
Normal file
19
Examples/python/multimap/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
53
Examples/python/multimap/example.c
Normal file
53
Examples/python/multimap/example.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/* File : example.c */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
int gcdmain(int argc, char *argv[]) {
|
||||
int x,y;
|
||||
if (argc != 3) {
|
||||
printf("usage: gcd x y\n");
|
||||
return -1;
|
||||
}
|
||||
x = atoi(argv[1]);
|
||||
y = atoi(argv[2]);
|
||||
printf("gcd(%d,%d) = %d\n", x,y,gcd(x,y));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count(char *bytes, int len, char c) {
|
||||
int i;
|
||||
int count = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (bytes[i] == c) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void capitalize(char *str, int len) {
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
str[i] = toupper(str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void circle(double x, double y) {
|
||||
double a = x*x + y*y;
|
||||
if (a > 1.0) {
|
||||
printf("Bad points %g, %g\n", x,y);
|
||||
} else {
|
||||
printf("Good points %g, %g\n", x,y);
|
||||
}
|
||||
}
|
||||
148
Examples/python/multimap/example.dsp
Normal file
148
Examples/python/multimap/example.dsp
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
# Microsoft Developer Studio Project File - Name="example" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=example - Win32 Release
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "example.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "example.mak" CFG="example - Win32 Release"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "example - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "example - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "example - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "$(PYTHON_INCLUDE)" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "_DEBUG"
|
||||
# ADD RSC /l 0x809 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib $(PYTHON_LIB) /nologo /dll /debug /machine:I386 /out:"example.dll" /pdbtype:sept
|
||||
|
||||
!ELSEIF "$(CFG)" == "example - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
|
||||
# ADD CPP /nologo /MT /W3 /GX /O2 /I "$(PYTHON_INCLUDE)" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "NDEBUG"
|
||||
# ADD RSC /l 0x809 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib $(PYTHON_LIB) /nologo /dll /machine:I386 /out:"example.dll"
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "example - Win32 Debug"
|
||||
# Name "example - Win32 Release"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example_wrap.c
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.i
|
||||
|
||||
!IF "$(CFG)" == "example - Win32 Debug"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\example.i
|
||||
InputName=example
|
||||
|
||||
"$(InputName)_wrap.c" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
echo In order to function correctly, please ensure the following environment variables are correctly set:
|
||||
echo PYTHON_INCLUDE: %PYTHON_INCLUDE%
|
||||
echo PYTHON_LIB: %PYTHON_LIB%
|
||||
echo on
|
||||
..\..\..\swig -python $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ELSEIF "$(CFG)" == "example - Win32 Release"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\example.i
|
||||
InputName=example
|
||||
|
||||
"$(InputName)_wrap.c" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
echo In order to function correctly, please ensure the following environment variables are correctly set:
|
||||
echo PYTHON_INCLUDE: %PYTHON_INCLUDE%
|
||||
echo PYTHON_LIB: %PYTHON_LIB%
|
||||
echo on
|
||||
..\..\..\swig -python $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# End Target
|
||||
# End Project
|
||||
83
Examples/python/multimap/example.i
Normal file
83
Examples/python/multimap/example.i
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%include exception.i
|
||||
%include typemaps.i
|
||||
|
||||
extern int gcd(int x, int y);
|
||||
|
||||
%typemap(in,fragment="t_output_helper") (int argc, char *argv[]) {
|
||||
int i;
|
||||
if (!PyList_Check($input)) {
|
||||
SWIG_exception(SWIG_ValueError, "Expecting a list");
|
||||
return NULL;
|
||||
}
|
||||
$1 = PyList_Size($input);
|
||||
if ($1 == 0) {
|
||||
SWIG_exception(SWIG_ValueError, "List must contain at least 1 element");
|
||||
return NULL;
|
||||
}
|
||||
$2 = (char **) malloc(($1+1)*sizeof(char *));
|
||||
for (i = 0; i < $1; i++) {
|
||||
PyObject *s = PyList_GetItem($input,i);
|
||||
if (!PyString_Check(s)) {
|
||||
SWIG_exception(SWIG_ValueError, "List items must be strings");
|
||||
free($2);
|
||||
return NULL;
|
||||
}
|
||||
$2[i] = PyString_AsString(s);
|
||||
}
|
||||
$2[i] = 0;
|
||||
}
|
||||
|
||||
extern int gcdmain(int argc, char *argv[]);
|
||||
|
||||
%typemap(python,in) (char *bytes, int len) {
|
||||
if (!PyString_Check($input)) {
|
||||
PyErr_SetString(PyExc_ValueError,"Expected a string");
|
||||
return NULL;
|
||||
}
|
||||
$1 = PyString_AsString($input);
|
||||
$2 = PyString_Size($input);
|
||||
}
|
||||
|
||||
extern int count(char *bytes, int len, char c);
|
||||
|
||||
|
||||
/* This example shows how to wrap a function that mutates a string */
|
||||
|
||||
/* Since str is modified, we make a copy of the Python object
|
||||
so that we don't violate it's mutability */
|
||||
|
||||
%typemap(python,in) (char *str, int len) {
|
||||
$2 = PyString_Size($input);
|
||||
$1 = (char *) malloc($2+1);
|
||||
memmove($1,PyString_AsString($input),$2);
|
||||
}
|
||||
|
||||
/* Return the mutated string as a new object. The t_output_helper
|
||||
function takes an object and appends it to the output object
|
||||
to create a tuple */
|
||||
|
||||
%typemap(python,argout) (char *str, int len) {
|
||||
PyObject *o;
|
||||
o = PyString_FromStringAndSize($1,$2);
|
||||
$result = t_output_helper($result,o);
|
||||
free($1);
|
||||
}
|
||||
|
||||
extern void capitalize(char *str, int len);
|
||||
|
||||
/* A multi-valued constraint. Force two arguments to lie
|
||||
inside the unit circle */
|
||||
|
||||
%typemap(check) (double cx, double cy) {
|
||||
double a = $1*$1 + $2*$2;
|
||||
if (a > 1.0) {
|
||||
SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
extern void circle(double cx, double cy);
|
||||
|
||||
|
||||
27
Examples/python/multimap/runme.py
Normal file
27
Examples/python/multimap/runme.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# file: example.py
|
||||
|
||||
import example
|
||||
|
||||
# Call our gcd() function
|
||||
|
||||
x = 42
|
||||
y = 105
|
||||
g = example.gcd(x,y)
|
||||
print "The gcd of %d and %d is %d" % (x,y,g)
|
||||
|
||||
# Call the gcdmain() function
|
||||
example.gcdmain(["gcdmain","42","105"])
|
||||
|
||||
# Call the count function
|
||||
print example.count("Hello World", "l")
|
||||
|
||||
# Call the capitalize function
|
||||
|
||||
print example.capitalize("hello world")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
10
Examples/python/operator/.cvsignore
Normal file
10
Examples/python/operator/.cvsignore
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
example.py
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
21
Examples/python/operator/Makefile
Normal file
21
Examples/python/operator/Makefile
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
36
Examples/python/operator/example.h
Normal file
36
Examples/python/operator/example.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* File : example.h */
|
||||
#include <math.h>
|
||||
|
||||
class Complex {
|
||||
private:
|
||||
double rpart, ipart;
|
||||
public:
|
||||
Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { }
|
||||
Complex(const Complex &c) : rpart(c.rpart), ipart(c.ipart) { }
|
||||
Complex &operator=(const Complex &c) {
|
||||
rpart = c.rpart;
|
||||
ipart = c.ipart;
|
||||
return *this;
|
||||
}
|
||||
Complex operator+(const Complex &c) const {
|
||||
return Complex(rpart+c.rpart, ipart+c.ipart);
|
||||
}
|
||||
Complex operator-(const Complex &c) const {
|
||||
return Complex(rpart-c.rpart, ipart-c.ipart);
|
||||
}
|
||||
Complex operator*(const Complex &c) const {
|
||||
return Complex(rpart*c.rpart - ipart*c.ipart,
|
||||
rpart*c.ipart + c.rpart*ipart);
|
||||
}
|
||||
Complex operator-() const {
|
||||
return Complex(-rpart, -ipart);
|
||||
}
|
||||
|
||||
double re() const { return rpart; }
|
||||
double im() const { return ipart; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
28
Examples/python/operator/example.i
Normal file
28
Examples/python/operator/example.i
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* This header file is a little tough to handle because it has overloaded
|
||||
operators and constructors. We're going to try and deal with that here */
|
||||
|
||||
/* This turns the copy constructor in a function ComplexCopy() that can
|
||||
be called */
|
||||
|
||||
%rename(ComplexCopy) Complex::Complex(Complex const &);
|
||||
|
||||
/* Now grab the original header file */
|
||||
%include "example.h"
|
||||
|
||||
/* An output method that turns a complex into a short string */
|
||||
%extend Complex {
|
||||
char *__str__() {
|
||||
static char temp[512];
|
||||
sprintf(temp,"(%g,%g)", self->re(), self->im());
|
||||
return temp;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
21
Examples/python/operator/runme.py
Normal file
21
Examples/python/operator/runme.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Operator overloading example
|
||||
import example
|
||||
|
||||
a = example.Complex(2,3)
|
||||
b = example.Complex(-5,10)
|
||||
|
||||
print "a =",a
|
||||
print "b =",b
|
||||
|
||||
c = a + b
|
||||
print "c =",c
|
||||
print "a*b =",a*b
|
||||
print "a-c =",a-c
|
||||
|
||||
e = example.ComplexCopy(a-c)
|
||||
print "e =",e
|
||||
|
||||
# Big expression
|
||||
f = ((a+b)*(c+b*e)) + (-a)
|
||||
print "f =",f
|
||||
|
||||
2
Examples/python/overload_feature/.cvsignore
Normal file
2
Examples/python/overload_feature/.cvsignore
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
example.py
|
||||
|
||||
9
Examples/python/pointer/.cvsignore
Normal file
9
Examples/python/pointer/.cvsignore
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -13,6 +13,7 @@ static::
|
|||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@
|
|||
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(int *x, int *y, int *result);
|
||||
%include pointer.i
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
|
|
|
|||
|
|
@ -4,9 +4,11 @@ import example;
|
|||
|
||||
# First create some objects using the pointer library.
|
||||
print "Testing the pointer library";
|
||||
a = example.ptrcreate("int",37);
|
||||
b = example.ptrcreate("int",42);
|
||||
c = example.ptrcreate("int");
|
||||
a = example.new_intp();
|
||||
b = example.new_intp();
|
||||
c = example.new_intp();
|
||||
example.intp_assign(a,37);
|
||||
example.intp_assign(b,42);
|
||||
|
||||
print " a =",a
|
||||
print " b =",b
|
||||
|
|
@ -16,13 +18,13 @@ print " c =",c
|
|||
example.add(a,b,c)
|
||||
|
||||
# Now get the result
|
||||
r = example.ptrvalue(c)
|
||||
r = example.intp_value(c)
|
||||
print " 37 + 42 =",r
|
||||
|
||||
# Clean up the pointers
|
||||
example.ptrfree(a)
|
||||
example.ptrfree(b)
|
||||
example.ptrfree(c)
|
||||
example.delete_intp(a)
|
||||
example.delete_intp(b)
|
||||
example.delete_intp(c)
|
||||
|
||||
# Now try the typemap library
|
||||
# This should be much easier. Now how it is no longer
|
||||
9
Examples/python/reference/.cvsignore
Normal file
9
Examples/python/reference/.cvsignore
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -14,6 +14,7 @@ static::
|
|||
TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core
|
||||
$(MAKE) -f $(TOP)/Makefile python_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
#include "example.h"
|
||||
%}
|
||||
|
||||
%rename(cprint) print;
|
||||
|
||||
class Vector {
|
||||
public:
|
||||
Vector(double x, double y, double z);
|
||||
|
|
@ -31,7 +33,7 @@ public:
|
|||
int size();
|
||||
|
||||
/* This wrapper provides an alternative to the [] operator */
|
||||
%addmethods {
|
||||
%extend {
|
||||
Vector &get(int index) {
|
||||
return (*self)[index];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,11 @@ import example
|
|||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:"
|
||||
a = example.new_Vector(3,4,5)
|
||||
b = example.new_Vector(10,11,12)
|
||||
a = example.Vector(3,4,5)
|
||||
b = example.Vector(10,11,12)
|
||||
|
||||
print " Created",example.Vector_print(a)
|
||||
print " Created",example.Vector_print(b)
|
||||
print " Created",a.cprint()
|
||||
print " Created",b.cprint()
|
||||
|
||||
# ----- Call an overloaded operator -----
|
||||
|
||||
|
|
@ -24,49 +24,41 @@ print " Created",example.Vector_print(b)
|
|||
|
||||
print "Adding a+b"
|
||||
c = example.addv(a,b)
|
||||
print " a+b =", example.Vector_print(c)
|
||||
print " a+b =", c.cprint()
|
||||
|
||||
# Note: Unless we free the result, a memory leak will occur
|
||||
example.delete_Vector(c)
|
||||
del c
|
||||
|
||||
# ----- Create a vector array -----
|
||||
|
||||
# Note: Using the high-level interface here
|
||||
print "Creating an array of vectors"
|
||||
va = example.new_VectorArray(10)
|
||||
va = example.VectorArray(10)
|
||||
print " va = ",va
|
||||
|
||||
# ----- Set some values in the array -----
|
||||
|
||||
# These operators copy the value of $a and $b to the vector array
|
||||
example.VectorArray_set(va,0,a)
|
||||
example.VectorArray_set(va,1,b)
|
||||
va.set(0,a)
|
||||
va.set(1,b)
|
||||
|
||||
# This will work, but it will cause a memory leak!
|
||||
|
||||
example.VectorArray_set(va,2,example.addv(a,b))
|
||||
|
||||
# The non-leaky way to do it
|
||||
|
||||
c = example.addv(a,b)
|
||||
example.VectorArray_set(va,3,c)
|
||||
example.delete_Vector(c)
|
||||
va.set(2,example.addv(a,b))
|
||||
|
||||
# Get some values from the array
|
||||
|
||||
print "Getting some array values"
|
||||
for i in range(0,5):
|
||||
print " va(%d) = %s" % (i, example.Vector_print(example.VectorArray_get(va,i)))
|
||||
print " va(%d) = %s" % (i, va.get(i).cprint())
|
||||
|
||||
# Watch under resource meter to check on this
|
||||
print "Making sure we don't leak memory."
|
||||
for i in xrange(0,1000000):
|
||||
c = example.VectorArray_get(va,i % 10)
|
||||
c = va.get(i % 10)
|
||||
|
||||
# ----- Clean up -----
|
||||
print "Cleaning up"
|
||||
|
||||
example.delete_VectorArray(va)
|
||||
example.delete_Vector(a)
|
||||
example.delete_Vector(b)
|
||||
del va
|
||||
del a
|
||||
del b
|
||||
|
||||
10
Examples/python/shadow/.cvsignore
Normal file
10
Examples/python/shadow/.cvsignore
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
example.py
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
*.dll
|
||||
*.dsw
|
||||
*.ncb
|
||||
*.opt
|
||||
*.plg
|
||||
Release
|
||||
Debug
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue