Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call an overloaded function based on enum typeinfo?

I want to draw some themes parts to several TImages. In my code below, GetElementDetails expects a certain enum value. I have the PTypeInfo for the enum type, but I don't know how to type-cast i to the enum type.

procedure TForm1.Button1Click(Sender: TObject);
  procedure drawType(c: tcanvas; ti: ptypeinfo);
  var
    r: trect;
    i: integer;
    details: TThemedElementDetails;
  begin
    r.Left := 0; r.Top := 0; r.Right := 19; r.Bottom := 19;
    for i := GetTypeData(ti).MinValue to GetTypeData(ti).MaxValue do begin
      // How to cast i to the enum type of ti?
      details := StyleServices.GetElementDetails( ???(i) );

      StyleServices.DrawElement(c.Handle, details, R);
      if (i mod 10 = 0) and (i > 0) then begin
        r.Left := 0; r.Right := 19; r.Top := r.Bottom + 3; r.Bottom := r.Bottom + 22;
      end
      else r.Inflate(-22,0,22,0);
    end;
  end;
begin
  drawType(image1.canvas, typeinfo(TThemedToolBar));
  drawType(image2.canvas, typeinfo(TThemedButton));
  drawType(image3.canvas, typeinfo(TThemedCategoryPanelGroup));
  drawType(image4.canvas, typeinfo(TThemedComboBox));
end;

I need to cast i to the type passed as second variable (TThemedToolBar, TThemedButton, etc.). How can I solve this?

like image 903
Kiril Hadjiev Avatar asked Aug 06 '14 10:08

Kiril Hadjiev


1 Answers

try this one:

type
   TAbstractStyleServicesFunction = reference to function(const styleServices: TAbstractStyleServices; const I: Integer)
      : TThemedElementDetails;


procedure TForm1.drawType(c: TCanvas; ti: PTypeInfo; const styleServicesFunction: TAbstractStyleServicesFunction);
var
   r: trect;
   I: Integer;
begin
   r.Left := 0;
   r.Top := 0;
   r.Right := 19;
   r.Bottom := 19;
   for I := GetTypeData(ti).MinValue to GetTypeData(ti).MaxValue do
   begin
      styleServices.DrawElement(c.Handle, styleServicesFunction(styleServices, I), r);

      if (I mod 10 = 0) and (I > 0) then
      begin
         r.Left := 0;
         r.Right := 19;
         r.Top := r.Bottom + 3;
         r.Bottom := r.Bottom + 22;
      end
      else
         r.Inflate(-22, 0, 22, 0);
   end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
   drawType(image1.canvas, typeinfo(TThemedToolBar),
      function(const styleServices: TAbstractStyleServices; const I: Integer): TThemedElementDetails
      begin
         Result := styleServices.GetElementDetails(TThemedToolBar(I));
      end);
end;
like image 134
Passella Avatar answered Oct 21 '22 08:10

Passella