{"id":555,"date":"2017-04-06T01:24:04","date_gmt":"2017-04-05T23:24:04","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=555"},"modified":"2017-04-06T01:24:04","modified_gmt":"2017-04-05T23:24:04","slug":"supports","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2017\/04\/06\/supports\/","title":{"rendered":"Supports"},"content":{"rendered":"<p>This week I had to maintain some legacy code that uses many interfaces and supports in order to find out if a specific interface is supported:<\/p>\n<pre class=\"lang:delphi decode:true\">type\r\n  IMyInterface = interface(IInterface)\r\n    ['{F500EEDD-032E-4D15-A8C0-EFF673C2AF01}']\r\n    procedure DoSomething;\r\n  end;\r\n\r\nprocedure MyProcedure(const AIntf: IInterface);\r\n\r\nimplementation\r\n\r\nuses\r\n  System.SysUtils;\r\n\r\nprocedure MyProcedure(const AIntf: IInterface);\r\nvar\r\n  lMyIntf: IMyInterface;\r\nbegin\r\n  if Supports(AIntf, IInterface, lMyIntf) then\r\n    lMyIntf.DoSomething;\r\nend;\r\n<\/pre>\n<p>One of the tasks I had to do is to replace the interfaces with pure Delphi objects. That&#8217;s why I wondered if there is a chance to write something similar for objects.<\/p>\n<p>I played a bit and found the following:<\/p>\n<pre class=\"lang:delphi decode:true \">type\r\n  TObjectHelper = class helper for TObject\r\n  public\r\n    function Supports&lt;T: class&gt;(out ADest: T): Boolean;\r\n  end;\r\n\r\n...\r\n\r\n{ TObjectHelper }\r\n\r\nfunction TObjectHelper.Supports&lt;T&gt;(out ADest: T): Boolean;\r\nbegin\r\n  if Self.InheritsFrom(T) then\r\n  begin\r\n    ADest := T(Self);\r\n    Result := True;\r\n  end\r\n  else\r\n  begin\r\n    ADest := nil;\r\n    Result := False;\r\n  end;\r\nend;\r\n\r\n...\r\n\r\nvar\r\n  lObject: TObject;\r\n  lMyObject: TMyObject;\r\nbegin\r\n  ...\r\n  if lObject.Supports&lt;TMyObject&gt;(lMyObject) then\r\n    lMyObject.DoSomething;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This week I had to maintain some legacy code that uses many interfaces and supports in order to find out if a specific interface is supported: type IMyInterface = interface(IInterface) [&#8216;{F500EEDD-032E-4D15-A8C0-EFF673C2AF01}&#8217;] procedure DoSomething; end; procedure MyProcedure(const AIntf: IInterface); implementation uses &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2017\/04\/06\/supports\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,35,3,1],"tags":[],"class_list":["post-555","post","type-post","status-publish","format-standard","hentry","category-delphi","category-rad-studio-10-2-tokyo","category-tipsandtricks","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":2,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/555\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}