Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 20, 2008 automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
I'd like the following: texture.bind() //class texture, method bind. { .. } //here texture.unbind would automatically be run. Is this possible? |
September 20, 2008 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | 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 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michel Fortin | 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 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Delegate it is then. Is there any performance penalty when using delegates? |
September 20, 2008 Re: automatic function call after closing block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | :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 ;)
|
Copyright © 1999-2021 by the D Language Foundation