Search code examples
c++c-preprocessorraii

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!


Solution

  • If your compiler supports __COUNTER__ (it probably does), you could try:

    // boiler-plate
    #define CONCATENATE_DETAIL(x, y) x##y
    #define CONCATENATE(x, y) CONCATENATE_DETAIL(x, y)
    #define MAKE_UNIQUE(x) CONCATENATE(x, __COUNTER__)
    
    // per-transform type
    #define GL_TRANSLATE_DETAIL(n, x, y, z) GlTranslate n(x, y, z)
    #define GL_TRANSLATE(x, y, z) GL_TRANSLATE_DETAIL(MAKE_UNIQUE(_trans_), x, y, z)
    

    For

    {
        GL_TRANSLATE(1.0, 0.0, 0.0);
    
        // becomes something like:
        GlTranslate _trans_1(1.0, 0.0, 0.0);
    
    } // auto popmatrix