Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2014-07

How to convert set to integer and vice versa

11 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Gennady Agranov

Hi,

I am new to FPC, but I do like it - thanks!

We are trying to switch from Delphi to FPC, but I am stuck with the following issue - in Delphi you can cast "set" to "integer" and get it back later - again by casting. In our case it is used for persistence purposes.

We have a lot of different set types - i really do not want to write set<->integer converter for every set type we have :(

Actually I did find a way - I use ^ and @ and cast pointers, but is there a way to write general set<->integer converter in FPC that I do not have to worry whether it will work or not?

And what actually -CPPACKSET=0 does - documentation is not clear - at least for me :)

Thanks,
Gennady

Reply by email

Marco van de Voort
to Gennady Agranov

In our previous episode, Gennady Agranov said:

Show quoted text 6 lines
I am new to FPC, but I do like it - thanks! We are trying to switch from Delphi to FPC, but I am stuck with the following issue - in Delphi you can cast "set" to "integer" and get it back later - again by casting. In our case it is used for persistence purposes.

Good. And afaik in Delphi mode this works too in FPC? Did you test?

And what actually -CPPACKSET=0 does - documentation is not clear - at least for me :)

The $PACKSET directive takes a numeric argument of 1, 2, 4 or 8.

Not mentioned in the documentation means undefined.

Reply by email

Unknown author
to Gennady Agranov: I am new to FPC, but I do like it - thanks! We are trying to switch from Delphi to FPC,…

You will find that if you are converting from a Delphi version equal to or before Delhi 2009, the {$Mode Delphi} or -Mdelphi directive does exactly what you want.

Gennady Agranov wrote 24 lines
Hi, I am new to FPC, but I do like it - thanks! We are trying to switch from Delphi to FPC, but I am stuck with the following issue - in Delphi you can cast "set" to "integer" and get it back later - again by casting. In our case it is used for persistence purposes. We have a lot of different set types - i really do not want to write set<->integer converter for every set type we have :( Actually I did find a way - I use ^ and @ and cast pointers, but is there a way to write general set<->integer converter in FPC that I do not have to worry whether it will work or not? And what actually -CPPACKSET=0 does - documentation is not clear - at least for me :) Thanks, Gennady _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Gennady Agranov
to Marco van de Voort: In our previous episode, Gennady Agranov said: Good. And afaik in Delphi mode this works…
Marco van de Voort wrote 10 lines
In our previous episode, Gennady Agranov said: > I am new to FPC, but I do like it - thanks! > > We are trying to switch from Delphi to FPC, but I am stuck with the > following issue - in Delphi you can cast "set" to "integer" and get it > back later - again by casting. In our case it is used for persistence > purposes. Good. And afaik in Delphi mode this works too in FPC? Did you test?

casting integer to set does not compile in FPC - and yes - I use Delphi mode :(

Show quoted text 7 lines
> And what actually -CPPACKSET=0 does - documentation is not clear - at > least for me :) The $PACKSET directive takes a numeric argument of 1, 2, 4 or 8. if you run fpc without parameters you get following:

-CP<x>=<y> packing settings
-CPPACKSET=<y> <y> set allocation: 0, 1 or DEFAULT or NORMAL, 2, 4 and 8

I wonder what 0 means - thanks!

Not mentioned in the documentation means undefined.

_______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Marco van de Voort
to Gennady Agranov

In our previous episode, Gennady Agranov said:

Show quoted text 6 lines
> > Good. And afaik in Delphi mode this works too in FPC? Did you test? > casting integer to set does not compile in FPC - and yes - I use Delphi mode :(
program setcast;
{$ifdef FPC}
{$mode delphi}
{$else}
{$APPTYPE CONSOLE}
{$endif}
uses
  SysUtils;
type tenum=0..31;
     tmyset= set of tenum;
var x:integer;
    y:tmyset;
begin
  x:=integer(y);
end.

compiles in both FPC (both 2.6.x and trunk) and Delphi (XE4)

Reply by email

Unknown author
to Marco van de Voort

Yes, Marco, as you previously suspected, It does.
I saw you gave already a similar answer to mine.

The pointer casts are definitely not necessary, Gennady.

Can you show a little example WHERE it doesn't work? In plain fpc, not lazarus code?

Marco van de Voort wrote 31 lines
In our previous episode, Gennady Agranov said: >> Good. And afaik in Delphi mode this works too in FPC? Did you test? >> > casting integer to set does not compile in FPC - and yes - I use Delphi > mode :( program setcast; {$ifdef FPC} {$mode delphi} {$else} {$APPTYPE CONSOLE} {$endif} uses SysUtils; type tenum=0..31; tmyset= set of tenum; var x:integer; y:tmyset; begin x:=integer(y); end. compiles in both FPC (both 2.6.x and trunk) and Delphi (XE4) _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Gennady Agranov
to Marco van de Voort: In our previous episode, Gennady Agranov said: program setcast; {$ifdef FPC} {$mode…

Thanks!

Your example does compile, but if you replace 31 with 32 - it will not compile - no matter what you use - integer, int64 or qword

And I guess I know the reason now - sizeof(left) should be equal to sizeof(right) and 33 bits enum has sizeof of 5 :(

Any suggestions?

Thanks,
Gennady

Marco van de Voort wrote 35 lines
In our previous episode, Gennady Agranov said: > > > > Good. And afaik in Delphi mode this works too in FPC? Did you test? > > > > casting integer to set does not compile in FPC - and yes - I use Delphi > mode :( program setcast; {$ifdef FPC} {$mode delphi} {$else} {$APPTYPE CONSOLE} {$endif} uses SysUtils; type tenum=0..31; tmyset= set of tenum; var x:integer; y:tmyset; begin x:=integer(y); end. compiles in both FPC (both 2.6.x and trunk) and Delphi (XE4) _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Gennady Agranov
to Gennady Agranov

Thanks again!

I am all set now - I needed {$PACKSET 4} :)

Thanks,
Gennady

program setcast;
 {$ifdef FPC}
 {$mode delphi}
 {$PACKSET 4}
 {$else}
 {$APPTYPE CONSOLE}
 {$endif}
 uses
   SysUtils;
 type tenum=0..32;
      tmyset= set of tenum;
 var x:int64;
     y:tmyset;
 begin
   x:=int64(y);
 end.
Gennady Agranov wrote 55 lines
Thanks! Your example does compile, but if you replace 31 with 32 - it will not compile - no matter what you use - integer, int64 or qword And I guess I know the reason now - sizeof(left) should be equal to sizeof(right) and 33 bits enum has sizeof of 5 :( Any suggestions? Thanks, Gennady On Tue, Jul 22, 2014 at 12:13 PM, Marco van de Voort wrote: > In our previous episode, Gennady Agranov said: > > > > > > Good. And afaik in Delphi mode this works too in FPC? Did you test? > > > > > > > casting integer to set does not compile in FPC - and yes - I use Delphi > > mode :( > > program setcast; > > {$ifdef FPC} > {$mode delphi} > {$else} > {$APPTYPE CONSOLE} > {$endif} > > uses > SysUtils; > > type tenum=0..31; > tmyset= set of tenum; > > var x:integer; > y:tmyset; > > begin > x:=integer(y); > end. > > compiles in both FPC (both 2.6.x and trunk) and Delphi (XE4) > > _______________________________________________ > fpc-devel maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel >

Reply by email

Marco van de Voort
to Gennady Agranov: Thanks! Your example does compile, but if you replace 31 with 32 - it will not compile -…

In our previous episode, Gennady Agranov said:

Your example does compile, but if you replace 31 with 32 - it will not compile - no matter what you use - integer, int64 or qword

But does that work in Delphi, did you test?

And I guess I know the reason now - sizeof(left) should be equal to sizeof(right) and 33 bits enum has sizeof of 5 :(

Correct.

Any suggestions?

Define using $packenum/$packset, or simply using a range 0..63.

Reply by email

Gennady Agranov
to Marco van de Voort
But does that work in Delphi, did you test?

You are correct - Delphi also requires that source and target are of same size - thanks!

I finally figured why existing Delphi code was not compiling - I did not use {$mode delphi} in the unit - i did not want to add {$mode dlephi} to every file - so I used -Mdelphi in the fpc command line - it

program setcast;
type
   tenum = 0..63;
   tmyset= set of tenum;
var x:int64;
     y:tmyset;
begin
    writeln(sizeof(tmyset));
    x:=int64(y);
    y := tmyset(x);
end.

If you compile this program "fpc -Mdelphi setcast.pas" - it will not complile :(
But if you add {$mode delphi} - it will :)

And I know why - if you have {$mode delphi} - sizeof(tmyset) is 8, but if you do not (and use -Mdelphi) sizeof(tmyset) is 32 Do you know why -Mdelphi is different from {$mode delphi} w.r.t to sizeof(tmyset)?

Thanks,
Gennady

Reply by email

Sven Barth
to Gennady Agranov
Gennady Agranov wrote
And I know why - if you have {$mode delphi} - sizeof(tmyset) is 8, but if you do not (and use -Mdelphi) sizeof(tmyset) is 32 Do you know why -Mdelphi is different from {$mode delphi} w.r.t to sizeof(tmyset)?

This should not be the case. Please test again whether this truly is the case and if so please open a bug report with a compilable little test program.

Regards,
Sven

Reply by email