Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Callback (https://www.delphipraxis.net/106725-callback.html)

Sämy 15. Jan 2008 15:49


Callback
 
Hallo

Mein Problem ist, dass ich probiere, Callback-funktionen in mein Programm ein zu bauen. Dies mach ich so:

Delphi-Quellcode:
TForEachBillpos = procedure(billpos: IInicBillpos) of object;
TForEachBillposProduct = procedure(billposProduct: IInicBillposProduct) of object;

procedure forEachBillpos(billing: IInicBilling; proc: TForEachBillpos);
procedure forEachBillposProduct(billing: IInicBilling; proc: TForEachBillposProduct); overload;
procedure forEachBillposProduct(billpos: IInicBillpos; proc: TForEachBillposProduct); overload;


function _getForEachBillposMethod(self: TObject; proc: Pointer): TForEachBillpos;
function _getForEachBillposProductMethod(self: TObject; proc: Pointer): TForEachBillposProduct;

[...]

procedure forEachBillpos(billing: IInicBilling; proc: TForEachBillpos);
var
  it: IJclIntfIterator;
begin
  it := billing.Billposes.Values.First;
  while it.HasNext do
    proc(it.Next as IInicBillpos); // <----- OK
end;

procedure forEachBillposProduct(billing: IInicBilling; proc: TForEachBillposProduct);
  procedure forEach(Self: pointer; billpos: IInicBillpos);
  begin
    forEachBillposProduct(billpos, proc);
  end;
begin
  forEachBillpos(billing, _getForEachBillposMethod(nil, @forEach));
end;

procedure forEachBillposProduct(billpos: IInicBillpos; proc: TForEachBillposProduct);
var
  it: IJclIntfIterator;
begin
  it := billpos.BillposProducts.Values.First;
  while it.HasNext do
    proc(it.Next as IInicBillposProduct); // <----- ACCESS VIOLATION
end;


function _getForEachBillposMethod(self: TObject; proc: Pointer): TForEachBillpos;
begin
  TMethod(Result).Code := proc;
  TMethod(Result).Data := self;
end;
function _getForEachBillposProductMethod(self: TObject; proc: Pointer): TForEachBillposProduct;
begin
  TMethod(Result).Code := proc;
  TMethod(Result).Data := self;
end;
Dieses Codestück rufe ich folgendermassen auf:

Delphi-Quellcode:
function hasBillingProduct(billing: IInicBilling; productId: integer): boolean;
  procedure forEach(Self: pointer; billposProduct: IInicBillposProduct);
  begin
    Result := Result or (billposProduct.Product.Pid = productId);
  end;
begin
  forEachBillposProduct(billing, _getForEachBillposProductMethod(nil, @forEach));
end;
Mein Problem ist nun, dass der bei der markierten Stelle eine access violation auftritt, wobei die mit "OK" kommentierte Stelle einwandfrei funktioniert.
Wisst ihr, an was dies liegen könnte, respk. wie man das Callback-prinzip sonst wie lösen könnte?

Besten Dank im Voraus

shmia 15. Jan 2008 16:55

Re: Callback
 
Es gibt Callback-Funktionen und Callback-Methoden.
In deinem Fall handelt es sich um Methoden, da du mit der Klausel "of object;" deklariert hast.
Wenn ich mir folgendes Codestück anschaue:
Delphi-Quellcode:
function hasBillingProduct(billing: IInicBilling; productId: integer): boolean;
  procedure forEach(Self: pointer; billposProduct: IInicBillposProduct);
  begin
    Result := Result or (billposProduct.Product.Pid = productId);
  end;
begin
  forEachBillposProduct(billing, _getForEachBillposProductMethod(nil, @forEach));
end;
dann fallen 2 Dinge auf:
* der self Zeiger ist auf nil gesetzt - das sollte bei Methodenzeigern nicht sein
(du greifst zwar später nicht auf self zu, aber es macht schon ein ungutes Gefühl)
* procedure forEach ist eine geschachtelte Prozedur. Diese darf eigentlich nur im Kontext der umliegenden
funktion/prozedur hasBillingProduct ausgeführt werden.
Durch deine Tricks hat die procedure forEach ein ganz anderes Stackumfeld, was wohl Probleme verursachen kann.

Sämy 16. Jan 2008 07:55

Re: Callback
 
Ich habe die ganze Sache mal in eine Klasse gepackt und habe nun als self die instanz der Klasse übergeben. Das Ergebnis ist jedoch das selbe.

Gibt es demnach keine "einfache" Lösung, bei der nicht bei jedem Aufruf ein riesiger Overhead an Definitionen und Zusatzmethoden geschrieben werden muss?


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:37 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz