diff options
Diffstat (limited to 'Examples/chicken/class')
| -rw-r--r-- | Examples/chicken/class/Makefile | 38 | ||||
| -rw-r--r-- | Examples/chicken/class/example.cxx | 28 | ||||
| -rw-r--r-- | Examples/chicken/class/example.h | 46 | ||||
| -rw-r--r-- | Examples/chicken/class/example.i | 10 | ||||
| -rw-r--r-- | Examples/chicken/class/test-lowlevel-class.scm | 76 | ||||
| -rw-r--r-- | Examples/chicken/class/test-tinyclos-class.scm | 76 |
6 files changed, 274 insertions, 0 deletions
diff --git a/Examples/chicken/class/Makefile b/Examples/chicken/class/Makefile new file mode 100644 index 0000000..1261ec5 --- /dev/null +++ b/Examples/chicken/class/Makefile @@ -0,0 +1,38 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +INTERFACE = example.i +SRCS = +CXXSRCS = example.cxx +TARGET = class +INCLUDE = +SWIGOPT = +CFLAGS = +VARIANT = + +# uncomment the following lines to build a static exe (only pick one of the CHICKEN_MAIN lines) +#CHICKEN_MAIN = test-lowlevel-class.scm +#CHICKEN_MAIN = test-tinyclos-class.scm +#VARIANT = _static + +all:: $(TARGET) $(TARGET)_proxy + +$(TARGET): $(INTERFACE) $(SRCS) + $(MAKE) -f $(TOP)/Makefile \ + SRCS='$(SRCS)' CXXSRCS='$(CXXSRCS)' CHICKEN_MAIN='$(CHICKEN_MAIN)' \ + INCLUDE='$(INCLUDE)' SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' \ + SWIG='$(SWIG)' INTERFACE='$(INTERFACE)' CHICKENOPTS='$(CHICKENOPTS)' chicken$(VARIANT)_cpp + +$(TARGET)_proxy: $(INTERFACE) $(SRCS) + $(MAKE) -f $(TOP)/Makefile \ + SRCS='$(SRCS)' CXXSRCS='$(CXXSRCS)' CHICKEN_MAIN='$(CHICKEN_MAIN)' \ + INCLUDE='$(INCLUDE)' SWIGOPT='$(SWIGOPT) -proxy' TARGET='$(TARGET)_proxy' \ + SWIG='$(SWIG)' INTERFACE='$(INTERFACE)' CHICKENOPTS='$(CHICKENOPTS)' chicken$(VARIANT)_cpp + +clean:: + $(MAKE) -f $(TOP)/Makefile chicken_clean + rm -f example.scm + rm -f $(TARGET) + +check:: + env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH csi test-lowlevel-class.scm + env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH csi test-tinyclos-class.scm diff --git a/Examples/chicken/class/example.cxx b/Examples/chicken/class/example.cxx new file mode 100644 index 0000000..1e8e203 --- /dev/null +++ b/Examples/chicken/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/chicken/class/example.h b/Examples/chicken/class/example.h new file mode 100644 index 0000000..210ba98 --- /dev/null +++ b/Examples/chicken/class/example.h @@ -0,0 +1,46 @@ +/* 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; + + enum SomeEnum { + First = 0, + Second, + Third, + Last = 1000 + }; +}; + +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/chicken/class/example.i b/Examples/chicken/class/example.i new file mode 100644 index 0000000..75700b3 --- /dev/null +++ b/Examples/chicken/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/chicken/class/test-lowlevel-class.scm b/Examples/chicken/class/test-lowlevel-class.scm new file mode 100644 index 0000000..7c59c0a --- /dev/null +++ b/Examples/chicken/class/test-lowlevel-class.scm @@ -0,0 +1,76 @@ +;; This file illustrates the low-level C++ interface generated +;; by SWIG. + +(load-library 'example "class.so") +(declare (uses example)) + +;; ----- Object creation ----- + +(display "Creating some objects:\n") +(define c (new-Circle 10.0)) +(display " Created circle ") +(display c) +(display "\n") +(define s (new-Square 10.0)) +(display " Created square ") +(display s) +(display "\n") + +;; ----- Access a static member ----- + +(display "\nA total of ") +(display (Shape-nshapes)) +(display " shapes were created\n") + +;; ----- Member data access ----- + +;; Set the location of the object + +(Shape-x-set c 20.0) +(Shape-y-set c 30.0) + +(Shape-x-set s -10.0) +(Shape-y-set s 5.0) + +(display "\nHere is their current position:\n") +(display " Circle = (") +(display (Shape-x-get c)) +(display ", ") +(display (Shape-y-get c)) +(display ")\n") +(display " Square = (") +(display (Shape-x-get s)) +(display ", ") +(display (Shape-y-get s)) +(display ")\n") + +;; ----- Call some methods ----- + +(display "\nHere are some properties of the shapes:\n") +(let + ((disp (lambda (o) + (display " ") + (display o) + (display "\n") + (display " area = ") + (display (Shape-area o)) + (display "\n") + (display " perimeter = ") + (display (Shape-perimeter o)) + (display "\n")))) + (disp c) + (disp s)) + +(display "\nGuess I'll clean up now\n") + +;; Note: this invokes the virtual destructor +(set! c #f) +(set! s #f) +(gc #t) + +(set! s 3) +(display (Shape-nshapes)) +(display " shapes remain\n") +(display "Goodbye\n") + +(exit) diff --git a/Examples/chicken/class/test-tinyclos-class.scm b/Examples/chicken/class/test-tinyclos-class.scm new file mode 100644 index 0000000..5ba1d6a --- /dev/null +++ b/Examples/chicken/class/test-tinyclos-class.scm @@ -0,0 +1,76 @@ +;; This file illustrates the proxy C++ interface generated +;; by SWIG. + +(load-library 'example "class_proxy.so") +(declare (uses example)) +(declare (uses tinyclos)) + +;; ----- Object creation ----- + +(display "Creating some objects:\n") +(define c (make <Circle> 10.0)) +(display " Created circle ") +(display c) +(display "\n") +(define s (make <Square> 10.0)) +(display " Created square ") +(display s) +(display "\n") + +;; ----- Access a static member ----- + +(display "\nA total of ") +(display (Shape-nshapes)) +(display " shapes were created\n") + +;; ----- Member data access ----- + +;; Set the location of the object + +(slot-set! c 'x 20.0) +(slot-set! c 'y 30.0) + +(slot-set! s 'x -10.0) +(slot-set! s 'y 5.0) + +(display "\nHere is their current position:\n") +(display " Circle = (") +(display (slot-ref c 'x)) +(display ", ") +(display (slot-ref c 'y)) +(display ")\n") +(display " Square = (") +(display (slot-ref s 'x)) +(display ", ") +(display (slot-ref s 'y)) +(display ")\n") + +;; ----- Call some methods ----- + +(display "\nHere are some properties of the shapes:\n") +(let + ((disp (lambda (o) + (display " ") + (display o) + (display "\n") + (display " area = ") + (display (area o)) + (display "\n") + (display " perimeter = ") + (display (perimeter o)) + (display "\n")))) + (disp c) + (disp s)) + +(display "\nGuess I'll clean up now\n") + +;; Note: Invoke the virtual destructors by forcing garbage collection +(set! c 77) +(set! s 88) +(gc #t) + +(display (Shape-nshapes)) +(display " shapes remain\n") +(display "Goodbye\n") + +(exit) |
