Added overloading example.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4487 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
2ab8daf295
commit
0749cef9e3
5 changed files with 119 additions and 0 deletions
34
Examples/ocaml/shapes/Makefile
Normal file
34
Examples/ocaml/shapes/Makefile
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SWIGOPT =
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
MLFILE = example.ml
|
||||
PROGFILE = example_prog.ml
|
||||
OBJS = example.o
|
||||
|
||||
all:: static static_top
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp
|
||||
|
||||
static_top::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp_toplevel
|
||||
|
||||
dynamic::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
|
||||
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_dynamic_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile MLFILE='$(MLFILE)' ocaml_clean
|
||||
|
||||
check: all
|
||||
20
Examples/ocaml/shapes/example.c
Normal file
20
Examples/ocaml/shapes/example.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* File : example.c */
|
||||
#include <stdio.h>
|
||||
#include "example.h"
|
||||
|
||||
shape::shape() { }
|
||||
|
||||
shape::~shape() { }
|
||||
|
||||
bool shape::cover( double x, double y ) { return false; }
|
||||
|
||||
void draw_shape_coverage( shape *s, int div_x, int div_y ) {
|
||||
double i,j;
|
||||
|
||||
for( i = 0; i < 1.0; i += 1.0 / ((float)div_y) ) {
|
||||
for( j = 0; j < 1.0; j += 1.0 / ((float)div_x) ) {
|
||||
if( s->cover( i,j ) ) putchar( 'x' ); else putchar( ' ' );
|
||||
}
|
||||
printf( "\n" );
|
||||
}
|
||||
}
|
||||
13
Examples/ocaml/shapes/example.h
Normal file
13
Examples/ocaml/shapes/example.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef EXAMPLE_H
|
||||
#define EXAMPLE_H
|
||||
|
||||
class shape {
|
||||
public:
|
||||
shape();
|
||||
virtual ~shape();
|
||||
virtual bool cover( double x, double y ); // does this shape cover this point?
|
||||
};
|
||||
|
||||
extern void draw_shape_coverage( shape *s, int div_x, int div_y );
|
||||
|
||||
#endif//EXAMPLE_H
|
||||
8
Examples/ocaml/shapes/example.i
Normal file
8
Examples/ocaml/shapes/example.i
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
/* File : example.i */
|
||||
%module(directors="1") example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%feature("director");
|
||||
%include "example.h"
|
||||
44
Examples/ocaml/shapes/example_prog.ml
Normal file
44
Examples/ocaml/shapes/example_prog.ml
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
(* example_prog.ml *)
|
||||
|
||||
open Example ;;
|
||||
|
||||
let side_length (ax,ay) (bx,by) =
|
||||
sqrt (((bx -. ax) ** 2.0) +. ((by -. ay) ** 2.0)) ;;
|
||||
|
||||
let triangle_area a_pt b_pt c_pt =
|
||||
let a = (side_length a_pt b_pt)
|
||||
and b = (side_length b_pt c_pt)
|
||||
and c = (side_length c_pt a_pt) in
|
||||
let s = (a +. b +. c) /. 2.0 in
|
||||
sqrt (s *. (s -. a) *. (s -. b) *. (s -. c)) ;;
|
||||
|
||||
let point_in_triangle (pta,ptb,ptc) x y =
|
||||
let delta = 0.0000001 in (* Error *)
|
||||
let ptx = (x,y) in
|
||||
begin
|
||||
let a_area = triangle_area pta ptb ptx
|
||||
and b_area = triangle_area ptb ptc ptx
|
||||
and c_area = triangle_area ptc pta ptx
|
||||
and x_area = triangle_area pta ptb ptc in
|
||||
let result = (abs_float (a_area +. b_area +. c_area -. x_area)) < delta
|
||||
in
|
||||
result
|
||||
end ;;
|
||||
|
||||
let triangle_class pts ob meth args =
|
||||
match meth with
|
||||
"cover" ->
|
||||
(match args with
|
||||
C_list [ x_arg ; y_arg ] ->
|
||||
C_bool (point_in_triangle
|
||||
pts (get_float x_arg) (get_float y_arg))
|
||||
| _ -> raise (Failure "cover needs two double arguments."))
|
||||
| _ -> (invoke ob) meth args ;;
|
||||
|
||||
let triangle =
|
||||
new_derived_object
|
||||
new_shape
|
||||
(triangle_class ((0.0,0.0),(0.5,1.0),(1.0,0.0)))
|
||||
C_void ;;
|
||||
|
||||
let _ = _draw_shape_coverage (C_list [ triangle ; C_int 60 ; C_int 20 ]) ;;
|
||||
Loading…
Add table
Add a link
Reference in a new issue