Pascal Object: how to do a typed forward declaration?

邮差的信 提交于 2019-12-06 11:21:31

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!