unit dhash_registry;

interface

uses
  dhash;

type
THashRegistry = object
private
  FHashWrapper: array of THashWrapper;
  FObjects: array of PHash;
public
  constructor Init;
  procedure Regist(const Name: String; Func: THashFunc); overload;
  procedure Regist(const Name: String; Obj: PHash); overload;
  function GetHashFunc(const Name: String): THashFunc;
  function GetHashWrapper(const Name: String): PHashWrapper;
  function GetHashObject(const Name: String): PHash;
  function GetHashName(Func: THashFunc): String;
end;

var
  HashRegistry: THashRegistry;

implementation

constructor THashRegistry.Init;
begin
  SetLength(FHashWrapper, 0);
end;

procedure THashRegistry.Regist(const Name: String; Func: THashFunc);
begin
  SetLength(FHashWrapper, Length(FHashWrapper) + 1);
  FHashWrapper[High(FHashWrapper)].Init(Name, Func);
end;

procedure THashRegistry.Regist(const Name: String; Obj: PHash);
begin
  SetLength(FObjects, Length(FObjects) + 1);
  FObjects[High(FObjects)] := Obj;
end;

function THashRegistry.GetHashFunc(const Name: String): THashFunc;
var
  W: PHashWrapper;
begin
  W := GetHashWrapper(Name);
  if W <> nil then
    Result := GetHashWrapper(Name).Func
  else
    Result := nil;	    
end;

function THashRegistry.GetHashWrapper(const Name: String): PHashWrapper;
var
  I: Integer;
begin
  for I := 0 to High(FHashWrapper) do
    if FHashWrapper[I].Name = Name then
      Exit(@FHashWrapper[I]);
  Exit(nil);
end;

function THashRegistry.GetHashObject(const Name: String): PHash;
var
  I: Integer;
begin
  for I := 0 to High(FObjects) do
    if FObjects[I].Name = Name then
      Exit(FObjects[I]);
  Exit(nil);
end;

function THashRegistry.GetHashName(Func: THashFunc): String;
var
  I: Integer;
begin
  for I := 0 to High(FHashWrapper) do
    if @FHashWrapper[I].Func = @Func then
      Exit(FHashWrapper[I].Name);
  Exit('?');
end;

initialization
  HashRegistry.Init;
end.
