From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q43Dup2Z029743 for ; Thu, 3 May 2012 15:56:53 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhwBADqNok/RVdIpkGdsb2JhbABEsm0IIgEBAQEJCQ0HFAQjggoBAQQSAhMZARsdAQMMBgULOyIBEQEFARwGGxqHXAEDCwubVQkDjCSCc4UaChknDVeIdgEFC5B9BJV+gRGNUT2EDA X-IronPort-AV: E=Sophos;i="4.75,523,1330902000"; d="scan'208";a="142450326" Received: from mail-pz0-f41.google.com ([209.85.210.41]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 03 May 2012 15:56:52 +0200 Received: by dakp5 with SMTP id p5so1490403dak.0 for ; Thu, 03 May 2012 06:56:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=qcrYMAC70qe8RfZjKKue1n51ZHeUIEto1gDU/XYPzZ4=; b=zFv09paOsMGNI7bluYsyzPwC/dSxdujM6iOHu2Kn1XQVEwulCmCGIj2cGVMVw50zBo Cmi3hKq3wTy2w1oK3o47j6UWRKZDOfunH8pyi+PzK6dmcGPZNHiQ0blWwHO3EwlENDv7 EqKbAmbcUXwzZKZ3sH8LVQchl2yYoquFDRNJWJr5wvwsjv0yyl2xNLLkOrR2GumyHU5e V19X27FITXTdSaPHZEymwpq0WVnSXycEt2W2ZXa3G6y9P8qakq04JHMkWbr+ffDqihWH 3kLv8eIpO0H/dEvInJ6ukbmIw49pXzxbsF9Cd0dc7bf3/Lhjvb8NkhMpk0y6rvxgxZOx axvg== MIME-Version: 1.0 Received: by 10.68.227.169 with SMTP id sb9mr7697471pbc.157.1336053410729; Thu, 03 May 2012 06:56:50 -0700 (PDT) Received: by 10.68.228.194 with HTTP; Thu, 3 May 2012 06:56:50 -0700 (PDT) In-Reply-To: <871un1z8sq.fsf@frosties.localnet> References: <871un1z8sq.fsf@frosties.localnet> Date: Thu, 3 May 2012 14:56:50 +0100 Message-ID: From: Joel Reymont To: Goswin von Brederlow Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] using modules to wrap c++ classes Say I have a library class AdmCallbacks class AdmCallbacks { public : AdmCallbacks(); virtual ~AdmCallbacks(); /* ---------------------------------------------------------------- */ virtual int Alert(AlertInfo * pInfo, void * pContext, int * aiCode) = 0; private : }; I subclass it and implement the Alert method to receive notifications. I then create an instance of my derived class and pass it to some initialization function so that the API knows to call the methods of my instance. My derived class may look like this class MyAdmCallbacks: public AdmCallbacks { public : MyAdmCallbacks() {}; ~MyAdmCallbacks() {}; /* ---------------------------------------------------------------- */ virtual int Alert(AlertInfo * pInfo, void * pContext, int * aiCode); }; int MyAdmCallbacks::Alert(AlertInfo * pInfo, void * pContext, int * aiCode) { int iIgnored; /* ---------------------------------------------------------------- */ cout << endl << endl; if (!pInfo -> dump(&iIgnored)) { cout << "error in pInfo -> dump : " << iIgnored << endl; } /* ---------------------------------------------------------------- */ *aiCode = API_OK; return (OK); } Now, suppose I want to implement the derived class on the OCaml side. What is the best way to do it? I can derive on the C++ side, keep a pointer to the OCaml instance (self) in a member variable, instantiate MyAdmCallbacks in the "MyAdmCallbacks_new" stub function which takes the OCaml self and then set the member variable. The Alert method on the C++ side would then dispatch to OCaml using the stored OCaml self to call the Alert method as per 18.3.5 of the manual callback(caml_get_public_method(foo, hash_variant("bar")), foo); Is there a better way to implement this on the OCaml side? Thanks, Joel -------------------------------------------------------------------------- Working on AlgoKit, a new algorithmic trading platform using Rithmic R|API ---------------------+------------+--------------------------------------- http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont ---------------------+------------+---------------------------------------