• 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: Alastair Houghton <email@hidden>
  • Date: Tue, 9 Mar 2004 15:15:38 +0000

On 9 Mar 2004, at 03:52, David Bainbridge 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?

What you need to remember is that Objective-C objects and C structures
are pretty much exactly the same thing; the only difference is that an
ObjC object has an isa (read "is a") pointer as the first element. So,
you can achieve effects like toll-free bridging by doing the following:

1. Define an Objective-C class and a struct as follows:

struct C_CList {
struct objc_class *isa; /* Need this initial member */
<contents>
};

@interface CListObject {
<contents>
}

<methods>
@end

2. When you initialise an instance of your structure, set the isa
pointer equal to [CListClass class].

The contents you list in the C_CList structure and the CListObject
should match; that way, you'll be able to access the members directly
in the Objective-C code, and you won't have any unpleasant surprises.
It may be best, therefore, to declare the contents in a separate
structure, or even a #define, so that it isn't possible to get them
out-of-step.

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.

Kind regards,

Alastair.

[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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: Gwynne <email@hidden>
References: 
 >Encapsulate C struct/toll-free bridging? (From: David Bainbridge <email@hidden>)

  • Prev by Date: Re: NSMutableArray morphs itself into an NSArray?
  • Next by Date: Re: NSMutableArray morphs itself into an NSArray?
  • Previous by thread: Encapsulate C struct/toll-free bridging?
  • Next by thread: Re: Encapsulate C struct/toll-free bridging?
  • Index(es):
    • Date
    • Thread