exception_handling.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (c) 2019-2025 The Khronos Group Inc.
  2. //
  3. // SPDX-License-Identifier: Apache-2.0 OR MIT
  4. //
  5. // Initial Author: Rylie Pavlik <[email protected]>
  6. //
  7. // Provides protection for C ABI functions if standard library functions may throw.
  8. #pragma once
  9. #ifdef OPENXR_HAVE_COMMON_CONFIG
  10. #include "common_config.h"
  11. #endif // OPENXR_HAVE_COMMON_CONFIG
  12. #ifdef XRLOADER_DISABLE_EXCEPTION_HANDLING
  13. #define XRLOADER_ABI_TRY
  14. #define XRLOADER_ABI_CATCH_BAD_ALLOC_OOM
  15. #define XRLOADER_ABI_CATCH_FALLBACK
  16. #else
  17. #include <stdexcept>
  18. #define XRLOADER_ABI_TRY try
  19. #define XRLOADER_ABI_CATCH_BAD_ALLOC_OOM \
  20. catch (const std::bad_alloc&) { \
  21. LoaderLogger::LogErrorMessage("", "failed allocating memory"); \
  22. return XR_ERROR_OUT_OF_MEMORY; \
  23. }
  24. #define XRLOADER_ABI_CATCH_FALLBACK \
  25. catch (const std::exception& e) { \
  26. LoaderLogger::LogErrorMessage("", "Unknown failure: " + std::string(e.what())); \
  27. return XR_ERROR_RUNTIME_FAILURE; \
  28. } \
  29. catch (...) { \
  30. LoaderLogger::LogErrorMessage("", "Unknown failure"); \
  31. return XR_ERROR_RUNTIME_FAILURE; \
  32. }
  33. #endif