Einzelnen Beitrag anzeigen

NicoDE
(Gast)

n/a Beiträge
 
#6

Re: Delphi-Equivalent zur C-union

  Alt 21. Jun 2005, 18:59
Das lässt sich nur mit vielen Tricks übersetzen.
(wer auch immer unbenannte unions benutzt, gehört...)

Delphi-Quellcode:
(** Unnamed unions are evil stuff.

typedef struct CvMat
{
  int  type;
  int  step;
  int* refcount;
  union
  {
    uchar*  ptr;
    short*  s;
    int*    i;
    float*  fl;
    double* db;
  } data;
  union
  {
    int rows;
    int height;
  };
  union
  {
    int cols;
    int width;
  };
} CvMat;

**)


type
  CvMat = record
    type_ : Integer;
    step : Integer;
    (* for internal use only *)
    refcount : PInteger;
    data : record
      case Integer of
        0: (ptr : PByte);
        1: (s : PSmallInt);
        2: (i : PInteger);
        3: (fl : PSingle);
        4: (db : PDouble)
    end;
    case Integer of
      0: (rows : Integer);
      1: (height: Integer;
    case Integer of
      0: (cols : Integer);
      1: (width : Integer))
  end;

// unit testing

procedure TForm1.FormCreate(Sender: TObject);
  function FielsOffset(const Struct; const Field): string;
  begin
    Result := IntToHex(Cardinal(Addr(Field)) - Cardinal(Addr(Struct)), 8);
  end;
  var
    Foo: CvMat;
    Txt: string;
begin
  Txt :=
    'type_ ' + FielsOffset(Foo, Foo.type_ ) + #13#10 + // 00000000
    'step ' + FielsOffset(Foo, Foo.step ) + #13#10 + // 00000004
    'refcount ' + FielsOffset(Foo, Foo.refcount) + #13#10 + // 00000008
    'data ' + FielsOffset(Foo, Foo.data ) + #13#10 + // 0000000C
    ' ptr ' + FielsOffset(Foo, Foo.data.ptr) + #13#10 + // 0000000C
    ' s ' + FielsOffset(Foo, Foo.data.s ) + #13#10 + // 0000000C
    ' i ' + FielsOffset(Foo, Foo.data.i ) + #13#10 + // 0000000C
    ' fl ' + FielsOffset(Foo, Foo.data.fl ) + #13#10 + // 0000000C
    ' db ' + FielsOffset(Foo, Foo.data.db ) + #13#10 + // 0000000C
    'rows ' + FielsOffset(Foo, Foo.rows ) + #13#10 + // 00000010
    'height ' + FielsOffset(Foo, Foo.height ) + #13#10 + // 00000010
    'cols ' + FielsOffset(Foo, Foo.cols ) + #13#10 + // 00000014
    'width ' + FielsOffset(Foo, Foo.width ) + #13#10 + // 00000014
    '(SizeOf) ' + IntToHex(SizeOf(CvMat), 8); // 00000018
  ShowMessage(Txt);
end;
ps: keine Ursache, hab's gerade nochmal vereinfacht (der Rest war Routine )
  Mit Zitat antworten Zitat