Einzelnen Beitrag anzeigen

moelski

Registriert seit: 31. Jul 2004
1.110 Beiträge
 
Delphi 2010 Professional
 
#13

Re: Liste (ähnlich dyn Array) mit fortlaufendem Index?

  Alt 25. Mär 2010, 09:54
Moin Dirk,

Anbei mal mein Code.
Wie würde das ansatzweise aussehen wenn ich verkettete Listen verwende?

Delphi-Quellcode:
unit DataClass;

interface

uses Classes, SyncObjs,
     SysUtils, Contnrs;

type
  TOneData = record
      ID : Int64;
      Zeichenkette : string[255];
      Value1, Value2, Value3, Value4 : Double;
      Kill : Boolean;
    end;

  TDataClass = class(TPersistent)
  private
    { Private-Deklarationen }
    fDataMutex : TMutex; // nur ein Thread darf Daten schreiben / löschen
    fNotify : TNotifyEvent;

    fData : Array of TOneData;

    fID : Int64; // eindeutiger Index
  public
    { Public-Deklarationen }
    constructor Create;
    Destructor Kill;

    Procedure Add(Name : String) ; overload;
    Procedure Add(V1, V2, V3, V4 : Double); overload;
    procedure DeleteItem(ID : Int64);
    Function GetLength : Integer;
    procedure GetIDs(var firstID, lastID : Int64);
    Function Read(Item : Integer) : String; overload;
    procedure Read(var V1, V2, V3, V4 : Double; Item : Integer); overload;
    function Done(Item : Integer) : Boolean;
  published
    property OnNotify: TNotifyEvent read fNotify write fNotify;
  end;

implementation

constructor TDataClass.Create;
begin
  inherited;
  fDataMutex := TMutex.Create(nil, False, 'DataMutex', True);
  fID := 0;
end;

Procedure TDataClass.Add(Name : String);
begin
  fDataMutex.Acquire;
  Inc(fID);
  SetLength(fdata, Length(fdata) + 1);
  fdata[Length(fData) - 1].Zeichenkette := DateTimeToStr(Now) + ' ' + Name;
  fdata[Length(fData) - 1].ID := fID;
  fDataMutex.Release;

  // Notify auslösen ...
  if Assigned(fNotify) then fNotify(Self);
end;

Procedure TDataClass.Add(V1, V2, V3, V4 : Double);
begin
  fDataMutex.Acquire;
  Inc(fID);
  SetLength(fdata, Length(fdata) + 1);
  fdata[Length(fData) - 1].Value1 := V1;
  fdata[Length(fData) - 1].Value2 := V2;
  fdata[Length(fData) - 1].Value3 := V3;
  fdata[Length(fData) - 1].Value4 := V4;
  fdata[Length(fData) - 1].ID := fID;
  fDataMutex.Release;
end;

procedure TDataClass.DeleteItem(ID : Int64);
begin
// fdata
end;

Function TDataClass.GetLength : Integer;
begin
  fDataMutex.Acquire;
  Result := Length(fData);
  fDataMutex.Release;
end;

procedure TDataClass.GetIDs(var firstID, lastID : Int64);
var I : Integer;
begin
  fDataMutex.Acquire;
  for I := 0 to Length(fData) - 1 do begin
    if fData[I].Kill = False then
      Break;
  end;

  firstID := I;

  fDataMutex.Release;
end;

Function TDataClass.Read(Item : Integer) : String;
begin
  fDataMutex.Acquire;
  Result := fData[Item].Zeichenkette;
  fDataMutex.Release;
end;

procedure TDataClass.Read(var V1, V2, V3, V4 : Double; Item : Integer);
begin
  fDataMutex.Acquire;
  V1 := fData[Item].Value1;
  V2 := fData[Item].Value2;
  V3 := fData[Item].Value3;
  V4 := fData[Item].Value4;
  fDataMutex.Release;
end;

function TDataClass.Done(Item : Integer) : Boolean;
begin
  fDataMutex.Acquire;
  Result := fData[Item].Kill;
  fDataMutex.Release;
end;

Destructor TDataClass.Kill;
begin
  inherited;
  fDataMutex.Free;
end;

end.
Dominik Schmidt
Greetz Dominik

I love Delphi 2007/2010
  Mit Zitat antworten Zitat