Subscribe to forum RSS Forum

automatic LeaveMethod call

New Topic Post Reply

Page: 1

Author Post
dummzeuch
Guest
Hi,

it just occurred to me that EnterMethod/LeaveMethod could use a feature (OK, not a feature but rather a known but undocumented behaviour) of the Delphi compiler for automatically doing a LeaveMethod call:

type
IAutoLeaveCaller = interface
end;

type
TAutoLeaveCaller = class(TInterfacedObject, IAutoLeaveCaller)
private
FSession: TSiSession;
FLevel: TSiLevel;
FMethod: WideString;
public
constructor Create(_Session: TSiSession; _Level: TSiLevel; const _MethodName: WideString);
destructor Destroy; override;
end;

constructor TAutoLeaveCaller.Create(_Session: TSiSession; _Level: TSiLevel; const _MethodName: WideString);
begin
inherited Create;
FSession := _Session;
FLevel := _Level;
FMethod := _MethodName;
FSession.EnterMethod(_Level, _MethodName);
end;

destructor TAutoLeaveCaller.Destroy;
begin
FSession.LeaveMethod(_Level, _MethodName);
inherited;
end;


Which is used like this:


procedure TSomething.DoWhatever;
begin
TAutoLeaveCaller.Create(FSession, lvVerbose, 'TSomething.DoWhatever');

....
end;


What this does is creating an implicit interface variable which will be kept until exiting the method, in which case it will automatically be freed.

This would solve the problem of having to insert LeaveMethod wherever the mehod exits (eg. calling exit or raising an exception). Of course, that's even worse than adding try...finally in terms of performance, but very convenient to code.

Of course by wrapping it into a TSiSession-Method it could be made even easier to use.

twm
Administrator
Registered: Sep 2007
Posts: 132
Hello twm,

Good idea, thanks. It's actually so good that this feature already exists in SmartInspect ;). The session method is called TrackMethod and does exactly what you are suggesting. And as you have already described, it uses the interface/reference counting feature to implement this.
dummzeuch
Guest
Hi Tobias,

Tobias Gurock wrote
The session method is called TrackMethod and does exactly what you are suggesting. And as you have already described, it uses the interface/reference counting feature to implement this.


Hm, I just checked the PDF manual and there is only one place where TrackMethod is mentioned: Chapter 2.2 What's New / SmartInspect 1.3.1

It's in the online help, though and even linked from EnterMethod, so I probably would have found it if I hadn't thought myself so very clever... :-(

twm
Administrator
Registered: Sep 2007
Posts: 132
Although the most important features are also available in the PDF manual, many of the library-related features are only in the API documentation. We will look into it if and how we can integrate the API documentation directly into Delphi, so that it's easier to lookup features/methods.

By the way, did you have the time to look at the asynchronous feature of the SmartInspect 3.0 EAP releases?
Member
Registered: Jan 2008
Posts: 10
unfortunately not. I changed the app for which I originally wanted this to work differently and, as always I am currently too busy to do any meaningful testing.
Administrator
Registered: Sep 2007
Posts: 132
Okay, thanks for the update.

New Topic Post Reply

Page: 1