[OCaml] Add a callback example

It is based on the the Python and Go examples.
This commit is contained in:
Zackery Spytz 2019-01-16 00:53:13 -07:00
commit a641966e0b
7 changed files with 95 additions and 2 deletions

View 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

View file

@ -0,0 +1,3 @@
/* File : example.c */
#include "example.h"

View file

@ -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(); }
};

View file

@ -0,0 +1,10 @@
/* File : example.i */
%module(directors="1") example
%{
#include "example.h"
%}
%feature("director") Callback;
%include "example.h"

View file

@ -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"

View file

@ -1,5 +1,6 @@
# see top-level Makefile.in
argout_ref
callback
class
contract
scoped_enum

View file

@ -7,6 +7,4 @@
#define ENABLE_STRING_VECTOR
%include stl.i
%feature("director");
%include example.h