IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Lazarus Pascal Discussion :

Migrer de uutlGenerics vers Generics.Collections


Sujet :

Lazarus Pascal

  1. #1
    Membre chevronné Avatar de der§en
    Homme Profil pro
    Bretagne
    Inscrit en
    Septembre 2005
    Messages
    1 047
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bretagne
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2005
    Messages : 1 047
    Par défaut Migrer de uutlGenerics vers Generics.Collections
    Bonjour,

    Je cherche à moderniser et à supprimer du vieux code plus maintenu par du code standard concernant les Generics.

    Voici le code de base :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    uses
      Classes, SysUtils, uutlGenerics,Windows;
     
    type
      TDirectoryNode = class;
      TDirectoryRootInfo = class;
     
      TDirectoryNodeList = class(specialize TutlList<TDirectoryNode>) 
      public
        procedure SortRecursive(aComparer: IComparer; const aReverse: boolean);
      end;
     
      TDirNodeSortBySize = class(TInterfacedObject, TDirectoryNodeList.IComparer)
      public
        function EqualityCompare(constref i1, i2: TDirectoryNode): boolean;
        function Compare(constref i1, i2: TDirectoryNode): integer;
      end;
    J'ai cherché à le remplacer par ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    uses
      Classes, SysUtils, Generics.Defaults, Generics.collections, Windows;
     
    type
      TDirectoryNode = class;
      TDirectoryRootInfo = class;
     
      TDirectoryNodeList = class(specialize TList<TDirectoryNode>)
      public
        procedure SortRecursive(aComparer: IComparer; const aReverse: boolean);
      end;
     
      TDirNodeSortBySize = class(TInterfacedObject, TDirectoryNodeList.IComparer)
      public
        function EqualityCompare(constref i1, i2: TDirectoryNode): boolean;
        function Compare(constref i1, i2: TDirectoryNode): integer;
      end;
    uutlGenerics provient des paquets, pas facile à trouver : bitSpaceControls et bitSpaceUtils...

    Sauf que mon Lazarus râle sur les ICOMPARER, et j'avoue je suis bien loin de maîtriser les generics couplés à une interface...

    Une idée de ce qui me manque pour résoudre le problème ?

    Merci d'avance de votre aide.

  2. #2
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 201
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 201
    Par défaut
    Hello,
    D'abord maintenant en free pascal (objectpascal) on utilise plus Generics.* mais FGL (Free Pascal Generics Library). Ensuite j'ai posé la question à ChatGpt pour convertir ton code. Il m'a sorti un code que j'ai essayé mais j'avais une erreur comparable à la tienne sur IComparer. Alors je lui ai dit que j'avais une erreur et il m'a répondu :
    Très bonne remarque ! Le type TComparison<T> et TComparer<T> font partie de Delphi, mais ne sont pas disponibles dans Free Pascal par défaut.

    Donc, pour corriger cette erreur dans Free Pascal, nous allons :


    ✅ Remplacer TComparison<T> et TComparer<T> par une fonction de comparaison classique
    Ensuite je lui ai demandé un exemple complet avec utilisation des répertoires windows et voici le code complet qui fonctionne chez moi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    program projectgeneric;
    {$mode objfpc}{$H+}
     
    uses
      Classes, SysUtils, FGL;
     
    type
      TDirectoryNode = class;
      TDirectoryNodeList = class;
     
      // Classe représentant un nœud de répertoire
      TDirectoryNode = class
      private
        FSize: Int64;
        FChildren: TDirectoryNodeList;
        FPath: string;
      public
        constructor Create(const aPath: string = '');
        destructor Destroy; override;
     
        procedure BuildFromPath;
     
        property Path: string read FPath write FPath;
        property Size: Int64 read FSize write FSize;
        property Children: TDirectoryNodeList read FChildren;
      end;
     
      TDirectoryNodeCompare = function(const A, B: TDirectoryNode): Integer;
     
      // Liste de TDirectoryNode avec tri récursif
      TDirectoryNodeList = class(specialize TFPGObjectList<TDirectoryNode>)
      public
        procedure SortRecursive(aComparer: TDirectoryNodeCompare; const aReverse: Boolean);
      end;
     
      // Comparateur de taille
      TDirNodeSortBySize = class
      public
        class function Compare(constref i1, i2: TDirectoryNode): Integer; static;
      end;
     
     constructor TDirectoryNode.Create(const aPath: string);
    begin
      inherited Create;
      FChildren := TDirectoryNodeList.Create(True);
      FPath := aPath;
      FSize := 0;
    end;
     
    destructor TDirectoryNode.Destroy;
    begin
      FChildren.Free;
      inherited Destroy;
    end;
     
    procedure TDirectoryNode.BuildFromPath;
    var
      SR: TSearchRec;
      SubNode: TDirectoryNode;
      FilePath: string;
    begin
      if not DirectoryExists(FPath) then
        Exit;
     
      if FindFirst(IncludeTrailingPathDelimiter(FPath) + '*', faAnyFile and not faVolumeID, SR) = 0 then
      begin
        repeat
          if (SR.Name = '.') or (SR.Name = '..') then
            Continue;
     
          FilePath := IncludeTrailingPathDelimiter(FPath) + SR.Name;
     
          if (SR.Attr and faDirectory) <> 0 then
          begin
            // Répertoire : créer un sous-nœud et récursion
            SubNode := TDirectoryNode.Create(FilePath);
            SubNode.BuildFromPath;
            FChildren.Add(SubNode);
            Inc(FSize, SubNode.Size);
          end
          else
          begin
            // Fichier : ajouter la taille
            Inc(FSize, SR.Size);
          end;
     
        until FindNext(SR) <> 0;
        FindClose(SR);
      end;
    end;
    function CompareBySize(const A, B: TDirectoryNode): Integer;
    begin
      if A.Size < B.Size then
        Result := -1
      else if A.Size > B.Size then
        Result := 1
      else
        Result := 0;
    end;
     
    function CompareBySizeDescending(const A, B: TDirectoryNode): Integer;
    begin
      Result := -CompareBySize(A, B);
    end;
     
    procedure TDirectoryNodeList.SortRecursive(aComparer: TDirectoryNodeCompare; const aReverse: Boolean);
    var
      i: Integer;
    begin
      if aReverse then
        Sort(@CompareBySizeDescending)
      else
        Sort(@CompareBySize);
     
      // Appel récursif
      for i := 0 to Count - 1 do
        Items[i].Children.SortRecursive(aComparer, aReverse);
    end;
     
    class function TDirNodeSortBySize.Compare(constref i1, i2: TDirectoryNode): Integer;
    begin
      if i1.Size < i2.Size then
        Result := -1
      else if i1.Size > i2.Size then
        Result := 1
      else
        Result := 0;
    end;
    procedure PrintDirectoryTree(Node: TDirectoryNode; Level: Integer = 0);
    var
      i: Integer;
    begin
      WriteLn(StringOfChar(' ', Level * 2), ExtractFileName(Node.Path), ' (', Node.Size, ' bytes)');
     
      for i := 0 to Node.Children.Count - 1 do
        PrintDirectoryTree(Node.Children[i], Level + 1);
    end;
     
    var
      Root: TDirectoryNode;
    begin
      Writeln('Start');
      Root := TDirectoryNode.Create('D:\tmp'); // À adapter
      Root.BuildFromPath;
      // Trier les répertoires et sous-répertoires par taille décroissante
      Root.Children.SortRecursive(@CompareBySize, True);
      PrintDirectoryTree(Root);
      // Libération
      Root.Free;
      Readln;
    end.
    Ami calmant, J.P
    Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko :zen:

  3. #3
    Membre chevronné Avatar de der§en
    Homme Profil pro
    Bretagne
    Inscrit en
    Septembre 2005
    Messages
    1 047
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bretagne
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2005
    Messages : 1 047
    Par défaut
    Merci pour l’info à propos de FGL

    je vais creuser la question…

Discussions similaires

  1. Une solution pour migrer de .NET vers MFC
    Par torNAdE dans le forum MFC
    Réponses: 1
    Dernier message: 03/05/2006, 22h23
  2. [DBA] Migrer une base vers un autre serveur
    Par Bridou dans le forum Oracle
    Réponses: 1
    Dernier message: 28/02/2006, 08h26
  3. [D5][Interbase]Migrer du BDE vers IBX
    Par delphi5user dans le forum Bases de données
    Réponses: 1
    Dernier message: 18/11/2005, 15h03
  4. Migrer de MySQL vers PostgreSQL
    Par Acti dans le forum PostgreSQL
    Réponses: 9
    Dernier message: 25/02/2005, 14h20
  5. Migrer de kmail vers Thunderbird
    Par calfater dans le forum Applications et environnements graphiques
    Réponses: 5
    Dernier message: 13/07/2004, 14h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo