Re: Calling an object from a C function
Re: Calling an object from a C function
- Subject: Re: Calling an object from a C function
- From: Antonio Nunes <email@hidden>
- Date: Wed, 20 Aug 2008 10:21:05 +0100
On 20 Aug 2008, at 01:06, Charlie Dickman wrote:
Now, how do I define things like 'self' and 'super' to a C program?
Put like this the question doesn't make sense. But maybe this is
useful: If you have a C function that _conceptually_ is part of an
object and that needs to access 'self' like the object's methods, then
you should pass in a pointer to self to those functions (which is
essentially what happens in method calls, albeit invisibly).
As a real world example, this is how you would handle the situation if
you define a C callback function:
Say you want to scan the contents of a PDFPage. Quartz provides the
CGPDFScanner that will parse the contents of a PDF page and allows you
to define callback functions that it calls as it encounters the
various building blocks of a PDF page:
You set up a table that will contain the callbacks for the scanner to
use:
CGPDFOperatorTableRef table = CGPDFOperatorTableCreate();
You add the callbacks to the table:
// End text object:
CGPDFOperatorTableSetCallback(table, "ET", operator_ET);
You create the scanner, passing in self as the pointer to data you
want passed to your callback functions:
CGPDFScannerRef scanner = CGPDFScannerCreate(contentStream, table,
self);
You define the callback function, casting the data pointer to the type
of your object:
void operator_ET(CGPDFScannerRef scanner, void *info)
{
MyObject *self = info;
[[self textObjects] addObject:[self currentTextObject]];
[self setCurrentTextObject:nil];
}
You could call the variable "self" anything else, but "self" is of
course a very convenient name to use.
I don't know how this would scale to 'super'. I don't think you can
pass in a pointer to super, as that is not how the mechanism works.
While "self" is a variable name "super" is a flag to the compiler
telling it where to begin searching for the method to perform. That
wouldn't work outside of the object's context like in a C function.
You would have to find a way access the superclass's methods within
the callback function. I suppose it would involve calling
objc_msgSendSuper or objc_msgSendSuper_stret directly, but that is
unknown territory to me. I'd be interested myself in how that is done
if anyone is willing to supply the answer.
António
----------------------------------------------------
Energy is like a muscle,
it grows stronger through being used.
----------------------------------------------------
_______________________________________________
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