Re: [fpc-pascal] Coercing record fields together

2018-07-09 Thread Ralf Quint

On 7/9/2018 9:22 AM, Ryan Joseph wrote:

Could I do some RTL magic on a record to loop over its fields and set them to 
another record by name? In the example below I want to set all the fields in 
TRecA to matching named fields in TRecB. Just curious if we can do this 
automatically using RTL.

type
TRecA = record
a: string;
b: string;
end;

type
TRecB = record
a: string;
b: string;
c: integer;
d: integer;
end;


for field in recA.GetFields do
recB.GetFieldByName(field.name).value := field.value

If you make sure that any variable of tRecB is declared first, you can use

Var RecB : tRecB;
   RecA : tRedA ABSOLUTE RecB;

Quite obviously, it doesn't work the other way around as tRecB is larger 
than tRecA...


Ralf

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Loss of precision when using math.Max()

2018-07-09 Thread gtt


Zitat von Florian Klaempfl :


I am happy to implement this in delphi mode, but it requires some  
documentation references how delphi evaluates such expressions  
(which precision is used when and why) and how they are handled in  
expressions.


These links may be of interest:

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/About_Floating-Point_Arithmetic#Understand_the_Data_Flow

and

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Floating_point_precision_control_%28Delphi_for_x64%29



___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Coercing record fields together

2018-07-09 Thread Ryan Joseph


> On Jul 9, 2018, at 11:57 AM, gabor  wrote:
> 
> Hi.
> Probably can be done with RTTI (1), enumerators (2) and generics (3) together.
> 
> 1. https://stackoverflow.com/questions/27803383/fpc-rtti-on-records
> 2. http://wiki.freepascal.org/for-in_loop#Traversing_container
> 3. http://wiki.freepascal.org/Generics
> 
> Michał.

Thanks for the tips but ouch that looks complicated, maybe more trouble than 
it’s worth.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Loss of precision when using math.Max()

2018-07-09 Thread Florian Klaempfl

Am 09.07.2018 um 19:55 schrieb g...@wolfgang-ehrhardt.de:


Zitat von Martok :
To make sure this works, one has to manually make the const expression 
be of the

type required:
const
  e: Extended = Extended(1.0) / 3.0;

I seem to remember that this was once documented somewhere for Delphi. 
Can't
seem to find it though, so maybe it was a 3rd-party book? There is no 
hint of it

in the FPC documentation, anyway.


As already noted, it is not necessary in Delphi

D:\Work\TMP>D:\DMX\M18\DCC32 -b exttest.dpr
Embarcadero Delphi for Win32 compiler version 25.0
Copyright (c) 1983,2013 Embarcadero Technologies, Inc.
exttest.dpr(14)
15 lines, 0.00 seconds, 20816 bytes code, 13864 bytes data.
D:\Work\TMP>exttest
   0.333433  0.333433
     0.    0.
     0.    0.

And with Delphi Tokio 10.2.3 Starter

G:\TMP>bds -b exttest.dpr
G:\TMP>exttest.exe
   0.333433  0.333433
     0.    0.
     0.    0.


I am happy to implement this in delphi mode, but it requires some 
documentation references how delphi evaluates such expressions (which 
precision is used when and why) and how they are handled in expressions.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Coercing record fields together

2018-07-09 Thread gabor

Hi.
Probably can be done with RTTI (1), enumerators (2) and generics (3) 
together.


1. https://stackoverflow.com/questions/27803383/fpc-rtti-on-records
2. http://wiki.freepascal.org/for-in_loop#Traversing_container
3. http://wiki.freepascal.org/Generics

Michał.

W dniu 2018-07-09 o 18:22, Ryan Joseph pisze:

Could I do some RTL magic on a record to loop over its fields and set them to 
another record by name? In the example below I want to set all the fields in 
TRecA to matching named fields in TRecB. Just curious if we can do this 
automatically using RTL.

type
TRecA = record
a: string;
b: string;
end;

type
TRecB = record
a: string;
b: string;
c: integer;
d: integer;
end;


for field in recA.GetFields do
recB.GetFieldByName(field.name).value := field.value


Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Loss of precision when using math.Max()

2018-07-09 Thread gtt


Zitat von Martok :
To make sure this works, one has to manually make the const  
expression be of the

type required:
const
  e: Extended = Extended(1.0) / 3.0;

I seem to remember that this was once documented somewhere for Delphi. Can't
seem to find it though, so maybe it was a 3rd-party book? There is  
no hint of it

in the FPC documentation, anyway.


As already noted, it is not necessary in Delphi

D:\Work\TMP>D:\DMX\M18\DCC32 -b exttest.dpr
Embarcadero Delphi for Win32 compiler version 25.0
Copyright (c) 1983,2013 Embarcadero Technologies, Inc.
exttest.dpr(14)
15 lines, 0.00 seconds, 20816 bytes code, 13864 bytes data.
D:\Work\TMP>exttest
  0.333433  0.333433
0.0.
0.0.

And with Delphi Tokio 10.2.3 Starter

G:\TMP>bds -b exttest.dpr
G:\TMP>exttest.exe
  0.333433  0.333433
0.0.
0.0.

And I also wrote that the explicit type cast can only be used with
very new Delphi, e.g. compiler version 25 gives for

{$apptype console}
const
  x1: extended = extended(1.0)/3.0;
begin
  writeln(x1:30:16);
end.

D:\Work\TMP>b18 exttest2.dpr
D:\Work\TMP>D:\DMX\M18\DCC32 -b exttest2.dpr
Embarcadero Delphi for Win32 compiler version 25.0
Copyright (c) 1983,2013 Embarcadero Technologies, Inc.
exttest2.dpr(3) Error: E2089 Invalid typecast
exttest2.dpr(7)

Tokyo compiles without error, I do know now which was the
first version which allows the type cast.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Coercing record fields together

2018-07-09 Thread DaWorm
Just because someone is bound to suggest it, might as well be me.

type
TRecA = record
a: string;
b: string;
end;

type
TRecB = record
A: TRecA;
c: integer;
d: integer;
end;

RecB.A := RecA;

Jeff

On Mon, Jul 9, 2018, 12:23 PM Ryan Joseph 
wrote:

> Could I do some RTL magic on a record to loop over its fields and set them
> to another record by name? In the example below I want to set all the
> fields in TRecA to matching named fields in TRecB. Just curious if we can
> do this automatically using RTL.
>
> type
> TRecA = record
> a: string;
> b: string;
> end;
>
> type
> TRecB = record
> a: string;
> b: string;
> c: integer;
> d: integer;
> end;
>
>
> for field in recA.GetFields do
> recB.GetFieldByName(field.name).value := field.value
>
>
> Regards,
> Ryan Joseph
>
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] How to use Google Protocol Buffer

2018-07-09 Thread Dennis

To connect to a server, I need to use Google's Protocol Buffer format.

The only workable parser to convert the protocol definition file to 
pascal reader/writer source code is:


the ProtoBufCodeGen in
https://github.com/fundamentalslib/fundamentals5/releases

Still, there is no documentation on how to call the generated codes.

For example, it generated these:

procedure RequestRecordInit(var A: TRequestRecord);
procedure RequestRecordFinalise(var A: TRequestRecord);

function  pbEncodeDataRequestRecord(var Buf; const BufSize: Integer; 
const A: TRequestRecord): Integer;


function  pbDecodeValueRequestRecord(const Buf; const BufSize: Integer; 
var Value: TRequestRecord): Integer;


function  pbEncodeFieldRequestRecord(var Buf; const BufSize: Integer; 
const FieldNum: Integer; const A: TRequestRecord): Integer;
procedure pbDecodeFieldRequestRecord(const Field: 
TpbProtoBufDecodeField; var Value: TRequestRecord);



I could call pbEncodeDataRequestRecord to convert the TRequestRecord to 
a stream of byte and store in the Buf
but there is not a routine to convert it back from a stream of bytes to 
the TRequestRecord.


The only seemingly useful routine pbDecodeValueRequestRecord, when 
called with the Buf generated by pbEncodeDataRequestRecord will raise 
exception "Invalid buffer "


Any help?

Dennis

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] Coercing record fields together

2018-07-09 Thread Ryan Joseph
Could I do some RTL magic on a record to loop over its fields and set them to 
another record by name? In the example below I want to set all the fields in 
TRecA to matching named fields in TRecB. Just curious if we can do this 
automatically using RTL.

type
TRecA = record
a: string;
b: string;
end;

type
TRecB = record
a: string;
b: string;
c: integer;
d: integer;
end;


for field in recA.GetFields do
recB.GetFieldByName(field.name).value := field.value


Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Loss of precision when using math.Max()

2018-07-09 Thread Martok
Am 03.07.2018 um 23:10 schrieb Florian Klämpfl:
>> OK, then two questions remain: Why does is occur/apply only for (newer?) 
>> 3.1.1 versions?
> I dug a little bit deeper, the reason is:
> https://bugs.freepascal.org/view.php?id=25121I tried figuring this out, 
> sharing what I found here for reference and a workaround.

In the example of 1.0 / 3.0, both numbers are represented exactly in IEEE-754,
so there is no difference caused by this "range-rounding". Typedef passing has
not changed, and the "minimal" types found in the parser at
compiler/pexpr.pas:1711 have not changed for years either.

So the expression is s32real(1.0) / s32real(3.0) {as before}, which then gets
evaluated by taddnode(typ=slashn) at bestreal precision {as before}, and
returned at s32real precision {as before} and "range rounded" to single {this is
new}. It is then stored in a typed const of whatever type was specified {as 
before}.

To make sure this works, one has to manually make the const expression be of the
type required:
const
  e: Extended = Extended(1.0) / 3.0;

I seem to remember that this was once documented somewhere for Delphi. Can't
seem to find it though, so maybe it was a 3rd-party book? There is no hint of it
in the FPC documentation, anyway.


Regards,
Martok



___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal