diff options
author | Zackery Spytz <zspytz@gmail.com> | 2019-01-16 00:53:13 -0700 |
---|---|---|
committer | Zackery Spytz <zspytz@gmail.com> | 2019-01-16 00:53:13 -0700 |
commit | a641966e0b06e748b745a17e5e8afdb2352e0d0b (patch) | |
tree | 05b6916b668f91f8bb184e5b596f9fe7da7adcc8 /Examples/ocaml | |
parent | fa0c3fe5c2cc1d592808025986b6b9d49bbab490 (diff) | |
download | swig-a641966e0b06e748b745a17e5e8afdb2352e0d0b.tar.gz |
[OCaml] Add a callback example
It is based on the the Python and Go examples.
Diffstat (limited to 'Examples/ocaml')
-rw-r--r-- | Examples/ocaml/callback/Makefile | 31 | ||||
-rw-r--r-- | Examples/ocaml/callback/example.c | 3 | ||||
-rw-r--r-- | Examples/ocaml/callback/example.h | 20 | ||||
-rw-r--r-- | Examples/ocaml/callback/example.i | 10 | ||||
-rw-r--r-- | Examples/ocaml/callback/runme.ml | 30 | ||||
-rw-r--r-- | Examples/ocaml/check.list | 1 | ||||
-rw-r--r-- | Examples/ocaml/stl/example.i | 2 |
7 files changed, 95 insertions, 2 deletions
diff --git a/Examples/ocaml/callback/Makefile b/Examples/ocaml/callback/Makefile new file mode 100644 index 000000000..4cb4ef3dd --- /dev/null +++ b/Examples/ocaml/callback/Makefile @@ -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 diff --git a/Examples/ocaml/callback/example.c b/Examples/ocaml/callback/example.c new file mode 100644 index 000000000..4ead01f58 --- /dev/null +++ b/Examples/ocaml/callback/example.c @@ -0,0 +1,3 @@ +/* File : example.c */ + +#include "example.h" diff --git a/Examples/ocaml/callback/example.h b/Examples/ocaml/callback/example.h new file mode 100644 index 000000000..12d888595 --- /dev/null +++ b/Examples/ocaml/callback/example.h @@ -0,0 +1,20 @@ +/* File : example.h */ + +#include <iostream> + +class Callback { +public: + virtual ~Callback() { std::cout << "Callback::~Callback()" << std::endl; } + virtual void run() { std::cout << "Callback::run()" << std::endl; } +}; + + +class Caller { + Callback *_callback; +public: + Caller(): _callback(0) {} + ~Caller() { delCallback(); } + void delCallback() { delete _callback; _callback = 0; } + void setCallback(Callback *cb) { delCallback(); _callback = cb; } + void call() { if (_callback) _callback->run(); } +}; diff --git a/Examples/ocaml/callback/example.i b/Examples/ocaml/callback/example.i new file mode 100644 index 000000000..584da73d9 --- /dev/null +++ b/Examples/ocaml/callback/example.i @@ -0,0 +1,10 @@ +/* File : example.i */ +%module(directors="1") example +%{ +#include "example.h" +%} + +%feature("director") Callback; + +%include "example.h" + diff --git a/Examples/ocaml/callback/runme.ml b/Examples/ocaml/callback/runme.ml new file mode 100644 index 000000000..a7d705241 --- /dev/null +++ b/Examples/ocaml/callback/runme.ml @@ -0,0 +1,30 @@ +(* file: runme.ml + +This file illustrates cross-language polymorphism using directors. *) + +open Swig +open Example + +let new_OCamlCallback ob meth args = + match meth with + | "run" -> print_endline "OCamlCallback.run()"; C_void + | _ -> (invoke ob) meth args + +let caller = new_Caller '() + +let _ = print_endline "Adding and calling a normal C++ callback" +let _ = print_endline "----------------------------------------" + +let callback = new_Callback '() +let _ = caller -> "setCallback" (callback) +let _ = caller -> "call" () +let _ = caller -> "delCallback" (0) + +let _ = print_endline "\nAdding and calling an OCaml callback" +let _ = print_endline "------------------------------------" + +let callback = new_derived_object new_Callback (new_OCamlCallback) '() +let _ = caller -> "setCallback" (callback) +let _ = caller -> "call" () +let _ = caller -> "delCallback" (0) +let _ = print_endline "\nOCaml exit" diff --git a/Examples/ocaml/check.list b/Examples/ocaml/check.list index fb7317b3a..cab5a6545 100644 --- a/Examples/ocaml/check.list +++ b/Examples/ocaml/check.list @@ -1,5 +1,6 @@ # see top-level Makefile.in argout_ref +callback class contract scoped_enum diff --git a/Examples/ocaml/stl/example.i b/Examples/ocaml/stl/example.i index 19a80a605..214ac47a5 100644 --- a/Examples/ocaml/stl/example.i +++ b/Examples/ocaml/stl/example.i @@ -7,6 +7,4 @@ #define ENABLE_STRING_VECTOR %include stl.i -%feature("director"); - %include example.h |