Re: C++ framework <-> Cocoa
Re: C++ framework <-> Cocoa
- Subject: Re: C++ framework <-> Cocoa
- From: "Dennis C. De Mars" <email@hidden>
- Date: Tue, 5 Jun 2001 19:24:38 -0000
Andreas Monitzer <email@hidden> said:
>
On Tuesday, June 5, 2001, at 04:52 , Dennis C. De Mars wrote:
>
>
> In theory, it should be possible to do this, although it would be a big
>
> pain. You'd have to write some Objective C code that passed the selectors
>
> back to the C++ code, then call the Objective C runtime method dispatcher
>
> to
>
> actually execute it. You'd have to link to the right libraries to get the
>
> Objective C runtime in there. And you'd have to understand how Objective
>
> C
>
> works to get this to work.
>
>
It's not that complicated:
>
>
[objCInstance setIntValue:4711];
>
>
translates to
>
>
#ifdef __cplusplus
>
extern "C" {
>
>
#include <objc/objc.h>
>
>
#endif
>
>
id objc_msgSend (id self, SEL _cmd, ...);
>
>
#ifdef __cplusplus
>
}
>
#endif
>
>
void main() {
>
id objCInstance=...;
>
SEL setIntValue = sel_getUid("setIntValue:");
>
>
objc_msgSend(objCInstance,setIntValue,4711);
>
}
>
>
At least that's how it worked in PB, I haven't tried to compile it since
>
then.
Well, first of all, I still think it is a big pain. If you mulitply that code by the
number of calls you might need to do to Cocoa objects if your UI is all in Cocoa, that
would qualify as a big pain (doable, but I think I'd wait for Obj-C++).
I'll have to admit that I thought it would be more complicated than that, though. I
didn't think you could get objc/objc.h to work in C++ just by wrapping it in extern "C"
(even if it is pure C, that often doesn't work without massaging some declaration in
the C header itself). Also, I thought that id at least was something that was
recongized by the Objective C compiler itself, so I thought you'd have to allocate your
objects in Objective C and pass the pointers back to C++. I also forgot about
sel_GetUid, so I thought you'd have to extract the selectors in Objective C also.
So, yes, if all of that really works (which I believe it does, since you actually did
it before) it's not as much work as I thought.
- Dennis D.