The great merge

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2002-11-30 22:01:28 +00:00
commit 516036631c
1508 changed files with 125983 additions and 44037 deletions

View file

@ -0,0 +1,13 @@
# see top-level Makefile.in
class
constants
enum
funcptr
import
multimap
operator
pointer
reference
simple
value
variables

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh_cpp_static
clean::
rm -f *_wrap* *.o *~ *.so mytclsh *.pyc .~* core
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View file

@ -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;
}

View 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 "$(TCL_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 "$(TCL_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 "$(TCL_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 "$(TCL_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 TCL_INCLUDE: %TCL_INCLUDE%
echo TCL_LIB: %TCL_LIB%
echo on
..\..\..\swig -tcl8 $(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 TCL_INCLUDE: %TCL_INCLUDE%
echo TCL_LIB: %TCL_LIB%
echo on
..\..\..\swig -c++ -tcl8 $(InputPath)
# End Custom Build
!ENDIF
# End Source File
# End Target
# End Project

View file

@ -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,8 +20,8 @@ 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 {
@ -29,8 +29,8 @@ private:
double width;
public:
Square(double w) : width(w) { };
virtual double area();
virtual double perimeter();
virtual double area(void);
virtual double perimeter(void);
};

View file

@ -22,11 +22,11 @@ puts "\nA total of $Shape_nshapes shapes were created"
# ----- Member data access -----
# Set the location of the object
# Note: the base class must be used since that's where x and y
# were declared.
Shape_x_set $c 20
Shape_y_set $c 30
# Now use the same functions in the base class
Shape_x_set $s -10
Shape_y_set $s 5
@ -69,6 +69,3 @@ delete_Shape $s
puts "$Shape_nshapes shapes remain"
puts "Goodbye"

View file

@ -11,7 +11,7 @@ catch { load ./example.dll example} ;# Windows
puts "Creating some objects:"
Circle c 10
puts " Created circle [c cget -this]"
Circle s 10
Square s 10
puts " Created square [s cget -this]"
# ----- Access a static member -----

View file

@ -120,14 +120,13 @@ For example:
<blockquote>
<pre>
Circle_x_set $c 15 ;# Set member data
Shape_x_set $c 15 ;# Set member data
set x [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 base class must
be used such as <tt>Shape_x_get</tt>
<p>
<li>To invoke a member function, you simply do this
@ -270,28 +269,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.
</ul>
<hr>

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o my_tclsh *~ .~* core *.so *.sl
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View file

@ -19,8 +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;
/* The following directives also produce constants */
%constant int iconst = 37;
%constant double fconst = 3.14;

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh_cpp_static
clean::
rm -f *_wrap* *.o *~ *.so mytclsh *.pyc .~* core
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o my_tclsh *~ .~* core *.so *.sl
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View file

@ -15,3 +15,5 @@ int sub(int a, int b) {
int mul(int a, int b) {
return a*b;
}
int (*funcvar)(int,int) = add;

View file

@ -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);

View file

@ -0,0 +1,4 @@
bar_wrap.cxx
base_wrap.cxx
foo_wrap.cxx
spam_wrap.cxx

View 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' tcl_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='foo' INTERFACE='foo.i' tcl_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='bar' INTERFACE='bar.i' tcl_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='spam' INTERFACE='spam.i' tcl_multi_cpp
clean::
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View 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 'tclsh runme.tcl' 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.

View 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);
}
};

View file

@ -0,0 +1,9 @@
%module bar
%{
#include "bar.h"
%}
%import base.i
%include "bar.h"

View 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);
}
};

View file

@ -0,0 +1,6 @@
%module base
%{
#include "base.h"
%}
%include base.h

View 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);
}
};

View file

@ -0,0 +1,8 @@
%module foo
%{
#include "foo.h"
%}
%import base.i
%include "foo.h"

View file

@ -0,0 +1,121 @@
# file: runme.py
# Test various properties of classes defined in separate modules
puts "Testing the %import directive"
catch { load ./base.so base}
catch { load ./base.dll base} ;# Windows
catch { load ./foo.so foo}
catch { load ./foo.dll foo} ;# Windows
catch { load ./bar.so bar}
catch { load ./bar.dll bar} ;# Windows
catch { load ./spam.so spam}
catch { load ./spam.dll spam} ;# Windows
# Create some objects
puts "Creating some objects"
set a [Base]
set b [Foo]
set c [Bar]
set d [Spam]
# Try calling some methods
puts "Testing some methods"
puts "Should see 'Base::A' ---> "
$a A
puts "Should see 'Base::B' ---> "
$a B
puts "Should see 'Foo::A' ---> "
$b A
puts "Should see 'Foo::B' ---> "
$b B
puts "Should see 'Bar::A' ---> "
$c A
puts "Should see 'Bar::B' ---> "
$c B
puts "Should see 'Spam::A' ---> "
$d A
puts "Should see 'Spam::B' ---> "
$d B
# Try some casts
puts "\nTesting some casts\n"
Base x -this [$a toBase]
puts "Should see 'Base::A' ---> "
x A
puts "Should see 'Base::B' ---> "
x B
rename x ""
Base x -this [$b toBase]
puts "Should see 'Foo::A' ---> "
x A
puts "Should see 'Base::B' ---> "
x B
rename x ""
Base x -this [$c toBase]
puts "Should see 'Bar::A' ---> "
x A
puts "Should see 'Base::B' ---> "
x B
rename x ""
Base x -this [$d toBase]
puts "Should see 'Spam::A' ---> "
x A
puts "Should see 'Base::B' ---> "
x B
rename x ""
Bar x -this [$d toBar]
puts "Should see 'Bar::B' ---> "
x B
rename x ""
puts "\nTesting some dynamic casts\n"
Base x -this [$d toBase]
puts "Spam -> Base -> Foo : "
set y [Foo_fromBase [x cget -this]]
if {$y != "NULL"} {
puts "bad swig"
} {
puts "good swig"
}
puts "Spam -> Base -> Bar : "
set y [Bar_fromBase [x cget -this]]
if {$y != "NULL"} {
puts "good swig"
} {
puts "bad swig"
}
puts "Spam -> Base -> Spam : "
set y [Spam_fromBase [x cget -this]]
if {$y != "NULL"} {
puts "good swig"
} {
puts "bad swig"
}
puts "Foo -> Spam : "
set y [Spam_fromBase $b]
if {$y != "NULL"} {
puts "bad swig"
} {
puts "good swig"
}

View 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);
}
};

View file

@ -0,0 +1,9 @@
%module spam
%{
#include "spam.h"
%}
%import bar.i
%include "spam.h"

View file

@ -35,6 +35,10 @@ certain C declarations are turned into constants.
</pre>
</blockquote>
<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>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):

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -0,0 +1,19 @@
TOP = ../..
SWIG = $(TOP)/../swig
SRCS = example.c
TARGET = my_tclsh
DLTARGET = example
INTERFACE = example.i
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(DLTARGET)' INTERFACE='$(INTERFACE)' tcl
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh
clean::
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View 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);
}
}

View 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 "$(TCL_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 $(TCL_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 "$(TCL_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 $(TCL_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 TCL_INCLUDE: %TCL_INCLUDE%
echo TCL_LIB: %TCL_LIB%
echo on
..\..\..\swig -tcl8 $(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 TCL_INCLUDE: %TCL_INCLUDE%
echo TCL_LIB: %TCL_LIB%
echo on
..\..\..\swig -tcl8 $(InputPath)
# End Custom Build
!ENDIF
# End Source File
# End Target
# End Project

View file

@ -0,0 +1,68 @@
/* File : example.i */
%module example
%include exception.i
extern int gcd(int x, int y);
%typemap(tcl8,in) (int argc, char *argv[]) {
Tcl_Obj **listobjv = 0;
int i;
if (Tcl_ListObjGetElements(interp,$input, &$1, &listobjv) == TCL_ERROR) {
SWIG_exception(SWIG_ValueError,"Expected a list");
return TCL_ERROR;
}
$2 = (char **) malloc(($1+1)*sizeof(char *));
for (i = 0; i < $1; i++) {
$2[i] = Tcl_GetStringFromObj(listobjv[i],0);
}
$2[i] = 0;
}
%typemap(tcl8,freearg) char *argv[] {
if ($1) {
free($1);
}
}
extern int gcdmain(int argc, char *argv[]);
%typemap(tcl8,in) (char *bytes, int len) {
$1 = Tcl_GetStringFromObj($input,&$2);
}
extern int count(char *bytes, int len, char c);
/* This example shows how to wrap a function that mutates a string */
%typemap(tcl8,in) (char *str, int len) {
char *temp;
temp = Tcl_GetStringFromObj($input,&$2);
$1 = (char *) malloc($2+1);
memmove($1,temp,$2);
}
/* Return the mutated string as a new object. */
%typemap(tcl8,argout) (char *str, int len) {
Tcl_Obj *o;
o = Tcl_NewStringObj($1,$2);
Tcl_ListObjAppendElement(interp,$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 TCL_ERROR;
}
}
extern void circle(double cx, double cy);

View file

@ -0,0 +1,25 @@
# file: example.tcl
# Try to load as a dynamic module.
catch { load ./example.so example}
catch { load ./example.dll example} ;# Windows
# Call our gcd() function
set x 42
set y 105
set g [gcd $x $y]
puts "The gcd of $x and $y is $g"
# call the gcdmain
gcdmain "gcdmain 42 105"
# call count
set c [count "Hello World" l]
puts $c
# call capitalize
set c [capitalize "helloworld"]
puts $c

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -0,0 +1,19 @@
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)' tcl_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View 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; }
};

View 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;
}
};

View file

@ -0,0 +1,31 @@
# Operator overloading example
catch { load ./example.so example}
catch { load ./example.dll example} ;# Windows
set a [Complex -args 2 3]
set b [Complex -args -5 10]
puts "a = $a [$a str]"
puts "b = $b [$b str]"
set c [$a + $b]
Complex -this $c
puts "c = $c [$c str]"
set d [$a * $b]
Complex -this $d
puts "a*b = [$d str]"
# Alternative calling convention
set e [Complex_- $a $c]
puts "a-c = [Complex_str $e]"
set f [new_ComplexCopy $e]
Complex -this $f
puts "f = [$f str]"
# Call assignment operator
$c = $f
puts "c = [$c str]"

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o my_tclsh *~ .~* core *.so *.sl
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View file

@ -6,7 +6,9 @@
/* 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 */

View file

@ -5,9 +5,12 @@ catch { load ./example.dll example} ;# Windows
# First create some objects using the pointer library.
puts "Testing the pointer library"
set a [ptrcreate int 37]
set b [ptrcreate int 42]
set c [ptrcreate int] ;# Memory for result
set a [new_intp]
set b [new_intp]
set c [new_intp] ;# Memory for result
intp_assign $a 37
intp_assign $b 42
puts " a = $a"
puts " b = $b"
@ -17,13 +20,13 @@ puts " c = $c"
add $a $b $c
# Now get the result
set r [ptrvalue $c]
set r [intp_value $c]
puts " 37 + 42 = $r"
# Clean up the pointers
ptrfree $a
ptrfree $b
ptrfree $c
delete_intp $a
delete_intp $b
delete_intp $c
# Now try the typemap library
# This should be much easier. Now how it is no longer

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh_cpp_static
clean::
rm -f *_wrap* *.o *~ *.so mytclsh *.pyc .~* core
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View file

@ -31,7 +31,7 @@ public:
int size();
/* This wrapper provides an alternative to the [] operator */
%addmethods {
%extend {
Vector &get(int index) {
return (*self)[index];
}

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o my_tclsh *~ .~* core *.so *.sl
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View 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 "$(TCL_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 "$(TCL_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 "$(TCL_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 "$(TCL_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 TCL_INCLUDE: %TCL_INCLUDE%
echo TCL_LIB: %TCL_LIB%
echo on
..\..\..\swig -tcl8 $(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 TCL_INCLUDE: %TCL_INCLUDE%
echo TCL_LIB: %TCL_LIB%
echo on
..\..\..\swig -tcl8 $(InputPath)
# End Custom Build
!ENDIF
# End Source File
# End Target
# End Project

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o my_tclsh *~ .~* core *.so *.sl
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View file

@ -0,0 +1,9 @@
*_wrap.c
*_wrap.cxx
example.dll
example.dsw
example.ncb
example.opt
example.plg
Release
Debug

View file

@ -14,6 +14,6 @@ static::
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o my_tclsh *~ .~* core *.so *.sl
$(MAKE) -f $(TOP)/Makefile tcl_clean
check: all

View file

@ -27,10 +27,10 @@ extern Point pt;
/* Some read-only variables */
%readonly {
extern int status;
extern char path[256];
}
%immutable;
extern int status;
extern char path[256];
%mutable;
/* Some helper functions to make it easier to test */
extern void print_vars();

View file

@ -47,21 +47,21 @@ Getting the "value" returns a pointer to the global variable. Setting the value
<h2>Creating read-only variables</h2>
The <tt>%readonly</tt> and <tt>%readwrite</tt> directives can be used to
The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to
specify a collection of read-only variables. For example:
<blockquote>
<pre>
%readonly
%immutable;
int status;
double blah;
...
%readwrite
%mutable;
</pre>
</blockquote>
The <tt>%readonly</tt> directive remains in effect until it is explicitly disabled
using the <tt>%readwrite</tt> directive.
The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled
using the <tt>%mutable</tt> directive.
<h2>Comments</h2>
<ul>