Cocoa Method to C Pointer
I have been reading up on how Objective-C is actually implemented dynamicly and not staticly like C++ or C. So I decided to give it a real try and I have seem to be caught on a problem. I would like to set up a class that will call glut functions quickly, although when setting up timer callback functions you need to pass a function pointer, and I'm not sure how to reference an object's method for this. Or is there a way?
Pretty sure you can't. You'll have to use C functions but these funtions can still call Objective-C methods on classes as long as they have a pointer to the instances involved (most probably through global variables).
Thats kinda of a bummer, function pointers are widely used in many APIs and my own games. I think this may be my deciding point to move over to C++. >.< Although is there a way to do this in C++? I have been trying to look these answers up although I have been un successful.
You actually can get a function pointer for an Objective-C method. Take a look at NSObject's +instanceMethodForSelector:.
- Alex Diener
- Alex Diener
Although that gives you an IMP, I do not believe you can convert them over to function pointers. Or more specifically I have been unable to.
C++ supports function pointers in the same way as C. However, if you want to have pointers to C++ methods, things get a bit trickier, because pointers to methods and function pointers are not directly interchangeable.
There are various things you can do to work around this, but the simplest solution is to supply a pointer to a normal function, and have that call the C++ method on an appropriate object (or the Objective C equivalent, as nabobnick said).
There are various things you can do to work around this, but the simplest solution is to supply a pointer to a normal function, and have that call the C++ method on an appropriate object (or the Objective C equivalent, as nabobnick said).
As NCarter mentioned you have the exact same problem in C++. C++ method pointers cannot be passed into functions expecting C function pointers as they are not compatible. The real problem is the interchange between object oriented languages and C libraries, you have to drop down to C to talk to the library correctly.
KiroNeem Wrote:Although that gives you an IMP, I do not believe you can convert them over to function pointers. Or more specifically I have been unable to.
An IMP is a function pointer:
developer.apple.com Wrote:typedef id (*IMP)(id, SEL, ...);
- Alex Diener
Yes but a specific type of function pointer that expects and id as the first parameter. Most C libraries requiring callbacks don't want parameters or specify specific parameters that don't include extras thrown in there like the hidden self reference that's in every Objective-C method call.
KiroNeem -
Any callback function that glut calls will be global in nature - so I assume that the class of the method you want to call will be a singleton in nature? Why not have a regular c function as the callback which then calls your object's method with any particulars. You could store your obj-c object in a global or use a -sharedInstance method.
(after typing this in I notice nabobnick has suggested the same thing - so Im 2nd'ing him!)
Any callback function that glut calls will be global in nature - so I assume that the class of the method you want to call will be a singleton in nature? Why not have a regular c function as the callback which then calls your object's method with any particulars. You could store your obj-c object in a global or use a -sharedInstance method.
(after typing this in I notice nabobnick has suggested the same thing - so Im 2nd'ing him!)
Yup thats what I'm doing now, thanks. Also if anyone is curious I ran a little test on how fast C, C++, and Objective-C function calls were.
Normally for 100,000,000 function calls, passing an int
C: 1749 milliseconds
C++: 3309 milliseconds
Obj-C: 5598 milliseconds
Although when you compile in XCode with "Accelerated Obj-C Dispatch" you get
C: 1749 milliseconds
C++: 3309 milliseconds
Obj-C: 3958 milliseconds
With Obj-Acceleration not passing anything
C: 1643
C++: 3122
Obj-C: 3738
Notes:
Powerbook G4
1.5 GHz G4
768 DDR SDRAM
167 MHz Bus Speed :.(
Normally for 100,000,000 function calls, passing an int
C: 1749 milliseconds
C++: 3309 milliseconds
Obj-C: 5598 milliseconds
Although when you compile in XCode with "Accelerated Obj-C Dispatch" you get
C: 1749 milliseconds
C++: 3309 milliseconds
Obj-C: 3958 milliseconds
With Obj-Acceleration not passing anything
C: 1643
C++: 3122
Obj-C: 3738
Notes:
Powerbook G4
1.5 GHz G4
768 DDR SDRAM
167 MHz Bus Speed :.(
For the most part, gaining access to C++ (and I assume Objective-C) classes and members from a C routine, you need to build up a C interface to those classes. Which really isn't too hard to do but leads to some work in getting the interface written. At work we use this to hook in C++'s STL into an older C based program.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Pascal language (pointer) problem | sealfin | 3 | 2,688 |
Nov 27, 2012 04:38 AM Last Post: sealfin |
|
| another c pointer question | NelsonMandella | 3 | 3,266 |
Mar 26, 2010 03:49 AM Last Post: DoG |
|
| Member function pointer | LongJumper | 6 | 4,162 |
Oct 31, 2005 05:53 PM Last Post: LongJumper |
|

