Merge branch 'master' into C

This commit is contained in:
Vadim Zeitlin 2016-04-08 15:35:32 +02:00
commit 7c402ad148
2760 changed files with 114689 additions and 39303 deletions

View file

@ -1,21 +1,23 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
LIBS = -lm
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' python_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile python_clean
rm -f $(TARGET).py
check: all
$(MAKE) -f $(TOP)/Makefile python_run
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' python_clean

View file

@ -1,4 +1,4 @@
/* File : example.c */
/* File : example.cxx */
#include "example.h"
#define M_PI 3.14159265358979323846
@ -11,18 +11,18 @@ void Shape::move(double dx, double dy) {
int Shape::nshapes = 0;
double Circle::area(void) {
double Circle::area() {
return M_PI*radius*radius;
}
double Circle::perimeter(void) {
double Circle::perimeter() {
return 2*M_PI*radius;
}
double Square::area(void) {
double Square::area() {
return width*width;
}
double Square::perimeter(void) {
double Square::perimeter() {
return 4*width;
}

View file

@ -43,7 +43,7 @@ RSC=rc.exe
# 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 CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "$(PYTHON_INCLUDE)" /D "SWIG_PYTHON_INTERPRETER_NO_DEBUG" /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"
@ -126,7 +126,7 @@ InputName=example
echo PYTHON_INCLUDE: %PYTHON_INCLUDE%
echo PYTHON_LIB: %PYTHON_LIB%
echo on
..\..\..\swig.exe -c++ -python $(InputPath)
..\..\..\swig.exe -c++ -python "$(InputPath)"
# End Custom Build
@ -141,7 +141,7 @@ InputName=example
echo PYTHON_INCLUDE: %PYTHON_INCLUDE%
echo PYTHON_LIB: %PYTHON_LIB%
echo on
..\..\..\swig.exe -c++ -python $(InputPath)
..\..\..\swig.exe -c++ -python "$(InputPath)"
# End Custom Build

View file

@ -7,11 +7,11 @@ public:
}
virtual ~Shape() {
nshapes--;
};
double x, y;
}
double x, y;
void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
virtual double area() = 0;
virtual double perimeter() = 0;
static int nshapes;
};
@ -19,21 +19,16 @@ class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
virtual double area(void);
virtual double perimeter(void);
Circle(double r) : radius(r) { }
virtual double area();
virtual double perimeter();
};
class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { };
virtual double area(void);
virtual double perimeter(void);
Square(double w) : width(w) { }
virtual double area();
virtual double perimeter();
};

View file

@ -11,4 +11,3 @@
/* Let's just grab the original header file here */
%include "example.h"

View file

@ -12,9 +12,7 @@
<H2>Wrapping a simple C++ class</H2>
<p>
This example illustrates the most primitive form of C++ class wrapping performed
by SWIG. In this case, C++ classes are simply transformed into a collection of
C-style functions that provide access to class members.
This example illustrates wrapping a simple C++ class to give a Python class.
<h2>The C++ Code</h2>
@ -32,8 +30,8 @@ public:
}
virtual ~Shape() {
nshapes--;
};
double x, y;
}
double x, y;
void move(double dx, double dy);
virtual double area() = 0;
virtual double perimeter() = 0;
@ -44,7 +42,7 @@ class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
Circle(double r) : radius(r) { }
virtual double area();
virtual double perimeter();
};
@ -53,7 +51,7 @@ class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { };
Square(double w) : width(w) { }
virtual double area();
virtual double perimeter();
};
@ -102,51 +100,34 @@ c = example.new_Circle(10.0)
</blockquote>
<p>
<li>To access member data, a pair of accessor functions are used.
<li>Member variables of the C++ class are wrapped as attributes of the Python class.
For example:
<blockquote>
<pre>
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 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
<blockquote>
<pre>
print "The area is ", example.Shape_area(c)
c.x = 15 # Set member data
x = c.x # Get member data
</pre>
</blockquote>
<p>
<li>Type checking knows about the inheritance structure of C++. For example:
<li>Member function are invoked as you would expect:
<blockquote>
<pre>
example.Shape_area(c) # Works (c is a Shape)
example.Circle_area(c) # Works (c is a Circle)
example.Square_area(c) # Fails (c is definitely not a Square)
print "The area is ", c.area()
</pre>
</blockquote>
<p>
<li>To invoke a destructor, simply do this
<li>To invoke a destructor, simply call <code>del</code> on the object:
<blockquote>
<pre>
example.delete_Shape(c) # Deletes a shape
del c # Deletes a shape
</pre>
</blockquote>
(Note: destructors are currently not inherited. This might change later).
<p>
<li>Static member variables are wrapped as C global variables. For example:
@ -162,52 +143,12 @@ 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.
Proxy classes provide a much higher-level interface.
<p>
<li>SWIG *does* know how to properly perform upcasting of objects in
<li>SWIG <b>does</b> 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
short and incomplete list:
<p>
<ul>
<li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
conflicts so you must give an alternative name to any overloaded method name using the
%name directive like this:
<blockquote>
<pre>
void foo(int a);
%name(foo2) void foo(double a, double b);
</pre>
</blockquote>
<p>
<li>Overloaded operators. Not supported at all. The only workaround for this is
to write a helper function. For example:
<blockquote>
<pre>
%inline %{
Vector *vector_add(Vector *a, Vector *b) {
... whatever ...
}
%}
</pre>
</blockquote>
<p>
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
<p>
<li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to
use C++ in moderation.
<li>C++ Namespaces - %nspace isn't yet supported for Python.
</ul>

View file

@ -3,7 +3,7 @@
# This file illustrates the proxy class C++ interface generated
# by SWIG.
import example
import example
# ----- Object creation -----
@ -15,7 +15,7 @@ print " Created square", s
# ----- Access a static member -----
print "\nA total of", example.cvar.Shape_nshapes,"shapes were created"
print "\nA total of", example.cvar.Shape_nshapes, "shapes were created"
# ----- Member data access -----
@ -28,16 +28,18 @@ 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)
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()
for o in [c, s]:
print " ", o
print " area = ", o.area()
print " perimeter = ", o.perimeter()
# prevent o from holding a reference to the last object looked at
o = None
print "\nGuess I'll clean up now"
@ -45,7 +47,5 @@ print "\nGuess I'll clean up now"
del c
del s
s = 3
print example.cvar.Shape_nshapes,"shapes remain"
print example.cvar.Shape_nshapes, "shapes remain"
print "Goodbye"