Cocoa Method to C Pointer

KiroNeem
Unregistered
 
Post: #1
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?
Quote this message in a reply
nabobnick
Unregistered
 
Post: #2
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).
Quote this message in a reply
KiroNeem
Unregistered
 
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
You actually can get a function pointer for an Objective-C method. Take a look at NSObject's +instanceMethodForSelector:.

- Alex Diener
Quote this message in a reply
KiroNeem
Unregistered
 
Post: #5
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.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #6
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).

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
nabobnick
Unregistered
 
Post: #7
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #8
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
Quote this message in a reply
nabobnick
Unregistered
 
Post: #9
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.
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #10
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!)
Quote this message in a reply
KiroNeem
Unregistered
 
Post: #11
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 :.(
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #12
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Pascal language (pointer) problem sealfin 3 4,392 Nov 27, 2012 04:38 AM
Last Post: sealfin
  another c pointer question NelsonMandella 3 3,882 Mar 26, 2010 03:49 AM
Last Post: DoG
  Diferences between array and pointer initialized strings in C ferum 16 6,751 Jan 4, 2006 11:38 PM
Last Post: kelvin
  Member function pointer LongJumper 6 4,887 Oct 31, 2005 05:53 PM
Last Post: LongJumper