Delphi XE7智能指针

Big*_*ion 6 delphi smart-pointers delphi-xe7

我是Delphi的新手,拥有C++背景,并试图弄清楚如何实现智能指针.我遇到了以下帖子,我试图将其作为我自己的起点:Delphi - 智能指针和泛型TList

但是我无法使用Delphi XE7编译以前的代码(编译器错误在代码中显示为注释).如果有人真正解释了代码的逻辑,我也会非常感激(最初我想将该类用作实用程序类中的一个drop,但现在我想了解实际发生的情况).我隐约明白,因为智能指针实现继承自TInterfacedObject,所以它是引用计数但超出此范围的任何东西对我来说都没有意义:)

unit SmartPointer;

interface

uses
  SysUtils, System.Generics.Collections;

type
  ISmartPointer<T> = reference to function: T;

  // complains ISmartPointer<T> expecting an interface type
  TSmartPointer<T: class, constructor> = class(TInterfacedObject,ISmartPointer<T>)
  private
    FValue: T;
  public
    constructor Create; overload;
    constructor Create(AValue: T); overload;
    destructor Destroy; override;
    function Invoke: T;
  end;

implementation

{ TSmartPointer<T> }

constructor TSmartPointer<T>.Create;
begin
  inherited;
  FValue := T.Create;
end;

// complains: overload procedure TSmartPointer.Create must be marked with the overload directive
constructor TSmartPointer<T>.Create(AValue: T);
begin
  inherited Create;
  if AValue = nil then
    FValue := T.Create
  else
    FValue := AValue;
end;

destructor TSmartPointer<T>.Destroy;
begin
  FValue.Free;
  inherited;
end;

function TSmartPointer<T>.Invoke: T;
begin
  Result := FValue;
end;

end.
Run Code Online (Sandbox Code Playgroud)

试图使用以前的智能指针与以下测试代码导致编译器错误...我错过了什么?

program TestSmartPointer;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, SmartPointer;

type
TPerson = class
  private
    _name : string;
    _age : integer;
  public

    property Name: string read _name write _name;
    property Age: integer read _age write _age;
  end;

var
  pperson : TSmartPointer<TPerson>;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    pperson := TSmartPointer<TPerson>.Create();
    // error on next line: undeclared Identifier: Name
    pperson.Name := 'John Doe';
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
Run Code Online (Sandbox Code Playgroud)

Dal*_*kar 6

您必须将引用变量声明为ISmartPointer<TPerson>:

var
  pperson : ISmartPointer<TPerson>;
Run Code Online (Sandbox Code Playgroud)

下面的代码也会编译,但在这种情况下它不会自动释放内存,因为当您将引用计数的对象实例存储到对象引用中时,您会搞乱其引用计数机制.根据代码,它可能导致内存泄漏或底层对象实例的过早破坏.

var
  pperson : TSmartPointer<TPerson>;

begin
  pperson := TSmartPointer<TPerson>.Create();
  pperson.Invoke.Name := 'John Doe';
Run Code Online (Sandbox Code Playgroud)

最后,以下代码说明了正确的智能指针用法:

var
  pperson : ISmartPointer<TPerson>;   // note pperson is ISmartPointer<TPerson>

begin
  pperson := TSmartPointer<TPerson>.Create();
  pperson.Name := 'John Doe';
Run Code Online (Sandbox Code Playgroud)

一些界面基础知识

接口定义了一个契约 - 实现接口的类必须具有的功能,而不提供特定的实现.该IFoo接口的声明意味着,当你有参照IFoo,你可以调用Foo上的参考方法,但仅此而已,你可以.

IFoo = interface
  procedure Foo;
end;
Run Code Online (Sandbox Code Playgroud)

当类实现接口时,它必须实现该接口的所有方法.方法FooIFoo将映射到方法FooTFooTOtherFoo.特定接口的实现可以在不同的类中不同.

TFoo = class(TInterfacedObject, IFoo)
public
  procedure Foo;
  procedure Bar;
end;

TOtherFoo = class(TInterfacedObject, IFoo)
public
  procedure Foo;
end;

procedure TFoo.Bar;
begin
  writeln('Bar');
end;

procedure TFoo.Foo;
begin
  writeln('Foo');
end;

procedure TOtherFoo.Foo;
begin
  writeln('Other Foo');
end;

var
  foo: IFoo;
  f: TFoo;

  foo := TFoo.Create;
  foo.Foo; // Output -> Foo

  // Compiler error -> foo is interface reference and only knows Foo from TFoo
  foo.Bar;

  foo := TOtherFoo.Create;
  foo.Foo; // Output -> Other Foo

  // Mixing object reference with reference counted object instance -> memory leaks
  f := TFoo.Create;
  foo.Foo; // output -> Foo
  foo.Bar; // f is TFoo object reference, and it knows everything from TFoo
Run Code Online (Sandbox Code Playgroud)

智能指针实际上是如何工作的

ISmartPointer<T> 被声明为匿名函数.

ISmartPointer<T> = reference to function: T;
Run Code Online (Sandbox Code Playgroud)

上面的声明相当于与Invoke函数的接口

ISmartPointer<T> = interface
  function Invoke: T;
end;
Run Code Online (Sandbox Code Playgroud)

两者之间的区别(我们感兴趣的是),使用匿名函数/方法,您不必显式调用Invoke; 编译器会为你做这件事.

由于ISmartPointer<T>是一个匿名函数,它实际上是TSmartPointer<T>类声明中的接口,因此Invoke将方法映射到ISmartPointer<T>.

  TSmartPointer<T: class, constructor> = class(TInterfacedObject, ISmartPointer<T>)
  private
    FValue: T;
  public
    constructor Create; overload;
    constructor Create(AValue: T); overload;
    destructor Destroy; override;
    function Invoke: T;
  end;

var
  pperson : ISmartPointer<TPerson>;
Run Code Online (Sandbox Code Playgroud)

因此,当您pperson.Name在窗帘后面写入转换为pperson.Invoke函数调用时,该函数调用返回一个TPerson实例,FValueTPerson具有Name编译器可以识别的属性.

由于TSmartPointer<T>是引用计数类,因此当您使用ISmartPointer<T>引用时,底层TSmartPointer<T>对象实例及其T包含的实例FValue将在ISmartPointer<T>引用超出范围时自动释放,或者您将其设置为nil代码.