[Openmcl-devel] wrapping foreign objects in lisp classes

Gary Byers gb at clozure.com
Mon Aug 16 23:29:49 UTC 2004



On Mon, 16 Aug 2004, Dan Knapp wrote:
>
>    Using the metaobject protocol doesn't avoid having to write the
> custom readers and
> writers; you still need to do that.  However, you should be able to do
> it in such a way
> that you only need to do it once instead of for every slot of every
> class.
>

The methods that a custom metaclass would have to override are
SLOT-VALUE-USING-CLASS and (SETF SLOT-VALUE-USING-CLASS).  Reader and
writer methods do the equivalent of calls to these methods in general
(in a case where a user-defined [SETF ]SLOT-VALUE-USING-CLASS method
could be applicable, the user-defined method will be invoked.)

The Cocoa bridge implements SLOT-VALUE-USING-CLASS (and SETF thereof)
by storing getter/setter functions (defined in terms of low-level
memory accessors) in specialized SLOT-DEFINITION objects.

In the bridge's case, it seemed natural to treat ObjC instance variables
as CLOS slots.  It may be simpler (in some ways) to simply define methods
on the fields of an encapsulated foreign structure, without trying to
make those methods be accessor methods.  As a poor example:

;; This definition might be encoded in a .cdb file
(def-foreign-type nil (:struct :point  ; this is a hypothetical example
                         (:x :float)
                         (:y :float)))

(defclass point ()
  ((real-point :initarg :real-point
               :accessor real-point
               :documentation "a pointer to a real point")))

(defmethod point-x ((p point))
  (let* ((real-point (real-point p)))
    (pref real-point :point.x)))


(defmethod (setf point-x) (new (p point))
  (let* ((real-point (real-point p)))
    (setf (pref real-point :point.x) new)))


You might want these methods to do a little more type- and sanity-checking
than is shown here, but it seems like most/all of that is boilerplate:
given a foreign structure definition, it's probably not too hard to
generate an encapsulating class and methods to access that structure's
fields.

(You wouldn't have the ability to use SLOT-VALUE or WITH-SLOTS on the
foreign field, but you'd be able to deal with a foreign structure and
its fields at a fairly high level.)



More information about the Openmcl-devel mailing list