Re: Encapsulate C struct/toll-free bridging?
Re: Encapsulate C struct/toll-free bridging?
- Subject: Re: Encapsulate C struct/toll-free bridging?
- From: Gwynne <email@hidden>
- Date: Tue, 9 Mar 2004 11:06:54 -0500
On Mar 9, 2004, at 10:15 AM, Alastair Houghton wrote:
I am passing a C data structure back and forth between Cocoa framework
and carbon CFM code. Is there an elegant way to encapsulate structs
such that I can integrate it better with Objective-C/Cocoa?
Also, don't forget that Objective-C is a superset of C, so you could
just use the structure on the Objective-C side; I suppose what I'm
saying is that you shouldn't get so carried away with OO that you make
things harder for yourself as a consequence.
This right here is the key paragraph. Objective-C objects can be
treated as C structures most easily this way:
@interface MyClass : NSObject
{
// instance vars
}
// methods
@end
struct MyClassC {
@defs(MyClass) // note: no semicolon
};
Now, an instance of MyClass* and MyClassC* are exactly identical,
except that the latter can be treated as a C structure. Since the
@defs() includes the isa pointer, you can cast back and forth between
the structure and the object freely. The major advantage to @defs() is
that the structures will not get out of sync, since the variables are
only declared in one place.
Note that using @defs() destroys all use of @protected and @private,
and if the Obj-C class has a superclass other than NSObject, all of the
superclass' instance variables, all the way up the class hierarchy,
will be included in the resultant C structure. For the detail-obsessed
among us, the isa pointer itself technically comes from the NSObject
class, not the runtime.
The formal documentation of @defs() can be found here:
<
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
3objc_language_overview/chapter_7_section_8.html#//apple_ref/doc/uid/
20001424/TPXREF164>
-- Gwynne, key to the Code that runs us all
Formerly known as Sailor Quasar.
Email: email@hidden
Web:
http://musicimage.plasticchicken.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.