如何将多个不同的记录(由于delphi限制而不是类)传递给函数?

发布于 2024-08-25 00:45:40 字数 786 浏览 15 评论 0原文

由于 Delphi 的限制,我有许多记录无法转换为类(所有记录都使用类运算符来实现比较)。但我必须通过将它们存储在一个不知道我正在使用哪种记录类型的类中。

像这样的事情:

type R1 = record
begin 
  x :Mytype;
  class operator Equal(a,b:R1)
end;

type R2 = record
begin 
  y :Mytype;
  class operator Equal(a,b:R2)
end;

type Rn = record
begin 
  z :Mytype;
  class operator Equal(a,b:Rn)
end;

type TC = class
begin
  x : TObject;
  y : Mytype;
  function payload (n:TObject)
end;

function TC.payload(n:TObject)
begin
  x := n;
end;

program:
  c : TC;
  x : R1;
  y : R2;
  ...
  c := TC.Create():
  n:=TOBject(x);
  c.payload(n);

现在,Delphi 不接受从记录到 TObject 的类型转换,并且由于 Delphi 的限制,我无法将它们创建为类。

任何人都知道如何将不同的记录传递给函数并在需要时识别它们的类型,就像我们对类所做的那样:

if x is TMyClass then TMyClass(x) ... 

???

I have a number of records I cannot convert to classes due to Delphi limitation (all of them uses class operators to implement comparisons). But I have to pass to store them in a class not knowing which record type I'm using.

Something like this:

type R1 = record
begin 
  x :Mytype;
  class operator Equal(a,b:R1)
end;

type R2 = record
begin 
  y :Mytype;
  class operator Equal(a,b:R2)
end;

type Rn = record
begin 
  z :Mytype;
  class operator Equal(a,b:Rn)
end;

type TC = class
begin
  x : TObject;
  y : Mytype;
  function payload (n:TObject)
end;

function TC.payload(n:TObject)
begin
  x := n;
end;

program:
  c : TC;
  x : R1;
  y : R2;
  ...
  c := TC.Create():
  n:=TOBject(x);
  c.payload(n);

Now, Delphi do not accept typecast from record to TObject, and I cannot make them classes due to Delphi limitation.

Anyone knows a way to pass different records to a function and recognize their type when needed, as we do with class:

if x is TMyClass then TMyClass(x) ... 

???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对岸观火 2024-09-01 00:45:40

我认为您不能将不同的记录传递给单个函数,但您可以创建多个重载函数。就像这样:

type TC = class
begin
  x : TObject;
  y : Mytype;
  function payload (aRec : R1); overload;
  function payload (aRec : R2); overload;
end;

这能解决你的问题吗?

I don't think you can pass different records to a single function, but you can make several overloaded functions. Like so:

type TC = class
begin
  x : TObject;
  y : Mytype;
  function payload (aRec : R1); overload;
  function payload (aRec : R2); overload;
end;

Will that solve your problem?

心凉 2024-09-01 00:45:40

可以将它们转换为类,只需使用虚拟的“IsEqual”函数对“Equal”运算符进行反混淆即可。它将简化您的问题并极大地阐明您的代码。

You can convert these to classes, you simply have to de-obfuscate the "Equal" operator with a virtual "IsEqual" function. It will simplify your problem and clarify your code enormously.

∞梦里开花 2024-09-01 00:45:40

记录没有运行时类型信息,这是您检测其类型所需的信息。

除了重载之外,你还可以传递类型,你可以这样做:

procedure Payload(aType : longint; data : Pointer);
var
  pr1 : ^R1;
  pr2 : ^R2;
  prN : ^RN;
begin
  case aType of
    0 : pr1 := data; // might require a cast
    1 : pr2 := data;
    2 : prN := data;
  end;
end;
Payload(0,@r1);

或者你可以这样做,但鸽子会哭泣:

procedure Payload(aType : longint; var data);
var
  r1 : R1 absolute data;
  r2 : R2 absolute data;
  rN : RN absolute data;
begin
  // just be sure to check type before using the variable
end;
Payload(0,r1);

Records don't have Run Time Type Information, which is what you'd need to detect their types.

Besides overloads, you can also pass the type, you can do this:

procedure Payload(aType : longint; data : Pointer);
var
  pr1 : ^R1;
  pr2 : ^R2;
  prN : ^RN;
begin
  case aType of
    0 : pr1 := data; // might require a cast
    1 : pr2 := data;
    2 : prN := data;
  end;
end;
Payload(0,@r1);

or you could do this, but doves will cry:

procedure Payload(aType : longint; var data);
var
  r1 : R1 absolute data;
  r2 : R2 absolute data;
  rN : RN absolute data;
begin
  // just be sure to check type before using the variable
end;
Payload(0,r1);
|煩躁 2024-09-01 00:45:40

如果您使用的是 Delphi 2010,您可能可以传递指向这些记录的指针,并使用运行时类型信息来检测实际传递的是哪一个。但这将是非常难看的代码,而且可能也很慢。

转换为类并使用函数调用而不是运算符重载会更具可读性。

If you are using Delphi 2010, you could probably pass around pointers to these records and use runtime type information to detect which one was actually passed. But this would be very ugly code and probably quite slow as well.

Converting to classes and using function calls rather than operator overloading would be much more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文