Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 10, 2008, 02:02 PM   #1
TurboLag
macrumors member
 
Join Date: Feb 2004
Calling an object's method from a C-style function

I am trying to access some instance variables from a C function in the same source file. Declaring the instance variables as public does not make them visible, so the only alternative is to declare them outside the @interface, thus making them global. I am sure this is not the proper approach.

Also, how can I call a method from within the C function if the object itself is not visible?

Thanks.
TurboLag is offline   0 Reply With Quote
Old Apr 10, 2008, 03:37 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
C functions work the same in Objective-C as they do in C++. They can't access instance variables or methods. You need to pass in a reference of your class to the function and then you can do whatever you want. For example:

Code:
void myNiftyFunc(SomeObject *obj) {
    id ivar = [obj ivar];
}


@implementation SomeObject

- (void)blah {
    myNiftyFunc(self);
}

- (id)ivar {
   return someIvar;
}

@end
kainjow is offline   0 Reply With Quote
Old Apr 10, 2008, 04:00 PM   #3
TurboLag
Thread Starter
macrumors member
 
Join Date: Feb 2004
The problem is that the C function is a callback routine, and I cant change how it is called. So I am stuck with the parameters it is defined with:

Code:
void callback  (void * target, unsigned long result, void * refcon, void * sender);
The setup for the callback is:

Code:
HIDresult = HIDSetQueueCallback (pFoundDevice, (IOHIDCallbackFunction)callback);
I could try wrapping the function as a ObjC method, such as:
Code:
- (void)callback:  (void *) target Result:(unsigned long) result  Refcon:(void *) refcon Sender:(void *) sender;
but I can't figure the proper syntax for the HIDSetQueueCallback function call. Any advice?
TurboLag is offline   0 Reply With Quote
Old Apr 10, 2008, 04:13 PM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Usually with a callback function you can setup it up to pass in a pointer, and for Objective-C you would pass in self, but for what you're doing I don't think it's possible since HIDSetQueueCallback() only takes two arguments.

Edit: looks like you're using some code from Apple's examples. They end up just calling setEventCallout() which sets up your callback. Looks like it could be changed to work around the issue but it'd probably be easier to use a global variable (ugly though).

Last edited by kainjow; Apr 10, 2008 at 04:20 PM.
kainjow is offline   0 Reply With Quote
Old Apr 10, 2008, 04:25 PM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
I feel somewhat dirty suggesting this, but it may work:

Have a global pointer defined in a file that points to the type of the object that has the member you need access to. In this file, define 2 functions:

Code:
objecttype *globalobj;

registerObject(objecttype *myobj) {
  globalobj = myobj;
}

id getIvar() {
  return [globalobj ivar];
}
Before any of the "worker" code, call registerObject passing the instance of your object where you have it available, or registerObject(self) from a method in your object instance.

Then in your callback function, you can call getIvar. This isn't really any better than having a global variable with the value in it, but it at least keeps you from having to keep that value "synchronized" with the objects member.

-Lee
lee1210 is offline   0 Reply With Quote
Old Apr 10, 2008, 05:32 PM   #6
Krevnik
macrumors 68020
 
Krevnik's Avatar
 
Join Date: Sep 2003
Since everyone else is throwing in information, I'll throw in my two cents: refcons are your friend in this case.

When you setup the callback, you want to also search around for how to set the refcon on the callback. The poster a couple posts above me has the same idea. You set the refcon to the Obj-C object you need to call into from the callback, and then from the C method, you can typecast refcon into the Obj-C object and call into it.

EDIT: I randomly stumbled on this post on the Apple dev lists which includes an updated HIDSetQueueCallback which will let you pass in a refcon pointer. Use it wisely.

http://lists.apple.com/archives/mac-.../msg00018.html
__________________
iMac 2013 27", 13" 2012 rMBP, iPad Air, iPhone 6
Krevnik is offline   0 Reply With Quote
Old Apr 11, 2008, 11:25 AM   #7
TurboLag
Thread Starter
macrumors member
 
Join Date: Feb 2004
Thanks Krevnik.

Works like a charm. I pass the setup function a pointer to my object, then typecast it in the callback. Much cleaner approach.

Now to remove those global variables . . .
TurboLag is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Recursively calling init method… am I doing this right? ArtOfWarfare Mac Programming 19 Feb 2, 2014 05:15 PM
Apple should make a method that returns an array's object that satisfies a predicate. moonman239 iPhone/iPad Programming 5 Nov 21, 2013 07:21 PM
Simple question about making / releasing an object within a method Ja Di ksw Mac Programming 5 Oct 16, 2013 04:28 AM
Class Method -> Function -> Instance Method? Blakeasd Mac Programming 11 Jun 19, 2013 02:53 AM
Resolved: Declaring object inside a function xArtx iPhone/iPad Programming 22 Jun 6, 2013 07:18 AM

Forum Jump

All times are GMT -5. The time now is 03:51 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC