Olivier Pons
Olivier Pons

Reputation: 15816

Pascal Object: how to do a typed forward declaration?

I'm translating the great fmod C header to Pascal, and I'm stuck because of a forward declaration. If I declare the function before the type, the error is "FMOD_CODEC_STATE: unknown", and if I declare the FMOD_CODEC_STATE before the function, the error is "FMOD_CODEC_METADATACALLBACK: unknown" Any idea how I could solve this problem? Thank you very much !

type
  FMOD_CODEC_STATE = Record
    numsubsounds: Integer;
    waveformat: array[0..0] of FMOD_CODEC_WAVEFORMAT;
    plugindata: Pointer;

    filehandle: Pointer;
    filesize: Cardinal;
    fileread: FMOD_FILE_READCALLBACK;
    fileseek: FMOD_FILE_SEEKCALLBACK;
    metadata: FMOD_CODEC_METADATACALLBACK;
  end;
  FMOD_CODEC_METADATACALLBACK    = function (codec_state: FMOD_CODEC_STATE; tagtype: FMOD_TAGTYPE; name: PChar; data: Pointer; datalen: Cardinal; datatype: FMOD_TAGDATATYPE; unique: Integer):FMOD_RESULT;

Upvotes: 3

Views: 3162

Answers (2)

Rob Kennedy
Rob Kennedy

Reputation: 163357

The record doesn't need to be passed by value. In fact, the original C code doesn't pass it by value anyway. It's passed by reference, with a pointer. Declare the pointer, then the function, and then the record:

type
  PFMOD_CODEC_STATE = ^FMOD_CODEC_STATE;
  FMOD_CODEC_METADATACALLBACK = function (codec_state: PFMOD_CODEC_STATE; tagtype: FMOD_TAGTYPE; name: PChar; data: Pointer; datalen: Cardinal; datatype: FMOD_TAGDATATYPE; unique: Integer):FMOD_RESULT;
  FMOD_CODEC_STATE = Record
    numsubsounds: Integer;
    waveformat: PFMOD_CODEC_WAVEFORMAT;
    plugindata: Pointer;

    filehandle: Pointer;
    filesize: Cardinal;
    fileread: FMOD_FILE_READCALLBACK;
    fileseek: FMOD_FILE_SEEKCALLBACK;
    metadata: FMOD_CODEC_METADATACALLBACK;
  end;

Yes, you're allowed to declare a pointer to something before you've declared the thing it points to. You're not allowed to forward-declare records, though, so the order given above is the only possible order for those three declarations.

Upvotes: 8

Blindy
Blindy

Reputation: 67487

Pascal has automatic forward type declaration for pointer classes, which is what I'm assuming that function actually takes. So simply changing your declarations to something like this (warning, I haven't used pascal in over 12 years) should work:

type
  PFMOD_CODEC_STATE=^FMOD_CODEC_STATE;
  FMOD_CODEC_METADATACALLBACK    = function (codec_state: PFMOD_CODEC_STATE; tagtype: FMOD_TAGTYPE; name: PChar; data: Pointer; datalen: Cardinal; datatype: FMOD_TAGDATATYPE; unique: Integer):FMOD_RESULT;

  FMOD_CODEC_STATE = Record
    numsubsounds: Integer;
    waveformat: array[0..0] of FMOD_CODEC_WAVEFORMAT;
    plugindata: Pointer;

    filehandle: Pointer;
    filesize: Cardinal;
    fileread: FMOD_FILE_READCALLBACK;
    fileseek: FMOD_FILE_SEEKCALLBACK;
    metadata: FMOD_CODEC_METADATACALLBACK;
  end;

Upvotes: 4

Related Questions