Recent

Author Topic: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.  (Read 1319 times)

dmitryb

  • Jr. Member
  • **
  • Posts: 62
Hi All

Why Classes.inc - procedure ObjectBinaryToText does not support vaCurrency in ReadPropList.

In the ProcessValue procedure
    procedure ProcessValue(ValueType: TValueType; Indent: String);

...

          end;
        {vaSingle: begin OutLn('!!Single!!'); exit end;
        vaCurrency: begin OutLn('!!Currency!!'); exit end;
        vaDate: begin OutLn('!!Date!!'); exit end;}
        vaUTF8String: begin
...

they just comment out code that read vaCurrency values.


I have components with list of Vairants as property value.
When value in list have vaCurrency value, writing data to text file crashed.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #1 on: August 18, 2019, 02:20:43 pm »
Floating point values cannot be published.
If you want real value fields in a class their visibility cannot be higher than public.
(later) Well, I was completely wrong about that!
« Last Edit: August 18, 2019, 05:26:23 pm by howardpc »

dmitryb

  • Jr. Member
  • **
  • Posts: 62
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #2 on: August 18, 2019, 02:50:50 pm »
Hello

I tested component with Currency and Variant

Code: Pascal  [Select][+][-]
  1.   TComponentWithCurr = class(TComponent)
  2.   private
  3.     FCurrProp: Currency;
  4.     FVarProp: Variant;
  5.   protected
  6.     procedure DefineProperties(Filer: TFiler); override;
  7.     procedure DoReadData(Reader: TReader); virtual;
  8.     procedure DoWriteData(Writer: TWriter); virtual;
  9.   public
  10.  
  11.   published
  12.     property CurrProp: Currency read FCurrProp write FCurrProp;
  13.     property VarProp: Variant read FVarProp write FVarProp;
  14.   end;  

It works without problem.

dmitryb

  • Jr. Member
  • **
  • Posts: 62
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #3 on: August 18, 2019, 02:58:26 pm »

Our values are not in published section.
They are written in DefineProperties.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #4 on: August 18, 2019, 03:27:01 pm »

Our values are not in published section.
They are written in DefineProperties.
In that case make sure the properties are defined in {$M+} mode. That doesn't  happen automatically, so use {$push}{$M+} ... definition ...{$pop} otherwise no rtti is generated and you have the behavior as you described.
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #5 on: August 18, 2019, 04:30:01 pm »

Our values are not in published section.
They are written in DefineProperties.
In that case make sure the properties are defined in {$M+} mode. That doesn't  happen automatically, so use {$push}{$M+} ... definition ...{$pop} otherwise no rtti is generated and you have the behavior as you described.

Afaik he inherits from tcomponent which inherits from TPersistent which is declared with {$M+}, so his class inherits that status.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #6 on: August 18, 2019, 04:46:49 pm »
I just mentioned it, just in case.
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #7 on: August 18, 2019, 04:59:28 pm »
Currency like other floating types can be published:
Code: Pascal  [Select][+][-]
  1. type
  2.   TSomeClass = class(TComponent)
  3.   strict private
  4.     FCurrProp: Currency;
  5.   published
  6.     property CurrProp: Currency read FCurrProp write FCurrProp;
  7.   end;
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);
  10. var
  11.   One, Two: TSomeClass;
  12.   MemStream: TMemoryStream;
  13.   StrStream: TStringStream;
  14. begin
  15.   One := TSomeClass.Create(Self);
  16.   One.CurrProp := 1.23456;
  17.   MemStream := TMemoryStream.Create;
  18.   try
  19.     MemStream.WriteComponent(One);
  20.     StrStream := TStringStream.Create('');
  21.     try
  22.       MemStream.Position := 0;
  23.       ObjectBinaryToText(MemStream, StrStream);
  24.       Memo1.Text := StrStream.DataString;  // 1
  25.       MemStream.Position := 0;
  26.       StrStream.Position := 0;
  27.       ObjectTextToBinary(StrStream, MemStream);
  28.     finally
  29.       StrStream.Free;
  30.     end;
  31.     MemStream.Position := 0;
  32.     RegisterClass(TSomeClass);
  33.     Two := MemStream.ReadComponent(nil) as TSomeClass;
  34.     try
  35.       Memo1.Append(CurrToStr(Two.CurrProp)); // 2
  36.     finally
  37.       Two.Free;
  38.     end;
  39.   finally
  40.     MemStream.Free;
  41.   end;
  42. end;

All floating types are written as Extended, so there is no separate code for Single, Date, etc.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #8 on: August 18, 2019, 05:58:53 pm »
isn't a Currency a 64 bit binary type? Two 32 bits ?
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #9 on: August 18, 2019, 06:22:13 pm »
Yes. (but) it is a scaled integer type internally https://freepascal.org/docs-html/ref/refsu5.html
« Last Edit: August 18, 2019, 06:23:50 pm by Thaddy »
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #10 on: August 18, 2019, 06:26:49 pm »
I guess my hint was that if the READER / WRITER is going to translate it to a float then it's possible to lose accuracy, maybe.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Why ObjectBinaryToText does not support vaCurrency in ReadPropList.
« Reply #11 on: August 18, 2019, 06:51:54 pm »
I guess my hint was that if the READER / WRITER is going to translate it to a float then it's possible to lose accuracy, maybe.
No, because of the inherit limitation of currency. You have to check how the compiler handles it if you want the details.
Basically it is the storage format.
Specialize a type, not a var.

 

TinyPortal © 2005-2018