| 17 | | function pointer c++ static |
| | 17 | For example, your .h file might look like this: |
| | 18 | |
| | 19 | {{{ |
| | 20 | class MyPortaudioClass |
| | 21 | { |
| | 22 | [snip] |
| | 23 | |
| | 24 | int myMemberCallback(const void *input, void *output, |
| | 25 | unsigned long frameCount, |
| | 26 | const PaStreamCallbackTimeInfo* timeInfo, |
| | 27 | PaStreamCallbackFlags statusFlags); |
| | 28 | |
| | 29 | static int myPaCallback( |
| | 30 | const void *input, void *output, |
| | 31 | unsigned long frameCount, |
| | 32 | const PaStreamCallbackTimeInfo* timeInfo, |
| | 33 | PaStreamCallbackFlags statusFlags, |
| | 34 | void *userData ) |
| | 35 | { |
| | 36 | return ((MyPortaudioClass*)userData) |
| | 37 | ->myMemberCallback(input, output, frameCount, timeInfo, statusFlags); |
| | 38 | } |
| | 39 | |
| | 40 | [snip] |
| | 41 | }; |
| | 42 | }}} |
| | 43 | |
| | 44 | and your .cpp file might look like this: |
| | 45 | |
| | 46 | {{{ |
| | 47 | int MyPortaudioClass::myMemberCallback(const void *input, void *output, |
| | 48 | unsigned long frameCount, |
| | 49 | const PaStreamCallbackTimeInfo* timeInfo, |
| | 50 | PaStreamCallbackFlags statusFlags) |
| | 51 | { |
| | 52 | // Do what you need (having access to every part of MyPortaudioClass) |
| | 53 | |
| | 54 | return paContinue; |
| | 55 | } |
| | 56 | }}} |
| | 57 | |
| | 58 | Once that's setup, you can open your stream using "this" as your userData: |
| | 59 | |
| | 60 | {{{ |
| | 61 | PaError pa_error = Pa_OpenStream( |
| | 62 | [snip]... |
| | 63 | &MyPortaudioClass::myPaCallback, // streamCallback |
| | 64 | this); // userData |
| | 65 | }}} |
| | 66 | |
| | 67 | You could also just use a reference to your class in your static function like (make sure the static function is a member of the class so |
| | 68 | that you have access to "private" declared variables): |
| | 69 | |
| | 70 | {{{ |
| | 71 | MyPortaudioClass* pThis = (MyPortaudioClass*)userData; |
| | 72 | }}} |
| | 73 | |
| | 74 | but this adds some complexity to the code. |
| | 75 | |
| | 76 | This topic is covered on many C++ web sites, so try googling for the following terms: function pointer c++ static |
| | 77 | |
| | 78 | |
| | 79 | Thanks to Robert Bielik for the sample code. |