17
hexc
5y

So I'm doing some OpenGL stuff in C++, for debugging I've created a macro that basically injects my error check code after every OpenGL API call. Basically I don't want any of the code in release builds but I want it to be in debug. Also it needs to be usable inline and accept any GL function return type. From what I can tell I've satisfied all requirements by making the macro generate a generic lambda that returns the original function call result but also creates a stack object that uses the scope to force my error check after the return statement by using the destructor.

Basically I can do:
Log(gl(GetString(...));
gl(DeleteShader(...));
Etc where the GL call can be a function parameter or not.

So my question is, is the code shown in the picture the best way to achieve my goals while providing the behaviour im going for?

Comments
  • 0
    Changed up the struct a bit to be simpler, same idea though...
  • 0
    I guess I should also mention, I'm not a massive fan of having to instantiate a stack object and create a generic lambda wrapper for every GL call just to exploit the objects destructor as sort of "onGLCallFinished" callback. Is there a better/cleaner way of setting up this type of behavior?
  • 0
    What is that ide ? Is it vim?
  • 0
    @thevariableman nah, visual studio 2017
  • 1
    @hexc looks like the default Ubuntu terminal so thought you Were running vim
  • 0
    Been messing around with a more generic solution that might be useful elsewhere. Still has the issue as mentioned in the original post though. Also in this example you cannot use any local variables within the ON_EXIT_SCOPE block... Though I think this can be worked around pretty easily. Still looking for any better ways of doing something like this though...
  • 0
    Here is an example that now supports using external variables internally. Kinda weird to think about considering I'm modifying a variable within a function after the the return statement is called, but before it actually exits...🤔
  • 0
    @hexc how do you know the destructor for your logger is running before any other variables in scope? Can't a's destructor get called before you modify it? Since it's a function argument I doubt this is ever the case, but could it be true for other variables?
  • 0
    @beegC0de not sure I follow..🤔
  • 0
    @hexc never mind, posted that when I woke up and forgot what macros do 🙃
  • 0
    @beegC0de lol all good
  • 1
    There's an extension for this, GL_KHR_debug. You register a callback and the driver will call it for each error it encounters.
  • 1
    @Polarina yeah, I had seen that, it's probably the way I'll go. I just thought this was an interesting exercise to go through being that it's more general purpose. And thought ide share my discoveries. Also if I'm not mistaken, the callback is an extension method not available on all platforms. Was thinking this could be a fallback in those situations.
Add Comment