Cover handler for a statement or a block of statements.
simendsjo
simen.endsjo at pandavre.com
Wed Jun 8 13:36:21 PDT 2011
On 08.06.2011 21:09, Jonathan M Davis wrote:
> On 2011-06-08 12:06, vincent wrote:
>> Cover handler for a statement or a block of statements.
>>
>> I would like to suggest a method for defining code statement
>> covering. Some examp1es of a cover are as follows :-
>> - try ... finally - this is defined in dotnet as a using
>> - try ... (catch)+
>> - cursor save and display others ... restore saved cursor
>>
>> a possible syntax -
>>
>> cover hourglass(Application&app)
>> {
>> Cursor save = app.cursor;
>> app.cursor = HOURGLASS_CURSOR;
>> ~ code ~
>> app.cursor = save;
>> }
>>
>> usage could be something like -
>>
>> void function(Application&app)
>> {
>> hourglass(app)
>> {
>> ... // code to be run by cover
>> }
>> }
>
> That would typically be done with either a struct with a destructor or with a
> scope statement.
>
> - Jonathan M Davis
To give Jonathan's answer an example.
import std.stdio;
void main() {
class Cursor {
string name = "regular";
}
class Application {
private Cursor _cursor;
@property Cursor cursor() {
return _cursor;
}
@property void cursor(Cursor value) {
writeln("setting cursor ", value.name);
_cursor = value;
}
this() { _cursor = new Cursor(); }
}
auto app = new Application();
auto HOURGLASS_CURSOR = new Cursor();
HOURGLASS_CURSOR.name = "hourglass";
writeln("try/finally:");
auto save = app.cursor;
try {
app.cursor = HOURGLASS_CURSOR;
writeln("code here");
} finally {
app.cursor = save;
}
writeln("\nor scope(exit):");
{
save = app.cursor;
scope(exit) app.cursor = save;
app.cursor = HOURGLASS_CURSOR;
writeln("code here");
}
}
Outputs
try/finally:
setting cursor hourglass
code here
setting cursor regular
or scope(exit):
setting cursor hourglass
code here
setting cursor regular
More information about the Digitalmars-d-learn
mailing list