Merge branch 'ZackerySpytz-OCaml-class-example'
* ZackerySpytz-OCaml-class-example: [OCaml] Re-enable the using_protected unit test [OCaml] Add a class example
This commit is contained in:
commit
36846e9342
9 changed files with 166 additions and 7 deletions
|
|
@ -1,11 +1,12 @@
|
|||
# see top-level Makefile.in
|
||||
argout_ref
|
||||
class
|
||||
contract
|
||||
scoped_enum
|
||||
shapes
|
||||
simple
|
||||
#std_string
|
||||
std_vector
|
||||
stl
|
||||
argout_ref
|
||||
shapes
|
||||
contract
|
||||
scoped_enum
|
||||
string_from_ptr
|
||||
strings_test
|
||||
|
|
|
|||
31
Examples/ocaml/class/Makefile
Normal file
31
Examples/ocaml/class/Makefile
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
TOP = ../..
|
||||
SWIGEXE = $(TOP)/../swig
|
||||
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
|
||||
SWIGOPT =
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
PROGFILE = runme.ml
|
||||
OBJS = example.o
|
||||
|
||||
check: build
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_run
|
||||
|
||||
build: static
|
||||
|
||||
static:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp
|
||||
|
||||
static_top:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
|
||||
ocaml_static_cpp_toplevel
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean
|
||||
28
Examples/ocaml/class/example.c
Normal file
28
Examples/ocaml/class/example.c
Normal file
|
|
@ -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() {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter() {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area() {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter() {
|
||||
return 4*width;
|
||||
}
|
||||
34
Examples/ocaml/class/example.h
Normal file
34
Examples/ocaml/class/example.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* 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();
|
||||
};
|
||||
9
Examples/ocaml/class/example.i
Normal file
9
Examples/ocaml/class/example.i
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
57
Examples/ocaml/class/runme.ml
Normal file
57
Examples/ocaml/class/runme.ml
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
(* file: runme.ml
|
||||
|
||||
This file illustrates the proxy class C++ interface generated
|
||||
by SWIG. *)
|
||||
|
||||
open Swig
|
||||
open Example
|
||||
|
||||
let repr o =
|
||||
Printf.sprintf "<%s at %#x>" (o -> ":classof" () as string) (o -> "&" () as int)
|
||||
|
||||
(* ----- Object creation ----- *)
|
||||
|
||||
let _ = print_endline "Creating some objects:"
|
||||
let c = new_Circle '(10)
|
||||
let _ = Printf.printf " Created circle %s\n" (repr c)
|
||||
let s = new_Square '(10)
|
||||
let _ = Printf.printf " Created square %s\n" (repr s)
|
||||
|
||||
(* ----- Access a static member ----- *)
|
||||
|
||||
let _ = Printf.printf "\nA total of %d shapes were created\n" (_Shape_nshapes '() as int)
|
||||
|
||||
(* ----- Member data access ----- *)
|
||||
|
||||
(* Set the location of the object *)
|
||||
|
||||
let _ = c -> "[x]" (20)
|
||||
let _ = c -> "[y]" (30)
|
||||
|
||||
(* Temp var to work around a swigp4 bug (it doesn't properly handle "-" in some cases). *)
|
||||
let arg = (-10. to float)
|
||||
let _ = s -> "[x]" (arg)
|
||||
let _ = s -> "[y]" (5)
|
||||
|
||||
let _ = print_endline "\nHere is their current position:"
|
||||
let _ = Printf.printf " Circle = (%f, %f)\n" (c -> "[x]" () as float) (c -> "[y]" () as float)
|
||||
let _ = Printf.printf " Square = (%f, %f)\n" (s -> "[x]" () as float) (s -> "[y]" () as float)
|
||||
|
||||
(* ----- Call some methods ----- *)
|
||||
|
||||
let _ = print_endline "\nHere are some properties of the shapes:"
|
||||
|
||||
let _ = List.iter (fun o ->
|
||||
Printf.printf " %s\n" (repr o);
|
||||
Printf.printf " area = %f\n" (o -> area () as float);
|
||||
Printf.printf " perimeter = %f\n" (o -> perimeter () as float)
|
||||
) [c; s]
|
||||
|
||||
let _ = print_endline "\nGuess I'll clean up now"
|
||||
|
||||
(* Note: this invokes the virtual destructor *)
|
||||
let _ = c -> "~" ()
|
||||
let _ = s -> "~" ()
|
||||
|
||||
let _ = Printf.printf "%d shapes remain\n" (_Shape_nshapes '() as int)
|
||||
let _ = print_endline "Goodbye"
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
using std::string;
|
||||
|
||||
double vec_write(std::vector<string> v) {
|
||||
void vec_write(std::vector<string> v) {
|
||||
int n = 0;
|
||||
for( std::vector<string>::iterator i = v.begin();
|
||||
i != v.end();
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ nested_directors \
|
|||
preproc_constants \
|
||||
smart_pointer_inherit \
|
||||
typedef_mptr \
|
||||
using_protected \
|
||||
|
||||
FAILING_C_TESTS = \
|
||||
enums \
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ open Swig
|
|||
open Using_protected
|
||||
|
||||
let f = new_FooBar C_void
|
||||
let _ = (invoke f) "x" (C_int 3)
|
||||
let _ = (invoke f) "[x]" (C_int 3)
|
||||
|
||||
let _ = if (invoke f) "blah" (C_int 4) <> (C_int 4) then
|
||||
raise (Failure "blah(int)")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue