• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: function pointer in Cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: function pointer in Cocoa


  • Subject: Re: function pointer in Cocoa
  • From: Wade Tregaskis <email@hidden>
  • Date: Fri, 4 Jun 2004 01:51:52 +1000

Is there anyone having experience using function pointer in Cocoa? I am currently developing an application for my experiment using OSX's CoreAudio. In particular, there is a function AudioDeviceStart, which calls a C-type function pointer (*AudioDeviceIOProc). To utilize this function, I create a class called "SoundCard" (I want fully control of the soundcard, Revolution 7.1). The question I have is, how to handle this function pointer? Can I declare it as the instance variable of "SoundCard" class? If so, how and where to define the function? Since the function needs to be implemented in C? I could make the program working if I put the definition of the C-function in the SoundCard.m file. However, it is really ugly. Any idea?

I'm no expert, but usually when you want a function callback to end up going to an object, there's not much you can do but write a bridge function for it.

Depending on what user-data you're allowed to setup for your callback, you might be able to do something simple like pass a pointer to your object as your user-data, so your bridge function need only call the appropriate method on the instance it is given. It requires a bit of care with regards to memory management while the callback is installed, but in most cases any clean design will see those issues through quite happily.

If you don't have that user-data luxury, then you might have some trouble if you need more than one callback installed at a time. With just one you can store the destination object in a global variable (yes, gross, but it's functional) and be done with it... otherwise, you'll need to figure out some alternate way to distinguish between destination objects based on whatever information your callback has, and store your destinations in a map (C++) or dictionary (ObjC), or whatever else is appropriate.

Wade Tregaskis (aim: wadetregaskis)
-- Sed quis custodiet ipsos custodes?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >function pointer in Cocoa (From: Dingcai Cao <email@hidden>)

  • Prev by Date: Re: Ugly bug in Foundation, beware!
  • Next by Date: Re: NSObjectController HELP
  • Previous by thread: function pointer in Cocoa
  • Next by thread: Re: function pointer in Cocoa
  • Index(es):
    • Date
    • Thread