casting question

Here is an audio callback routine for use with portaudio.
The part I don't understand is how or why you can use the name of the class AudioReaderPortAudio as a type in a cast, like the line
int sz = static_cast<AudioReaderPortAudio*>(userdata)->sizes.advance_size;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*! Callback for reading audio data
 */
int AudioReaderPortAudio::callback(const void *input, void *output, unsigned long frameCount,
                                   const PaStreamCallbackTimeInfo* timeInfo,
                                   PaStreamCallbackFlags statusFlags, void *userdata)
{
    Q_UNUSED(output);
    Q_UNUSED(frameCount);
    Q_UNUSED(timeInfo);
    Q_UNUSED(statusFlags);
    int           sz       = static_cast<AudioReaderPortAudio*>(userdata)->sizes.advance_size;
    int           bp       = static_cast<AudioReaderPortAudio*>(userdata)->bptr;
    unsigned char *ptr_in  = (unsigned char *) input;
    unsigned char *ptr_out = static_cast<AudioReaderPortAudio*>(userdata)->buff;

    // copy data into circular buffer
    for (int i = 0; i < sz; i++) {
        ptr_out[bp * sz + i] = *ptr_in++;
    }
    static_cast<AudioReaderPortAudio*>(userdata)->emitAudioReady();
    return(paContinue);
}
Parameter userdata is defined as void *. So you can say nothing about what object it refers to. After the casting the compiler knows that the object has data member sizes.advance_size of type int.
Topic archived. No new replies allowed.