[plt-scheme] External interface to C++ code on MacOSX

From: Jim Witte (jswitte at bloomington.in.us)
Date: Tue Aug 8 00:24:48 EDT 2006

  Hi all,

   This is kind of an off-the-wall question, and I know *nothing*  
about the innards of PLT, so..  But it seems similar to Ivanka's  
recent question regarding calling external programs.  I'm trying to  
do something much more ambitious - make an external package (on OSX  
initially) written in C which would essentially define a set of C++  
objects, with Scheme accessible accessors/functions.  The C++ objects  
would maintain state, so they'd have to 'live' in their own memory  
somewhere (and the programmer might therefore have to remember to get  
rid of them if PLT won't do that)  What this is is a VERY large set  
of C++ written objects to do access the the Apple Newton:.  What I'm  
envisioning on the Scheme side is something like this (to compile and  
install a simple application package over a serial link):

(load external-module DCL)

(let ((func1src ((type 'newtonscript)
                  (code "func1() begin return 2 end"))
       (func2src ((type newtonscript)
                  (code "func2(x) begin return x*func1() end")))))

   (let ((app-frame ((class 'protoFloatNGo)
                     (DoThis (DCL compile-function func1src))
                     (DoThat (DCL compile-function func2src))
                     (viewChildren (((class 'protoButton)
                                     (viewBounds ((left 10)
                                                  (top 10)
                                                  (bottom 40)
                                                  (right 70)))
                                     (code
                     "begin setValue(_parent.txt, 'text, NumberStr 
(DoThis()) end"))
                                    ((class 'protoTextFld)
                                     (viewBounds ((left 50)
                                                  (top 150)
                                                  (right 100)
                                                  (bottom 170)))
                                     (text "Initial Text")))))))
     (app-package (DCL make-package app-frame))


     (let ((connect-frame ((type 'serial)
                           (baud 9660))))
       (let ((MY_NEWT (DCL wait-for-connect connect-frame)))
         (let ((success (MY_NEWT install-package app-package)))
           (if (= success 0) (display "Successfully installed")
               (display "Didn't install, error %d", success)))
         (MY-NEWT disconnect)))))


   There of course could be more error checking (probably handled by  
whatever Scheme has for exceptions (its been a while since I'd done  
scheme..)

   The thins in caps (DCL and MY-NEWT) are actual Scheme objects -  
DCL is the external module, and MY-NEWT is an object that DCL created  
and returned.  Is it possible to have an external library have this  
level of interaction with the Scheme environment?  I know external  
programs can access bindings in the scheme environment, but I want to  
create entire objects (which are essentially Scheme functions), which  
will then have certain commands (first parameters -  disconnect in  
the case of the created MY-NEWT) which will then call back to the C++  
library.  Of course, I'd need a reference to the C++ structure that  
underlied all of this, but that would be easy with a "hidden"  
variable like (__CPLUSPLUSPTR 0x000000).

     I'm contemplating making a replacement for the now almost  
COMPLETELY obsolete Newton Toolkit, either with a Cocoa front-end for  
the GUI stuff (or maybe Scheme, it would be more portable, but more  
tedious as I don't know how to do GUIs in PLT), and then either a  
Scheme or (Objective) C middle layer, linking to the DCL as the C++  
backend.

Jim


Posted on the users mailing list.