The great merge

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

View file

@ -1,27 +1,38 @@
# Makefile for Guile. Used by all of the example programs.
subdirs = simple matrix
subdirs = simple matrix port constants multimap multivalue
top_srcdir = @top_srcdir@
SWIG = ../$(top_srcdir)/swig@release_suffix@
CC = @CC@
CXX = @CXX@
CFLAGS = @CFLAGS@
GUILEINCLUDE = @GUILEINCLUDE@
GUILELINK = @GUILELINK@
SWIGOPT =
WRAP = $(IFILE:.i=_wrap.c)
CXXWRAP = $(IFILE:.i=_wrap.cxx)
all:
for d in $(subdirs) ; do (cd $$d ; $(MAKE)) ; done
clean::
cd simple; make clean
cd matrix; make clean
for d in $(subdirs) ; do (cd $$d ; $(MAKE) clean) ; done
rm -f *~ .~*
guile_clean:
rm -f *.@OBJEXT@ *$(SO) *_wrap* *~ .~* core my-guile $(TARGET)
# This is meant to be used w/ "make -f ../Makefile" from subdirs.
# Doesn't make sense to use it from here.
sub-all::
../$(top_srcdir)/swig -guile $(IFILE)
$(CC) -o $(TARGET) $(SRCS) $(WRAP) $(GUILEINCLUDE) $(GUILELINK)
$(SWIG) -guile $(SWIGOPT) $(IFILE)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(WRAP) $(GUILEINCLUDE) $(GUILELINK)
sub-all-cxx::
$(SWIG) -c++ -guile $(SWIGOPT) $(IFILE)
$(CXX) $(CFLAGS) -o $(TARGET) $(SRCS) $(CXXWRAP) $(GUILEINCLUDE) $(GUILELINK)
# Makefile ends here

View file

@ -1,11 +1,17 @@
This directory contains examples for Guile.
simple - The simple example from the user manual.
matrix - A very simple Matrix example.
constants -- handling #define and %constant literals
matrix -- a very simple Matrix example
multimap -- typemaps with multiple sub-types
multivalue -- using the %values_as_list directive
port -- scheme ports as temporary FILE streams
simple -- the simple example from the user manual
std_vector -- C++ STL vector<int> and vector<double>
Note that the examples in this directory build a special version of
Guile which includes the wrapped functions in the top-level module.
Guile which includes the wrapped functions in the top-level module.
If you want to put the wrapped functions into an own module,
statically or dynamically linked, see the Examples/GIFPlot/Guile
directory.
directory.

View file

@ -0,0 +1,7 @@
# see top-level Makefile.in
constants
matrix
simple
port
multimap
multivalue

View file

@ -0,0 +1,2 @@
example_wrap.c
my-guile

View file

@ -0,0 +1,17 @@
SRCS =
TARGET = my-guile
IFILE = example.i
MKDIR = ..
all::
$(MAKE) -f $(MKDIR)/Makefile \
SRCS='$(SRCS)' \
TARGET=$(TARGET) \
IFILE=$(IFILE) \
sub-all
clean::
$(MAKE) -f $(MKDIR)/Makefile TARGET='$(TARGET)' guile_clean
check: all
./my-guile -s constants.scm

View file

@ -0,0 +1,10 @@
(or (= (ICONST) 42) (exit 1))
(or (< (abs (- (FCONST) 2.1828)) 0.00001) (exit 1))
(or (char=? (CCONST) #\x) (exit 1))
(or (char=? (CCONST2) #\newline) (exit 1))
(or (string=? (SCONST) "Hello World") (exit 1))
(or (string=? (SCONST2) "\"Hello World\"") (exit 1))
(or (< (abs (- (EXPR) (+ (ICONST) (* 3 (FCONST))))) 0.00001) (exit 1))
(or (= (iconst) 37) (exit 1))
(or (< (abs (- (fconst) 3.14)) 0.00001) (exit 1))
(exit 0)

View file

@ -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;
%include guilemain.i

View file

@ -1,4 +1,3 @@
CC = gcc
SRCS = matrix.c vector.c
TARGET = matrix
IFILE = package.i
@ -10,11 +9,10 @@ all::
SRCS='$(SRCS)' \
TARGET=$(TARGET) \
IFILE=$(IFILE) \
CC=$(CC) \
MODULE=$(MODULE) \
sub-all
clean::
rm -f matrix *_wrap* *~ .~* core
$(MAKE) -f $(MKDIR)/Makefile TARGET='$(TARGET)' guile_clean
check: all

View file

@ -1,7 +1,7 @@
Matrix example. To run the example, execute the program 'matrix' and
type the following :
(load 'matrix.scm)
(load "matrix.scm")
(do-test 0)
Alternatively, use the command-line:

View file

@ -1,4 +1,6 @@
/* FILE : matrix.c : some simple 4x4 matrix operations */
#include <stdlib.h>
#include <stdio.h>
double **new_matrix() {

View file

@ -12,7 +12,8 @@ double get_m(double **M, int i, int j) {
}
%}
%section "Matrix Operations"
/*** Matrix Operations ***/
extern double **new_matrix();
/* Creates a new matrix and returns a pointer to it */

View file

@ -1,7 +1,8 @@
// FILE : package.i
// See the SWIG users manual
%title "Matrix and vector package"
/*** Matrix and vector package ***/
%module Matrix
%{
#include <math.h>

View file

@ -1,5 +1,7 @@
/* File : vector.c */
#include <stdlib.h>
#include <stdio.h>
#include "vector.h"
Vector *createv(double x, double y, double z, double w) {

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)' guile
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='my-guile' INTERFACE='$(INTERFACE)' guile_static
clean::
$(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' guile_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,75 @@
/* File : example.i */
%module example
%include exception.i
%include typemaps.i
extern int gcd(int x, int y);
%typemap(guile,in) (int argc, char *argv[]) {
int i;
SCM *v;
if (!(SCM_NIMP($input) && SCM_VECTORP($input))) {
SWIG_exception(SWIG_ValueError, "Expecting a vector");
return;
}
$1 = SCM_LENGTH($input);
if ($1 == 0) {
SWIG_exception(SWIG_ValueError, "Vector must contain at least 1 element");
}
$2 = (char **) malloc(($1+1)*sizeof(char *));
v = SCM_VELTS($input);
for (i = 0; i < $1; i++) {
if (!(SCM_NIMP(v[i]) && SCM_STRINGP(v[i]))) {
free($2);
SWIG_exception(SWIG_ValueError, "Vector items must be strings");
return;
}
$2[i] = SCM_CHARS(v[i]);
}
$2[i] = 0;
}
%typemap(guile,freearg) (int argc, char *argv[]) {
free($2);
}
extern int gcdmain(int argc, char *argv[]);
%typemap(guile,in) (char *bytes, int len) {
if (!(SCM_NIMP($input) && SCM_STRINGP($input))) {
SWIG_exception(SWIG_ValueError, "Expecting a string");
}
$1 = SCM_CHARS($input);
$2 = SCM_LENGTH($input);
}
extern int count(char *bytes, int len, char c);
/* This example shows how to wrap a function that mutates a string */
%typemap(guile,in) (char *str, int len) {
$1 = gh_scm2newstr($input,&$2);
}
/* Return the mutated string as a new object. */
%typemap(guile,argout) (char *str, int len) {
SWIG_APPEND_VALUE(gh_str2scm($1,$2));
if ($1) scm_must_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);

View file

@ -0,0 +1,30 @@
;;; Test out some multi-argument typemaps
(use-modules (example))
; Call the GCD function
(define x 42)
(define y 105)
(define g (gcd x y))
(display "The gcd of ")
(display x)
(display " and ")
(display y)
(display " is ")
(display g)
(newline)
; Call the gcdmain() function
(gcdmain #("gcdmain" "42" "105"))
; Call the count function
(display (count "Hello World" #\l))
(newline)
; Call the capitalize function
(display (capitalize "hello world"))
(newline)

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)' guile
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='my-guile' INTERFACE='$(INTERFACE)' guile_static
clean::
$(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' guile_clean
check: all

View file

@ -0,0 +1,18 @@
void divide_l(int a, int b, int *quotient_p, int *remainder_p)
{
*quotient_p = a/b;
*remainder_p = a%b;
}
void divide_v(int a, int b, int *quotient_p, int *remainder_p)
{
*quotient_p = a/b;
*remainder_p = a%b;
}
void divide_mv(int a, int b, int *quotient_p, int *remainder_p)
{
*quotient_p = a/b;
*remainder_p = a%b;
}

View file

@ -0,0 +1,26 @@
/* -*- c -*- */
%module example;
/* Multiple values as lists. By default, if more than one value is to
be returned, a list of the values is created and returned; to switch
back to this behavior, use: */
%values_as_list;
void divide_l(int a, int b, int *OUTPUT, int *OUTPUT);
/* Multiple values as vectors. By issueing: */
%values_as_vector;
/* vectors instead of lists will be used. */
void divide_v(int a, int b, int *OUTPUT, int *OUTPUT);
/* Multiple values for multiple-value continuations.
(This is the most elegant way.) By issueing: */
%multiple_values;
/* multiple values are passed to the multiple-value
continuation, as created by `call-with-values' or the
convenience macro `receive'. (See the Scheme file.) */
void divide_mv(int a, int b, int *OUTPUT, int *OUTPUT);

View file

@ -0,0 +1,66 @@
;;;; Show the three different ways to deal with multiple return values
(use-modules (example))
;;; Multiple values as lists. By default, if more than one value is to
;;; be returned, a list of the values is created and returned. The
;;; procedure divide-l does so:
(let* ((quotient/remainder (divide-l 37 5))
;; divide-l returns a list of the two values, so get them:
(quotient (car quotient/remainder))
(remainder (cadr quotient/remainder)))
(display "37 divided by 5 is ")
(display quotient)
(display ", remainder ")
(display remainder)
(newline))
;;; Multiple values as vectors. You can get vectors instead of lists
;;; if you want:
(let* ((quotient-remainder-vector (divide-v 40 7))
;; divide-v returns a vector of two values, so get them:
(quotient (vector-ref quotient-remainder-vector 0))
(remainder (vector-ref quotient-remainder-vector 1)))
(display "40 divided by 7 is ")
(display quotient)
(display ", remainder ")
(display remainder)
(newline))
;;; Multiple values for multiple-value continuations. (The most
;;; elegant way.) You can get multiple values passed to the
;;; multiple-value continuation, as created by `call-with-values'.
(call-with-values (lambda ()
;; the "producer" procedure
(divide-mv 91 13))
(lambda (quotient remainder)
;; the "consumer" procedure
(display "91 divided by 13 is ")
(display quotient)
(display ", remainder ")
(display remainder)
(newline)))
;;; SRFI-8 has a very convenient macro for this construction:
(use-modules (srfi srfi-8))
;;; If your Guile is too old, you can define the receive macro yourself:
;;;
;;; (define-macro (receive vars vals . body)
;;; `(call-with-values (lambda () ,vals)
;;; (lambda ,vars ,@body)))
(receive (quotient remainder)
(divide-mv 111 19) ; the "producer" form
;; In the body, `quotient' and `remainder' are bound to the two
;; values.
(display "111 divided by 19 is ")
(display quotient)
(display ", remainder ")
(display remainder)
(newline))

View file

@ -1,4 +1,3 @@
CC = gcc
SRCS = port.c
TARGET = port
IFILE = port.i
@ -10,11 +9,10 @@ all::
SRCS='$(SRCS)' \
TARGET=$(TARGET) \
IFILE=$(IFILE) \
CC=$(CC) \
MODULE=$(MODULE) \
sub-all
clean::
rm -f $(TARGET) *_wrap* *~ .~* core test.out
$(MAKE) -f $(MKDIR)/Makefile TARGET='$(TARGET)' guile_clean
check: all

View file

@ -1,3 +1,5 @@
%module port
%include guilemain.i
/* Include the required FILE * typemaps */
@ -7,5 +9,7 @@
#include <stdio.h>
%}
%inline %{
void print_int(FILE *f, int i);
int read_int(FILE *f);
%}

View file

@ -1,18 +1,19 @@
CC = gcc
SRCS = example.c
TARGET = my-guile
IFILE = example.i
MKDIR = ..
all::
all: $(TARGET)
$(TARGET):
$(MAKE) -f $(MKDIR)/Makefile \
SRCS='$(SRCS)' \
TARGET=$(TARGET) \
IFILE=$(IFILE) \
CC=$(CC) \
sub-all
clean::
rm -f *_wrap* my-guile *~ .~* core
$(MAKE) -f $(MKDIR)/Makefile TARGET='$(TARGET)' guile_clean
check: all
check: $(TARGET)
./$(TARGET) -s example.scm > /dev/null

View file

@ -2,7 +2,7 @@ A very simple example.
To run it, start the program 'my-guile' and type:
(load 'example.scm)
(load "example.scm")
Alternatively, you can use the shell command:

View file

@ -1,7 +1,7 @@
/* Simple example from documentation */
/* File : example.c */
#include </usr/include/time.h>
#include <time.h>
double My_variable = 3.0;

View file

@ -1,23 +1,14 @@
;;;
;;; Guile script for simple example.
;;; Is a little clumsy since I'm not the greatest scheme programmer.
;;;
;;; example.scm
(display (get-time))
(display "My variable = ")
(display (My-variable))
(newline)
(define (mdisplay-newline . args) ; does guile-1.3.4 have `format #t'?
(for-each display args)
(newline))
(define (facts x max)
(if (< x max)
(begin
(display (string-append (number->string x) " factorial is "
(number->string (fact x))))
(newline)
(facts (+ x 1) max))))
(facts 0 14)
(mdisplay-newline (get-time) "My variable = " (My-variable))
(do ((i 0 (1+ i)))
((= 14 i))
(mdisplay-newline i " factorial is " (fact i)))
(define (mods i imax j jmax)
(if (< i imax)
@ -27,9 +18,11 @@
(mods i imax (+ j 1) jmax))
(mods (+ i 1) imax 1 jmax))))
(mods 1 250 1 250)
(mods 1 150 1 150)
(display (string-append "My-variable = " (number->string (My-variable))))
(newline)
(mdisplay-newline "My-variable = " (My-variable))
(exit (and (= 1932053504 (fact 13))
(= 745470.0 (My-variable))))
;;; example.scm ends here

View file

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

View file

@ -0,0 +1,18 @@
TOP = ../..
SWIG = $(TOP)/../swig
SRCS =
TARGET = example
INTERFACE = example.i
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile_cpp
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='my-guile' INTERFACE='$(INTERFACE)' guile_static_cpp
clean::
$(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' guile_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,54 @@
;; run with mzscheme -r example.scm
(use-modules (example))
; repeatedly invoke a procedure with v and an index as arguments
(define (with-vector v proc size-proc)
(let ((size (size-proc v)))
(define (with-vector-item v i)
(if (< i size)
(begin
(proc v i)
(with-vector-item v (+ i 1)))))
(with-vector-item v 0)))
(define (with-IntVector v proc)
(with-vector v proc IntVector-length))
(define (with-DoubleVector v proc)
(with-vector v proc DoubleVector-length))
(define (print-DoubleVector v)
(with-DoubleVector v (lambda (v i) (display (DoubleVector-ref v i))
(display " ")))
(newline))
; Call average with a Scheme list...
(display (average '(1 2 3 4)))
(newline)
; ... or a wrapped std::vector<int>
(define v (new-IntVector 4))
(with-IntVector v (lambda (v i) (IntVector-set! v i (+ i 1))))
(display (average v))
(newline)
(delete-IntVector v)
; half will return a Scheme vector.
; Call it with a Scheme vector...
(display (half #(1 1.5 2 2.5 3)))
(newline)
; ... or a wrapped std::vector<double>
(define v (new-DoubleVector))
(map (lambda (i) (DoubleVector-push! v i)) '(1 2 3 4))
(display (half v))
(newline)
; now halve a wrapped std::vector<double> in place
(halve-in-place v)
(print-DoubleVector v)
(delete-DoubleVector v)