Re: How do I get NSRect to be the same as CGRect?
Re: How do I get NSRect to be the same as CGRect?
- Subject: Re: How do I get NSRect to be the same as CGRect?
- From: "Clark Cox" <email@hidden>
- Date: Sun, 16 Dec 2007 18:44:24 -0800
On Dec 16, 2007 4:56 PM, Rick Mann <email@hidden> wrote:
>
> On Dec 16, 2007, at 4:29 PM, Clark Cox wrote:
>
> > Before Leopard, NSRect and CGRect were completely different types.
> > Apple took the opportunity provided by the transition to 64-bit to
> > eliminate the discrepancy. But this change could break binary
> > compatibility, so it couldn't be done across the board for 32-bit
> > code.
>
> Really? NSGeometry.h defines NSRect like this (in the non-64-bit case):
>
> > typedef struct _NSPoint {
> > CGFloat x;
> > CGFloat y;
> > } NSPoint;
> >
> > typedef struct _NSSize {
> > CGFloat width; /* should never be negative */
> > CGFloat height; /* should never be negative */
> > } NSSize;
> >
> >
> > typedef struct _NSRect {
> > NSPoint origin;
> > NSSize size;
> > } NSRect;
> >
>
>
> And CGGeometry.h defines CGRect like this:
>
> > struct CGPoint {
> > CGFloat x;
> > CGFloat y;
> > };
> > typedef struct CGPoint CGPoint;
> >
> > /* Sizes. */
> >
> > struct CGSize {
> > CGFloat width;
> > CGFloat height;
> > };
> > typedef struct CGSize CGSize;
> >
> >
> > struct CGRect {
> > CGPoint origin;
> > CGSize size;
> > };
> > typedef struct CGRect CGRect;
> >
>
>
> Which seem to have identical layouts to me; that is, there should be
> no binary incompatibility. Furthermore, how could 32-bit portions of
> the API know if client code had defined it one way or another?
You're forgetting C++ name mangling. To a C++ compiler, the difference
is quite significant:
void Foo(NSRect r);
This function will have different names depending on whether or not
NSRect is its own type or if it's just a typedef of CGRect.
> In my own case, I did an ugly cast from NSRect to CGRect so that my
> existing C++ code could go ahead and draw. It seems to work fine.
>
> However, what I'd really like is for here to be a definition of NSRect
> that could be parsed by the C++ compiler (not having any Obj-C in it)
> so that my pre-existing Rect class could be made to easily be
> initialized from an NSRect.
>
> > Yes, just define NS_BUILD_32_LIKE_64. This was the first link on
> > Google when searching for "NS_BUILD_32_LIKE_64":
> >
> > <http://developer.apple.com/documentation/Cocoa/Conceptual/Cocoa64BitGuide/64BitChangesCocoa/chapter_3_section_3.html
> > >
>
> I'm not really going for a 64-bit app here, and I understand there's
> some penalty for making an app 64-bit if you don't need it.
That's why that macro exists, for exactly your case: you want the
types to be defined as they are on the 64-bit side, but you don't want
to actually make it 64-bit.
--
Clark S. Cox III
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden