帮我解决一下 FPC 中虚拟静态场可以解决的问题

发布于 2024-08-22 20:35:38 字数 445 浏览 4 评论 0原文

我正在 Freepascal 中做一个事件管理器
每个事件都是一个对象类型TEvent(=对象),每种事件都必须从该类派生。
事件通过动态分配的整数标识符来区分。
问题是我想检索实例的事件 id,但我做得不好。

  • 类(对象)的所有实例都有一个唯一的 id =>所以它应该是静态字段。
  • 所有类都有不同的 id =>所以它应该是虚拟的。
  • 事件 ID 在运行时分配,并且可以更改 =>所以这不是一个简单的方法

总之,我无法将所有这些放在一起。
我正在寻找一个优雅的解决方案,我不想编写一个硬编码表,在每个构造函数中实现它......等等,我更喜欢利用多态性的东西
任何人都可以帮助我提供其他技术或设计解决方案吗?
我说我不想使用类而不是对象构造。(属性不适用于对象?:(

I'm doing an event manager in Freepascal
Each event is an object type TEvent (=object), each kind of event must derive from this class.
Events are differentiated by an integer identificator, assigned dynamically.
The problem is that i want to retrieve the event id of an instance, and i can't do it well.

  • All instances of a class(object) have a unique id => so it should be static field.
  • All classes have a diferent id => so it should be virtual.
  • Event ids are assignated in run time, and can change => so it can't be a simple method

In sum, I can't put all this together.
I'm looking for an elegant solution, i don't want to write a hardcoded table, actualizing it in every constructor... etc, i'd prefer something taking advantage of the polymorphism
Can anyone help me with another technical or design solution?
I remark I don't want to use class instead of object construct.(property doesn't work on objects? :(

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

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

发布评论

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

评论(2

小瓶盖 2024-08-29 20:35:38

您可能需要类 var 之类的东西,就像在较新的 delphi 中一样。但这仅适用于 FPC 的开发版本(2.5.1+)。

请注意,对象类型是 TP 遗留的,并且在本世纪还没有开发出来,我不希望这种情况发生改变。如果您需要的内容超出了它提供的内容,我建议使用课程。

You might need class var like stuff, like in newer delphi's. But that is in the development version of FPC only (2.5.1+).

Note that the object type is TP legacy, and hasn't been developed on in this millenium, and I don't expect that to change. If you need more than it offers, I suggest to use classes.

无风消散 2024-08-29 20:35:38

你可以像这样制作一个简单的表格/列表:

unit classids;

{$mode objfpc}{$H+}

interface

function GetClassID(c:TClass):Integer;
procedure SetClassID(c:TClass; id:Integer);

property ClassID[c:TClass]:Integer read GetClassID write SetClassID;

implementation
uses Maps;

var Map:TMap;

function GetClassID(c:TClass):Integer;
begin
 if not Map.GetData(c,Result) then
  Result:=0; //Or any default you like
end;

procedure SetClassID(c:TClass; id:Integer);
begin
 Map.Delete(c);
 Map.Add(c,id);
end;

initialization
 Map:=TMap.Create(itu4,SizeOf(Integer));
finalization
 FreeAndNil(Map);
end.

然后你可以获取/设置 id 祝

ClassID[TMyObject]:=12;
ShowMessage(IntToStr(ClassID[TMyObject])); //shows 12

你好运

You can make a simple table/list like this:

unit classids;

{$mode objfpc}{$H+}

interface

function GetClassID(c:TClass):Integer;
procedure SetClassID(c:TClass; id:Integer);

property ClassID[c:TClass]:Integer read GetClassID write SetClassID;

implementation
uses Maps;

var Map:TMap;

function GetClassID(c:TClass):Integer;
begin
 if not Map.GetData(c,Result) then
  Result:=0; //Or any default you like
end;

procedure SetClassID(c:TClass; id:Integer);
begin
 Map.Delete(c);
 Map.Add(c,id);
end;

initialization
 Map:=TMap.Create(itu4,SizeOf(Integer));
finalization
 FreeAndNil(Map);
end.

Then you can get/set the id with

ClassID[TMyObject]:=12;
ShowMessage(IntToStr(ClassID[TMyObject])); //shows 12

Good luck

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