Jump to page: 1 2
Thread overview
automatic function call after closing block
Sep 20, 2008
Saaa
Sep 20, 2008
Denis Koroskin
Sep 20, 2008
Saaa
Sep 20, 2008
Sergey Gromov
Sep 20, 2008
Saaa
Sep 20, 2008
Sergey Gromov
Sep 20, 2008
Michel Fortin
Sep 20, 2008
Saaa
Sep 20, 2008
Frank Benoit
Sep 20, 2008
Saaa
Sep 20, 2008
Saaa
Sep 20, 2008
Christopher Wright
Sep 21, 2008
Saaa
September 20, 2008
I'd like the following:

texture.bind() //class texture, method bind.
{
    ..
} //here texture.unbind would automatically be run.

Is this possible?




September 20, 2008
On Sat, 20 Sep 2008 20:15:17 +0400, Saaa <empty@needmail.com> wrote:

>
> I'd like the following:
>
> texture.bind() //class texture, method bind.
> {
>     ..
> } //here texture.unbind would automatically be run.
>
> Is this possible?
>
>
>
>

Yep!

texture.bind()
{
   scope(exit) texture.unbind()
   ...
}
September 20, 2008
Ah, that's at least nicer, but I meant that I somehow change texture.bind() to include texture.unbind()

class texture
{
public void bind()
{
    ..
    underlying scope(exit) texture.unbind()
}

private void unbind()
{
    ..
}

}


..
texture.bind()
{
    ..
}
..


>
> Yep!
>
> texture.bind()
> {
>    scope(exit) texture.unbind()
>    ...
> }


September 20, 2008
On 2008-09-20 12:15:17 -0400, "Saaa" <empty@needmail.com> said:

> I'd like the following:
> 
> texture.bind() //class texture, method bind.
> {
>     ..
> } //here texture.unbind would automatically be run.
> 
> Is this possible?

You could create a scope class, bind in the constructor and unbind in the destructor. Something like this:

scope class Binding {
	Texture t;
	this(Texture t) { this.t = t; t.bind() };
	~this() { t.unbind(); }
}

then use it like this:

{
	scope b = new Binding(texture);
	...
	// automatic destruction of b;
}

Perhaps it could be made more elegant in D2 with struct destructors.

-- 
Michel Fortin
michel.fortin@michelf.com
http://michelf.com/

September 20, 2008
Saaa schrieb:
> I'd like the following:
> 
> texture.bind() //class texture, method bind.
> {
>     ..
> } //here texture.unbind would automatically be run.
> 
> Is this possible?
> 
> 
> 
> 


void bind( Texture t, void delegate() dg ){
  t.bind();
  dg();
  t.unbind();
}


bind( texture, {
  ...
});
September 20, 2008
Saaa <empty@needmail.com> wrote:
> Ah, that's at least nicer, but I meant that I somehow change texture.bind() to include texture.unbind()

You can do something very similar:

class Texture {
  public void bind(void delegate() body) {
    // bind a texture here
    body();
    unbind();
  }
  private void unbind() {
    // unbind the texture here
  }
}


texture.bind(
{
  // use the bound texture here
}
);
September 20, 2008
On Sat, Sep 20, 2008 at 12:24 PM, Saaa <empty@needmail.com> wrote:
> Ah, that's at least nicer, but I meant that I somehow change texture.bind()
> to include texture.unbind()
>
> class texture
> {
> public void bind()
> {
>    ..
>    underlying scope(exit) texture.unbind()
> }
>
> private void unbind()
> {
>    ..
> }
>
> }
>
>
> ..
> texture.bind()
> {
>    ..
> }
> ..
>
>
>>
>> Yep!
>>
>> texture.bind()
>> {
>>    scope(exit) texture.unbind()
>>    ...
>> }
>
>
>

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!
};


It has a nice functional-sounding flair to it, with the 'in' there ;)
September 20, 2008
Never used/heart of a scope class, thanks!
But I'll try the delegate thing first ;)


>
>> I'd like the following:
>>
>> texture.bind() //class texture, method bind.
>> {
>>     ..
>> } //here texture.unbind would automatically be run.
>>
>> Is this possible?
>
> You could create a scope class, bind in the constructor and unbind in the destructor. Something like this:
>
> scope class Binding {
> Texture t;
> this(Texture t) { this.t = t; t.bind() };
> ~this() { t.unbind(); }
> }
>
> then use it like this:
>
> {
> scope b = new Binding(texture);
> ...
> // automatic destruction of b;
> }
>
> Perhaps it could be made more elegant in D2 with struct destructors.
>
> -- 
> Michel Fortin
> michel.fortin@michelf.com
> http://michelf.com/
> 


September 20, 2008
Delegate it is then.
Is there any performance penalty when using delegates?


September 20, 2008
: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!
> };
>
>
> It has a nice functional-sounding flair to it, with the 'in' there ;)


« First   ‹ Prev
1 2