On 2 May 2004 at 18:43, Neil Santos wrote:
I've been using Pascal a while, but it was only recently that I decided to use Pascal for OO programming. Having only used records and typed files up to this point, I'm wondering how objects are saved to stream.
Am I right in supposing that only instance variables are saved? If so, then what about the instance variables of objects inside an object? And the instance variables of objects inside an object, that's inside an object (and... you get the idea)?
I know I should just try it and see for myself, but there are times when trial-and-error must take a backseat to give way to asking, and I think this is one of those times.
You would do it however it suits your needs. In the FreeVision objects unit (copy obtainable from gnu-pascal.de/contrib/chief/) there is a TStream object and a few descendants. There are Get and Put methods which you can override. The unit is compatible with the BP objects unit.
Here is a simple example of what you could do (this example compiles under BP, gpc, Virtual Pascal, and Delphi - it works, but has virtually no error checking). You are obviously free to do stuff that is much more complex than this:
Program StrmTest; { usage: "strmtest stream.bin" }
USES objects;
{$ifdef Win32} {$H+} {$ifndef VirtualPascal} {$apptype console} {$endif} {$else} {$endif}
{$ifdef __GPC__} TYPE Word = Word16; {$endif}
TYPE pMyStream = ^TMyStream; TMyStream = OBJECT ( TDosStream ) Size, SelfID : Longint; Name : String [32]; PROCEDURE Put ( P : PObject ); FUNCTION Get : PObject; END;
FUNCTION schar ( CONST s : string ) : {$ifdef Win32}pchar{$else}string{$endif}; BEGIN schar := {$ifdef Win32}pchar{$endif} ( s ); END;
PROCEDURE TMyStream.Put ( P : PObject ); BEGIN IF ( p <> NIL ) THEN Size := Sizeof ( p^ ) ELSE Size := 0; WRITE ( Size, Sizeof ( Size ) ); { Store object size } IF ( p <> NIL ) THEN WRITE ( p^, Size ); { store object itself } END;
FUNCTION TMyStream.Get : PObject; VAR p : pObject; BEGIN READ ( Size, Sizeof ( Size ) ); { read stored object size } IF ( Size = 0 ) THEN Get := NIL ELSE BEGIN Getmem ( p, Size ); READ ( p^, Size ); { read stored object } Get := p; END; END;
{ tests writing to a stream } PROCEDURE StreamTest ( Const s, code : string ); VAR p : pMyStream; t : TMyStream; i : Word; BEGIN IF s = '' THEN Exit; IF ( code = 'r' ) THEN i := stOpen { read = "r"} ELSE IF ( code = 'w' ) THEN i := stCreate { else create new file } ELSE exit;
t.init ( sChar ( s ), i ); IF t.status = stok THEN WITH t DO BEGIN IF i = stCreate THEN BEGIN Name := 'I am a customised stream' + #0#0; SelfID := 1022; Size := Sizeof ( t ); p := @t; Put ( p ); Writeln ( 'Name=', Name ); writeln ( 'handle=', handle ); Writeln ( 'SelfID=', SelfID ); Writeln ( 'Size=', Size ); END ELSE BEGIN pObject ( p ) := Get; WITH TMyStream ( p^ ) DO BEGIN Writeln ( 'SelfID=', SelfID ); writeln ( 'handle=', handle ); Writeln ( 'Name=', Name ); Writeln ( 'Size=', Size ); END; Dispose ( p ); END; { if i = stCreate } Done; END;{ with t} END;
{ Program } VAR s, s1 : String [255]; BEGIN Writeln ( 'Now we shall read from/write to a STREAM (file)!' ); IF ParamCount = 1 THEN s := ParamStr ( 1 ) ELSE BEGIN WRITE ( 'Filename to read from/write to stream: ' ); Readln ( s ); END; WRITE ('Access code ("w"= write to stream; "r" - read from stream):'); Readln ( s1 );
StreamTest ( s, s1 ); END.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.bigfoot.com/~african_chief/