Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ macro/template blackmagic to generate unique name

Macros are fine. Templates are fine. Pretty much whatever it works is fine.

The example is OpenGL; but the technique is C++ specific and relies on no knowledge of OpenGL.

Precise problem:

I want an expression E; where I do not have to specify a unique name; such that a constructor is called where E is defined, and a destructor is called where the block E is in ends.

For example, consider:

class GlTranslate {   GLTranslate(float x, float y, float z); {     glPushMatrix();     glTranslatef(x, y, z);   }   ~GlTranslate() { glPopMatrix(); } }; 

Manual solution:

{   GlTranslate foo(1.0, 0.0, 0.0); // I had to give it a name   ..... } // auto popmatrix 

Now, I have this not only for glTranslate, but lots of other PushAttrib/PopAttrib calls too. I would prefer not to have to come up with a unique name for each var. Is there some trick involving macros templates ... or something else that will automatically create a variable who's constructor is called at point of definition; and destructor called at end of block?

Thanks!

like image 588
anon Avatar asked Mar 10 '10 18:03

anon


1 Answers

I would not do this personally but just come up with unique names. But if you want to do it, one way is to use a combination of if and for:

#define FOR_BLOCK(DECL) if(bool _c_ = false) ; else for(DECL;!_c_;_c_=true) 

You can use it like

FOR_BLOCK(GlTranslate t(1.0, 0.0, 0.0)) {   FOR_BLOCK(GlTranslate t(1.0, 1.0, 0.0)) {     ...   } } 

Each of those names are in separate scopes and won't conflict. The inner names hide the outer names. The expressions in the if and for loops are constant and should be easily optimized by the compiler.


If you really want to pass an expression, you can use the ScopedGuard trick (see Most Important const), but it will need some more work to write it. But the nice side is, that we can get rid of the for loop, and let our object evaluate to false:

struct sbase {    operator bool() const { return false; }  };  template<typename T> struct scont : sbase {    scont(T const& t):t(t), dismiss() {      t.enter();   }   scont(scont const&o):t(o.t), dismiss() {     o.dismiss = true;   }   ~scont() { if(!dismiss) t.leave(); }    T t;    mutable bool dismiss; };  template<typename T> scont<T> make_scont(T const&t) { return scont<T>(t); }  #define FOR_BLOCK(E) if(sbase const& _b_ = make_scont(E)) ; else 

You then provide the proper enter and leave functions:

struct GlTranslate {   GLTranslate(float x, float y, float z)     :x(x),y(y),z(z) { }    void enter() const {     glPushMatrix();     glTranslatef(x, y, z);   }    void leave() const {     glPopMatrix();   }    float x, y, z; }; 

Now you can write it entirely without a name on the user side:

FOR_BLOCK(GlTranslate(1.0, 0.0, 0.0)) {   FOR_BLOCK(GlTranslate(1.0, 1.0, 0.0)) {     ...   } } 

If you want to pass multiple expressions at once, it's a bit more tricky, but you can write an expression template that acts on operator, to collect all expressions into a scont.

template<typename Derived> struct scoped_obj {    void enter() const { }    void leave() const { }     Derived const& get_obj() const {     return static_cast<Derived const&>(*this);   } };  template<typename L, typename R> struct collect    : scoped_obj< collect<L, R> > {   L l;   R r;    collect(L const& l, R const& r)     :l(l), r(r) { }   void enter() const { l.enter(); r.enter(); }   void leave() const { r.leave(); l.leave(); } };  template<typename D1, typename D2>  collect<D1, D2> operator,(scoped_obj<D1> const& l, scoped_obj<D2> const& r) {   return collect<D1, D2>(l.get_obj(), r.get_obj()); }  #define FOR_BLOCK(E) if(sbase const& _b_ = make_scont((E))) ; else 

You need to inherit the RAII object from scoped_obj<Class> like the following shows

struct GLTranslate : scoped_obj<GLTranslate> {   GLTranslate(float x, float y, float z)     :x(x),y(y),z(z) { }    void enter() const {     std::cout << "entering ("               << x << " " << y << " " << z << ")"                << std::endl;   }    void leave() const {     std::cout << "leaving ("               << x << " " << y << " " << z << ")"                << std::endl;   }    float x, y, z; };  int main() {   // if more than one element is passed, wrap them in parentheses   FOR_BLOCK((GLTranslate(10, 20, 30), GLTranslate(40, 50, 60))) {     std::cout << "in block..." << std::endl;   } } 

All of these involve no virtual functions, and the functions involved are transparent to the compiler. In fact, with the above GLTranslate changed to add a single integer to a global variable and when leaving subtracting it again, and the below defined GLTranslateE, i did a test:

// we will change this and see how the compiler reacts. int j = 0;  // only add, don't subtract again struct GLTranslateE : scoped_obj< GLTranslateE > {   GLTranslateE(int x):x(x) { }    void enter() const {     j += x;   }    int x; };  int main() {   FOR_BLOCK((GLTranslate(10), GLTranslateE(5))) {     /* empty */   }   return j; } 

In fact, GCC at optimization level -O2 outputs this:

main:     sub     $29, $29, 8     ldw     $2, $0, j     add     $2, $2, 5     stw     $2, $0, j .L1:     add     $29, $29, 8     jr      $31 

I wouldn't have expected that, it optimized quite well!

like image 155
Johannes Schaub - litb Avatar answered Sep 27 '22 20:09

Johannes Schaub - litb