Einzelnen Beitrag anzeigen

blackdrake

Registriert seit: 21. Aug 2003
Ort: Bammental
618 Beiträge
 
Delphi 10.3 Rio
 
#18

Re: "Array" mit Strings als Indizes (Zuordnungstab

  Alt 27. Mär 2009, 08:50
OK, hier nun mein fertiger Code:

Delphi-Quellcode:
unit ObjMan;

interface

uses
  SysUtils, IniFiles;

type
  TObjectManager = class(THashedStringList)
  private
    function GetObjectByString(const Str: String): TObject;
    procedure SetObjectByString(const Str: String; const Value: TObject);
    function GetStringByObject(const Obj: TObject): String;
    procedure SetStringByObject(const Obj: TObject; const Value: String);
  public
    function StringExists(const Str: String): boolean;
    function ObjectExists(const Obj: TObject): boolean;
    function DeleteString(const Str: String): boolean;
    property ObjectByString[const Str: string]: TObject
      read GetObjectByString write SetObjectByString;
    property StringByObject[const Obj: TObject]: String
      read GetStringByObject write SetStringByObject;
  end;

  EStringNotFound = class(Exception);
  EObjectNotFound = class(Exception);

resourcestring
  E_STRING_NOT_FOUND = 'The string "%s" was not found in the list.';
  E_OBJECT_NOT_FOUND = 'Object was not found in the list.';

implementation

{ TObjectManager } 

function TObjectManager.GetObjectByString(const Str: string): TObject;
var
  i: integer;
begin
  i := IndexOf(Str);
  if i <> -1 then
    result := Objects[i]
  else
    raise EStringNotFound.CreateFmt(E_STRING_NOT_FOUND, [Str]);
end;

procedure TObjectManager.SetObjectByString(const Str: string;
  const Value: TObject);
var
  i: integer;
begin
  i := IndexOf(Str);
  if i <> -1 then
    Objects[i] := Value
  else
    AddObject(Str, Value);
end;

function TObjectManager.StringExists(const Str: string): boolean;
begin
  result := IndexOf(Str) <> -1;
end;

function TObjectManager.ObjectExists(const Obj: TObject): boolean;
begin
  result := IndexOfObject(Obj) <> -1;
end;

function TObjectManager.DeleteString(const Str: string): boolean;
var
  i: integer;
begin
  i := IndexOf(Str);
  result := i <> -1;
  if result then Delete(i);
end;

function TObjectManager.GetStringByObject(const Obj: TObject): String;
var
  i: integer;
begin
  i := IndexOfObject(Obj);
  if i <> -1 then
    result := Strings[i]
  else
    raise EObjectNotFound.Create(E_OBJECT_NOT_FOUND);
end;

procedure TObjectManager.SetStringByObject(const Obj: TObject;
  const Value: String);
var
  i: integer;
begin
  i := IndexOfObject(Obj);
  if i <> -1 then
    Strings[i] := Value
  else
    AddObject(Value, Obj);
end;

end.
Die Exceptions bei "Verschmelzung" von Einträgen habe ich doch nicht gemacht, da ein solches Verhalten nicht nur durch Case-Sensitive entstehen kann, sondern auch, wenn man einfach zwei Einträge mit identischen Strings hinzufügt. Wenn man aber Konsequent nur mit ObjectByString[] arbeitet, kommt es zu keiner Überdeckung.

Man beachte bitte das dynamische Verhalten von SetStringByObject() und SetObjectByString(). Diese fügen einen neuen Eintrag hinzu, wenn man auf ein unbekanntes Element schreibend zugreift.

Gruß
blackdrake
Angehängte Dateien
Dateityp: pas objman_949.pas (2,4 KB, 1x aufgerufen)
Daniel Marschall
  Mit Zitat antworten Zitat