) Another implementation of Smart pointers-Smart pointers in Delphi

Source: Internet
Author: User
Tags class operator

Stronugly-typed smart pointers are now possible in Delphi, leveraging the work on generics. Here's a simple smart pointer type:

TSmartPointer <T: class> = record
Strict private
FValue: T;
FLifetime: IInterface;
Public
Constructor Create (const AValue: T); overload;
Class operator Implicit (const AValue: T): TSmartPointer <T>;
Property Value: T read FValue;
End;

Here it is in action, where TLifetimeWatcher is a little class that executes some code when it dies:

Procedure UseIt;
Var
X: TSmartPointer <TLifetimeWatcher>;
Begin
X: = TLifetimeWatcher. Create (procedure
Begin
Writeln ('I died .');
End );
End;

Here's the full project code that defines TSmartPointer <>, TLifetimeWatcher, and the above test routine:

{$ Apptype console}

Uses
SysUtils;

Type
TLifetimeWatcher = class (TInterfacedObject)
Private
FWhenDone: TProc;
Public
Constructor Create (const AWhenDone: TProc );
Destructor Destroy; override;
End;

{TLifetimeWatcher}

Constructor TLifetimeWatcher. Create (const AWhenDone: TProc );
Begin
FWhenDone: = AWhenDone;
End;

Destructor TLifetimeWatcher. Destroy;
Begin
If Assigned (FWhenDone) then
FWhenDone;
Inherited;
End;

Type
TSmartPointer <T: class> = record
Strict private
FValue: T;
FLifetime: IInterface;
Public
Constructor Create (const AValue: T); overload;
Class operator Implicit (const AValue: T): TSmartPointer <T>;
Property Value: T read FValue;
End;

{TSmartPointer <T>}

Constructor TSmartPointer <T>. Create (const AValue: T );
Begin
FValue: = AValue;
FLifetime: = TLifetimeWatcher. Create (procedure
Begin
AValue. Free;
End );
End;

Class operator TSmartPointer <T>. Implicit (const AValue: T): TSmartPointer <T>;
Begin
Result: = TSmartPointer <T>. Create (AValue );
End;

Procedure UseIt;
Var
X: TSmartPointer <TLifetimeWatcher>;
Begin
X: = TLifetimeWatcher. Create (procedure
Begin
Writeln ('I died .');
End );
End;

Begin
Try
UseIt;
Readln;
Except
On E: Exception do
Writeln (E. Classname, ':', E. Message );
End;
End.

 

Update:Fixed capture-was capturing a location in the structure, which of course will be freely copied etc.

 

Original post address: http://barrkel.blogspot.com/2008/09/smart-pointers-in-delphi.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.