diff options
Diffstat (limited to 'Examples/pike')
28 files changed, 809 insertions, 0 deletions
diff --git a/Examples/pike/check.list b/Examples/pike/check.list new file mode 100644 index 0000000..a8d348b --- /dev/null +++ b/Examples/pike/check.list @@ -0,0 +1,5 @@ +# see top-level Makefile.in +class +constants +enum +simple diff --git a/Examples/pike/class/Makefile b/Examples/pike/class/Makefile new file mode 100644 index 0000000..981ccef --- /dev/null +++ b/Examples/pike/class/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lm + +all: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' pike_cpp + +static: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='mypike' INTERFACE='$(INTERFACE)' pike_cpp_static + +clean: + $(MAKE) -f $(TOP)/Makefile pike_clean + +check: all diff --git a/Examples/pike/class/example.cxx b/Examples/pike/class/example.cxx new file mode 100644 index 0000000..c7a3194 --- /dev/null +++ b/Examples/pike/class/example.cxx @@ -0,0 +1,46 @@ +/* File : example.c */ + +#include "example.h" + +#include <stdio.h> + +#define M_PI 3.14159265358979323846 + +// Static member initializer +int Shape::nshapes = 0; + +// Constructor +Shape::Shape() { + nshapes++; +} + +// Move the shape to a new location +void Shape::move(double dx, double dy) { + x += dx; + y += dy; +} + +// Destructor +Shape::~Shape() { + nshapes--; +} + +// Circle area +double Circle::area() const { + return M_PI*radius*radius; +} + +// Circle perimeter +double Circle::perimeter() const { + return 2*M_PI*radius; +} + +// Square area +double Square::area() const { + return width*width; +} + +// Square perimeter +double Square::perimeter() const { + return 4*width; +} diff --git a/Examples/pike/class/example.h b/Examples/pike/class/example.h new file mode 100644 index 0000000..f74a4fe --- /dev/null +++ b/Examples/pike/class/example.h @@ -0,0 +1,35 @@ +/* File : example.h */ + +class Shape { +public: + Shape(); + virtual ~Shape(); + double x, y; + void move(double dx, double dy); + virtual double area() const = 0; + virtual double perimeter() const = 0; + static int nshapes; +}; + +class Circle : public Shape { +private: + double radius; +public: + Circle(double r) : radius(r) { }; + virtual double area() const; + virtual double perimeter() const; +}; + +class Square : public Shape { +private: + double width; +public: + Square(double w) : width(w) { }; + virtual double area() const; + virtual double perimeter() const; +}; + + + + + diff --git a/Examples/pike/class/example.i b/Examples/pike/class/example.i new file mode 100644 index 0000000..75700b3 --- /dev/null +++ b/Examples/pike/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/pike/class/runme.pike b/Examples/pike/class/runme.pike new file mode 100755 index 0000000..a637760 --- /dev/null +++ b/Examples/pike/class/runme.pike @@ -0,0 +1,53 @@ +import .example; + +int main() +{ + // ----- Object creation ----- + + write("Creating some objects:\n"); + Circle c = Circle(10.0); + write(" Created circle.\n"); + Square s = Square(10.0); + write(" Created square.\n"); + + // ----- Access a static member ----- + + write("\nA total of " + Shape_nshapes_get() + " shapes were created\n"); + + // ----- Member data access ----- + + // Set the location of the object + + c->x_set(20.0); + c->y_set(30.0); + + s->x_set(-10.0); + s->y_set(5.0); + + write("\nHere is their current position:\n"); + write(" Circle = (%f, %f)\n", c->x_get(), c->y_get()); + write(" Square = (%f, %f)\n", s->x_get(), s->y_get()); + + // ----- Call some methods ----- + + write("\nHere are some properties of the shapes:\n"); + write(" The circle:\n"); + write(" area = %f.\n", c->area()); + write(" perimeter = %f.\n", c->perimeter()); + write(" The square:\n"); + write(" area = %f.\n", s->area()); + write(" perimeter = %f.\n", s->perimeter()); + + write("\nGuess I'll clean up now\n"); + + /* See if we can force 's' to be garbage-collected */ + s = 0; + + /* Now we should be down to only 1 shape */ + write("%d shapes remain\n", Shape_nshapes_get()); + + /* Done */ + write("Goodbye\n"); + + return 0; +} diff --git a/Examples/pike/constants/Makefile b/Examples/pike/constants/Makefile new file mode 100644 index 0000000..7fa4938 --- /dev/null +++ b/Examples/pike/constants/Makefile @@ -0,0 +1,18 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = +TARGET = example +INTERFACE = example.i + +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' pike + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='mypike' INTERFACE='$(INTERFACE)' pike_static + +clean:: + $(MAKE) -f $(TOP)/Makefile pike_clean + +check: all diff --git a/Examples/pike/constants/example.i b/Examples/pike/constants/example.i new file mode 100644 index 0000000..4f7b1a4 --- /dev/null +++ b/Examples/pike/constants/example.i @@ -0,0 +1,27 @@ +/* File : example.i */ +%module example + +/* A few preprocessor macros */ + +#define ICONST 42 +#define FCONST 2.1828 +#define CCONST 'x' +#define CCONST2 '\n' +#define SCONST "Hello World" +#define SCONST2 "\"Hello World\"" + +/* This should work just fine */ +#define EXPR ICONST + 3*(FCONST) + +/* This shouldn't do anything */ +#define EXTERN extern + +/* Neither should this (BAR isn't defined) */ +#define FOO (ICONST + BAR) + +/* The following directives also produce constants */ + +%constant int iconst = 37; +%constant double fconst = 3.14; + + diff --git a/Examples/pike/constants/runme.pike b/Examples/pike/constants/runme.pike new file mode 100755 index 0000000..a8d9f94 --- /dev/null +++ b/Examples/pike/constants/runme.pike @@ -0,0 +1,24 @@ +int main() +{ + write("ICONST = %d (should be 42)\n", .example.ICONST); + write("FCONST = %f (should be 2.1828)\n", .example.FCONST); + write("CCONST = %c (should be 'x')\n", .example.CCONST); + write("CCONST2 = %c (this should be on a new line)\n", .example.CCONST2); + write("SCONST = %s (should be 'Hello World')\n", .example.SCONST); + write("SCONST2 = %s (should be '\"Hello World\"')\n", .example.SCONST2); + write("EXPR = %f (should be 48.5484)\n", .example.EXPR); + write("iconst = %d (should be 37)\n", .example.iconst); + write("fconst = %f (should be 3.14)\n", .example.fconst); + + if (search(indices(.example), "EXTERN") == -1) + write("EXTERN isn't defined (good)\n"); + else + write("EXTERN is defined (bad)\n"); + + if (search(indices(.example), "FOO") == -1) + write("FOO isn't defined (good)\n"); + else + write("FOO is defined (bad)\n"); + + return 0; +} diff --git a/Examples/pike/enum/Makefile b/Examples/pike/enum/Makefile new file mode 100644 index 0000000..0ae1021 --- /dev/null +++ b/Examples/pike/enum/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lm + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' pike_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='mypike' INTERFACE='$(INTERFACE)' pike_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile pike_clean + +check: all diff --git a/Examples/pike/enum/README b/Examples/pike/enum/README new file mode 100644 index 0000000..055aa9f --- /dev/null +++ b/Examples/pike/enum/README @@ -0,0 +1,13 @@ +This example will not compile with Pike versions 7.4.20 unless you first +patch the Pike sources. The problem is for line 91 of Pike's "stralloc.h" +(usually installed as /usr/local/pike/7.4.10/include/pike/stralloc.h). That +line reads: + + tmp.ptr=ptr; + +but should be patched to read: + + tmp.ptr=(p_wchar0 *) ptr; + +This bug has been reported to the Pike developers. + diff --git a/Examples/pike/enum/example.cxx b/Examples/pike/enum/example.cxx new file mode 100644 index 0000000..6785e57 --- /dev/null +++ b/Examples/pike/enum/example.cxx @@ -0,0 +1,37 @@ +/* File : example.c */ + +#include "example.h" +#include <stdio.h> + +void Foo::enum_test(speed s) { + if (s == IMPULSE) { + printf("IMPULSE speed\n"); + } else if (s == WARP) { + printf("WARP speed\n"); + } else if (s == LUDICROUS) { + printf("LUDICROUS speed\n"); + } else { + printf("Unknown speed\n"); + } +} + +void enum_test(color c, Foo::speed s) { + if (c == RED) { + printf("color = RED, "); + } else if (c == BLUE) { + printf("color = BLUE, "); + } else if (c == GREEN) { + printf("color = GREEN, "); + } else { + printf("color = Unknown color!, "); + } + if (s == Foo::IMPULSE) { + printf("speed = IMPULSE speed\n"); + } else if (s == Foo::WARP) { + printf("speed = WARP speed\n"); + } else if (s == Foo::LUDICROUS) { + printf("speed = LUDICROUS speed\n"); + } else { + printf("speed = Unknown speed!\n"); + } +} diff --git a/Examples/pike/enum/example.h b/Examples/pike/enum/example.h new file mode 100644 index 0000000..525d62a --- /dev/null +++ b/Examples/pike/enum/example.h @@ -0,0 +1,13 @@ +/* File : example.h */ + +enum color { RED, BLUE, GREEN }; + +class Foo { + public: + Foo() { } + enum speed { IMPULSE, WARP, LUDICROUS }; + void enum_test(speed s); +}; + +void enum_test(color c, Foo::speed s); + diff --git a/Examples/pike/enum/example.i b/Examples/pike/enum/example.i new file mode 100644 index 0000000..23ee8a8 --- /dev/null +++ b/Examples/pike/enum/example.i @@ -0,0 +1,11 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ + +%include "example.h" + diff --git a/Examples/pike/enum/runme.pike b/Examples/pike/enum/runme.pike new file mode 100644 index 0000000..4846356 --- /dev/null +++ b/Examples/pike/enum/runme.pike @@ -0,0 +1,28 @@ +int main() +{ + write("*** color ***\n"); + write(" RED = " + .example.RED + "\n"); + write(" BLUE = " + .example.BLUE + "\n"); + write(" GREEN = " + .example.GREEN + "\n"); + + write("\n*** Foo::speed ***\n"); + write(" Foo_IMPULSE = " + .example.Foo.IMPULSE + "\n"); + write(" Foo_WARP = " + .example.Foo.WARP + "\n"); + write(" Foo_LUDICROUS = " + .example.Foo.LUDICROUS + "\n"); + + write("\nTesting use of enums with functions\n\n"); + + .example.enum_test(.example.RED, .example.Foo.IMPULSE); + .example.enum_test(.example.BLUE, .example.Foo.WARP); + .example.enum_test(.example.GREEN, .example.Foo.LUDICROUS); + .example.enum_test(1234, 5678); + + write("\nTesting use of enum with class method\n"); + .example.Foo f = .example.Foo(); + + f->enum_test(.example.Foo.IMPULSE); + f->enum_test(.example.Foo.WARP); + f->enum_test(.example.Foo.LUDICROUS); + + return 0; +} diff --git a/Examples/pike/overload/Makefile b/Examples/pike/overload/Makefile new file mode 100644 index 0000000..60af005 --- /dev/null +++ b/Examples/pike/overload/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lstdc++ -lm + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' pike_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='mypike' INTERFACE='$(INTERFACE)' pike_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile pike_clean + +check: all diff --git a/Examples/pike/overload/example.cxx b/Examples/pike/overload/example.cxx new file mode 100644 index 0000000..3760fdd --- /dev/null +++ b/Examples/pike/overload/example.cxx @@ -0,0 +1,115 @@ +#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; +} + diff --git a/Examples/pike/overload/example.h b/Examples/pike/overload/example.h new file mode 100644 index 0000000..e47a122 --- /dev/null +++ b/Examples/pike/overload/example.h @@ -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 diff --git a/Examples/pike/overload/example.i b/Examples/pike/overload/example.i new file mode 100644 index 0000000..ddcd006 --- /dev/null +++ b/Examples/pike/overload/example.i @@ -0,0 +1,28 @@ +/* File : example.i */ +%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; + +%ignore Bar::Bar(Bar *); +%ignore Bar::Bar(long); + +%ignore Bar::foo(const Bar&); +%ignore Bar::foo(long); + +%ignore ::foo(const Bar&); +%ignore ::foo(int); + +/* Let's just grab the original header file here */ +%include "example.h" diff --git a/Examples/pike/overload/runme.pike b/Examples/pike/overload/runme.pike new file mode 100644 index 0000000..d30e947 --- /dev/null +++ b/Examples/pike/overload/runme.pike @@ -0,0 +1,83 @@ +// import .example; + +int main() +{ + // 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 *) + foo(Bar.new); + + // Skip a line + write("\n"); + + // This should invoke Bar::Bar(double) + Bar.new(3.14159); + + // This should invoke Bar::Bar(double, char *) + Bar.new(3.14159, "Pi"); + + // This should invoke Bar::Bar(int, int) + Bar.new(3, 4); + + // This should invoke Bar::Bar(char *) + Bar.new("This is a test"); + + // This should invoke Bar::Bar(int) + Bar.new(42); + + // This should invoke Bar::Bar() for the input argument, + // followed by Bar::Bar(const Bar&). + Bar.new(Bar.new); + + // Skip a line + write("\n"); + */ + + // Construct a new Bar instance (invokes Bar::Bar()) + /* + bar = 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); + */ + + write("Goodbye\n"); + + return 0; +} diff --git a/Examples/pike/simple/Makefile b/Examples/pike/simple/Makefile new file mode 100644 index 0000000..544c97b --- /dev/null +++ b/Examples/pike/simple/Makefile @@ -0,0 +1,18 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i + +all: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' pike + +static: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='mypike' INTERFACE='$(INTERFACE)' pike_static + +clean: + $(MAKE) -f $(TOP)/Makefile pike_clean + +check: all diff --git a/Examples/pike/simple/example.c b/Examples/pike/simple/example.c new file mode 100644 index 0000000..1c2af78 --- /dev/null +++ b/Examples/pike/simple/example.c @@ -0,0 +1,18 @@ +/* File : example.c */ + +/* A global variable */ +double Foo = 3.0; + +/* 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; +} + + diff --git a/Examples/pike/simple/example.i b/Examples/pike/simple/example.i new file mode 100644 index 0000000..24093b9 --- /dev/null +++ b/Examples/pike/simple/example.i @@ -0,0 +1,7 @@ +/* File : example.i */ +%module example + +%inline %{ +extern int gcd(int x, int y); +extern double Foo; +%} diff --git a/Examples/pike/simple/runme.pike b/Examples/pike/simple/runme.pike new file mode 100644 index 0000000..a6a78e9 --- /dev/null +++ b/Examples/pike/simple/runme.pike @@ -0,0 +1,20 @@ +int main() +{ + /* Call our gcd() function */ + int x = 42; + int y = 105; + int g = .example.gcd(x, y); + write("The gcd of %d and %d is %d\n", x, y, g); + + /* Manipulate the Foo global variable */ + /* Output its current value */ + write("Foo = %f\n", .example->Foo_get()); + + /* Change its value */ + .example->Foo_set(3.1415926); + + /* See if the change took effect */ + write("Foo = %f\n", .example->Foo_get()); + + return 0; +} diff --git a/Examples/pike/template/Makefile b/Examples/pike/template/Makefile new file mode 100644 index 0000000..b3f0129 --- /dev/null +++ b/Examples/pike/template/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-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)' pike_cpp + +static: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='mypike' INTERFACE='$(INTERFACE)' pike_cpp_static + +clean: + $(MAKE) -f $(TOP)/Makefile pike_clean + +check: all diff --git a/Examples/pike/template/example.h b/Examples/pike/template/example.h new file mode 100644 index 0000000..7401df6 --- /dev/null +++ b/Examples/pike/template/example.h @@ -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 +}; + diff --git a/Examples/pike/template/example.i b/Examples/pike/template/example.i new file mode 100644 index 0000000..8f94c4d --- /dev/null +++ b/Examples/pike/template/example.i @@ -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>; + diff --git a/Examples/pike/template/runme.pike b/Examples/pike/template/runme.pike new file mode 100755 index 0000000..36825c3 --- /dev/null +++ b/Examples/pike/template/runme.pike @@ -0,0 +1,33 @@ +int main() +{ + // Call some templated functions + write(sprintf("%d\n", .example.maxint(3, 7))); + write(sprintf("%f\n", .example.maxdouble(3.14, 2.18))); + + // Create some objects + .example.vecint iv = .example.vecint(100); + .example.vecdouble dv = .example.vecdouble(1000); + + for (int i = 0; i < 100; i++) { + iv->setitem(i, 2*i); + } + + for (int i = 0; i < 1000; i++) { + dv->setitem(i, 1.0/(i+1)); + } + + int isum = 0; + for (int i = 0; i < 100; i++) { + isum += iv->getitem(i); + } + + write(sprintf("%d\n", isum)); + + float fsum = 0.0; + for (int i = 0; i < 1000; i++) { + fsum += dv->getitem(i); + } + write(sprintf("%f\n", fsum)); + + return 0; +} |
