diff options
author | Jan Jezabek <jezabek@poczta.onet.pl> | 2008-08-16 12:48:49 +0000 |
---|---|---|
committer | Jan Jezabek <jezabek@poczta.onet.pl> | 2008-08-16 12:48:49 +0000 |
commit | 4bdd67e89fbd19941cd6b60bdafd67a23917d5c3 (patch) | |
tree | 62f748195e1e6c7f040d4822af4cf33c61d73fff | |
parent | 83eff7b8672912730ad2f4e07f3e1f05ed19b2ba (diff) | |
download | swig-4bdd67e89fbd19941cd6b60bdafd67a23917d5c3.tar.gz |
Added a VBS run-test. Added 'class' example.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-jezabek@10768 626c5289-ae23-0410-ae9c-e8d60b6d4f22
-rw-r--r-- | Examples/Makefile.in | 3 | ||||
-rw-r--r-- | Examples/com/check.list | 1 | ||||
-rw-r--r-- | Examples/com/class/Makefile | 17 | ||||
-rw-r--r-- | Examples/com/class/example.cxx | 28 | ||||
-rw-r--r-- | Examples/com/class/example.h | 39 | ||||
-rw-r--r-- | Examples/com/class/example.i | 10 | ||||
-rw-r--r-- | Examples/com/class/index.html | 207 | ||||
-rw-r--r-- | Examples/com/class/runme.vbs | 69 | ||||
-rw-r--r-- | Examples/test-suite/com/aggregate_runme.vbs | 44 |
9 files changed, 418 insertions, 0 deletions
diff --git a/Examples/Makefile.in b/Examples/Makefile.in index eb0bb6cb2..4cd3f3d0d 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -1132,3 +1132,6 @@ com_cpp: $(SRCS) com_compile: $(SRCS) $(COMPILETOOL) $(COM_CC) $(COM_CC_SWITCHES) $(COM_CC_OUT_SWITCHES)$(TARGET)$(COM_EXEC_SUFFIX) $(COMSRCS) $(COM_LIBS) +com_clean: + rm -f *_wrap* *.idl *.def *_rc.* *.o *.obj *.map *.lib *.tlb *.exp *.dll$(COM_EXEC_SUFFIX) *~ + diff --git a/Examples/com/check.list b/Examples/com/check.list index ae45293b9..f9b363696 100644 --- a/Examples/com/check.list +++ b/Examples/com/check.list @@ -1 +1,2 @@ # see top-level Makefile.in +class diff --git a/Examples/com/class/Makefile b/Examples/com/class/Makefile new file mode 100644 index 000000000..abe9fb7a1 --- /dev/null +++ b/Examples/com/class/Makefile @@ -0,0 +1,17 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = + +all:: com + +com:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' com_cpp + +clean:: + $(MAKE) -f $(TOP)/Makefile com_clean + +check: all diff --git a/Examples/com/class/example.cxx b/Examples/com/class/example.cxx new file mode 100644 index 000000000..1e8e203dd --- /dev/null +++ b/Examples/com/class/example.cxx @@ -0,0 +1,28 @@ +/* File : example.c */ + +#include "example.h" +#define M_PI 3.14159265358979323846 + +/* 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; +} diff --git a/Examples/com/class/example.h b/Examples/com/class/example.h new file mode 100644 index 000000000..46d901361 --- /dev/null +++ b/Examples/com/class/example.h @@ -0,0 +1,39 @@ +/* File : example.h */ + +class Shape { +public: + Shape() { + nshapes++; + } + virtual ~Shape() { + nshapes--; + }; + double x, y; + 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); +}; + + + + + diff --git a/Examples/com/class/example.i b/Examples/com/class/example.i new file mode 100644 index 000000000..75700b305 --- /dev/null +++ b/Examples/com/class/example.i @@ -0,0 +1,10 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/Examples/com/class/index.html b/Examples/com/class/index.html new file mode 100644 index 000000000..ddfa74a44 --- /dev/null +++ b/Examples/com/class/index.html @@ -0,0 +1,207 @@ +<html> +<head> +<title>SWIG:Examples:com:class</title> +</head> + +<body bgcolor="#ffffff"> + + +<tt>SWIG/Examples/com/class/</tt> +<hr> + +<H2>Wrapping a simple C++ class</H2> + +<p> +This example illustrates the high level form of C++ class wrapping performed +by SWIG. In this case, a C++ class has a proxy COM class, which +provides access to C++ class members. + +<h2>The C++ Code</h2> + +Suppose you have some C++ classes described by the following (and admittedly lame) +header file: + +<blockquote> +<pre> +/* File : example.h */ + +class Shape { +public: + Shape() { + nshapes++; + } + virtual ~Shape() { + nshapes--; + }; + double x, y; + void move(double dx, double dy); + virtual double area() = 0; + virtual double perimeter() = 0; + static int nshapes; +}; + +class Circle : public Shape { +private: + double radius; +public: + Circle(double r) : radius(r) { }; + virtual double area(); + virtual double perimeter(); +}; + +class Square : public Shape { +private: + double width; +public: + Square(double w) : width(w) { }; + virtual double area(); + virtual double perimeter(); +}; +</pre> +</blockquote> + +<h2>The SWIG interface</h2> + +A simple SWIG interface for this can be built by simply grabbing the header file +like this: + +<blockquote> +<pre> +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" +</pre> +</blockquote> + +Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this: +<blockquote> +<pre> +% swig -c++ -com example.i +</pre> +</blockquote> + +<h2>A sample VBScript program</h2> + +Click <a href="runme.vbs">here</a> to see a VBScript program that calls the C++ functions using COM. Please note that you will need to compile your C/C++ +module and the wrapper to a DLL and then register it using <tt>regsvr32</tt> +before your module can be used from VBScript. +Further details are available in the SWIG manual. + +<h2>Key points</h2> + +<ul> +<li>Usually you'll want to create an object of the module class: + +<blockquote> +<pre> +Dim example +Set example = CreateObject("example.example") +</pre> +</blockquote> + +<p> +<li>To create a new object, you call a constructor like this: + +<blockquote> +<pre> +Dim c +Set c = example.Circle.new_Circle(10) +</pre> +</blockquote> + +<p> +<li>If your class has a default constructor you can use an alternative +way of creating the object (note that this will not work with the +interface presented above): + +<blockquote> +<pre> +Dim c +Set c = CreateObject("example.Circle") +</pre> +</blockquote> + +<p> +<li>You can access member data using COM properties, which means that +the syntax is very similar to C/C++. For example: + +<blockquote> +<pre> +Rem Set member data +c.x = 15 +Rem Get member data +x = c.x +</pre> +</blockquote> + +<p> +<li>To invoke a member function, you simply do this + +<blockquote> +<pre> +WScript.Echo "The area is " & c.area() +</pre> +</blockquote> + +<p> +<li>If you want to destroy an object you will need to destroy all +references to it, e.g.: + +<blockquote> +<pre> +c = empty +</pre> +</blockquote> + +<p> +<li>Static member variables are wrapped as properties of the 'class object' which can be accessed from the module class object. For example: + +<blockquote> +<pre> +n = example.Shape.nshapes +example.Shape.nshapes = 13 +</pre> +</blockquote> + +</ul> + +<h2>General Comments</h2> + +<ul> +<li>A limitation of SWIG's COM module is that it supports only single +inheritance. This might change in the future. + +<p> +<li>A wide variety of C++ features are not currently supported by SWIG. Here is the +short and incomplete list: + +<p> +<ul> +<li>Overloaded methods and functions. COM does not support name overloading +so you must give an alternative name to any overloaded method name using the +%rename directive like this: + +<blockquote> +<pre> +void foo(int a); +%rename(foo2) void foo(double a, double b); +</pre> +</blockquote> + +<p> +<li>Namespaces. The namespace syntax is understood by SWIG, but namespace +declarations are ignored. You can specify a prefix for the IDispatch +names of your objects using the <tt>-namespace</tt> command line option. + +</ul> +</ul> + +<hr> +</body> +</html> diff --git a/Examples/com/class/runme.vbs b/Examples/com/class/runme.vbs new file mode 100644 index 000000000..42ae2c43e --- /dev/null +++ b/Examples/com/class/runme.vbs @@ -0,0 +1,69 @@ +Rem This example illustrates how C++ classes can be used from COM using SWIG. +Rem The COM objects get mapped onto the C++ objects + +Rem ----- Create an object of the module class ----- + +Dim example +Set example = CreateObject("example.example") + +Rem ----- Object creation ----- + +Dim c, s +WScript.Echo "Creating some objects:" +Set c = example.Circle.new_Circle(10) +WScript.Echo " Created circle" +Set s = example.Square.new_Square(10) +WScript.Echo " Created square" + +Rem ----- Access a static member ----- + +WScript.Echo "" +WScript.Echo "A total of " & example.Shape.nshapes & " shapes were created" + +Rem ----- Member data access ----- + +c.x = 20 +c.y = 30 + +s.x = -10 +s.y = 5 + +WScript.Echo "" +WScript.Echo "Here is their current position:" +WScript.Echo " Circle = (" & c.x & " " & c.y & ")" +WScript.Echo " Square = (" & s.x & " " & s.y & ")" + +Rem ----- Call some methods ----- + +WScript.Echo "" +WScript.Echo "Here are some properties of the shapes:" + +Dim shapes(2), names(2) +Set shapes(1) = c +names(1) = "Circle" +Set shapes(2) = s +names(2) = "Square" + +For i = 1 to 2 + WScript.Echo " " & names(i) + WScript.Echo " area = " & shapes(i).area() + WScript.Echo " perimeter = " & shapes(i).perimeter() +Next + +Rem Notice how the area() and perimeter() functions really +Rem invoke the appropriate virtual method on each object. + +Rem ----- Delete everything ----- + +WScript.Echo "" +WScript.Echo "Guess I'll clean up now" + +Rem Note: this decreases the objects' reference count +Rem You could leave this to the script host's garbage collector +c = empty +s = empty +shapes(1) = empty +shapes(2) = empty + +WScript.Echo example.Shape.nshapes & " shapes remain" +WScript.Echo "Goodbye" diff --git a/Examples/test-suite/com/aggregate_runme.vbs b/Examples/test-suite/com/aggregate_runme.vbs new file mode 100644 index 000000000..ed0a6d206 --- /dev/null +++ b/Examples/test-suite/com/aggregate_runme.vbs @@ -0,0 +1,44 @@ +Dim aggregate, result + +Set aggregate = CreateObject("aggregate.aggregate") + +result = aggregate.move(aggregate.UP) + +If result <> aggregate.UP Then + WScript.Echo "UP failed" + WScript.Quit 1 +End If + +result = aggregate.move(aggregate.DOWN) + +If result <> aggregate.DOWN Then + WScript.Echo "DOWN failed" + WScript.Quit 1 +End If + +result = aggregate.move(aggregate.LEFT) + +If result <> aggregate.LEFT Then + WScript.Echo "LEFT failed" + WScript.Quit 1 +End If + +result = aggregate.move(aggregate.RIGHT) + +If result <> aggregate.RIGHT Then + WScript.Echo "RIGHT failed" + WScript.Quit 1 +End If + +Rem Confirm that move() raises an exception when the contract is violated + +On Error Resume Next +result = 99 +result = aggregate.move(0) + +Rem Second assignment to result should not have been done + +If result <> 99 Then + WScript.Echo "0 test failed" + WScript.Quit 1 +End If |