automatic function call after closing block
Sergey Gromov
snake.scaly at gmail.com
Sat Sep 20 12:58:59 PDT 2008
As to how it works. The t.bindBlock() in {...} can be rewritten using
C++-like syntax:
void worker()
{
// some rendering code!
}
BindBlock bb = t.bindBlock();
bb.opIn(&worker);
The code below simply writes all this in one line using a temporary
BindBlock structure, an overloaded operator 'in', and an inline
anonymous delegate syntax.
Saaa <empty at needmail.com> wrote:
> :D
> Looks cool!
> No way I could have come up with that.
> Same question (as I have no clue how this internally works):
> Is there a performance penalty?
>
> > How about a horrible/wonderful misuse of the 'in' operator? (credited
> > to Tom S):
> >
> > class Texture
> > {
> > ...
> > struct BindBlock
> > {
> > Texture self;
> > void opIn(void delegate() dg)
> > {
> > self.bind();
> > scope(exit) self.unbind();
> > dg();
> > }
> > }
> >
> > BindBlock bindBlock() { return BindBlock(this); }
> > ...
> > }
> >
> > auto t = new Texture("foo.png");
> >
> > t.bindBlock() in
> > {
> > // some rendering code!
> > };
More information about the Digitalmars-d-learn
mailing list