{"id":385943,"date":"2024-06-29T06:25:15","date_gmt":"2024-06-29T06:25:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=385943"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T06:25:15","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=385943","title":{"rendered":"<span>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Smart Pointers \u0432 Delphi, \u0438 \u0435\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Delphi \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u043c\u043d\u043e\u0433\u043e \u043d\u043e\u0432\u044b\u0445, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439. \u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0441 \u0438\u0445 \u043f\u043e\u043c\u043e\u0449\u044c\u044e, \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043b\u0430\u0434\u0435\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c, \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430 \u043d\u0435\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442 \u0441\u0441\u044b\u043b\u043e\u043a.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u0432\u0443\u0445 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440, \u043f\u0435\u0440\u0432\u0430\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f, \u0432\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b. \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e.<\/p>\n<pre><code class=\"delphi\">procedure ClassicVersion; var   Reader, Writer: TFileStream; begin   Reader := TFileStream.Create('C:\\hiberfil.sys', fmOpenRead);   try     Writer := TFileStream.Create('D:\\dummy.sys', fmCreate);     try       \/\/ \u0414\u0435\u043b\u0430\u0435\u043c \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435     finally       Writer.Free;     end;   finally     Reader.Free;   end; end;  procedure ARCVersion; var   Reader, Writer: AutoRef&lt;TFileStream>; begin   Reader := Ref&lt;TFileStream>(TFileStream.Create('C:\\hiberfil.sys', fmOpenRead));   Writer := Ref&lt;TFileStream>(TFileStream.Create('D:\\dummy.sys', fmCreate));   \/\/ \u0414\u0435\u043b\u0430\u0435\u043c \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 end;<\/code><\/pre>\n<p>\u0412 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0435 ClassicVersion, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u0441 \u043f\u043e\u0440\u0442\u044f\u043d\u043a\u043e\u0439 \u0438\u0437 try .. finally \u0434\u043b\u044f \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432. \u0412\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 (\u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432) \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0420\u0430\u0437\u043d\u0438\u0446\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430, \u0434\u0432\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043a\u043e\u0434\u0430, \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u0435\u0432\u044f\u0442\u0438. \u041a\u043e\u0434 \u043a\u043e\u0440\u043e\u0447\u0435 \u0438 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0435\u0435. \u042f \u0434\u0443\u043c\u0430\u044e \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 Delphi \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0438\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<pre><code class=\"delphi\">type   AutoRef&lt;T: class> = reference to function: T;<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e &#8212; \u0442\u0430\u043a \u0438 \u0435\u0441\u0442\u044c. \u0421\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 &#8212; \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432 Delphi, \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0441\u044b\u043b\u043a\u0430, \u043d\u043e \u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441! \u0414\u043b\u044f \u043c\u0435\u043d\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u0442\u043e \u0431\u044b\u043b\u0430 \u043d\u043e\u0432\u043e\u0441\u0442\u044c. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0437\u043d\u0430\u0447\u0438\u0442: \u043e\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 Automatic Reference Counting (ARC) \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442:<\/p>\n<pre><code class=\"delphi\">TAutoRef = class(TInterfacedObject, AutoRef&lt;T>) protected   function Invoke: T; end;<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 Invoke \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0443 \u043d\u0430\u0448\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 &#8212; \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442! \u041f\u043e \u0441\u0443\u0442\u0438 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c:<\/p>\n<pre><code>TAutoRef = class(TInterfacedObject, AutoRef&lt;T>) private   FValue: T; protected   function  Invoke: T; public   constructor Create(const Value: T);   destructor Destroy; override; end;<\/code><\/pre>\n<p>\u0418 \u0442\u0430\u043a, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0442.\u0435. \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430\u0448 \u043e\u0431\u044a\u0435\u043a\u0442, \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u0435\u0433\u043e \u0447\u043b\u0435\u043d\u0430\u043c. \u0418 \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 AutoRef&lt;T>, \u0442.\u0435. \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 ARC, \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 &lt;T>.<\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0430\u0445\u0430\u0440\u0430, \u0441 \u044f\u0432\u043d\u044b\u043c \u0438 \u043d\u0435 \u044f\u0432\u043d\u044b\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043d\u0430\u0448\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430:<\/p>\n<pre><code class=\"delphi\">Ref&lt;T: class> = record private type   TAutoRef = class(TInterfacedObject, AutoRef&lt;T>)   private     FValue: T;   protected     function  Invoke: T;   public     constructor Create(const Value: T);     destructor Destroy; override;   end; private   FValue: T; public   class function Create(const Value: T): AutoRef&lt;T>; static; inline;   class operator Implicit(const Value: Ref&lt;T>): AutoRef&lt;T>; static; inline;   class operator Explicit(const Value: T): Ref&lt;T>; static; inline; end;<\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u044d\u0442\u0438\u0445 \u0442\u0438\u043f\u043e\u0432, \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430, \u043d\u043e \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 Unit&#8217;\u0430 \u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439. \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u044f\u0432\u043d\u043e\u0433\u043e \u0438 \u043d\u0435\u044f\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432, \u043e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043a\u043e\u0434, \u043a \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435: <code>MyRef := Ref&lt;TObject>(MyObj);<\/code> \u0430 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e Create.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 (\u043d\u0443 \u0438\u043b\u0438 \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a), \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u043c\u044b \u043d\u0430\u0448\u0435\u043c\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c nil. \u041a\u043e\u0433\u0434\u0430 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442, \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d.<\/p>\n<p>\u041c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 \u0431\u043e\u043d\u0443\u0441, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0438\u043f DeferredRef, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u043e\u0442 \u0436\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0435\u0433\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u043d\u0435\u043c\u0443, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u0435\u043c\u0443 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u0442-\u0436\u0435 AutoRef&lt;T>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<pre><code class=\"delphi\">DeferredRef&lt;T: class> = record private type   TDeferredRef = class(TInterfacedObject, AutoRef&lt;T>)   private     FCreator: AutoRef&lt;T>;     FValue: T;   protected     function  Invoke: T;   public     constructor Create(const Creator: AutoRef&lt;T>);     destructor Destroy; override;   end; private   FCreator: AutoRef&lt;T>; public   class function Create(const Creator: AutoRef&lt;T>): AutoRef&lt;T>; static; inline;   class operator Implicit(const Value: DefferedRef&lt;T>): AutoRef&lt;T>; static; inline;   class operator Explicit(const Value: AutoRef&lt;T>): DefferedRef&lt;T>; static; inline; end;<\/code><\/pre>\n<p>\u0427\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438, \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435, \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0435 \u0447\u0430\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e, \u043d\u0443 \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043c\u043d\u0435. \u041d\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438\u0441\u044c \u043f\u0440\u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e Thread Safe.<\/p>\n<p>\u041a\u0430\u043a \u043e\u0431\u0435\u0449\u0430\u043b, \u043f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442:<\/p>\n<pre><code class=\"delphi\">unit Primitives;  interface  uses   System.Types, System.SysUtils;  type   \/\/\/&lt;summary>   \/\/\/  Because of Delphi realisation of anonymous methods,   \/\/\/  this type can be treated not only as Delegate,   \/\/\/  but also as interface with Invoke method that returns generics type &lt;T>   \/\/\/&lt;\/summary>   AutoRef&lt;T: class> = reference to function: T;    Shared = class   private     class var FLock: TObject;   protected     class constructor Create;     class destructor Destroy;   public     class procedure Initialize&lt;T: class>(var Value: T; const Initializer: AutoRef&lt;T>); static; inline;   end;    \/\/\/&lt;summary>   \/\/\/  This type realizes creating ARC owner of object instance type that freed   \/\/\/  owned object when out of scope (reference count = 0)   \/\/\/&lt;\/summary>   Ref&lt;T: class> = record   private type     TAutoRef = class(TInterfacedObject, AutoRef&lt;T>)     private       FValue: T;     protected       function  Invoke: T;     public       constructor Create(const Value: T);       destructor Destroy; override;     end;   private     FValue: T;   public     class function Create(const Value: T): AutoRef&lt;T>; static; inline;     class operator Implicit(const Value: Ref&lt;T>): AutoRef&lt;T>; static; inline;     class operator Explicit(const Value: T): Ref&lt;T>; static; inline;   end;    \/\/\/&lt;summary>   \/\/\/  This type same as Ref&lt;T>, but with deferred creation of owned object.   \/\/\/  Also, this type is thread safe.   \/\/\/&lt;\/summary>   DeferredRef&lt;T: class> = record   private type     TDeferredRef = class(TInterfacedObject, AutoRef&lt;T>)     private       FCreator: AutoRef&lt;T>;       FValue: T;     protected       function  Invoke: T;     public       constructor Create(const Creator: AutoRef&lt;T>);       destructor Destroy; override;     end;   private     FCreator: AutoRef&lt;T>;   public     class function Create(const Creator: AutoRef&lt;T>): AutoRef&lt;T>; static; inline;     class operator Implicit(const Value: DeferredRef&lt;T>): AutoRef&lt;T>; static; inline;     class operator Explicit(const Value: AutoRef&lt;T>): DeferredRef&lt;T>; static; inline;   end;  implementation  { Shared }  class constructor Shared.Create; begin   FLock := TObject.Create; end;  class destructor Shared.Destroy; begin   FreeAndNil(FLock); end;  class procedure Shared.Initialize&lt;T>(var Value: T; const Initializer: AutoRef&lt;T>); begin   if not Assigned(Value) then   begin     System.TMonitor.Enter(FLock);     try       if not Assigned(Value) then         Value := Initializer();     finally       System.TMonitor.Exit(FLock);     end;   end; end;  { Ref&lt;T>.TAutoRef }  constructor Ref&lt;T>.TAutoRef.Create(const Value: T); begin   FValue := Value; end;  destructor Ref&lt;T>.TAutoRef.Destroy; begin   FreeAndNil(FValue); end;  function Ref&lt;T>.TAutoRef.Invoke: T; begin   Result := FValue; end;  { Ref&lt;T> }  class function Ref&lt;T>.Create(const Value: T): AutoRef&lt;T>; begin   Result := TAutoRef.Create(Value); end;  class operator Ref&lt;T>.Implicit(const Value: Ref&lt;T>): AutoRef&lt;T>; begin   Result := TAutoRef.Create(Value.FValue); end;  class operator Ref&lt;T>.Explicit(const Value: T): Ref&lt;T>; begin   Result.FValue := Value; end;  { DeferredRef&lt;T> }  class function DeferredRef&lt;T>.Create(const Creator: AutoRef&lt;T>): AutoRef&lt;T>; begin   Result := TDeferredRef.Create(Creator); end;  class operator DeferredRef&lt;T>.Explicit(const Value: AutoRef&lt;T>): DeferredRef&lt;T>; begin   Result.FCreator := Value; end;  class operator DeferredRef&lt;T>.Implicit(const Value: DeferredRef&lt;T>): AutoRef&lt;T>; begin   Result := TDeferredRef.Create(Value.FCreator); end;  { DeferredRef&lt;T>.TDeferredRef }  constructor DeferredRef&lt;T>.TDeferredRef.Create(const Creator: AutoRef&lt;T>); begin   FCreator := Creator;   FValue := nil; end;  destructor DeferredRef&lt;T>.TDeferredRef.Destroy; begin   FreeAndNil(FValue); end;  function DeferredRef&lt;T>.TDeferredRef.Invoke: T; begin   Shared.Initialize&lt;T>(FValue, FCreator);   Result := FValue; end;<\/code><\/pre>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043a\u043e\u043c\u0443 \u0442\u043e \u044d\u0442\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u043e.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/587240\/\"> https:\/\/habr.com\/ru\/articles\/587240\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 Delphi \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u043c\u043d\u043e\u0433\u043e \u043d\u043e\u0432\u044b\u0445, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439. \u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0441 \u0438\u0445 \u043f\u043e\u043c\u043e\u0449\u044c\u044e, \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043b\u0430\u0434\u0435\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c, \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430 \u043d\u0435\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442 \u0441\u0441\u044b\u043b\u043e\u043a.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u0432\u0443\u0445 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440, \u043f\u0435\u0440\u0432\u0430\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f, \u0432\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b. \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e.<\/p>\n<pre><code class=\"delphi\">procedure ClassicVersion; var   Reader, Writer: TFileStream; begin   Reader := TFileStream.Create('C:\\hiberfil.sys', fmOpenRead);   try     Writer := TFileStream.Create('D:\\dummy.sys', fmCreate);     try       \/\/ \u0414\u0435\u043b\u0430\u0435\u043c \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435     finally       Writer.Free;     end;   finally     Reader.Free;   end; end;  procedure ARCVersion; var   Reader, Writer: AutoRef&lt;TFileStream>; begin   Reader := Ref&lt;TFileStream>(TFileStream.Create('C:\\hiberfil.sys', fmOpenRead));   Writer := Ref&lt;TFileStream>(TFileStream.Create('D:\\dummy.sys', fmCreate));   \/\/ \u0414\u0435\u043b\u0430\u0435\u043c \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 end;<\/code><\/pre>\n<p>\u0412 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0435 ClassicVersion, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u0441 \u043f\u043e\u0440\u0442\u044f\u043d\u043a\u043e\u0439 \u0438\u0437 try .. finally \u0434\u043b\u044f \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432. \u0412\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 (\u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432) \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0420\u0430\u0437\u043d\u0438\u0446\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430, \u0434\u0432\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043a\u043e\u0434\u0430, \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u0435\u0432\u044f\u0442\u0438. \u041a\u043e\u0434 \u043a\u043e\u0440\u043e\u0447\u0435 \u0438 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0435\u0435. \u042f \u0434\u0443\u043c\u0430\u044e \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 Delphi \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0438\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<pre><code class=\"delphi\">type   AutoRef&lt;T: class> = reference to function: T;<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e &#8212; \u0442\u0430\u043a \u0438 \u0435\u0441\u0442\u044c. \u0421\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 &#8212; \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432 Delphi, \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0441\u044b\u043b\u043a\u0430, \u043d\u043e \u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441! \u0414\u043b\u044f \u043c\u0435\u043d\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u0442\u043e \u0431\u044b\u043b\u0430 \u043d\u043e\u0432\u043e\u0441\u0442\u044c. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0437\u043d\u0430\u0447\u0438\u0442: \u043e\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 Automatic Reference Counting (ARC) \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442:<\/p>\n<pre><code class=\"delphi\">TAutoRef = class(TInterfacedObject, AutoRef&lt;T>) protected   function Invoke: T; end;<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 Invoke \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0443 \u043d\u0430\u0448\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 &#8212; \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442! \u041f\u043e \u0441\u0443\u0442\u0438 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c:<\/p>\n<pre><code>TAutoRef = class(TInterfacedObject, AutoRef&lt;T>) private   FValue: T; protected   function  Invoke: T; public   constructor Create(const Value: T);   destructor Destroy; override; end;<\/code><\/pre>\n<p>\u0418 \u0442\u0430\u043a, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0442.\u0435. \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430\u0448 \u043e\u0431\u044a\u0435\u043a\u0442, \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u0435\u0433\u043e \u0447\u043b\u0435\u043d\u0430\u043c. \u0418 \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 AutoRef&lt;T>, \u0442.\u0435. \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 ARC, \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 &lt;T>.<\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0430\u0445\u0430\u0440\u0430, \u0441 \u044f\u0432\u043d\u044b\u043c \u0438 \u043d\u0435 \u044f\u0432\u043d\u044b\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043d\u0430\u0448\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430:<\/p>\n<pre><code class=\"delphi\">Ref&lt;T: class> = record private type   TAutoRef = class(TInterfacedObject, AutoRef&lt;T>)   private     FValue: T;   protected     function  Invoke: T;   public     constructor Create(const Value: T);     destructor Destroy; override;   end; private   FValue: T; public   class function Create(const Value: T): AutoRef&lt;T>; static; inline;   class operator Implicit(const Value: Ref&lt;T>): AutoRef&lt;T>; static; inline;   class operator Explicit(const Value: T): Ref&lt;T>; static; inline; end;<\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u044d\u0442\u0438\u0445 \u0442\u0438\u043f\u043e\u0432, \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430, \u043d\u043e \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 Unit&#8217;\u0430 \u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439. \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u044f\u0432\u043d\u043e\u0433\u043e \u0438 \u043d\u0435\u044f\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432, \u043e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043a\u043e\u0434, \u043a \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435: <code>MyRef := Ref&lt;TObject>(MyObj);<\/code> \u0430 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e Create.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 (\u043d\u0443 \u0438\u043b\u0438 \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a), \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u043c\u044b \u043d\u0430\u0448\u0435\u043c\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c nil. \u041a\u043e\u0433\u0434\u0430 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442, \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d.<\/p>\n<p>\u041c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 \u0431\u043e\u043d\u0443\u0441, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0438\u043f DeferredRef, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u043e\u0442 \u0436\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0435\u0433\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u043d\u0435\u043c\u0443, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u0435\u043c\u0443 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u0442-\u0436\u0435 AutoRef&lt;T>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<pre><code class=\"delphi\">DeferredRef&lt;T: class> = record private type   TDeferredRef = class(TInterfacedObject, AutoRef&lt;T>)   private     FCreator: AutoRef&lt;T>;     FValue: T;   protected     function  Invoke: T;   public     constructor Create(const Creator: AutoRef&lt;T>);     destructor Destroy; override;   end; private   FCreator: AutoRef&lt;T>; public   class function Create(const Creator: AutoRef&lt;T>): AutoRef&lt;T>; static; inline;   class operator Implicit(const Value: DefferedRef&lt;T>): AutoRef&lt;T>; static; inline;   class operator Explicit(const Value: AutoRef&lt;T>): DefferedRef&lt;T>; static; inline; end;<\/code><\/pre>\n<p>\u0427\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438, \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435, \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0435 \u0447\u0430\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e, \u043d\u0443 \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043c\u043d\u0435. \u041d\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438\u0441\u044c \u043f\u0440\u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e Thread Safe.<\/p>\n<p>\u041a\u0430\u043a \u043e\u0431\u0435\u0449\u0430\u043b, \u043f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442:<\/p>\n<pre><code class=\"delphi\">unit Primitives;  interface  uses   System.Types, System.SysUtils;  type   \/\/\/&lt;summary>   \/\/\/  Because of Delphi realisation of anonymous methods,   \/\/\/  this type can be treated not only as Delegate,   \/\/\/  but also as interface with Invoke method that returns generics type &lt;T>   \/\/\/&lt;\/summary>   AutoRef&lt;T: class> = reference to function: T;    Shared = class   private     class var FLock: TObject;   protected     class constructor Create;     class destructor Destroy;   public     class procedure Initialize&lt;T: class>(var Value: T; const Initializer: AutoRef&lt;T>); static; inline;   end;    \/\/\/&lt;summary>   \/\/\/  This type realizes creating ARC owner of object instance type that freed   \/\/\/  owned object when out of scope (reference count = 0)   \/\/\/&lt;\/summary>   Ref&lt;T: class> = record   private type     TAutoRef = class(TInterfacedObject, AutoRef&lt;T>)     private       FValue: T;     protected       function  Invoke: T;     public       constructor Create(const Value: T);       destructor Destroy; override;     end;   private     FValue: T;   public     class function Create(const Value: T): AutoRef&lt;T>; static; inline;     class operator Implicit(const Value: Ref&lt;T>): AutoRef&lt;T>; static; inline;     class operator Explicit(const Value: T): Ref&lt;T>; static; inline;   end;    \/\/\/&lt;summary>   \/\/\/  This type same as Ref&lt;T>, but with deferred creation of owned object.   \/\/\/  Also, this type is thread safe.   \/\/\/&lt;\/summary>   DeferredRef&lt;T: class> = record   private type     TDeferredRef = class(TInterfacedObject, AutoRef&lt;T>)     private       FCreator: AutoRef&lt;T>;       FValue: T;     protected       function  Invoke: T;     public       constructor Create(const Creator: AutoRef&lt;T>);       destructor Destroy; override;     end;   private     FCreator: AutoRef&lt;T>;   public     class function Create(const Creator: AutoRef&lt;T>): AutoRef&lt;T>; static; inline;     class operator Implicit(const Value: DeferredRef&lt;T>): AutoRef&lt;T>; static; inline;     class operator Explicit(const Value: AutoRef&lt;T>): DeferredRef&lt;T>; static; inline;   end;  implementation  { Shared }  class constructor Shared.Create; begin   FLock := TObject.Create; end;  class destructor Shared.Destroy; begin   FreeAndNil(FLock); end;  class procedure Shared.Initialize&lt;T>(var Value: T; const Initializer: AutoRef&lt;T>); begin   if not Assigned(Value) then   begin     System.TMonitor.Enter(FLock);     try       if not Assigned(Value) then         Value := Initializer();     finally       System.TMonitor.Exit(FLock);     end;   end; end;  { Ref&lt;T>.TAutoRef }  constructor Ref&lt;T>.TAutoRef.Create(const Value: T); begin   FValue := Value; end;  destructor Ref&lt;T>.TAutoRef.Destroy; begin   FreeAndNil(FValue); end;  function Ref&lt;T>.TAutoRef.Invoke: T; begin   Result := FValue; end;  { Ref&lt;T> }  class function Ref&lt;T>.Create(const Value: T): AutoRef&lt;T>; begin   Result := TAutoRef.Create(Value); end;  class operator Ref&lt;T>.Implicit(const Value: Ref&lt;T>): AutoRef&lt;T>; begin   Result := TAutoRef.Create(Value.FValue); end;  class operator Ref&lt;T>.Explicit(const Value: T): Ref&lt;T>; begin   Result.FValue := Value; end;  { DeferredRef&lt;T> }  class function DeferredRef&lt;T>.Create(const Creator: AutoRef&lt;T>): AutoRef&lt;T>; begin   Result := TDeferredRef.Create(Creator); end;  class operator DeferredRef&lt;T>.Explicit(const Value: AutoRef&lt;T>): DeferredRef&lt;T>; begin   Result.FCreator := Value; end;  class operator DeferredRef&lt;T>.Implicit(const Value: DeferredRef&lt;T>): AutoRef&lt;T>; begin   Result := TDeferredRef.Create(Value.FCreator); end;  { DeferredRef&lt;T>.TDeferredRef }  constructor DeferredRef&lt;T>.TDeferredRef.Create(const Creator: AutoRef&lt;T>); begin   FCreator := Creator;   FValue := nil; end;  destructor DeferredRef&lt;T>.TDeferredRef.Destroy; begin   FreeAndNil(FValue); end;  function DeferredRef&lt;T>.TDeferredRef.Invoke: T; begin   Shared.Initialize&lt;T>(FValue, FCreator);   Result := FValue; end;<\/code><\/pre>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043a\u043e\u043c\u0443 \u0442\u043e \u044d\u0442\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u043e.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/587240\/\"> https:\/\/habr.com\/ru\/articles\/587240\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-385943","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/385943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=385943"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/385943\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=385943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=385943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=385943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}