I
indy2718
Hi,
I've been doing a reoccuring programming error. I'm using a Guard
class (like as in a mutex guard).
The Guard is doing a glBindTexture(new texture) in the constructor,
and then a unbinding BindTexture (original texture) in the
destructor. (see code at the end of this message).
Example:
{
Guard g(x); // constructor called, bind
int i;
// some code
} // destructor of g called, unbind
But sometimes I make a mistake and do:
{
Guard(x); //constructor, destructor calls
// BUG, NOT BOUND
..
}
Is there a way to enforce no temporaries for an object, using any
combination of techniques or consts, or special function calls?
Thanks in advance!
Chris
class BindGuard
{
public:
BindGuard(GlTexture2D * t)
{
int i;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &i);
_oldTex = i;
if (_oldTex != t->texId())
{
glBindTexture(GL_TEXTURE_2D, t->texId());
}
}
~BindGuard()
{
if (_oldTex != _t->texId())
{
glBindTexture(GL_TEXTURE_2D, _oldTex);
}
}
private:
GlTexture2D * _t;
GLuint _oldTex;
};
I've been doing a reoccuring programming error. I'm using a Guard
class (like as in a mutex guard).
The Guard is doing a glBindTexture(new texture) in the constructor,
and then a unbinding BindTexture (original texture) in the
destructor. (see code at the end of this message).
Example:
{
Guard g(x); // constructor called, bind
int i;
// some code
} // destructor of g called, unbind
But sometimes I make a mistake and do:
{
Guard(x); //constructor, destructor calls
// BUG, NOT BOUND
..
}
Is there a way to enforce no temporaries for an object, using any
combination of techniques or consts, or special function calls?
Thanks in advance!
Chris
class BindGuard
{
public:
BindGuard(GlTexture2D * t)
{
int i;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &i);
_oldTex = i;
if (_oldTex != t->texId())
{
glBindTexture(GL_TEXTURE_2D, t->texId());
}
}
~BindGuard()
{
if (_oldTex != _t->texId())
{
glBindTexture(GL_TEXTURE_2D, _oldTex);
}
}
private:
GlTexture2D * _t;
GLuint _oldTex;
};