the source code (and comments) gets messed up with google translate, ill have to try out the examples to see what they do
after removing some procedure code I got it to work but it doesnt serialize an array:
Code:
(*******************************************************************************
Serializacja klas w Delphi cz.2
DaThoX 2004-2008
Maciej Izak (hnb.code[at]gmail[dot]com)
Na dzisiejszej lekcji zajmiemy się obsługą zapisu przypisanej metody do pola
(Delphi umożliwia zapamiętanie jaką metodę przypisaliśmy polu typu
proceduralnego obiektowego)
Poznamy także procedury umozliwiajace odczyt/zapis klasy z/do pliku
******************************************************************************)
program Lesson_02;
{$APPTYPE CONSOLE}
uses
SysUtils,
Classes, // <- podstawowe klasy z kt??rych bedziemy dziedziczyć
Dialogs,
dtxUtils; // <- napisałem procedury ułatwiające serializację klas
{-------------------------------------------------------------------------------
Na dole w gł??wnym bloku begin ... end. znajduje się opis zapisu i odczytu klas
z jak i do pliku ...
------------------------------------------------------------------------------
/// Funkcje pomocnicze z dtxUtils.pas ///
------------------------------------------------------------------------------
---
1 procedure StringToComponent(Value: string; Comp: TComponent);
---
------------------------------------------------------------------------------
Konwertuje dane z łańcucha znak??w na dane do obiektu i inicjalizuje
jego dane. Przekazywana klasa Comp musi być wczesniej stworzona.
---
2 function ComponentToString(Component: TComponent): string;
---
------------------------------------------------------------------------------
Konwertuje komponent na łańcuch
---
3 procedure SaveComponent(AFile: string; AComponent: TComponent;
--- AText: boolean = true);
------------------------------------------------------------------------------
Zapisuje komponent do pliku tekstowego. Parametr AText ustawia czy ma być
to w postaci czytelnej dla człowieka (tekstowej) czy też binarnej.
Wersja binarna zajmuje nieco mniej miejsca.
---
4 procedure LoadComponent(AFile: string; AComponent: TComponent;
--- AText: boolean = true);
------------------------------------------------------------------------------
Robi dokładnie to co SaveComponent tylko w drugą stronę
------------------------------------------------------------------------------
/// Funkcje i typy pomocnicze z "Classes.pas" ///
------------------------------------------------------------------------------
---
1 type
--- TStreamOriginalFormat = (sofUnknown, sofBinary, sofText);
------------------------------------------------------------------------------
Typ m??wiący o tym w jakiej postaci zapisaliśmy obiekt (binarna/tekstowa)
---
2 procedure ObjectBinaryToText(Input, Output: TStream); overload;
--- procedure ObjectBinaryToText(Input, Output: TStream;
var OriginalFormat: TStreamOriginalFormat); overload;
procedure ObjectTextToBinary(Input, Output: TStream); overload;
procedure ObjectTextToBinary(Input, Output: TStream;
var OriginalFormat: TStreamOriginalFormat); overload;
------------------------------------------------------------------------------
Funkcje konwertujące obiekt w postaci binarnej do tekstowej (i odwrotnie)
operujące na strumieniach
---
3 procedure ObjectResourceToText(Input, Output: TStream); overload;
--- procedure ObjectResourceToText(Input, Output: TStream;
var OriginalFormat: TStreamOriginalFormat); overload;
procedure ObjectTextToResource(Input, Output: TStream); overload;
procedure ObjectTextToResource(Input, Output: TStream;
var OriginalFormat: TStreamOriginalFormat); overload;
------------------------------------------------------------------------------
Funkcje konwertujące postać tekstową do zasob??w i odwrotnie
---
4 function TestStreamFormat(Stream: TStream): TStreamOriginalFormat;
---
------------------------------------------------------------------------------
Funkcja sprawdzająca jakiego typu są dane zserializowanego obiektu
------------------------------------------------------------------------------}
// wrsja player 2:) - it contains everything that is contained in the previous class
// + small part of a news item :)
type
ship =class(Tcomponent)
public
shipname:string;
published
property pshipname:string read shipname write shipname;
end;
type DynamicshipArray = array of ship;
type
TPlayer2 = class(TComponent)
private
FName: string;
FRememberPasswd: boolean;
FPasswd: string;
ship_array: DynamicshipArray ;
public
// ---
protected
// ---
published
property Name: string read FName write FName;
property RememberPasswd: boolean read FRememberPasswd write FRememberPasswd;
property Passwd: string read FPasswd write FPasswd stored FRememberPasswd;
property Pship_array: DynamicshipArray read ship_array write ship_array;
// tytaj zaczynają się nowości :) ...
// pole pamiętające nasz wyb??r (możemy przypisać mu dowolną metodę będacą
// w sekcji published z klasy w kt??rej ??w właściwość jest zadeklarowana)
end;
{ TPlayer2 }
var
Player: TPlayer2;
c: char;
{-------------------------------------------------------------------------------
Below are the procedures used to record the reading class files and streams.
------------------------------------------------------------------------------}
begin
Player := TPlayer2.Create(nil);
setlength(player.ship_array,6);
player.ship_array[0]:=ship.create(nil);
player.ship_array[0].shipname:='brian';
WriteLn;
WriteLn('That looks like our class converted to string :', sLineBreak, sLineBreak,
ComponentToString(Player)
);
// write to the file
SaveComponent('Player.txt', Player);
//and release
Player.Free;
WriteLn(sLineBreak,
'Class has been written to Player.txt''and''exempt from the memory. '+
'You can open the saved file and edit it - for the moment TPlayer2 class'+
'Will be created again and already the Player.txt''''will be loaded' +
'Content class. Edit the file as you want and press [ENTER] '
+ 'Initialize class and provoke property OnPrint.');
ReadLn;
// tworzymy obiekt od nowa
Player := TPlayer2.Create(nil);
LoadComponent('Player.txt', Player);
WriteLn('That looks like our class is loaded from a file :', sLineBreak, sLineBreak,
ComponentToString(Player)
);
// Call the property :)
WriteLn('For the moment calls the method recorded in the property OnPrint :');
// if c = '1' then
//begin
//Player.OnPrint1;
//end
//else
//begin
//Player.OnPrint2;
//end;
WriteLn;
// zakończ ...
ReadLn;
Player.Free;
end.
it just ignores the array completely
also, using properties seems to be a bust because freepascal doesnt have
GetDynArrayProp
Bookmarks