Recent

Author Topic: C style structs  (Read 3885 times)

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
C style structs
« on: July 06, 2015, 08:37:44 am »
How to translate C style structs in delphi mode. Thanks.

For WIN32 platform that is.
« Last Edit: July 06, 2015, 09:05:48 am by Fiji »

Basile B.

  • Guest
Re: C style structs
« Reply #1 on: July 06, 2015, 10:58:12 am »
Do you need to translate a header ?

If so then use h2pas.
And if the output is in object FPC then fix the incompatibilities.

Otherwise a struct is a just like a record...

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: C style structs
« Reply #2 on: July 06, 2015, 11:00:43 am »
Yes how to translate this to FPC?

Code: [Select]
struct mystruct
{
   uint16_t Reserved1   :3;
   uint16_t WordErr     :1;
   uint16_t SyncErr     :1;
   uint16_t WordCntErr  :1;
   uint16_t Reserved2   :10;
};

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: C style structs
« Reply #3 on: July 06, 2015, 11:06:35 am »
With Pascal style records.

There are some issues though. 

  • Pascal can't have fields after an union/variable (case) record, but that can often be resolved by moving those fields into one branch of the union (into the case of block of the record).
  • That does expose another problem, since Pascal lacks anonymous unions, in nested union cases those fields might acquire a prefix.
  • Finally while FPC supports bitpacked records now, I don't know if that works in Delphi mode. TBT
  • Sometimes there is preprocessor abuse in structs, where alternate names of fields are defined with #define. This of course won't work

There used to be a nice article on Rudy Velthuis site, but it seems to be down.

Basile B.

  • Guest
Re: C style structs
« Reply #4 on: July 06, 2015, 11:08:43 am »
Yes how to translate this to FPC?

Code: [Select]
struct mystruct
{
   uint16_t Reserved1   :3;
   uint16_t WordErr     :1;
   uint16_t SyncErr     :1;
   uint16_t WordCntErr  :1;
   uint16_t Reserved2   :10;
};

Code: [Select]
MyRecord = record
  Reserved1: word;
  WordErr: word;
  SyncErr: word;
  WordCntErr: word;
  Reserved2: word;
end;

the value after the : is the default value;
you can ensure the initialization like this:

Code: [Select]
function newStuff(): MyRecord;
type
  PMyRecord = ^MyRecord;
begin
  result := new(PMyRecord)^;
  result.Reserved1 := 3;
  // etc...
end; 
« Last Edit: July 06, 2015, 11:15:39 am by BBasile »

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: C style structs
« Reply #5 on: July 06, 2015, 11:12:09 am »
I wonder if bitpacked mode works with delphi mode :)
« Last Edit: July 06, 2015, 11:24:56 am by Fiji »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: C style structs
« Reply #6 on: July 06, 2015, 12:02:11 pm »
the value after the : is the default value;
No, that's the number of BITS the field requires.
The whole:
Code: [Select]
struct mystruct
{
   uint16_t Reserved1   :3;
   uint16_t WordErr     :1;
   uint16_t SyncErr     :1;
   uint16_t WordCntErr  :1;
   uint16_t Reserved2   :10;
};
eats 16 bits instead of 6 * sizeof(uint16_t). That's why FPC equivalent would be using bitpacked records.
Code: [Select]
type
  T1Bit = 0 .. (2 shl 0) - 1;
  T3Bit = 0 .. (2 shl 2) - 1;
  T10Bit = 0 .. (2 shl 9) - 1;

  mystruct = bitpacked record
    Reserved1: T3Bit;
    WordErr,SyncErr,WordCntErr: T1Bit;
    Reserved2: T10Bit;
  end;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: C style structs
« Reply #7 on: July 06, 2015, 12:21:01 pm »
bitpacked seems to work fine in delphi mode it seems

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: C style structs
« Reply #8 on: July 06, 2015, 12:26:04 pm »
Fantastic!  :)

 

TinyPortal © 2005-2018