473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

disabling temporaries for a specific class

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

Mar 16 '07 #1
7 1395
in******@gmail.com wrote:
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?
....

Not that I know of.

One suggestion is to not allow access to x other than through the guard.
i.e.

{
Guard g(x);

g.DoStuffToX( blah );
}

or even

{
Guard(x).DoStuffToX( blah );
}

That would mean you would need to stop pushing around raw ogl pointers.

Mar 16 '07 #2
Gianni Mariani wrote:
in******@gmail.com wrote:
>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?
...

Not that I know of.

One suggestion is to not allow access to x other than through the guard.
i.e.

{
Guard g(x);

g.DoStuffToX( blah );
}

or even

{
Guard(x).DoStuffToX( blah );
}

That would mean you would need to stop pushing around raw ogl pointers.
Yeah, using the Guard as a proxy as Gianni suggested is the best way.
However, you are not guaranteed to have the destructor to be called at
any particular time. I.e. It will be destroyed between the last time
it is used to the end of the enclosing scope. When that will happen
depends on the compiler. To ensure that it is destroyed when you
expect, you must enclose the section with braces and not have other
things in it, just as Gianni's example shows.
Adrian

--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 17 '07 #3
Adrian Hawryluk wrote:
Gianni Mariani wrote:
....
>One suggestion is to not allow access to x other than through the
guard. i.e.

{
Guard g(x);

g.DoStuffToX( blah );
}

or even

{
Guard(x).DoStuffToX( blah );
}

That would mean you would need to stop pushing around raw ogl pointers.
Yeah, using the Guard as a proxy as Gianni suggested is the best way.
However, you are not guaranteed to have the destructor to be called at
any particular time. I.e. It will be destroyed between the last time
it is used to the end of the enclosing scope. When that will happen
depends on the compiler. To ensure that it is destroyed when you
expect, you must enclose the section with braces and not have other
things in it, just as Gianni's example shows.
The compiler would be non compliant if it deletes the temporary before
calling DoStuffToX in either case.
Mar 17 '07 #4
On Mar 17, 12:51 am, Gianni Mariani <gi3nos...@mariani.wswrote:
Adrian Hawryluk wrote:
Gianni Mariani wrote:
...
One suggestion is to not allow access to x other than through the
guard. i.e.
{
Guard g(x);
g.DoStuffToX( blah );
}
or even
{
Guard(x).DoStuffToX( blah );
}
That would mean you would need to stop pushing around raw ogl pointers.
Yeah, using the Guard as a proxy as Gianni suggested is the best way.
However, you are not guaranteed to have the destructor to be called at
any particular time. I.e. It will be destroyed between the last time
it is used to the end of the enclosing scope. When that will happen
depends on the compiler. To ensure that it is destroyed when you
expect, you must enclose the section with braces and not have other
things in it, just as Gianni's example shows.

The compiler would be non compliant if it deletes the temporary before
calling DoStuffToX in either case.
I made a solution, thanks for your input guys!
#include <iostream>
#include <assert.h>

class NonTemp
{
public:
NonTemp(NonTemp & tmp)
{
assert(&tmp == this);
}
};

class Guard : public NonTemp
{
public:
Guard(NonTemp & t, int i) : NonTemp(t), _i(i)
{
std::cout << "interesting Non Temporary hack my Chris
in******@gmail.com" << std::endl;
}
~Guard()
{
std::cout << "dest" << std::endl;
}
private:
int _i;
};
int main()
{
Guard g(g, 1);
std::cout << "middle" << std::endl;

// impossible to make a temp!
}



Mar 17 '07 #5
in******@gmail.com wrote:
....
I made a solution, thanks for your input guys!
#include <iostream>
#include <assert.h>

class NonTemp
{
public:
NonTemp(NonTemp & tmp)
{
assert(&tmp == this);
}
};

class Guard : public NonTemp
{
public:
Guard(NonTemp & t, int i) : NonTemp(t), _i(i)
{
std::cout << "interesting Non Temporary hack my Chris
in******@gmail.com" << std::endl;
}
~Guard()
{
std::cout << "dest" << std::endl;
}
private:
int _i;
};
int main()
{
Guard g(g, 1);
std::cout << "middle" << std::endl;

// impossible to make a temp!
}
OK - while you're at it, you should also make the copy constructor and
assignment operator private and not implemented.
Mar 17 '07 #6
Gianni Mariani wrote:
Adrian Hawryluk wrote:
>Gianni Mariani wrote:
...
>>One suggestion is to not allow access to x other than through the
guard. i.e.

{
Guard g(x);

g.DoStuffToX( blah );
}

or even

{
Guard(x).DoStuffToX( blah );
}

That would mean you would need to stop pushing around raw ogl pointers.
Yeah, using the Guard as a proxy as Gianni suggested is the best way.
However, you are not guaranteed to have the destructor to be called at
any particular time. I.e. It will be destroyed between the last time
it is used to the end of the enclosing scope. When that will happen
depends on the compiler. To ensure that it is destroyed when you
expect, you must enclose the section with braces and not have other
things in it, just as Gianni's example shows.

The compiler would be non compliant if it deletes the temporary before
calling DoStuffToX in either case.
Ether case? I don't understand.

Calling DoStuffToX() would be the last case it was used, so deletion of
the proxy would happen sometime after the call to the end of the
enclosing scope.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 17 '07 #7
Yup good idea Gianni, sometimes I don't think of these things.
See what I use it for is like this, I have many functions and I don't
want to keep making helper functions (DoStuffToX), it would complicate
things.

void GlTexture2D::sendToGL(GlBuffer * buffer)
{
BindGuard g(g, this);
GlBuffer::BindBufferGuard bg(bg, buffer);
DEBUG_TIME(1, "sendToGL()");
glTexSubImage2D(target(), 0,
0 /*xoffset*/, 0 /*yoffset*/,
buffer->width(),
buffer->height(),
buffer->dataFormat(),
buffer->dataType(),
BUFFER_OFFSET(0));
}

On Mar 17, 8:16 am, Adrian Hawryluk <adrian.hawryluk-at-
gmail....@nospam.comwrote:
Gianni Mariani wrote:
Adrian Hawryluk wrote:
Gianni Mariani wrote:
...
>One suggestion is to not allow access to x other than through the
guard. i.e.
>{
Guard g(x);
> g.DoStuffToX( blah );
}
>or even
>{
Guard(x).DoStuffToX( blah );
}
>That would mean you would need to stop pushing around raw ogl pointers.
Yeah, using the Guard as a proxy as Gianni suggested is the best way.
However, you are not guaranteed to have the destructor to be called at
any particular time. I.e. It will be destroyed between the last time
it is used to the end of the enclosing scope. When that will happen
depends on the compiler. To ensure that it is destroyed when you
expect, you must enclose the section with braces and not have other
things in it, just as Gianni's example shows.
The compiler would be non compliant if it deletes the temporary before
calling DoStuffToX in either case.

Ether case? I don't understand.

Calling DoStuffToX() would be the last case it was used, so deletion of
the proxy would happen sometime after the call to the end of the
enclosing scope.

Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/

Mar 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kaede | last post by:
Hi all, Consider the following code fragment: // some data structure class Data { ... } // Container for the data structure Class Container {
13
by: John Harrison | last post by:
Rather a long program I'm afraid but I don't think I can cut it down any further. What I'm trying to do is construct a complex object Y from several X objects in a complex expression. I'm trying...
4
by: Asfand Yar Qazi | last post by:
Sorry about this, but its driving me up the wall. First some code: typedef unsigned int size_t; struct AddOp { template<class I1, class I2> static inline float call(size_t i, const I1&...
3
by: PB | last post by:
What is the rationalle for disabling JavaScript. AFAIK, the primary reason is for "security purposes" - but what specific kind of threats does the protect against? AND - is the disabling of...
1
by: Neelesh Bodas | last post by:
Hello all, Please consider this code : class X { int x; public: X(int p ) : x(p) { } operator int() { x = 1; return x; } };
3
by: bb | last post by:
Hi, Have a query regarding the life of temporaries. Here is the code... class MyNumber { public: MyNumber(int n) : n(n) { cout << "Object Constructed." << endl; }
7
by: REH | last post by:
Though unsafe, is this legal: const int& foo(const int& i) { return i; } int j = foo(5); does the temporary live long enough to be assigned to j?
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
6
by: wizwx | last post by:
Is there anything wrong with the following code? class A { ... }; class B : public A { ... }; // definitions of class A and B, these are OK Foo() { A & a = B(); // ?? A * p = &B(); // ??...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.