List Info

Thread: "ocaml_beginners"::[] Re: multiple callbacks




"ocaml_beginners"::[] Re: multiple callbacks
country flaguser name
Germany
2007-09-19 02:39:57
Hi,

the example helped 

below is my solution:
you manage the callback list in ocaml and register just one
callback to the 
ocamlside: that callback calls the dumplist function in
ocaml.
You can add more code to remove functions from the list
etc.
I didn't tried it, but it should be o.k.....

cheers
 Michael
------------------------------------------------------------
---

// the add_cb and dump_list functions in OCaml, they share
the callback list:

let add_cb, dump_list =
	let callbacks = ref [] in
	let add = fun cb  -> callbacks := cb :: !callbacks  in
	let call_them = fun () -> List.iter  (fun f -> f())
(List.rev !callbacks) in
	add, call_them
;;

(* to initialize the c-side  *)
external cb_init: unit -> unit =
"ml_init_cb";;

(* to initialize the all *)
let init_callbacks () = 
	Callback.register "ml_dump_list" dump_list;
	cb_init()
;;

//////////////////////////////////// C - part
// you can cache the output of caml_named_value

void call_caml(void)
{
 caml_callback(*caml_named_value("ml_dump_list"),
Val_unit);
}

CAMLprim ml_init_cb(value zip)
{
CAMLparam1(zip);
add_cb(call_caml);
CAMLreturn(Val_unit);
}

(*  to use: *)
init_callbacks();
add_cb( fun () -> print_endline "A");
add_cb( fun () -> print_endline "B");
dump_list()



Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/

The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    ht
tp://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:ocaml_beginners-digest@yahoogroups.com 
    mailto:ocaml_beginners-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 

"ocaml_beginners"::[] Re: multiple callbacks
country flaguser name
France
2007-09-19 16:13:34
> the example helped 
fortunatly 
and Richard Jones asked me to post it on the list, as I
guess joined pieces 
are not allowed, it's there: http://n
orth.chez-alice.fr/dummy_example.tgz

> below is my solution:
> you manage the callback list in ocaml and register just
one callback to the
> ocamlside: that callback calls the dumplist function in
ocaml.
> You can add more code to remove functions from the list
etc.
> I didn't tried it, but it should be o.k.....

the problem is that in the small sample example, dump_list()
simulates events 
in the library that makes call the callbacks, and with your
solution the 
library won't be able to call individualy each callbacks.
(Sorry your solution does work with my small sample example,
because I haven't 
made a complete event management in the sample, I don't even
know how to do 
this in C, lol)
Putting the callbacks in an ocaml list is a solution close
to the one for 
glutTimerFunc() in LablGlut, but in LablGlut this is a
Hashtable so callbacks 
can be retrieved individualy with an index.



Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/

The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    ht
tp://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:ocaml_beginners-digest@yahoogroups.com 
    mailto:ocaml_beginners-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )