通用锁定池,将通用添加到非通用 tlist

发布于 2024-09-26 19:12:54 字数 1947 浏览 3 评论 0原文

我尝试了一个通用类,第二次尝试时我尝试制作一个通用锁定池。我差一点就可以工作了 我偶然发现我想将通用类型类放入从 tthreadlist 获取的锁定 tlist 中。

主要问题是:

  • 有人知道这个问题的解决方案吗? (请参阅源代码中的“问题点”)

提示,小问题:

  • 我是否需要一个额外的约束来表示引用? (我尝试添加对已经存在的类和构造函数的引用)
  • 是否知道所有“特殊”通用约束(类、构造函数)的良好概述页面。手册上找不到太多内容。
  • 该公司处于 D2009 年,但我有一个用于迁移准备目的的单一许可证 DXE。

这个池使用的对象是tobject,更糟糕的是,其中一些对象有一些必须内联的关键方法。 (它是一个图像处理应用程序,这也是为什么我不太关心相对简单的锁。粒度很粗糙)。我提到这一点,因为它可能会使基于接口的解决方案变得困难。

type
     TLockedPool<T:class,constructor>   =   class
                                  private
                                    lst : tthreadlist;
                                  public
                                    type sometype =t;  // part of workarounds.
                                    destructor  destroy;
                                    constructor create;
                                    function    getitem:T;
                                    procedure   putitem(var b:T);
                                   end;

 constructor TLockedPool<T>.create;
 begin
  lst:=TThreadlist.Create;
 end;

destructor TLockedPool<T>.destroy;
var i : integer;
    v: tlist;
begin
  v:=lst.locklist;
  for i:=0 to v.count-1 do
    Tobject(v[i]).Free;
  lst.unlocklist;
  v.clear;
  freeandnil(lst);
 inherited;
end;

function TLockedPool<T>.getitem: T;
var cnt:integer;
   v : tlist;
begin
  v:=lst.LockList;
  cnt:=v.Count;
  if cnt>0 then
    begin
      result:=tobject(v[cnt-1]);
      v.delete(cnt-1);
    end
  else
    begin
     result:=T.create;
    end;
  lst.UnlockList;
end;

procedure TLockedPool<T>.putitem(var b: T);
var  v : Tlist;
 x : sometype;
begin
if assigned(b) then // some older parts of the framework are dirty and try to put in NILs.
    begin
      v:=lst.LockList;
      x:=b;
      v.Add(pointer(sometype(x)));  // <--- the problemspot
      lst.unlocklist;
    end;
 b:=nil;
end;

I tried my hand at a generic class, and on a second attempt I've tried to make a generic locked pool. I almost got it to work
I stumble on the spot where I want to put a generic typed class into a locked tlist obtained from tthreadlist.

The main question is:

  • Does anybody know a solution to this problem? (see "problem spot" in the source)

Hints, minor questions:

  • Do I need an additional constraint that signals reference? (I tried adding ,reference to the already existing class and constructor)
  • Does sb know a good overview page of all "special" generic constraints (class,constructor) . Couldn't find much in the manual.
  • the company is at D2009, but I've a single license DXE for migration preparation purposes.

The objects used by this pool are tobject, and worse, some of them have some crucial methods that must be inlined. (it is an image processing app, which is also why I'm not that concerned with relative simply locks. Granularity is coarse). I mention this, since it might make interface based solutions difficult.

type
     TLockedPool<T:class,constructor>   =   class
                                  private
                                    lst : tthreadlist;
                                  public
                                    type sometype =t;  // part of workarounds.
                                    destructor  destroy;
                                    constructor create;
                                    function    getitem:T;
                                    procedure   putitem(var b:T);
                                   end;

 constructor TLockedPool<T>.create;
 begin
  lst:=TThreadlist.Create;
 end;

destructor TLockedPool<T>.destroy;
var i : integer;
    v: tlist;
begin
  v:=lst.locklist;
  for i:=0 to v.count-1 do
    Tobject(v[i]).Free;
  lst.unlocklist;
  v.clear;
  freeandnil(lst);
 inherited;
end;

function TLockedPool<T>.getitem: T;
var cnt:integer;
   v : tlist;
begin
  v:=lst.LockList;
  cnt:=v.Count;
  if cnt>0 then
    begin
      result:=tobject(v[cnt-1]);
      v.delete(cnt-1);
    end
  else
    begin
     result:=T.create;
    end;
  lst.UnlockList;
end;

procedure TLockedPool<T>.putitem(var b: T);
var  v : Tlist;
 x : sometype;
begin
if assigned(b) then // some older parts of the framework are dirty and try to put in NILs.
    begin
      v:=lst.LockList;
      x:=b;
      v.Add(pointer(sometype(x)));  // <--- the problemspot
      lst.unlocklist;
    end;
 b:=nil;
end;

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

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

发布评论

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

评论(1

够运 2024-10-03 19:12:54

使用 v.Add(TObject(x)) 或者,如果你必须的话(它可能在 2009 年不起作用,我很难检查),v.Add(PPointer(@x)^ )

Use v.Add(TObject(x)) or, if you must (it may not work in 2009, awkward for me to check), v.Add(PPointer(@x)^).

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