具有在Delphi 6中转换的位域的C ++结构

时间:2019-03-07 14:15:39

标签: delphi-6

感谢Rudy's Delphi Corner article on C++ -> Delphi conversion我已经设法转换了或多或少的复杂结构。

我想问这个社区我做的是否正确。 此外,我还有一些疑问需要解决。

这就是我所做的:

通过C ++代码:

typedef struct CorrId_t_ {
    unsigned int  size:8;       
    unsigned int  valueType:4; 
    unsigned int  classId:16;  
    unsigned int  reserved:4;  
    unsigned long long  intValue;
    } CorrId_t;

我确实在Delphi中翻译了以下代码(我写了一些函数来访问位域):

interface

type
   CorrId_t_ = record
      bitfield:Cardinal; //$000000FF=size; $00000F00=valueType; $0FFFF000=classId; $F0000000=reserved;
      intValue:Cardinal; //should be Uint64 but such integer type doesn't exist in Delphi 6
   end;

   CorrId_t = CorrId_t_;
   PCorrId_t = ^CorrId_t;

   procedure CorrID_setSize(var rec:CorrId_t; value:byte);
   procedure CorrID_setValueType(var rec:CorrId_t; value:byte);
   procedure CorrID_setClassId(var rec:CorrId_t; value:Word);

   function CorrID_getSize(rec:CorrId_t):Byte;
   function CorrID_getValueType(rec:CorrId_t):Byte;
   function CorrID_getClassId(rec:CorrId_t):Word;


implementation

procedure CorrID_setSize(var rec:CorrId_t; Value:Byte);
begin
    rec.bitfield := (rec.bitfield and $FFFFFF00) or Value;
end;

procedure CorrID_setValueType(var rec:CorrId_t; Value:Byte);
begin
    rec.bitfield := (rec.bitfield and $FFFFF0FF) or Value;
end;

procedure CorrID_setClassId(var rec:CorrId_t; Value:Word);
begin
    rec.bitfield := (rec.bitfield and $F0000FFF) or Value;
end;


function CorrID_getSize(rec:CorrId_t):Byte;
begin
  Result := rec.bitfield and $000000FF;
end;

function CorrID_getValueType(rec:CorrId_t):Byte;
begin
  Result := rec.bitfield and $00000F00;
end;

function CorrID_getClassId(rec:CorrId_t):Word;
begin
  Result := rec.bitfield and $0FFFF000;
end;

这是我要问的问题:

1)(在设置/获取访问函数内部)按位运算正确吗?

2)在Delphi 6中,“ unsigned long long”整数类型没有任何对应关系。我可以在 Int64 (但它是 signed )或32-位无符号整数类型,例如Cardinal。您认为哪种方式最好使用?

提前谢谢!

0 个答案:

没有答案