PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sgl/Utility/Error.h

https://bitbucket.org/DikobrAz/simplegl-core
C Header | 152 lines | 99 code | 28 blank | 25 comment | 0 complexity | 8ee8ad21c7c9785f31e818258e83ef95 MD5 | raw file
  1. #ifndef SIMPLE_GL_UTILITY_ERROR_H
  2. #define SIMPLE_GL_UTILITY_ERROR_H
  3. #include "../Config.h"
  4. #include "Referenced.h"
  5. #include <iostream>
  6. namespace sgl {
  7. /** Used to determine the result or status of the executed operation */
  8. enum SGL_HRESULT
  9. {
  10. SGL_OK, /// operation succeeded
  11. SGLERR_INVALID_CALL, /// function call with invalid arguments
  12. SGLERR_OUT_OF_MEMORY, /// couldn't allocate memory
  13. SGLERR_FILE_NOT_FOUND, /// couldn't find specified file
  14. SGLERR_IO, /// error in input/output operation
  15. SGLERR_UNSUPPORTED, /// operation not supported by the hardware
  16. SGLERR_UNAVAILABLE, /// resource is unavailable at the moment
  17. SGLERR_UNKNOWN /// unknow error
  18. };
  19. /** Handler to handle simple gl errors. For example log it into the file */
  20. class SGL_DLLEXPORT ErrorHandler
  21. {
  22. public:
  23. /** Handle sgl error
  24. * @param result - type of the error
  25. * @param msg - error message
  26. */
  27. virtual void HandleError(SGL_HRESULT result, const char* msg) = 0;
  28. virtual ~ErrorHandler() {}
  29. };
  30. } // namespace sgl
  31. /** Get last occured error */
  32. extern "C" SGL_DLLEXPORT sgl::SGL_HRESULT SGL_DLLCALL sglGetLastError();
  33. /** Get last error message */
  34. extern "C" SGL_DLLEXPORT const char* SGL_DLLCALL sglGetErrorMsg();
  35. /** Set error handler to the sgl */
  36. extern "C" SGL_DLLEXPORT void SGL_DLLCALL sglSetErrorHandler(sgl::ErrorHandler* _handler);
  37. /** Get error handler of the sgl */
  38. extern "C" SGL_DLLEXPORT sgl::ErrorHandler* SGL_DLLCALL sglGetErrorHandler();
  39. /* Set sgl error */
  40. extern "C" SGL_DLLEXPORT void SGL_DLLCALL sglSetError(sgl::SGL_HRESULT _type, const char* _msg);
  41. namespace sgl {
  42. /** Error handler that logs errors to the platform specific log (cerr, android log, ...). */
  43. class SGL_DLLEXPORT PrintErrorHandler :
  44. public ErrorHandler
  45. {
  46. public:
  47. // Override ErrorHandler
  48. void HandleError(SGL_HRESULT type, const char* msg);
  49. virtual ~PrintErrorHandler() {}
  50. };
  51. /** Error occurs when calling function with invalid arguments */
  52. inline SGL_HRESULT EInvalidCall()
  53. {
  54. sglSetError(SGLERR_INVALID_CALL, "Invalid function call");
  55. return SGLERR_INVALID_CALL;
  56. }
  57. /** Error occurs when calling function with invalid arguments */
  58. inline SGL_HRESULT EInvalidCall(const char* msg)
  59. {
  60. sglSetError(SGLERR_INVALID_CALL, msg);
  61. return SGLERR_INVALID_CALL;
  62. }
  63. /** Error occurs when we are out of memory */
  64. inline SGL_HRESULT EOutOfMemory()
  65. {
  66. sglSetError(SGLERR_OUT_OF_MEMORY, "Out of memory");
  67. return SGLERR_OUT_OF_MEMORY;
  68. }
  69. /** Error occurs when we are out of memory */
  70. inline SGL_HRESULT EOutOfMemory(const char* msg)
  71. {
  72. sglSetError(SGLERR_OUT_OF_MEMORY, msg);
  73. return SGLERR_OUT_OF_MEMORY;
  74. }
  75. /** Error occurs when we are trying to open unexisted file for reading */
  76. inline SGL_HRESULT EFileNotFound()
  77. {
  78. sglSetError(SGLERR_FILE_NOT_FOUND, "File not found");
  79. return SGLERR_FILE_NOT_FOUND;
  80. }
  81. /** Error occurs when we are trying to open unexisted file for reading */
  82. inline SGL_HRESULT EFileNotFound(const char* msg)
  83. {
  84. sglSetError(SGLERR_FILE_NOT_FOUND, msg);
  85. return SGLERR_FILE_NOT_FOUND;
  86. }
  87. /** Error occured during IO operation */
  88. inline SGL_HRESULT EIOError()
  89. {
  90. sglSetError(SGLERR_IO, "IO error");
  91. return SGLERR_IO;
  92. }
  93. /** Error occured during IO operation */
  94. inline SGL_HRESULT EIOError(const char* msg)
  95. {
  96. sglSetError(SGLERR_IO, msg);
  97. return SGLERR_IO;
  98. }
  99. /** Error occurs when we are trying to perform operation unsupported by the hardware */
  100. inline SGL_HRESULT EUnsupported()
  101. {
  102. sglSetError(SGLERR_UNSUPPORTED, "Operation unsupported by the hardware");
  103. return SGLERR_UNSUPPORTED;
  104. }
  105. /** Error occurs when we are trying to perform operation unsupported by the hardware */
  106. inline SGL_HRESULT EUnsupported(const char* msg)
  107. {
  108. sglSetError(SGLERR_UNSUPPORTED, msg);
  109. return SGLERR_UNSUPPORTED;
  110. }
  111. /** Mystic error */
  112. inline SGL_HRESULT EUnknown()
  113. {
  114. sglSetError(SGLERR_UNKNOWN, "Unknown error");
  115. return SGLERR_UNKNOWN;
  116. }
  117. /** Mystic error */
  118. inline SGL_HRESULT EUnknown(const char* msg)
  119. {
  120. sglSetError(SGLERR_UNKNOWN, msg);
  121. return SGLERR_UNKNOWN;
  122. }
  123. } // namespace sgl
  124. #endif // SIMPLE_GL_UTILITY_ERROR_H