This is the mail archive of the guile@sources.redhat.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: guile and C++


On Sat, Sep 02, 2000 at 01:10:39PM +0200, Richard Guenther wrote:
: Are there any issues with using guile with C++ code, i.e.
: registering C++ functions to guile (not methods) and/or the
: guile startup in main? Has anyone tried to export more complex
: objects (C++ classes and methods) to guile?

I've started binding C++ classes to Guile w/Goops, and it has been
painless so far.

What I did was to create a smob for the class, which just contained
a pointer to the object.  Then I wrote SCM wrappers for the methods
like this:

  int class::method( int arg )

is wrapped by (compact pseudocode):

  SCM class_method( SCM smob, SCM arg ) {
    class * inst = SCM_CDR(smob);
    int arg1 = gh_scm2int(arg);
    return gh_int2scm( inst->method(arg1) );
  }

Then you hook it up:

  #define PREFIX "prefix-"
  scm_make_gsubr( PREFIX "class-method", 2, 0, 0, (SCM (*)(...)) class_method );

In Goops, you can do something like this:

(define-class <myclass> ()
  (myclass-smob #:getter get-myclass-smob #:setter set-myclass-smob))

(define-method method ((myclass <myclass>) arg1)
  (prefix-myclass-method (get-myclass-smob viewer) arg1))

;; the "constructor"
(define-method initialize ((myclass <myclass>) . args)
  (set-myclass-smob myclass (prefix-new-myclass)))


Then you can write Guile-code like this:

(define instance (make <myclass>))
(method instance)

I haven't gotten around to hook up virtual functions yet, but I got a pretty
good idea how that can be done and it doesn't seem too hard (will involve
a lot of extra work, though, compared to the above member method).

  Lars J
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]