Recent

Author Topic: TStringReader... what does it actually do?  (Read 2559 times)

dsiders

  • Hero Member
  • *****
  • Posts: 1052
TStringReader... what does it actually do?
« on: December 22, 2018, 10:53:39 pm »
TStringReader in streamex.pas

I understand it's supposed to be a string-based wrapper for TTextReader. The problem is it doesn't implement any methods that can do anything other than create and free the class instance. Reset, Close, ReadLine, and IsEof all call un-implemented abstract virtual methods in TTextReader.

So the question is.... what the heck is it supposed to do?
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TStringReader... what does it actually do?
« Reply #1 on: December 22, 2018, 11:02:42 pm »
TStringReader in streamex.pas

I understand it's supposed to be a string-based wrapper for TTextReader. The problem is it doesn't implement any methods that can do anything other than create and free the class instance. Reset, Close, ReadLine, and IsEof all call un-implemented abstract virtual methods in TTextReader.

So the question is.... what the heck is it supposed to do?

I've never used it (and haven't--yet--dived into the code*) but my understanding is that allows you to use a normal string as if it were a TTextStream. Are you saying it's not fully implemented?


*I shall do so in a jiffy now you asked ... Done.

I think I see where the confussion comes from: it's implemented through a TStreamReader connected to a TStringStream, but the (private) field is declared as a TTextReader, so following it you end up in an almost purely abstract class.

The key is in the constructor:
Code: Pascal  [Select][+][-]
  1. constructor TStringReader.Create(const AString: string; ABufferSize: Integer);
  2. begin
  3.   inherited Create;
  4.   FReader := TStreamReader.Create(TStringStream.Create(AString), ABufferSize, True);
  5. end;
As you can see, it creates a TStreamReader instead of the abstract daddy TTextReader.
« Last Edit: December 22, 2018, 11:15:15 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: TStringReader... what does it actually do?
« Reply #2 on: December 22, 2018, 11:25:51 pm »
As you can see, it creates a TStreamReader instead of the abstract daddy TTextReader.

I overlooked that small detail. Thanks.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: TStringReader... what does it actually do?
« Reply #3 on: December 22, 2018, 11:51:03 pm »
TStringReader in streamex.pas

I understand it's supposed to be a string-based wrapper for TTextReader. The problem is it doesn't implement any methods that can do anything other than create and free the class instance. Reset, Close, ReadLine, and IsEof all call un-implemented abstract virtual methods in TTextReader.

So the question is.... what the heck is it supposed to do?

I think I see where the confussion comes from: it's implemented through a TStreamReader connected to a TStringStream, but the (private) field is declared as a TTextReader, so following it you end up in an almost purely abstract class.

The key is in the constructor:

Code: Pascal  [Select][+][-]
  1. constructor TStringReader.Create(const AString: string; ABufferSize: Integer);
  2. begin
  3.   inherited Create;
  4.   FReader := TStreamReader.Create(TStringStream.Create(AString), ABufferSize, True);
  5. end;

As you can see, it creates a TStreamReader instead of the abstract daddy TTextReader.

After looking at it, TFileReader uses the same technique.

So why would these classes specify FReader: TTextReader in their interface instead of FReader: TStreamReader;? They both require TStreamReader in their implementation, and do not provide overloaded constructors that result in the use of another TTextReader descendant for the member.

Just trying to understand the thought process here... cause using TTextReader just masks the fact that a concrete descendant is actually required.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TStringReader... what does it actually do?
« Reply #4 on: December 23, 2018, 12:13:16 am »
After looking at it, TFileReader uses the same technique.

So why would these classes specify FReader: TTextReader in their interface instead of FReader: TStreamReader;? They both require TStreamReader in their implementation, and do not provide overloaded constructors that result in the use of another TTextReader descendant for the member.

Just trying to understand the thought process here... cause using TTextReader just masks the fact that a concrete descendant is actually required.

That's actually a question I was making to myself while eye-greping the sources. The answer is inside the mind of the original coder but I suppose (s)he wanted the freedom to be able to replace TStreamReader for any other TTextReader descendant if needed.

Either that or it is a remnant of the first stages of development :o :)

ETA:
Also, using TTextReader ensures that only methods and properties present in TTextReader can be used in the implementation (without typecasting). Whether that is relevant or not, I can't say.
« Last Edit: December 23, 2018, 12:18:49 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018