• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Encapsulate C struct/toll-free bridging?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Encapsulate C struct/toll-free bridging?


  • Subject: Re: Encapsulate C struct/toll-free bridging?
  • From: David Bainbridge <email@hidden>
  • Date: Tue, 9 Mar 2004 11:43:01 -0600

On Mar 9, 2004, at 10:06 AM, Gwynne wrote:

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>


This looks very interesting.. I may not be able to take full advantage of this because the data structure is coming from a codewarrior project that is mac68k aligned... and as a pascal record (gasp!).. I currently have a C struct that is identical to the pascal record which are both mac68k aligned. I could however change the record so that it will have room for the isa pointer, but what alignment should the record/struct be so that it matches exactly what the obj-c runtime expects?
_______________________________________________
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.


  • Follow-Ups:
    • Re: Encapsulate C struct/toll-free bridging?
      • From: Clark Cox <email@hidden>
References: 
 >Encapsulate C struct/toll-free bridging? (From: David Bainbridge <email@hidden>)
 >Re: Encapsulate C struct/toll-free bridging? (From: Alastair Houghton <email@hidden>)
 >Re: Encapsulate C struct/toll-free bridging? (From: Gwynne <email@hidden>)

  • Prev by Date: Re: Convert Charatcers
  • Next by Date: Re: [OT] Apple Developer Certification
  • Previous by thread: Re: Encapsulate C struct/toll-free bridging?
  • Next by thread: Re: Encapsulate C struct/toll-free bridging?
  • Index(es):
    • Date
    • Thread