Changeset 8895


Ignore:
Timestamp:
Mar 26, 2008, 2:57:22 PM (11 years ago)
Author:
mikel
Message:

working on objective-c bridge docs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/doc/src/objc-bridge.xml

    r8894 r8895  
    218218        We do it like this:</para>
    219219      <programlisting>
    220         (rlet ((r :&lt;NSR>ect))
    221         (send/stret r v1 'bounds)
    222         (send v2 :set-bounds r))
     220(rlet ((r :&lt;NSR&gt;ect))
     221          (send/stret r v1 'bounds)
     222          (send v2 :set-bounds r))
    223223      </programlisting>
    224224      <para>The rlet allocates the storage (but doesn't initialize
     
    238238        above could have been written more tersely as:</para>
    239239      <programlisting>
    240         (slet ((r (send v1 'bounds)))
    241         (send v2 :set-bounds r))
     240(slet ((r (send v1 'bounds)))
     241      (send v2 :set-bounds r))
    242242          </programlisting>
    243243      <para>Second, when one call to <literal>send</literal> is made
     
    246246        just write:</para>
    247247      <programlisting>
    248         (send v1 :set-bounds (send v2 'bounds))
     248(send v1 :set-bounds (send v2 'bounds))
    249249      </programlisting>
    250250      <para>There are also several psuedo-functions provided for convenience
     
    254254      <para>These pseudo-functions can be used within an SLET initform:</para>
    255255      <programlisting>
    256         (slet ((p (ns-make-point 100.0 200.0)))
    257         (send w :set-frame-origin p))
     256(slet ((p (ns-make-point 100.0 200.0)))
     257      (send w :set-frame-origin p))
    258258      </programlisting>
    259259      <para>Or within a call to <literal>send</literal>:</para>
    260260      <programlisting>
    261         (send w :set-origin (ns-make-point 100.0 200.0))
     261(send w :set-origin (ns-make-point 100.0 200.0))
    262262      </programlisting>
    263263      <para>However, since these aren't real functions, a call like the
    264264        following won't work:</para>
    265265      <programlisting>
    266         (setq p (ns-make-point 100.0 200.0))
     266(setq p (ns-make-point 100.0 200.0))
    267267      </programlisting>
    268268      <para>To extract fields from these objects, there are also some
     
    283283        formatted strings:</para>
    284284      <programlisting>
    285         [NSClass stringWithFormat: "%f %f" x y]
     285[NSClass stringWithFormat: "%f %f" x y]
    286286      </programlisting>
    287287      <para>In Lisp, this would be written:</para>
    288288      <programlisting>
    289         (send (find-class 'ns:ns-string)
    290         :string-with-format #@"%f %f"
    291         (:double-float x :double-float y))
     289(send (find-class 'ns:ns-string)
     290      :string-with-format #@"%f %f"
     291      (:double-float x :double-float y))
    292292      </programlisting>
    293293      <para>Note that it's necessary to specify the foreign types of the
     
    317317        example:</para>
    318318      <programlisting>
    319         (send (the ns:ns-window w) 'center)
     319(send (the ns:ns-window w) 'center)
    320320          </programlisting>
    321321      <para>Note that there is no way in Objective-C to name the class of a
     
    325325        ns:ns-color):</para>
    326326      <programlisting>
    327         (let ((c (find-class 'ns:ns-color)))
    328         (declare ((ccl::@metaclass ns:ns-color) c))
    329         (send c 'white-color))
     327(let ((c (find-class 'ns:ns-color)))
     328  (declare ((ccl::@metaclass ns:ns-color) c))
     329  (send c 'white-color))
    330330      </programlisting>
    331331      <para>The other case that alllows optimization is when only
     
    351351        you should rebuild the table:</para>
    352352      <programlisting>
    353         ? (update-type-signatures)
     353? (update-type-signatures)
    354354      </programlisting>
    355355      <para>Because <literal>send</literal> and its relatives
     
    392392      new slots), we could do:</para>
    393393    <programlisting>
    394       (defclass example-window (ns:ns-window)
    395       ()
    396       (:metaclass ns:+ns-object))
     394(defclass example-window (ns:ns-window)
     395  ()
     396  (:metaclass ns:+ns-object))
    397397    </programlisting>
    398398    <para>That'll create a new Objective-C class named EXAMPLE-WINDOW whose
     
    417417        say:</para>
    418418      <programlisting>
    419         (defclass key-event-counting-window (ns:ns-window)
    420         ((key-event-count :foreign-type :int
    421         :initform 0
    422         :accessor window-key-event-count))
    423         (:metaclass ns:+ns-object))
     419(defclass key-event-counting-window (ns:ns-window)
     420  ((key-event-count :foreign-type :int
     421                    :initform 0
     422                    :accessor window-key-event-count))
     423  (:metaclass ns:+ns-object))
    424424      </programlisting>
    425425      <para>Foreign slots are always SLOT-BOUNDP, and the initform
     
    435435        with "an instance of a foreign class". For instance:</para>
    436436      <programlisting>
    437         (defclass hemlock-buffer-string (ns:ns-string)
    438         ((hemlock-buffer :type hi::hemlock-buffer
    439         :initform hi::%make-hemlock-buffer
    440         :accessor string-hemlock-buffer))
    441         (:metaclass ns:+ns-object))
     437(defclass hemlock-buffer-string (ns:ns-string)
     438  ((hemlock-buffer :type hi::hemlock-buffer
     439                   :initform hi::%make-hemlock-buffer
     440                   :accessor string-hemlock-buffer))
     441  (:metaclass ns:+ns-object))
    442442          </programlisting>
    443443      <para>As one might expect, this has memory-management
     
    471471      examples.  Let us define a class to use in them:</para>
    472472    <programlisting>
    473       (defclass data-window-controller (ns:ns-window-controller)
    474       ((window :foreign-type :id :accessor window)
    475       (data :initform nil :accessor data))
    476       (:metaclass ns:+ns-object))
     473(defclass data-window-controller (ns:ns-window-controller)
     474  ((window :foreign-type :id :accessor window)
     475   (data :initform nil :accessor data))
     476  (:metaclass ns:+ns-object))
    477477    </programlisting>
    478478    <para>There's nothing special about this class.  It inherits
     
    485485      that's not important:</para>
    486486    <programlisting>
    487       (define-objc-method ((:id get-window)
    488       data-window-controller)
    489       (window self))
     487(define-objc-method ((:id get-window)
     488                     data-window-controller)
     489    (window self))
    490490    </programlisting>
    491491    <para>The return type of this method is the foreign type :id,
     
    500500      symbol, but with a parameter, it's a keyword:</para>
    501501    <programlisting>
    502       (define-objc-method ((:id :init-with-multiplier (:int multiplier))
    503       data-window-controller)
    504       (setf (data self) (make-array 100))
    505       (dotimes (i 100)
    506       (setf (aref (data self) i)
    507       (* i multiplier)))
    508       self)
     502(define-objc-method ((:id :init-with-multiplier (:int multiplier))
     503                     data-window-controller)
     504    (setf (data self) (make-array 100))
     505  (dotimes (i 100)
     506    (setf (aref (data self) i)
     507          (* i multiplier)))
     508  self)
    509509    </programlisting>
    510510    <para>To Objective-C code which uses the class, the name of this
     
    516516    <para>Here's an example with more than one parameter:</para>
    517517    <programlisting>
    518       (define-objc-method ((:id :init-with-multiplier (:int multiplier)
    519       :and-addend (:int addend))
    520       data-window-controller)
    521       (setf (data self) (make-array size))
    522       (dotimes (i 100)
    523       (setf (aref (data self) i)
    524       (+ (* i multiplier)
    525       addend)))
    526       self)
     518(define-objc-method ((:id :init-with-multiplier (:int multiplier)
     519                          :and-addend (:int addend))
     520                     data-window-controller)
     521    (setf (data self) (make-array size))
     522  (dotimes (i 100)
     523    (setf (aref (data self) i)
     524          (+ (* i multiplier)
     525             addend)))
     526  self)
    527527    </programlisting>
    528528    <para>To Objective-C, the name of this method is
     
    535535      one says :void for the return type:</para>
    536536    <programlisting>
    537       (define-objc-method ((:void :take-action (:id sender))
    538       data-window-controller)
    539       (declare (ignore sender))
    540       (dotimes (i 100)
    541       (setf (aref (data self) i)
    542       (- (aref (data self) i)))))
     537(define-objc-method ((:void :take-action (:id sender))
     538                     data-window-controller)
     539    (declare (ignore sender))
     540  (dotimes (i 100)
     541    (setf (aref (data self) i)
     542          (- (aref (data self) i)))))
    543543    </programlisting>
    544544    <para>This method would be called "takeAction:" in Objective-C.  The
     
    552552      following two method definitions are equivalent:</para>
    553553    <programlisting>
    554       (define-objc-method ("applicationShouldTerminate:"
    555       "LispApplicationDelegate")
    556       (:id sender :&lt;BOOL>)
     554(define-objc-method ("applicationShouldTerminate:"
     555                     "LispApplicationDelegate")
     556    (:id sender :&lt;BOOL>)
     557         (declare (ignore sender))
     558         nil)
     559 
     560  (define-objc-method ((:&lt;BOOL>
     561                        :application-should-terminate sender)
     562                       lisp-application-delegate)
    557563      (declare (ignore sender))
    558       nil)
    559 
    560       (define-objc-method ((:&lt;BOOL>
    561       :application-should-terminate sender)
    562       lisp-application-delegate)
    563       (declare (ignore sender))
    564       nil)
     564    nil)
    565565    </programlisting>
    566566    <sect2 id="Method-Redefinition-Constraints">
     
    618618      example:</para>
    619619    <programlisting>
    620       (ccl::make-objc-instance "WiErDclass")
    621       (ccl::send o "WiErDmEsSaGe:WithARG:" x y)
     620(ccl::make-objc-instance "WiErDclass")
     621(ccl::send o "WiErDmEsSaGe:WithARG:" x y)
    622622    </programlisting>
    623623    <para>Alternatively, you can define a special translation rule
     
    625625      you need to use througout your code.  Some examples:</para>
    626626    <programlisting>
    627       (ccl::define-classname-translation "WiErDclass" wierd-class)
    628       (ccl::define-message-translation "WiErDmEsSaGe:WithARG:" (:weird-message :with-arg))
    629       (ccl::define-init-translation "WiErDiNiT:WITHOPTION:" (:weird-init :option))
     627(ccl::define-classname-translation "WiErDclass" wierd-class)
     628(ccl::define-message-translation "WiErDmEsSaGe:WithARG:" (:weird-message :with-arg))
     629(ccl::define-init-translation "WiErDiNiT:WITHOPTION:" (:weird-init :option))
    630630    </programlisting>
    631631    <para>The normal rule in Objective-C names is that each word begins with a
     
    637637      in the bridge, but you can define new ones as follows:</para>
    638638    <programlisting>
    639       (ccl::define-special-objc-word "QuickDraw")
     639(ccl::define-special-objc-word "QuickDraw")
    640640    </programlisting>
    641641    <para>Note that message keywords in a SEND such as (SEND V
Note: See TracChangeset for help on using the changeset viewer.