我使用的是JCL的表达式计算器TeEvaluator(barry kelly捐赠的一个了不起的作品)。(谢谢你,巴里!)
出身背景
function MyFunc:double;
begin
// calculations here
Result:=1;
end;
可以使用AddFunc方法使函数可用:
AddFunc('MyFunc', MyFunc);
问题是。。。
我需要调用对象上的方法,而不是独立例程。
原因是我有一个提供值的对象列表。
假设我们有一个车辆对象列表。每个对象都有一个权重函数。我希望能够在公式中使用每个对象的权重。
一个愚蠢的例子,但很容易解释:
type
TVehicle=class
private
public
function Weight:double;
end;
function StrangeCalculation:double;
var
vehicle:TVehicle;
begin
for iVehicle = 0 to Count - 1 do
begin
vehicle:=GetVehicle(iVehicle);
// E2250 There is no overloaded version of 'AddFunc' that can be called with these arguments
eval.AddFunc(vehicle.Name, vehicle.Weight);
end;
Result:=eval.Evaluate('JeepTJWeight + FordF150Weight * 2');
end;
我的选择:
-
-
带有独立函数的AddFunc()。无法执行此操作,因为变量的名称(和数量)在运行时之前是未知的。
-
如果找不到变量,请修改对象以添加回调。我实际上已经这样做了,但需要编辑源代码的副本,以便回调,使其能够这样做。
-
制作一个能够使用方法函数的AddFunc()。
TFloat64MethodFunc = function(c:pointer): TFloat64;
procedure TEasyEvaluator.AddFunc(const AName: string; AFunc: TFloat64MethodFunc);
begin
FOwnContext.Add(TExprFloat64MethodFuncSym.Create(AName, AFunc));
end;
TExprFloat64MethodFuncSym = class(TExprAbstractFuncSym)
private
FFunc: TFloat64MethodFunc;
public
constructor Create(const AIdent: string; AFunc: TFloat64MethodFunc);
function Evaluate: TFloat; override;
// not using function Compile: TExprNode; override;
end;
谢谢你的帮助!
议员