[Openmcl-devel] CCL <-> C++ interface generator in alpha, reviewers wanted

Jason E. Aten j.e.aten at gmail.com
Sun Apr 10 21:10:52 PDT 2011


Thanks again to Daniel and the other folks who replied.  I think I've got my
basic C++ overloaded functions mapped into CLOS. Horray!

One caveat though:  Except for the overloaded C++ constructors.  Yikes!   Is
it possible to overload constructors in CLOS? (So that one ctor takes zero
arguments, and another takes two args, for example).  When I try, it doesn't
work.

Here is what I'm trying, without much success. If I try to defie a 2nd
initialize-instance method, it seems to erase the first.

Suggestions welcome(!)

- Jason

;; specific example
(require 'cffi)
(shadowing-import 'cffi:defcallback)
(use-package :cffi)

; // declare a C++ class with overloaded constructor.
; // in overctor.h:
; class Vlad {
;  public:
;   Vlad(int a, double d);
;   Vlad();
; };
; // in overctor.cpp
; Vlad::Vlad(int a, double d) { printf("Vlad(int,double) ctor called\n"); }
; Vlad::Vlad() { printf("Vlad() ctor called\n"); }
;
; build library with SWIG and g++ using:
;
; swig -cffi -c++  overctor.i
; g++ -c overctor_wrap.cxx -fpic -o overctor_wrap.o
; g++ -c overctor.cpp      -fpic -o overctor.o
; g++ -shared overctor.o overctor_wrap.o -o overctor.so


; load the library
(:cd "/home/jaten/dj/overctor")
(open-shared-library "./overctor.so")


; declare the C functions that should get called by the CLOS initializers
(cffi:defcfun ("_wrap_new_Vlad__SWIG_0" new_Vlad__SWIG_0) :pointer
  (a :int)
  (d :double))

(cffi:defcfun ("_wrap_new_Vlad__SWIG_1" new_Vlad__SWIG_1) :pointer)

(cl:export 'new_Vlad__SWIG_0)
(cl:export 'new_Vlad__SWIG_1)

; test them

; ? (new_Vlad__SWIG_0 3 4.0d0 )
; Vlad(int,double) ctor called
; #<A Foreign Pointer #xE363C0>
; ?

; ? (new_Vlad__SWIG_1 )
; Vlad() ctor called
; #<A Foreign Pointer #xE363E0>
; ?

; they work fine. Now try to get the CLOS wrapper stuf working.


; declare CLOS class

(cl:defclass Vlad ()
  ((ff-pointer :reader ff-pointer)))

(cl:export 'Vlad)

; this works:
(cl:defmethod initialize-instance :after ((obj Vlad) &key (a cl:integer) (d
cl:number))
  (setf (slot-value obj 'ff-pointer) (new_Vlad__SWIG_0 a d)))

(setf o2 (make-instance 'Vlad :a 2 :d 1.0d0))
; returns:
; Vlad(int,double) ctor called
; #<Vlad #x30200144DF7D>


(cl:defmethod initialize-instance :after ((obj Vlad) &key)
  (setf (slot-value obj 'ff-pointer) (new_Vlad__SWIG_1)))

(setf o  (make-instance 'Vlad))
; returns:
; Vlad() ctor called
; #<Vlad #x30200147E3BD>

(setf o3 (make-instance 'Vlad :a 2 :d 1.0d0))
; returns:
;
; > Error: :a is an invalid initarg to initialize-instance for
#<standard-class Vlad>.
; >        Valid initargs: nil.
; > While executing: ccl::check-initargs, in process listener(1).
;
; ????? what to try?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20110410/7ea92d48/attachment.htm>


More information about the Openmcl-devel mailing list