[cffi-devel] Re: The results of your email commands

Bruno Daniel bruno.daniel at gmx.net
Thu Oct 25 11:00:14 UTC 2007


Dear Attila,

> forget swig, use verrazzano instead
> (http://common-lisp.net/project/fetter/); avoid
> common-lisp-controller; and only send the interesting part of the
> output next time.
> 
> my 0.02,

So the Verrazano project has been resurrected? Does it handle methods that
are both overloaded and overridden? Is it possible to subclass a C++ class
in Lisp via Verrazano? I started to develop support for both these features in
Swig; maybe I should switch to Verrazano.

My subclassing-mechanism would look like the following :

(defun <subclass-name>-new (<constructor-lambda-list>)
  (create-object-subclassing-cpp
   (<constructor arguments for the superclass>)
   (make-<subclass-name>-struct
     <initializers for the additional attributes of the subclass>)
   <virtual method1> <lisp callback used for that method>
   <virtual method2> <lisp callback used for that method>
   ...))

Here's an example:
---------------------------------------------------------------------------
class C1 {
 protected:
  double a;
 public:
  C1(double a1) { a = a1; }
  C1(int a1) { a = a1; }
  virtual void print() { cout << "print: " << a << endl; }
};

class C2 : public C1 {
 private:
  double b;
 public:
  C2(double a1, double b1) : C1(a1) { b = b1; }
  virtual void print() { cout << "print: " << a << " " << b << endl; }
};

(defstruct c3-struct
  (c3attr1 0 :type double-float)
  (c3attr2 0 :type double-float))

(defcallback1 c3-print :void ((cobj :pointer))
  (with-struct (c3-struct- c3attr1 c3attr2) (cobj->lisp-struct cobj)
    (format t "~s~%" (list "from lisp" c3attr1 c3attr2))))

(defun c3_new (a1 b1)
  (create-object-subclassing-cpp
   (c2 a1 b1) (make-c3-struct :c3attr1 (+ a1 b1) :c3attr2 (- a1 b1))
   print c3-print))

(let ((c3obj (c3_new 2d0 3d0)))
  (c2_swig_subcl_print c3obj)
  (c1_swig_subcl_print c3obj))
  ;; Both of these will call the overriding methods. The polymorphism is
  ;; handled in C++, not in Lisp.
---------------------------------------------------------------------------
  
But this will only support single inheritance. Handling the additional
attributes of the subclass is a little bit tricky: The function
cobj->lisp-struct currently uses the c-object's address to lookup the
lisp-struct in a hash-table. This is not as slow as it sounds in SBCL, but
maybe there's a better solution. Since the garbage collector may move the Lisp
objects around, there's no point in saving any addresses of Lisp objects in
C. I also thought of closures in order to get rid of the struct, but closures
used as C-callbacks are extremely inefficient (see the discussion on
http://groups.google.com/group/sbcl-help-archive/browse_thread/thread/e01e8891c5b8b428/6f2adf6a9425a5cc?hl=en#6f2adf6a9425a5cc
). Then I had another idea: I could save the closures in some global variable,
array or hash and use a non-closure as a callback that delegates to the
closure, see
http://groups.google.com/group/sbcl-help-archive/browse_thread/thread/39c65b2f08229118/017b9d81c3af905f?hl=en#017b9d81c3af905f
.
   
Best regards,
  Bruno Daniel



More information about the cffi-devel mailing list