[OCaml] Add a callback example
It is based on the the Python and Go examples.
This commit is contained in:
parent
fa0c3fe5c2
commit
a641966e0b
7 changed files with 95 additions and 2 deletions
31
Examples/ocaml/callback/Makefile
Normal file
31
Examples/ocaml/callback/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
|
||||
3
Examples/ocaml/callback/example.c
Normal file
3
Examples/ocaml/callback/example.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
20
Examples/ocaml/callback/example.h
Normal file
20
Examples/ocaml/callback/example.h
Normal 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(); }
|
||||
};
|
||||
10
Examples/ocaml/callback/example.i
Normal file
10
Examples/ocaml/callback/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* File : example.i */
|
||||
%module(directors="1") example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%feature("director") Callback;
|
||||
|
||||
%include "example.h"
|
||||
|
||||
30
Examples/ocaml/callback/runme.ml
Normal file
30
Examples/ocaml/callback/runme.ml
Normal 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"
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
# see top-level Makefile.in
|
||||
argout_ref
|
||||
callback
|
||||
class
|
||||
contract
|
||||
scoped_enum
|
||||
|
|
|
|||
|
|
@ -7,6 +7,4 @@
|
|||
#define ENABLE_STRING_VECTOR
|
||||
%include stl.i
|
||||
|
||||
%feature("director");
|
||||
|
||||
%include example.h
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue