tags:

views:

800

answers:

2

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 ti 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!

+12  A: 

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!

Johannes Schaub - litb
Does doing `bool _c_ = false` get rid of compiler warnings or something? (Over just `false`) EDIT: Derp, never-mind, I see why. What a clever trick. :]
GMan
@GMan I've taken this trickery from the `BOOST_FOREACH` macro :)
Johannes Schaub - litb
+1 interesting (and hopefully useful) tricks.
Tronic
@Johannes Schaub - litb: I am getting a feeling that I am missing something, but what is the problem with [this](http://ideone.com/7B9lM). How is your if-for construct better? The problem is that both of these allow us to only work with one object at any particular point of scope, so we actually do need multiple different names. What did I miss?
Lazer
@eSKay there is no problem with your linked code. My if/for construct expands to code equivalent to your manual blocks. However, for my last solution in particular, you don't need names anymore, which is what the questioner wanted to achieve ultimately. Personally, i like to give those objects names that reflect their purpose. For example a translation that puts an object at the origin, i will do `Trans toOrigin(-x, -y, -z);` instead of just writing `Trans(-x, -y, -z)` or `Trans foo(-x, -y, -z);` which won't convey the real purpose of the transformaion.
Johannes Schaub - litb
Those RAII objects sharing the same name isn't a problem: The only purpose is that they do some action in their constructor, and another action in their destructor. You don't need to access the objects after they're created. So the inner one shadowing the outer one is alright. One thing the if/for solution gives you is a prettier look, since you don't need explicit braces. You can write `FOR(Trans t(a, b, c)) foo();` for example. But really, the first solution is my least preferred one. If i wanted to write no names, i would choose the last solution presented.
Johannes Schaub - litb
+7  A: 

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
GMan
You should avoid starting identifiers with the underscore character. Names starting with underscore are reserved for the compiler, and you might get a name collision that is difficult to track down if you generate them yourself. (So, for example, replace "_trans_" with "trans_" or something more unique)
Magnus Hoff
@Magnus, GMan is fine using `_trans`. These names are only reserved in the global namespace or in the std namespace. Names that are reserved everywhere are the ones that look like `_Trans` or `__trans`.
Johannes Schaub - litb
'__LINE__ can be used as well as '__COUNTER__
Corwin