加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 将泛型与不同约束相结合

发布时间:2020-12-15 04:10:29 所属栏目:大数据 来源:网络整理
导读:我有这个原子乐观的初始化类: type AtomicT: IInterface = class type TFactory = reference to function: T; class function Initialize(var storage: T; factory: TFactory): T; end;class function AtomicT.Initialize(var storage: T; factory: TFactor
我有这个原子乐观的初始化类:
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;

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

type
  Atomic<T: class> = 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)^,nil) = nil then
      tmpIntf.Free;
  end;
  Result := storage;
end;

我可以在两个单独的类中完成这两个,但我真的想把两个初始化器放在同一个伞下.我想,我最好还是喜欢这个

var
  o: TObject;
  i: IInterface;

Atomic<TObject>.Initialize(o,CreateObject);
Atomic<IInterface>.Initialize(i,CreateInterface);

我找不到任何好的解决方案.我得到的唯一想法是将类声明为Atomic< T> (没有约束)然后以某种方式(还不知道如何)在运行时检查T的RTTI并相应地继续.

我不太喜欢这个想法,我正在寻找一个更好的方法.

解决方法

您似乎无法指定“类或接口”类型的约束.因此,最简单的解决方案似乎是删除约束(您可以在运行时使用RTTI强制执行它).

对于RTTI方法,您可以使用TypeInfo功能:

uses
  ...,TypInfo;    

class function Atomic<T>.Initialize(var storage: T; factory: TFactory): T;
var
  tmpT: T;
begin
  if not assigned(PPointer(@storage)^) then begin
    tmpT := factory();
    if InterlockedCompareExchangePointer(PPointer(@storage)^,PPointer(@tmpT)^,nil) = nil then begin
      case PTypeInfo(TypeInfo(T))^.Kind of
        tkInterface:
          PPointer(@tmpT)^ := nil;
        tkClass:
          TObject(tmpT).Free;
        else
          raise Exception.Create('Atomic<T>.Initialize: Unsupported type');
      end;
    end;
  end;
  Result := storage;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读