I always declare my foreign types as pointers to that type and then dereference them prior to passing them to functions that take references as arguments. To make sure that my pointer types are garbage collected properly, I define a c-lambda that will delete the object and attach that to a will that catches when the pointer is no longer referenced.
An example may be helpful: (c-declare #<<c-end #include <cstdlib> class Point2d { public: double myDist(Point2d &p2); int x; int y; };
c-end )
(c-define-type Point2d (pointer (type "Point2d")))
(define (point2d? obj) (and (foreign? obj) (eq? (car (foreign-tags obj)) 'Point2d*)))
(define make-point2d (lambda () (let ((obj ((c-lambda () Point2d "___result_voidstar = malloc(sizeof(Point2d));")))) (make-will obj free-point2d) obj)))
(define free-point2d (c-lambda (Point2d) void "free(___arg1);"))
(define point2d-get-x (c-lambda (Point2d) int "___result = ___arg1->x;"))
(define point2d-get-y (c-lambda (Point2d) int "___result = ___arg1->y;"))
(define point2d-set-x! (lambda (self newx) ((c-lambda (Point2d int) void "___arg1->x = ___arg2;") self (inexact->exact newx))))
(define point2d-set-y! (lambda (self newy) ((c-lambda (Point2d int) void "___arg1->y = ___arg2;") self (inexact->exact newy))))
'(define point2d-my-dist (lambda (self pt2) ((c-lambda (Point2d Point2d) double "___result = ___arg1->myDist(*___arg2);") self pt2)))
On Sun, Aug 23, 2009 at 4:21 AM, Ivan Kellyivan@bleurgh.com wrote:
Im porting a C++ library to gambit and have numerous functions which take references as parameters or return type.
(define QObject-setObjectName (c-lambda (QObject* const_QString&) (void) "arg1->setObjectName(arg2);"))
(define QObject-children (c-lambda (QObject*) (const_QList<QObject*>&) "____result = arg1->children();"))
My problem is I don't know how to c-define-type the reference types. On the one hand I want them to act as "type", when being passed in as parameters. On the other I want pointer behaviour, so that the whole object isn't copied on return. Has anyone else run into this problem with c++, and how did you solve it?
Regards Ivan _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list