I pass an anonymous method to an external function. An anonymous method is an integrand, and an external function will calculate a certain integral. Since the integration function is external, it does not understand anonymous methods. Therefore, I need to pass the anonymous method as an untyped pointer. To make this clearer, it works as follows:
function ExternalIntegrand(data: Pointer; x: Double): Double; cdecl;
begin
Result := GetAnonMethod(data)(x);
end;
....
var
Integrand: TFunc<Double,Double>;
Integral: Double;
....
Integral := CalcIntegral(ExternalIntegrand, CastToPointer(Integrand), xlow, xhigh);
Here CalcIntegralis the external function that calls ExternalIntegrand. This, in turn, takes an untyped pointer, which is passed, retrieves an anonymous method, and gets it to do the job.
The problem is that I cannot write CastToPointercleanly. If I do this:
Pointer(Integrand)
compiler objects:
[dcc32 Error]: E2035 Not enough actual parameters
, .
:
function CastToPointer(const F: TFunc<Double,Double>): Pointer; inline;
begin
Move(F, Result, SizeOf(Result));
end;
:
function CastToPointer(const F: TFunc<Double,Double>): Pointer; inline;
var
P: Pointer absolute F;
begin
Result := P;
end;
, , , .
, , . :
function ExternalIntegrand(data: Pointer; x: Double): Double; cdecl;
var
F: ^TFunc<Double,Double>;
begin
F := data;
Result := F^(x);
end;
....
Integral := CalcIntegral(ExternalIntegrand, @Integrand, xlow, xhigh);
, , .
- ? , skullduggery , , , , , .