Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Thanassis Tsiodras, Dr.-Ing
- // My C++11 trick for common GLerror handling (regardless of whether the API returns something or not)
- // March 13, 2012
- // User code:
- ...
- // this API (glCreateShader) returns something
- shader_ = GLERROR(glCreateShader(VertexShader==shaderType?GL_VERTEX_SHADER:GL_FRAGMENT_SHADER));
- // this (glShaderSource) doesn't
- GLERROR(glShaderSource(shader_, 1, &pShaderCode, NULL));
- // neither does this (glCompileShader)
- GLERROR(glCompileShader(shader_));
- ...
- // Library code: how I did it with lambdas and std::function
- void commonCode(const char *file, size_t lineno) {
- GLenum err = glGetError();
- if (err != GL_NO_ERROR) {
- while (err != GL_NO_ERROR) {
- std::cerr <<
- "glError: " << (char *)gluErrorString(err) <<
- " (" << file << ":" << lineno << ")" << std::endl;
- err = glGetError();
- }
- throw "Failure in GLSL...";
- }
- }
- template <class T>
- auto glError(std::function<T()>&& t, const char *file, size_t lineno) -> decltype(t()) {
- auto ret = t();
- commonCode(file,lineno);
- return ret;
- }
- template<>
- void glError(std::function<void(void)>&& t, const char *file, size_t lineno) {
- t();
- commonCode(file,lineno);
- }
- template <class T>
- auto helper (T&& t) -> std::function<decltype(t())()>
- {
- std::function<decltype(t())()> tmp = t;
- return tmp;
- }
- #define GLERROR( expr ) \
- [&]() { \
- return glError( helper( [&]() { return expr; } ), __FILE__, __LINE__); \
- }()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement