1

I am trying to add some syntax sugar in my OpenGL code. I started with

    GLint mapView, mapProjection, f1;
    ...
    mapView = glGetUniformLocation( program, "mapView" );
    mapProjection = glGetUniformLocation( program, "mapProjection" );
    f1 = glGetUniformLocation( program, "f1" );

Converting this to a macro was easy enough

    #define BindUniform(name) name = glGetUniformLocation( program, #name )
    ...
    BindUniform( mapView );
    BindUniform( mapProjection );
    BindUniform( f1 );

Now I want to convert to a variadic macro

    #define BindUniforms(...) ? foreach(x) x = glGetUniformLocation( program, #x) ?
    ...
    BindUniforms( mapView, mapProjection, f1 );

Is it possible to achieve elegantly enough? I'm hoping for a 2-3 lines solution. I saw a 30+ lines huge macro set solution that builds on recursions but that looks like an overkill. Also note that the macro is writing to the argument, unlike some examples that I tried and can only read from it.

EDIT. I did some searching before posting this question. This question Is it possible to iterate over arguments in variadic macros? has a few solutions, but they are either too many lines of code or don't write to arguments. So, the idea behind my question is how to achieve it with as little code as possible.

Anton Duzenko
  • 2,366
  • 1
  • 21
  • 26
  • Why not using a variadic template instead? – πάντα ῥεῖ Feb 23 '19 at 10:01
  • I'm not very fluent with C++ in general and templates in particular. Could you maybe post your solution as an answer? – Anton Duzenko Feb 23 '19 at 10:03
  • @AntonDuzenko You have to generate a lot of boilerplate macros to iterate over comma-separated lists of arguments, as the dupe link explains. Instead of doing it yourself, I suggest using Boost.Preprocessor library. Alternatively, if you can settle for a different syntax: `BindUniforms((mapView)(mapProjection)(f1));`, you can avoid generating boilerplate code: [demo](http://coliru.stacked-crooked.com/a/a2c4bda0ee004a3c). – HolyBlackCat Feb 23 '19 at 10:09
  • I would like to avoid that boiler-plating because I am working on a proof of concept algo and I would like to keep it as simple and minimalist as possible. – Anton Duzenko Feb 23 '19 at 10:14
  • @AntonDuzenko Well, it's like to swallow one toad in order to avoid swallowing many of them ;-) – πάντα ῥεῖ Feb 23 '19 at 10:15

0 Answers0