调试函数包装器

时间:2016-05-07 14:17:35

标签: c++ templates c++11 variadic

我使用模板函数来包装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返回类型的函数。我应该如何指定模板来处理void返回函数以及非void。

1 个答案:

答案 0 :(得分:1)

解决方案

可能对某人有帮助。 下一个代码适用于(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__)