(* Collection of code snippets by Arne Vajhøj *)
(* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) *)
program FPtr2;

uses
  Classes, Contnrs, SysUtils;

Type
  P = class
  public
    procedure M(s : string); virtual; abstract;
  end;
  C1 = class(P)
  public
    procedure M(s : string); override;
  end;
  C2 = class(P)
  public
    procedure M(s : string); override;
  end;
  VFP = procedure(s : string);
  Key2VFP = record
    Key : string;
    F : VFP;
  end;

procedure C1.M(s : string);

begin
  writeln('C1.M : ' + s);
end;

procedure C2.M(s : string);

begin
  writeln('C2.M : ' + s);
end;

procedure M1(s : string);

begin
  writeln('M1 : ' + s);
end;

procedure M2(s : string);

begin
  writeln('M2 : ' + s);
end;

function MFind(a : array of Key2VFP; key : string) : Key2VFP;

label
  fin;

var
  res : Key2VFP;

begin
  for res in a do begin
    if res.Key = key then begin
      MFind := res;
      goto fin;
    end;
  end;
  raise Exception.Create('Ooops');
fin:
end;

var
  map : TFPHashObjectList; (* Lazaruz/FPC - use TObjectDictionary for Delphi *)
  fa : array[0..1] of Key2VFP;

begin
  (* The OO way *)
  map := TFPHashObjectList.Create(true);
  map.Add('#1', C1.Create);
  map.Add('#2', C2.Create);
  P(map.Find('#1')).M('foo');
  P(map.Find('#2')).M('bar');
  map.Free;
  (* The old way *)
  fa[0].Key := '#1';
  fa[0].F := @M1;
  fa[1].Key := '#2';
  fa[1].F := @M2;
  MFind(fa, '#1').F('foo');
  MFind(fa, '#2').F('bar');
end.
