相关疑难解决方法(0)

将泛型与不同约束相结合

我有这个原子乐观的初始化类:

type
  Atomic<T: IInterface> = class
    type TFactory = reference to function: T;
    class function Initialize(var storage: T; factory: TFactory): T;
  end;

class function Atomic<T>.Initialize(var storage: T; factory: TFactory): T;
var
  tmpIntf: T;
begin
  if not assigned(storage) then begin
    tmpIntf := factory();
    if InterlockedCompareExchangePointer(PPointer(@storage)^, PPointer(@tmpIntf)^, nil) = nil then
      PPointer(@tmpIntf)^ := nil;
  end;
  Result := storage;
end;
Run Code Online (Sandbox Code Playgroud)

现在我想为对象实现相同的模式.

type
  Atomic<T: class> = class
    type TFactory = reference to function: T;
    class function Initialize(var storage: T; factory: TFactory): T;
  end;

class …
Run Code Online (Sandbox Code Playgroud)

delphi generics

6
推荐指数
1
解决办法
416
查看次数

标签 统计

delphi ×1

generics ×1