调试函数包装器

Debug function wrapper

我使用模板函数包装 OpenGL API 调用(用于捕获错误代码):

template<typename Function, typename ... Args>
auto __glFunction(const char *file, int line, Function f, Args ...args) -> decltype(f(args...)) {
    auto result = f(args...);
    auto error = glGetError();
    if (error != GL_NO_ERROR) {
        switch(error) {
            case GL_INVALID_ENUM:
                std::cout << "GL_INVALID_ENUM";
                break;
            case GL_INVALID_VALUE:
                std::cout << "GL_INVALID_VALUE";
                break;
            case GL_INVALID_OPERATION:
                std::cout << "GL_INVALID_OPERATION";
                break;
            case GL_INVALID_FRAMEBUFFER_OPERATION:
                std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION";
                break;
            case GL_OUT_OF_MEMORY:
                std::cout << "GL_OUT_OF_MEMORY";
                break;
            case GL_STACK_UNDERFLOW:
                std::cout << "GL_STACK_UNDERFLOW";
                break;
            case GL_STACK_OVERFLOW:
                std::cout << "GL_STACK_OVERFLOW";
                break;
            default:
                std::cout << "GL_ERROR #" << error;
                break;
        }
        std::cout << " at " << file << ":" << line << std::endl;
    }

    return result;
};

#define glFunction(function, ...) __glFunction(__FILE__, __LINE__, function, ##__VA_ARGS__)

它适用于具有非 void return 类型的函数。我应该如何指定模板以使用 void-returning 函数以及非 void.

解决方案

可能会对某些人有所帮助。 下一个代码适用于(void / non-void)返回函数:

void __glPrintError(const char *file, const int line) {
    auto error = glGetError();
    if (error != GL_NO_ERROR) {
        switch(error) {
            case GL_INVALID_ENUM:
                std::cout << "GL_INVALID_ENUM";
                break;
            case GL_INVALID_VALUE:
                std::cout << "GL_INVALID_VALUE";
                break;
            case GL_INVALID_OPERATION:
                std::cout << "GL_INVALID_OPERATION";
                break;
            case GL_INVALID_FRAMEBUFFER_OPERATION:
                std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION";
                break;
            case GL_OUT_OF_MEMORY:
                std::cout << "GL_OUT_OF_MEMORY";
                break;
            case GL_STACK_UNDERFLOW:
                std::cout << "GL_STACK_UNDERFLOW";
                break;
            case GL_STACK_OVERFLOW:
                std::cout << "GL_STACK_OVERFLOW";
                break;
            default:
                std::cout << "GL_ERROR #" << error;
                break;
        }
        std::cout << " at " << file << ":" << line << std::endl;
    }
}

template<typename Function, typename ... Args>
auto __glFunction(const char *file, const int line, Function f, Args ...args) ->
    typename std::enable_if<!std::is_same<decltype(f(args...)), void>::value, decltype(f(args...))>::type {

    auto result = f(args...);
    __glPrintError(file, line);
    return result;
};

template<typename Function, typename ... Args>
auto __glFunction(const char *file, const int line, Function f, Args ...args) ->
    typename std::enable_if<std::is_same<decltype(f(args...)), void>::value, decltype(f(args...))>::type {

    f(args...);
    __glPrintError(file, line);
};

#define glFunction(function, ...) __glFunction(__FILE__, __LINE__, function, ##__VA_ARGS__)