[Openmcl-devel] Using CFStrings in OpenMCL

David Steuber david at david-steuber.com
Sat Dec 18 02:47:30 UTC 2004


On Dec 17, 2004, at 6:35 PM, Gary Byers wrote:

> The code that Dan mentions deals with NSSTRINGs (and may only be
> available in the "bleeding edge" version of the Cocoa sources, where
> it's quite likely defined in an obscure place ...)

I'm running 0.14.2-p1 with some updates from CVS.  I assume this is not 
what you mean by "bleeding edge".

> NSSTRINGs and CFSTRINGs are pretty much interchangeable, but David
> Steuber's (reasonable-looking) code will work in Carbon as well as
> Cocoa.  (You might consider doing the #_CFRelease in an UNWIND-PROTECT
> cleanup.)

Good idea.

(defmacro with-cfstring ((sym str) &rest body)
   "Create, use, and then release a CFString."
   `(let ((,sym (make-cfstring ,str)))
      (unwind-protect (progn , at body)
        (#_CFRelease ,sym))))

Code looks cleaner to boot.

> It's also worth looking at what the CFSTR C macro expands into; it's
> generally used to create a constant string (sort of like @"FOO" in
> Objective-C, or #@"FOO" in the Cocoa bridge.)  A constant NSString
> never gets released (its -release method is a no-op), and I assume
> that that's true of constant CFStrings as well.  When trying to
> translate C Carbon code to Lisp, that may be an important 
> consideration.

For anyone who doesn't feel like running cpp:

int main(int, char**) {
   void* cfstrptr = CFSTR("foo");
   return 0;
}

Expands into:

int main(int, char**) {
   void* cfstrptr = __CFStringMakeConstantString("" "foo" "");
   return 0;
}

The ADC documentation included with Xcode says --- CFString is 
“toll-free bridged” with its Cocoa Foundation counterpart, NSString. 
This means that the Core Foundation type is interchangeable in function 
or method calls with the bridged Foundation object. Therefore, in a 
method where you see an NSString * parameter, you can pass in a 
CFStringRef, and in a function where you see a CFStringRef parameter, 
you can pass in an NSString instance. This fact also applies to 
concrete subclasses of NSString. See Integrating Carbon and Cocoa in 
Your Application for more information on toll-free bridging.

I think that's pretty clever.  I'm not up to speed with Objective-C 
yet.  Lisp is obscure enough ;-).  Once I'm comfortable with Carbon, I 
might look at Cocoa.  Either way, for a CFString or NSString, I still 
have to release it so far as I can tell.





More information about the Openmcl-devel mailing list