Pascal Object: how to do a typed forward declaration?

半城伤御伤魂 提交于 2019-12-22 18:27:55

问题


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;

回答1:


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.




回答2:


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;


来源:https://stackoverflow.com/questions/6018510/pascal-object-how-to-do-a-typed-forward-declaration

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