Re: Access Obj-C method/attribute from a C method
Re: Access Obj-C method/attribute from a C method
- Subject: Re: Access Obj-C method/attribute from a C method
- From: Eric MORAND <email@hidden>
- Date: Sat, 7 Jul 2007 11:03:07 +0200
Thanks to the valuable help of the three of you, I'm now using the
"refCon" parameter to send a dictionary to my callback functions.
This dictionary contains self, and any other parameters I may need in
the callback function.
It works like a charm !
Again, thanks for the help.
Eric.
Le 7 juil. 07 à 10:23, E. Wing a écrit :
Date: Sat, 7 Jul 2007 09:39:50 +0200
From: Eric MORAND <email@hidden>
Unfortunately, I can't. ObjectAdded is a callback function (of type
IOServiceMatchingCallback) with a fixed number of parameters.
I don't know IOKit, but I suspect the "void *refCon" exists for this
very purpose. It is a very common design idiom for C function pointers
to provide a void* parameter for user data so cases like this one can
be handled.
A quick look at the documentation implies to me that that the function
IOServiceAddMatchingNotification is the one and only function you go
through to setup your callback. You are expected to pass a "void*
refCon" as a parameter. This is where you should shove your object's
instance pointer.
Then in the callback, you'll get back this pointer as the "void*
refCon". Just cast the pointer back to FooBar* and use as the other
messages described.
-Eric
> IOServiceMatchingCallback
>
>
>
> Callback function to be notified of IOService publication.
>
> typedef void (*IOServiceMatchingCallback)(
> void *refcon,
> io_iterator_t iterator );
> Parameters
> refcon
> The refcon passed when the notification was installed.
> iterator
> The notification iterator which now has new objects.
Le 7 juil. 07 à 09:25, Scott Stevenson a écrit :
>
> On Jul 7, 2007, at 12:13 AM, Eric MORAND wrote:
>
>> void ObjectAdded(void * refCon, io_iterator_t iterator)
>> {
>> // A lot of things are done...
>>
>> [self addObjectToFooArray:anObject];
>> }
>>
>> Unfortunately, it doesn't compile :
>>
>> error: 'self' undeclared (first use in this function)
>>
>> Is there a way to achieve what I need ?
>
> Just pass in the object that you want to work with as an argument:
>
> void ObjectAdded(void * refCon, io_iterator_t iterator, FooBar*
> myObject)
> {
> // A lot of things are done...
>
> [myObject addObjectToFooArray:anObject];
> }
>
>
>
> - Scott
_______________________________________________
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
_______________________________________________
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