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:
Dave Beazley 2002-11-30 22:01:28 +00:00
commit 12a43edc2d
1508 changed files with 125983 additions and 44037 deletions

21
Examples/ruby/check.list Normal file
View file

@ -0,0 +1,21 @@
# see top-level Makefile.in
class
constants
enum
funcptr
funcptr2
functor
hashargs
import
template
mpointer
multimap
operator
overloading
pointer
reference
simple
std_vector
template
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='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
rm -f *_wrap* *.o *~ *.so myruby .~* core
$(MAKE) -f $(TOP)/Makefile ruby_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,154 @@
# 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 "$(RUBY_INCLUDE)" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /D NT=1 /D "IMPORT" /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 "$(RUBY_LIB)" /nologo /dll /debug /machine:I386 /out:"example.dll" /pdbtype:sept /EXPORT:Init_example
# SUBTRACT LINK32 /pdb:none
!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 "$(RUBY_INCLUDE)" /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /D NT=1 /D "IMPORT" /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 "$(RUBY_LIB)" /nologo /dll /machine:I386 /out:"example.dll" /EXPORT:Init_example
# SUBTRACT LINK32 /pdb:none
!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.c" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
echo In order to function correctly, please ensure the following environment variables are correctly set:
echo RUBY_INCLUDE: %RUBY_INCLUDE%
echo RUBY_LIB: %RUBY_LIB%
echo on
..\..\..\swig -c++ -ruby $(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 RUBY_INCLUDE: %RUBY_INCLUDE%
echo RUBY_LIB: %RUBY_LIB%
echo on
..\..\..\swig -c++ -ruby $(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,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);
};

View file

@ -188,29 +188,9 @@ 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 Ruby extension. Sorry.
<p>
<li>Dave's snide remark: Like a large bottle of strong Tequilla, it's better to
use C++ in moderation.

View file

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

View file

@ -13,6 +13,6 @@ static::
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_static
clean::
rm -f *_wrap* *.o *~ *.so myruby .~* core
$(MAKE) -f $(TOP)/Makefile ruby_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

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='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
rm -f *_wrap* *.o *~ *.so myruby .~* core
$(MAKE) -f $(TOP)/Makefile ruby_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

@ -13,6 +13,6 @@ static::
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_static
clean::
rm -f *_wrap* *.o *~ *.so myruby .~* core
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View file

@ -8,8 +8,8 @@
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;

View file

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

View file

@ -0,0 +1,18 @@
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)' ruby
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

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

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

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

View file

@ -0,0 +1,18 @@
require 'example'
a = 37
b = 42
# Now call our C function with a bunch of callbacks
puts "Trying some C callback functions"
puts " a = #{a}"
puts " b = #{b}"
puts " ADD(a,b) = #{Example.do_op(a,b,Example::ADD)}"
puts " SUB(a,b) = #{Example.do_op(a,b,Example::SUB)}"
puts " MUL(a,b) = #{Example.do_op(a,b,Example::MUL)}"
puts "Here is what the C callback function objects look like in Ruby"
puts " ADD = #{Example::ADD}"
puts " SUB = #{Example::SUB}"
puts " MUL = #{Example::MUL}"

View file

@ -0,0 +1,2 @@
example_wrap.cxx
example.dll

View file

@ -0,0 +1,18 @@
TOP = ../..
SWIG = $(TOP)/../swig
TARGET = example
INTERFACE = example.i
LIBS = -lm
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ruby_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View file

@ -0,0 +1,26 @@
/* 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 Ruby.
* 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>;

View file

@ -0,0 +1,17 @@
# Operator overloading example
require 'example'
a = Example::IntSum.new(0)
b = Example::DoubleSum.new(100.0)
# Use the objects. They should be callable just like a normal
# Ruby function.
(0..100).each do |i|
a.call(i) # note: function call
b.call(Math.sqrt(i)) # note: function call
end
puts a.result
puts b.result

View file

@ -0,0 +1 @@
example_wrap.c

20
Examples/ruby/hashargs/Makefile Executable file
View file

@ -0,0 +1,20 @@
TOP = ../..
SWIG = $(TOP)/../swig
SRCS =
TARGET = example
INTERFACE = example.i
LIBS = -lm
SWIGOPT =
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ruby
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View file

@ -0,0 +1,36 @@
%module example
%typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_ary, int i, VALUE key, VALUE val) {
Check_Type($input, T_HASH);
$1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
$2 = NULL;
$3 = NULL;
if ($1 > 0) {
$2 = (char **) malloc($1*sizeof(char *));
$3 = (int *) malloc($1*sizeof(int));
keys_ary = rb_funcall($input, rb_intern("keys"), 0, NULL);
for (i = 0; i < $1; i++) {
key = rb_ary_entry(keys_ary, i);
val = rb_hash_aref($input, key);
Check_Type(key, T_STRING);
Check_Type(val, T_FIXNUM);
$2[i] = STR2CSTR(key);
$3[i] = NUM2INT(val);
}
}
}
%typemap(freearg) (int nattributes, const char **names, const int *values) {
free((void *) $2);
free((void *) $3);
}
%inline %{
void setVitalStats(const char *person, int nattributes, const char **names, const int *values) {
int i;
printf("Name: %s\n", person);
for (i = 0; i < nattributes; i++) {
printf(" %s => %d\n", names[i], values[i]);
}
}
%}

26
Examples/ruby/hashargs/runme.rb Executable file
View file

@ -0,0 +1,26 @@
require 'example'
include Example
# Pass arguments in as one or more key-value pairs
setVitalStats("Fred",
'age' => 42,
'weight' => 270
)
# The order doesn't matter
setVitalStats("Sally",
'age' => 29,
'weight' => 115,
'height' => 72
)
# Can also pass a hash directly
params = {
'ears' => 2,
'eyes' => 2
}
setVitalStats("Bob", params)
# An empty hash is fine too
setVitalStats("Joe", {})

View file

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

View file

@ -0,0 +1,20 @@
TOP = ../..
SWIG = $(TOP)/../swig
SWIGOPT = -c
#If your system requires linking with the runtime libraries then set the directory location here
RUNTIMEDIR = /usr/local/lib
all::
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='base' INTERFACE='base.i' ruby_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='foo' INTERFACE='foo.i' ruby_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='bar' INTERFACE='bar.i' ruby_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='spam' INTERFACE='spam.i' ruby_multi_cpp
clean::
$(MAKE) -f $(TOP)/Makefile ruby_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 'ruby runme.rb' 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,21 @@
#include "base.h"
class Bar : public Base {
public:
Bar() { }
~Bar() { }
virtual const char * A() const {
return "Bar::A";
}
const char * B() const {
return "Bar::B";
}
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,16 @@
class Base {
public:
Base() { };
~Base() { };
virtual const char * A() const {
return "Base::A";
}
const char * B() const {
return "Base::B";
}
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 const char * A() const {
return "Foo::A";
}
const char * B() const {
return "Foo::B";
}
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,90 @@
# file: runme.rb
# Test various properties of classes defined in separate modules
puts "Testing the %import directive"
require 'base'
require 'foo'
require 'bar'
require 'spam'
# Create some objects
puts "Creating some objects"
a = Base::Base.new
b = Foo::Foo.new
c = Bar::Bar.new
d = Spam::Spam.new
# 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"
x = a.toBase
puts "Should see 'Base::A' ---> #{x.A}"
puts "Should see 'Base::B' ---> #{x.B}"
x = b.toBase
puts "Should see 'Foo::A' ---> #{x.A}"
puts "Should see 'Base::B' ---> #{x.B}"
x = c.toBase
puts "Should see 'Bar::A' ---> #{x.A}"
puts "Should see 'Base::B' ---> #{x.B}"
x = d.toBase
puts "Should see 'Spam::A' ---> #{x.A}"
puts "Should see 'Base::B' ---> #{x.B}"
x = d.toBar
puts "Should see 'Bar::B' ---> #{x.B}"
puts "\nTesting some dynamic casts\n"
x = d.toBase
puts " Spam -> Base -> Foo : "
y = Foo::Foo.fromBase(x)
if y != nil
puts "bad swig"
else
puts "good swig"
end
puts " Spam -> Base -> Bar : "
y = Bar::Bar.fromBase(x)
if y != nil
puts "good swig"
else
puts "bad swig"
end
puts " Spam -> Base -> Spam : "
y = Spam::Spam.fromBase(x)
if y != nil
puts "good swig"
else
puts "bad swig"
end
puts " Foo -> Spam : "
y = Spam::Spam.fromBase(b)
if y != nil
puts "bad swig"
else
puts "good swig"
end

View file

@ -0,0 +1,24 @@
#include "bar.h"
class Spam : public Bar {
public:
Spam() { }
~Spam() { }
virtual const char * A() const {
return "Spam::A";
}
const char * B() const {
return "Spam::B";
}
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

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

View file

@ -0,0 +1,20 @@
TOP = ../..
SWIG = $(TOP)/../swig
SWIGOPT = -c
#If your system requires linking with the runtime libraries then set the directory location here
RUNTIMEDIR = /usr/local/lib
all::
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='base' INTERFACE='base.i' ruby_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='foo' INTERFACE='foo.i' ruby_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='bar' INTERFACE='bar.i' ruby_multi_cpp
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
RUNTIMEDIR='$(RUNTIMEDIR)' TARGET='spam' INTERFACE='spam.i' ruby_multi_cpp
clean::
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View 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 'ruby runme.rb' 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"
template<class T> class Bar : public Base<T> {
public:
Bar() { }
~Bar() { }
virtual const char * A() const {
return "Bar::A";
}
const char * B() const {
return "Bar::B";
}
virtual Base<T> *toBase() {
return static_cast<Base<T> *>(this);
}
static Bar<T> *fromBase(Base<T> *b) {
return dynamic_cast<Bar<T> *>(b);
}
};

View file

@ -0,0 +1,11 @@
%module bar
%{
#include "bar.h"
%}
%import base.i
%include "bar.h"
%template(IntBar) Bar<int>;

View file

@ -0,0 +1,18 @@
#include <stdio.h>
template<class T> class Base {
public:
Base() { };
~Base() { };
virtual const char * A() const {
return "Base::A";
}
const char * B() const {
return "Base::B";
}
virtual Base<T> *toBase() {
return static_cast<Base<T> *>(this);
}
};

View file

@ -0,0 +1,7 @@
%module base
%{
#include "base.h"
%}
%include base.h
%template(IntBase) Base<int>;

View file

@ -0,0 +1,21 @@
#include "base.h"
template<class T> class Foo : public Base<T> {
public:
Foo() { }
~Foo() { }
virtual const char * A() const {
return "Foo::A";
}
const char * B() const {
return "Foo::B";
}
virtual Base<T> *toBase() {
return static_cast<Base<T> *>(this);
}
static Foo<T> *fromBase(Base<T> *b) {
return dynamic_cast<Foo<T> *>(b);
}
};

View file

@ -0,0 +1,10 @@
%module foo
%{
#include "foo.h"
%}
%import base.i
%include "foo.h"
%template(IntFoo) Foo<int>;

View file

@ -0,0 +1,92 @@
# file: runme.rb
# Test various properties of classes defined in separate modules
puts "Testing the %import directive with templates"
require 'base'
require 'foo'
require 'bar'
require 'spam'
# Create some objects
puts "Creating some objects"
a = Base::IntBase.new
b = Foo::IntFoo.new
c = Bar::IntBar.new
d = Spam::IntSpam.new
# Try calling some methods
puts "Testing some methods"
puts ""
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"
puts ""
x = a.toBase
puts "Should see 'Base::A' ---> #{x.A}"
puts "Should see 'Base::B' ---> #{x.B}"
x = b.toBase
puts "Should see 'Foo::A' ---> #{x.A}"
puts "Should see 'Base::B' ---> #{x.B}"
x = c.toBase
puts "Should see 'Bar::A' ---> #{x.A}"
puts "Should see 'Base::B' ---> #{x.B}"
x = d.toBase
puts "Should see 'Spam::A' ---> #{x.A}"
puts "Should see 'Base::B' ---> #{x.B}"
x = d.toBar
puts "Should see 'Bar::B' ---> #{x.B}"
puts "\nTesting some dynamic casts\n"
x = d.toBase
puts " Spam -> Base -> Foo : "
y = Foo::IntFoo.fromBase(x)
if y != nil
puts "bad swig"
else
puts "good swig"
end
puts " Spam -> Base -> Bar : "
y = Bar::IntBar.fromBase(x)
if y != nil
puts "good swig"
else
puts "bad swig"
end
puts " Spam -> Base -> Spam : "
y = Spam::IntSpam.fromBase(x)
if y != nil
puts "good swig"
else
puts "bad swig"
end
puts " Foo -> Spam : "
y = Spam::IntSpam.fromBase(b)
if y != nil
puts "bad swig"
else
puts "good swig"
end

View file

@ -0,0 +1,24 @@
#include "bar.h"
template<class T> class Spam : public Bar<T> {
public:
Spam() { }
~Spam() { }
virtual const char * A() const {
return "Spam::A";
}
const char * B() const {
return "Spam::B";
}
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);
}
};

View file

@ -0,0 +1,10 @@
%module spam
%{
#include "spam.h"
%}
%import bar.i
%include "spam.h"
%template(IntSpam) Spam<int>;

View file

@ -36,7 +36,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):

View file

@ -0,0 +1,2 @@
example_wrap.cxx
example.dll

View file

@ -0,0 +1,20 @@
TOP = ../..
SWIG = $(TOP)/../swig
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
LIBS = -lm
SWIGOPT =
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ruby_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

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

View file

@ -0,0 +1,47 @@
/* 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);

View file

@ -0,0 +1,15 @@
/* 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;

View file

@ -0,0 +1,48 @@
# Example using pointers to member functions
require 'example'
# Get the pointers
area_pt = Example::areapt
perim_pt = Example::perimeterpt
puts "area_pt = #{area_pt}"
puts "perim_pt = #{perim_pt}"
# Create some objects
c = Example::Circle.new(4)
s = Example::Square.new(10)
# Do some calculations
puts "Circle area = #{Example::do_op(c, area_pt)}"
puts "Circle perim = #{Example::do_op(c, perim_pt)}"
puts "Square area = #{Example::do_op(s, area_pt)}"
puts "Square perim = #{Example::do_op(s, perim_pt)}"
puts "areavar = #{Example::areavar}"
puts "perimetervar = #{Example::perimetervar}"
# Try the variables
puts "Circle area = #{Example::do_op(c, Example::areavar)}"
puts "Circle perim = #{Example::do_op(c, Example::perimetervar)}"
puts "Square area = #{Example::do_op(s, Example::areavar)}"
puts "Square perim = #{Example::do_op(s, Example::perimetervar)}"
# Modify one of the variables
Example::areavar = perim_pt
puts "Circle perimeter = #{Example::do_op(c, Example::areavar)}"
# Try the constants
puts "Example::AREAPT = #{Example::AREAPT}"
puts "Example::PERIMPT= #{Example::PERIMPT}"
puts "Example::NULLPT = #{Example::NULLPT}"
puts "Circle area = #{Example::do_op(c, Example::AREAPT)}"
puts "Circle perim = #{Example::do_op(c, Example::PERIMPT)}"
puts "Square area = #{Example::do_op(s, Example::AREAPT)}"
puts "Square perim = #{Example::do_op(s, Example::PERIMPT)}"

View file

@ -0,0 +1,2 @@
example_wrap.c
example.dll

View file

@ -0,0 +1,18 @@
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)' ruby
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_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,150 @@
# 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 "$(RUBY_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 $(RUBY_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 "$(RUBY_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 $(RUBY_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 RUBY_INCLUDE: %RUBY_INCLUDE%
echo RUBY_LIB: %RUBY_LIB%
echo **WARNING** I could not get Ruby (1.6.4) and SWIG to work under Visual Studio (6)
echo on
..\..\..\swig -ruby $(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 RUBY_INCLUDE: %RUBY_INCLUDE%
echo RUBY_LIB: %RUBY_LIB%
echo **WARNING** I could not get Ruby (1.6.4) and SWIG to work under Visual Studio (6)
echo on
..\..\..\swig -ruby $(InputPath)
# End Custom Build
!ENDIF
# End Source File
# End Target
# End Project

View file

@ -0,0 +1,83 @@
%module example
%include exception.i
%include typemaps.i
extern int gcd(int x, int y);
%typemap(in) (int argc, char *argv[]) {
int i;
if (TYPE($input) != T_ARRAY) {
SWIG_exception(SWIG_ValueError, "Expected an array");
}
$1 = RARRAY($input)->len;
if ($1 == 0) {
SWIG_exception(SWIG_ValueError, "List must contain at least 1 element");
}
$2 = (char **) malloc(($1+1)*sizeof(char *));
for (i = 0; i < $1; i++) {
VALUE s = rb_ary_entry($input,i);
if (TYPE(s) != T_STRING) {
free($2);
SWIG_exception(SWIG_ValueError, "List items must be strings");
}
$2[i] = STR2CSTR(s);
}
$2[i] = 0;
}
%typemap(freearg) (int argc, char *argv[]) {
free($2);
}
extern int gcdmain(int argc, char *argv[]);
%typemap(in) (char *bytes, int len) {
if (TYPE($input) != T_STRING) {
SWIG_exception(SWIG_ValueError, "Expected a string");
}
$1 = STR2CSTR($input);
$2 = RSTRING($input)->len;
}
extern int count(char *bytes, int len, char c);
/* This example shows how to wrap a function that mutates a string */
%typemap(in) (char *str, int len) {
char *temp;
if (TYPE($input) != T_STRING) {
SWIG_exception(SWIG_ValueError,"Expected a string");
}
temp = STR2CSTR($input);
$2 = RSTRING($input)->len;
$1 = (char *) malloc($2+1);
memmove($1,temp,$2);
}
/* Return the mutated string as a new object. */
%typemap(argout, fragment="output_helper") (char *str, int len) {
VALUE o;
o = rb_str_new($1,$2);
$result = 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");
}
}
extern void circle(double cx, double cy);

22
Examples/ruby/multimap/runme.rb Executable file
View file

@ -0,0 +1,22 @@
# file: run.rb
require 'example'
# Call our gcd() function
x = 42
y = 105
g = Example.gcd(x,y)
printf "The gcd of %d and %d is %d\n",x,y,g
# Call the gcdmain() function
Example.gcdmain(["gcdmain","42","105"])
# Call the count function
printf "%d\n",Example.count("Hello World","l")
# Call the capitalize function
printf "%s\n",Example.capitalize("hello world")

View file

@ -0,0 +1,2 @@
example_wrap.cxx
example.dll

View file

@ -0,0 +1,20 @@
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)' ruby_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_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,23 @@
/* 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 */
/* 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,25 @@
# Operator overloading example
require 'example'
include Example
a = Complex.new(2, 3)
b = Complex.new(-5, 10)
puts "a = #{a}"
puts "b = #{b}"
c = a + b
puts "c = #{c}"
puts "a*b = #{a*b}"
puts "a-c = #{a-c}"
# This should invoke Complex's copy constructor
e = Complex.new(a-c)
e = a - c
puts "e = #{e}"
# Big expression
f = ((a+b)*(c+b*e)) + (-a)
puts "f = #{f}"

View file

@ -0,0 +1 @@
example_wrap.cxx

View file

@ -0,0 +1,19 @@
TOP = ../..
SWIG = $(TOP)/../swig
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
LIBS = -lm
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ruby_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View file

@ -0,0 +1,125 @@
#include <iostream>
#include "example.h"
// Overloaded constructors for class Bar
Bar::Bar() {
std::cout << "Called Bar::Bar()" << std::endl;
}
Bar::Bar(const Bar&) {
std::cout << "Called Bar::Bar(const Bar&)" << std::endl;
}
Bar::Bar(double x) {
std::cout << "Called Bar::Bar(double) with x = " << x << std::endl;
}
Bar::Bar(double x, char *y) {
std::cout << "Called Bar::Bar(double, char *) with x, y = " << x << ", \"" << y << "\"" << std::endl;
}
Bar::Bar(int x, int y) {
std::cout << "Called Bar::Bar(int, int) with x, y = " << x << ", " << y << std::endl;
}
Bar::Bar(char *x) {
std::cout << "Called Bar::Bar(char *) with x = \"" << x << "\"" << std::endl;
}
Bar::Bar(int x) {
std::cout << "Called Bar::Bar(int) with x = " << x << std::endl;
}
Bar::Bar(long x) {
std::cout << "Called Bar::Bar(long) with x = " << x << std::endl;
}
Bar::Bar(Bar *x) {
std::cout << "Called Bar::Bar(Bar *) with x = " << x << std::endl;
}
// Overloaded member functions
void Bar::foo(const Bar& x) {
std::cout << "Called Bar::foo(const Bar&) with &x = " << &x << std::endl;
}
void Bar::foo(double x) {
std::cout << "Called Bar::foo(double) with x = " << x << std::endl;
}
void Bar::foo(double x, char *y) {
std::cout << "Called Bar::foo(double, char *) with x, y = " << x << ", \"" << y << "\"" << std::endl;
}
void Bar::foo(int x, int y) {
std::cout << "Called Bar::foo(int, int) with x, y = " << x << ", " << y << std::endl;
}
void Bar::foo(char *x) {
std::cout << "Called Bar::foo(char *) with x = \"" << x << "\"" << std::endl;
}
void Bar::foo(int x) {
std::cout << "Called Bar::foo(int) with x = " << x << std::endl;
}
void Bar::foo(long x) {
std::cout << "Called Bar::foo(long) with x = " << x << std::endl;
}
void Bar::foo(Bar *x) {
std::cout << "Called Bar::foo(Bar *) with x = " << x << std::endl;
}
void Bar::spam(int x, int y, int z) {
std::cout << "Called Bar::spam(int, int, int) with x, y, z = " << x << ", " << y << ", " << z << std::endl;
}
void Bar::spam(double x, int y, int z) {
std::cout << "Called Bar::spam(double, int, int) with x, y, z = " << x << ", " << y << ", " << z << std::endl;
}
// Overloaded global methods
void foo(const Bar& x) {
std::cout << "Called foo(const Bar& x) with &x = " << &x << std::endl;
}
void foo(double x) {
std::cout << "Called foo(double) with x = " << x << std::endl;
}
void foo(double x, char *y) {
std::cout << "Called foo(double, char *) with x, y = " << x << ", \"" << y << "\"" << std::endl;
}
void foo(int x, int y) {
std::cout << "Called foo(int, int) with x, y = " << x << ", " << y << std::endl;
}
void foo(char *x) {
std::cout << "Called foo(char *) with x = \"" << x << "\"" << std::endl;
}
void foo(int x) {
std::cout << "Called foo(int) with x = " << x << std::endl;
}
void foo(long x) {
std::cout << "Called foo(long) with x = " << x << std::endl;
}
void foo(Bar *x) {
std::cout << "Called foo(Bar *) with x = " << x << std::endl;
}
// Overloaded global spam() functions
void spam(int x, int y, int z) {
std::cout << "Called spam(int, int, int) with x, y, z = " << x << ", " << y << ", " << z << std::endl;
}
void spam(double x, int y, int z) {
std::cout << "Called spam(double, int, int) with x, y, z = " << x << ", " << y << ", " << z << std::endl;
}

View file

@ -0,0 +1,41 @@
#ifndef EXAMPLE_H
#define EXAMPLE_H
class Bar {
public:
Bar();
Bar(const Bar&);
Bar(double);
Bar(double, char *);
Bar(int, int);
Bar(char *);
Bar(long);
Bar(int);
Bar(Bar *);
void foo(const Bar&);
void foo(double);
void foo(double, char *);
void foo(int, int);
void foo(char *);
void foo(long);
void foo(int);
void foo(Bar *);
void spam(int x, int y=2, int z=3);
void spam(double x, int y=2, int z=3);
};
void foo(const Bar&);
void foo(double);
void foo(double, char *);
void foo(int, int);
void foo(char *);
void foo(int);
void foo(long);
void foo(Bar *);
void spam(int x, int y=2, int z=3);
void spam(double x, int y=2, int z=3);
#endif

View file

@ -0,0 +1,24 @@
%module example
%{
#include "example.h"
%}
/**
* These overloaded declarations conflict with other overloads (as far as
* SWIG's Ruby module's implementation for overloaded methods is concerned).
* One option is use the %rename directive to rename the conflicting methods;
* here, we're just using %ignore to avoid wrapping some of the overloaded
* functions altogether.
*/
%ignore Bar::Bar(Bar *);
%ignore Bar::Bar(long);
%ignore Bar::foo(const Bar&);
%ignore Bar::foo(long);
%ignore ::foo(const Bar&);
%ignore ::foo(int);
%include example.h

View file

@ -0,0 +1,88 @@
require 'example'
# This should invoke foo(double)
Example.foo(3.14159)
# This should invoke foo(double, char *)
Example.foo(3.14159, "Pi")
# This should invoke foo(int, int)
Example.foo(3, 4)
# This should invoke foo(char *)
Example.foo("This is a test")
# This should invoke foo(long)
Example.foo(42)
# This should invoke Bar::Bar() followed by foo(Bar *)
Example.foo(Example::Bar.new)
# Skip a line
puts ""
# Each of the following three calls should invoke spam(int, int, int)
Example.spam(3)
Example.spam(3, 4)
Example.spam(3, 4, 5)
# Skip a line
puts ""
# Each of the following three calls should invoke spam(double, int, int)
Example.spam(3.0)
Example.spam(3.0, 4)
Example.spam(3.0, 4, 5)
# Skip a line
puts ""
# This should invoke Bar::Bar(double)
Example::Bar.new(3.14159)
# This should invoke Bar::Bar(double, char *)
Example::Bar.new(3.14159, "Pi")
# This should invoke Bar::Bar(int, int)
Example::Bar.new(3, 4)
# This should invoke Bar::Bar(char *)
Example::Bar.new("This is a test")
# This should invoke Bar::Bar(int)
Example::Bar.new(42)
# This should invoke Bar::Bar() for the input argument,
# followed by Bar::Bar(const Bar&).
Example::Bar.new(Example::Bar.new)
# Skip a line
puts ""
# Construct a new Bar instance (invokes Bar::Bar())
bar = Example::Bar.new
# This should invoke Bar::foo(double)
bar.foo(3.14159)
# This should invoke Bar::foo(double, char *)
bar.foo(3.14159, "Pi")
# This should invoke Bar::foo(int, int)
bar.foo(3, 4)
# This should invoke Bar::foo(char *)
bar.foo("This is a test")
# This should invoke Bar::foo(int)
bar.foo(42)
# This should invoke Bar::Bar() to construct the input
# argument, followed by Bar::foo(Bar *).
bar.foo(Example::Bar.new)
# This should invoke Bar::spam(int x, int y, int z)
bar.spam(1)
# This should invoke Bar::spam(double x, int y, int z)
bar.spam(3.14159)

View file

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

View file

@ -13,6 +13,6 @@ static::
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_static
clean::
rm -f *_wrap* *.o *~ *.so myruby .~* core
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View file

@ -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 */

View file

@ -4,9 +4,12 @@ require 'example'
# First create some objects using the pointer library.
print "Testing the pointer library\n"
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}\n"
print " b = #{b}\n"
@ -16,13 +19,13 @@ print " c = #{c}\n"
Example::add(a, b, c)
# Now get the result
r = Example::ptrvalue(c)
r = Example::intp_value(c)
print " 37 + 42 = #{r}\n"
# 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

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='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
rm -f *_wrap* *.o *~ *.so myruby .~* core
$(MAKE) -f $(TOP)/Makefile ruby_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

@ -13,6 +13,6 @@ static::
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_static
clean::
rm -f *_wrap* *.o *~ *.so myruby .~* core
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View file

@ -0,0 +1,150 @@
# 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 "$(RUBY_INCLUDE)" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /D NT=1 /D "IMPORT" /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 "$(RUBY_LIB)" /nologo /dll /debug /machine:I386 /out:"example.dll" /pdbtype:sept /EXPORT:Init_example
# SUBTRACT LINK32 /pdb:none
!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 "$(RUBY_INCLUDE)" /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /D NT=1 /D "IMPORT" /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 "$(RUBY_LIB)" /nologo /dll /machine:I386 /out:"example.dll" /EXPORT:Init_example
# SUBTRACT LINK32 /pdb:none
!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 RUBY_INCLUDE: %RUBY_INCLUDE%
echo RUBY_LIB: %RUBY_LIB%
echo on
..\..\..\swig -ruby $(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 RUBY_INCLUDE: %RUBY_INCLUDE%
echo RUBY_LIB: %RUBY_LIB%
echo on
..\..\..\swig -ruby $(InputPath)
# End Custom Build
!ENDIF
# End Source File
# End Target
# End Project

View file

View file

@ -0,0 +1 @@
example_wrap.cxx

View file

@ -0,0 +1,20 @@
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)' ruby_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View file

@ -0,0 +1,25 @@
/* File : example.h */
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
double average(std::vector<int> v) {
return std::accumulate(v.begin(),v.end(),0.0)/v.size();
}
std::vector<double> half(const std::vector<double>& v) {
std::vector<double> w(v);
for (unsigned int i=0; i<w.size(); i++)
w[i] /= 2.0;
return w;
}
void halve_in_place(std::vector<double>& v) {
// would you believe this is the same as the above?
std::transform(v.begin(),v.end(),v.begin(),
std::bind2nd(std::divides<double>(),2.0));
}

View file

@ -0,0 +1,17 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
%include stl.i
/* instantiate the required template specializations */
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
}
/* Let's just grab the original header file here */
%include "example.h"

View file

@ -0,0 +1,36 @@
# file: runme.rb
require 'example'
# Call average with a Ruby array...
puts Example::average([1,2,3,4])
# ... or a wrapped std::vector<int>
v = Example::IntVector.new(4)
0.upto(v.length-1) { |i| v[i] = i+1 }
puts Example::average(v)
# half will return a Ruby array.
# Call it with a Ruby array...
w = Example::half([1.0, 1.5, 2.0, 2.5, 3.0])
0.upto(w.length-1) { |i| print w[i],"; " }
puts
# ... or a wrapped std::vector<double>
v = Example::DoubleVector.new
[1,2,3,4].each { |i| v.push(i) }
w = Example::half(v)
0.upto(w.length-1) { |i| print w[i],"; " }
puts
# now halve a wrapped std::vector<double> in place
Example::halve_in_place(v)
0.upto(v.length-1) { |i| print v[i],"; " }
puts

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,20 @@
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)' ruby_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile ruby_clean
check: all

View file

@ -0,0 +1,32 @@
/* File : example.h */
// Some template definitions
template<class T> T max(T a, T b) { return a>b ? a : b; }
template<class T> class vector {
T *v;
int sz;
public:
vector(int _sz) {
v = new T[_sz];
sz = _sz;
}
T &get(int index) {
return v[index];
}
void set(int index, T &val) {
v[index] = val;
}
#ifdef SWIG
%extend {
T getitem(int index) {
return self->get(index);
}
void setitem(int index, T val) {
self->set(index,val);
}
}
#endif
};

View file

@ -0,0 +1,17 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Let's just grab the original header file here */
%include "example.h"
/* Now instantiate some specific template declarations */
%template(maxint) max<int>;
%template(maxdouble) max<double>;
%template(Vecint) vector<int>;
%template(Vecdouble) vector<double>;

View file

@ -0,0 +1,25 @@
# file: runme.rb
require 'example'
# Call some templated functions
puts Example::maxint(3, 7)
puts Example::maxdouble(3.14, 2.18)
# Create some class
iv = Example::Vecint.new(100)
dv = Example::Vecdouble.new(1000)
100.times { |i| iv.setitem(i, 2*i) }
1000.times { |i| dv.setitem(i, 1.0/(i+1)) }
sum = 0
100.times { |i| sum = sum + iv.getitem(i) }
puts sum
sum = 0.0
1000.times { |i| sum = sum + dv.getitem(i) }
puts sum

View file

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

Some files were not shown because too many files have changed in this diff Show more