Hello,
Vladimir Ulchenko wrote:
> > that was a joke. I have written numerous macros which would easily > > resist being ported to Delphi, but I don't really use them in > > production code :) > > why not? I do it all the time
the small ones are all right, but I was rather thinking of a little framework which I built upon macros and templates and which basically emulated Delphi's "safecall" convention (i.e. COM exception firewalling) in C++.
> I wonder whether is is possible to implement something similar in > newer delphi versions in order to avoid aforementioned ubiquitous > nested try/finally boredom?
With Delphi's generics it's possible to build some kind of equivalent for std::auto_ptr<>. However generics differ from templates in that they're strictly typed at declaration time. I assume your MakeBeginEndUpdate() is a function template that generates calls to obj->BeginUpdate() and obj->EndUpdate() regardless of what type obj has (some kind of static duck typing). You can't do that with generics; you'd have to write specific scopeguards for TListBox, TTreeView etc.
My generic scope guard, however, is easily portable using anonymous methods:
// ----- uses SysUtils;
type TScopeGuard = record private FExitProc: TProc; public constructor Create (InitProc, ExitProc: TProc); end;
constructor TScopeGuard.Create (InitProc, ExitProc: TProc); begin InitProc; FExitProc := ExitProc; // note: assign AFTER calling InitProc! end;
.... procedure TForm1.Button1Click (Sender: TObject); var LVSG: TScopeGuard; begin LVSG.Create (procedure begin ListView1.BeginUpdate end, procedure begin ListView1.EndUpdate end); ... end; // -----
-- Moritz
"Hey, it compiles! Ship it!"