Results 1 to 2 of 2

Thread: Generics Test

  1. #1
    Win32.Trojan.Heur.Herby
    Join Date
    Dec 2003
    Location
    Nuenen of all places
    Posts
    229

    Generics Test

    Hoi

    Ik ben eens wat aan het stoeien gegaan met generics.
    Volgens mij lukt het aardig, alleen het opslaan is nog even een uitdaging.
    Kan iemand me op de juiste weg helpen hiermee?


    Code:
    program GenericsTest;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils,
      System.Classes,
      System.Generics.Collections;
    
    type
      TMyClass      = class
      private
        FS1         : string;
        FS2         : string;
        FS3         : string;
      public
        constructor Create(const aS1, aS2, aS3: string);
        property S1 : string read FS1 write FS1;
        property S2 : string read FS2 write FS2;
        property S3 : string read FS3 write FS3;
      end;
    
      TMyTest       = class
      private
        FObjectList : TObjectList<TMyClass>;
        function    GetCount : integer;
      public
        constructor Create;
        procedure   AddItem  (const aS1, aS2, aS3: string);
        procedure   DelItem  (aIndex: Integer);
        procedure   EditItem (aIndex: Integer; const aS1, aS2, aS3: string);
        function    ReadFile (aFileName : string) : boolean;
        function    SaveFile (aFileName : string) : boolean;
        destructor  Destroy; override;
    
        property    ObjectList  : TObjectList<TMyClass>   read FObjectList    write FObjectList;
        property    Count       : integer                 read GetCount;
      end;
    
    
    { TMyClass }
    
    constructor TMyClass.Create(const AS1, AS2, AS3: String);
    begin
      FS1 := AS1;
      FS2 := AS2;
      FS3 := AS3;
    end;
    
    { TMyTest }
    
    constructor TMyTest.Create;
    begin
      ObjectList := TObjectList<TMyClass>.Create();
    end;
    
    procedure TMyTest.AddItem(const aS1, aS2, aS3: string);
    begin
      ObjectList.Add(TMyClass.Create(aS1, aS2, aS3));
      WriteLn(format('Added %s. Count is now %d ',[aS1, Count]));
    end;
    
    procedure TMyTest.DelItem  (aIndex: Integer);
    begin
      ObjectList.Delete(aIndex);
    end;
    
    procedure TMyTest.EditItem  (aIndex: Integer; const aS1, aS2, aS3: string);
    var
      oldS1 : string;
      oldS2 : string;
      oldS3 : string;
    begin
      OldS1 := ObjectList[aIndex].S1;
      OldS2 := ObjectList[aIndex].S2;
      OldS3 := ObjectList[aIndex].S3;
    
      ObjectList[aIndex].S1 := aS1;
      ObjectList[aIndex].S2 := aS2;
      ObjectList[aIndex].S3 := aS3;
    
      WriteLn(format('Edited. S1 %s->%s | S2 %s->%s | S3 %s->%s ',[oldS1, aS1, oldS2, aS2, oldS3, aS3]));
    end;
    
    function TMyTest.GetCount : integer;
    begin
      result := ObjectList.Count;
    end;
    
    
    function TMyTest.ReadFile(aFileName: string): boolean;
    begin
      result := false;
    end;
    
    function TMyTest.SaveFile(aFileName: string): boolean;
    var
      I : integer;
      FS : TFileStream;
      MS : TMemoryStream;
    begin
      FS := TFileStream.Create(aFilename, fmOpenWrite or fmShareExclusive);
      try
        MS := TMemoryStream.Create;
        try
          for I := 0 to Count -1 do
            begin
              // ???
              MS.Write(ObjectList[I], SizeOf(ObjectList[I]));
    
            end;
    
    
          //tijdelijk --> komt AES encryption tussen
          FS.CopyFrom(MS, MS.Size);
        finally
          MS.Free
        end;
      finally
        FS.Free
      end;
    
    
    end;
    
    destructor TMyTest.Destroy;
    begin
      ObjectList.Free;
      inherited;
    end;
    
    
    
    var
      MyTest : TMyTest;
      I      : integer;
    begin
      ReportMemoryLeaksOnShutdown := true;
      MyTest := TMyTest.Create;
      try
        WriteLn(format('Start -> Count is now %d ',[MyTest.Count]));
    
        WriteLn ('TEST ADD');
        MyTest.AddItem('1a','1b','1c');
        MyTest.AddItem('2a','2b','2c');
        MyTest.AddItem('3a','3b','3c');
    
        WriteLn ('TEST GET');
        // Test get info
        for I := 0 to MyTest.Count -1 do
          begin
            WriteLn(format('S1=%s | S2=%s | S3=%s',[MyTest.ObjectList[I].S1,
                                                    MyTest.ObjectList[I].S2,
                                                    MyTest.ObjectList[I].S3]));
          end;
    
        WriteLn ('TEST EDIT');
        MyTest.EditItem(1,'2a-edit','2b-edit','2c-edit');
    
        WriteLn ('TEST GET');
        // Test get info
        for I := 0 to MyTest.Count -1 do
          begin
            WriteLn(format('S1=%s | S2=%s | S3=%s',[MyTest.ObjectList[I].S1,
                                                    MyTest.ObjectList[I].S2,
                                                    MyTest.ObjectList[I].S3]));
          end;
    
        MyTest.SaveFile('testfile.dat');
    
      finally
        MyTest.Free;
      end;
    
    
    
    
      WriteLn ('Enter....');
      ReadLn;
    end.

  2. #2
    Win32.Trojan.Heur.Herby
    Join Date
    Dec 2003
    Location
    Nuenen of all places
    Posts
    229
    Nou het is gelukt, geen idee of het volgens enige standaard is, maar het werkt prima.
    Door een SaveToStream en LoadFromStream toe te voegen aan TMyClass kun je makkelijk per item de zaak saven.

    Code:
    program GenericsTest;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils,
      System.Classes,
      System.Generics.Collections;
    
    type
      TMyClass      = class
      private
        FS1         : string;
        FS2         : string;
        FS3         : string;
      public
        constructor Create; overload;
        constructor Create(const aS1, aS2, aS3: string); overload;
        procedure   SaveToStream(aStream: TStream);
        procedure   LoadFromStream(aStream: TStream);
        property S1 : string read FS1 write FS1;
        property S2 : string read FS2 write FS2;
        property S3 : string read FS3 write FS3;
      end;
    
      TMyTest       = class
      private
        FObjectList : TObjectList<TMyClass>;
        FCOunt      : integer;
        function    GetCount : integer;
      public
        constructor Create;
        procedure   AddItem  (const aS1, aS2, aS3: string);
        procedure   DelItem  (aIndex: Integer);
        procedure   EditItem (aIndex: Integer; const aS1, aS2, aS3: string);
        function    ReadFile (aFileName : string) : boolean;
        function    SaveFile (aFileName : string) : boolean;
        destructor  Destroy; override;
    
        property    ObjectList  : TObjectList<TMyClass>   read FObjectList    write FObjectList;
        property    Count       : integer                 read GetCount       write FCount;
      end;
    
    
    { TMyClass }
    
    constructor TMyClass.Create;
    begin
      FS1 := '';
      FS2 := '';
      FS3 := '';
    end;
    
    constructor TMyClass.Create(const AS1, AS2, AS3: String);
    begin
      FS1 := AS1;
      FS2 := AS2;
      FS3 := AS3;
    end;
    
    procedure TMyClass.SaveToStream(aStream: TStream);
    var
      Len: Integer;
    begin
      Len := Length(FS1);
      aStream.Write(Len, SizeOf(Len));
      aStream.Write(PChar(FS1)^, Len * SizeOf(Char));
    
      Len := Length(FS2);
      aStream.Write(Len, SizeOf(Len));
      aStream.Write(PChar(FS2)^, Len * SizeOf(Char));
    
      Len := Length(FS3);
      aStream.Write(Len, SizeOf(Len));
      aStream.Write(PChar(FS3)^, Len * SizeOf(Char));
    end;
    
    
    procedure TMyClass.LoadFromStream(aStream: TStream);
    var
      Len: Integer;
    begin
      aStream.Read(Len, SizeOf(Len));
      SetString(FS1, PChar(nil), Len);
      aStream.Read(PChar(FS1)^, Len * SizeOf(Char));
    
      aStream.Read(Len, SizeOf(Len));
      SetString(FS2, PChar(nil), Len);
      aStream.Read(PChar(FS2)^, Len * SizeOf(Char));
    
      aStream.Read(Len, SizeOf(Len));
      SetString(FS3, PChar(nil), Len);
      aStream.Read(PChar(FS3)^, Len * SizeOf(Char));
    
    end;
    
    
    
    { TMyTest }
    
    constructor TMyTest.Create;
    begin
      ObjectList := TObjectList<TMyClass>.Create();
    end;
    
    procedure TMyTest.AddItem(const aS1, aS2, aS3: string);
    begin
      ObjectList.Add(TMyClass.Create(aS1, aS2, aS3));
    end;
    
    procedure TMyTest.DelItem  (aIndex: Integer);
    begin
      ObjectList.Delete(aIndex);
    end;
    
    procedure TMyTest.EditItem  (aIndex: Integer; const aS1, aS2, aS3: string);
    var
      oldS1 : string;
      oldS2 : string;
      oldS3 : string;
    begin
      OldS1 := ObjectList[aIndex].S1;
      OldS2 := ObjectList[aIndex].S2;
      OldS3 := ObjectList[aIndex].S3;
    
      ObjectList[aIndex].S1 := aS1;
      ObjectList[aIndex].S2 := aS2;
      ObjectList[aIndex].S3 := aS3;
    end;
    
    function TMyTest.GetCount : integer;
    begin
      FCount := ObjectList.Count;
      result := FCount;
    end;
    
    
    function TMyTest.ReadFile(aFileName: string): boolean;
    var
      FS : TFileStream;
      MS : TMemoryStream;
      ListCount : integer;
      I : integer;
    begin
      ListCount := 0;
    
      FS := TFileStream.Create(aFilename, fmOpenRead or fmShareExclusive);
      try
        MS := TMemoryStream.Create;
        try
          //tijdelijk --> komt AES encryption tussen
          FS.Position := 0;
          MS.CopyFrom(FS, FS.Size);
          //
          if MS.Size > 0 then
            begin
              MS.Position := 0;
              // Read ListCount
              MS.Read(ListCount, SizeOf(ListCount));
    
              // Loop through Objectlists
              for I := 0 to ListCount -1 do
                begin
                  ObjectList.Add(TMyClass.Create);
                 (Objectlist[I] as TMyClass).LoadFromStream(MS);
                end;
            end;
        finally
          MS.Free;
        end;
      finally
        FS.Free;
      end;
    end;
    
    function TMyTest.SaveFile(aFileName: string): boolean;
    var
      I : integer;
      FS : TFileStream;
      MS : TMemoryStream;
      ListCount : integer;
      MyClass : TMyClass;
    begin
      ListCount := Count;
      if ListCount = 0 then Exit;
    
      FS := TFileStream.Create(aFilename, fmCreate or fmOpenWrite or fmShareExclusive);
      try
        MS := TMemoryStream.Create;
        try
          // Write ListCount
          MS.Write(ListCount, SizeOf(ListCount));
          // Loop through Objectlists
          for I := 0 to Count -1 do
             (Objectlist[I] as TMyClass).SaveToStream(MS);
          //tijdelijk --> komt AES encryption tussen
          MS.Position := 0;
          FS.CopyFrom(MS, MS.Size);
        finally
          MS.Free
        end;
      finally
        FS.Free
      end;
    end;
    
    
    destructor TMyTest.Destroy;
    begin
      ObjectList.Free;
      inherited;
    end;
    
    
    
    var
      MyTest : TMyTest;
      I      : integer;
    begin
      ReportMemoryLeaksOnShutdown := true;
      MyTest := TMyTest.Create;
      try
        WriteLn(format('Start -> Count is now %d ',[MyTest.Count]));
    
        // Adding 20 items
        for I := 0 to 19 do
          MyTest.AddItem('#A'+I.ToString,'#B'+I.ToString, '#C'+I.ToString);
    
        // Change 5th item
        MyTest.EditItem(4,'blah','blahblah','blahblahblah');
    
        // Getting items
        for I := 0 to MyTest.Count -1 do
          begin
            WriteLn(format('S1=%s | S2=%s | S3=%s',[MyTest.ObjectList[I].S1,
                                                    MyTest.ObjectList[I].S2,
                                                    MyTest.ObjectList[I].S3]));
          end;
    
        // Save to file
        MyTest.SaveFile('.\testfile.dat');
    
      finally
        MyTest.Free;
      end;
    
      WriteLn ('=============================================================');
    
      MyTest := TMyTest.Create;
      try
        // Load file
        MyTest.ReadFile('.\testfile.dat');
    
        // Getting items
        for I := 0 to MyTest.Count -1 do
          begin
            WriteLn(format('S1=%s | S2=%s | S3=%s',[MyTest.ObjectList[I].S1,
                                                    MyTest.ObjectList[I].S2,
                                                    MyTest.ObjectList[I].S3]));
          end;
    
      finally
        MyTest.Free;
      end;
    
    
      WriteLn ('Enter....');
      ReadLn;
    end.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •