Exceptional.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2021, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #pragma once
  34. #ifndef AI_INCLUDED_EXCEPTIONAL_H
  35. #define AI_INCLUDED_EXCEPTIONAL_H
  36. #ifdef __GNUC__
  37. #pragma GCC system_header
  38. #endif
  39. #include <assimp/DefaultIOStream.h>
  40. #include <assimp/TinyFormatter.h>
  41. #include <stdexcept>
  42. using std::runtime_error;
  43. #ifdef _MSC_VER
  44. #pragma warning(disable : 4275)
  45. #endif
  46. class ASSIMP_API DeadlyErrorBase : public runtime_error {
  47. protected:
  48. DeadlyErrorBase(Assimp::Formatter::format f);
  49. template<typename... T, typename U>
  50. DeadlyErrorBase(Assimp::Formatter::format f, U&& u, T&&... args) :
  51. DeadlyErrorBase(std::move(f << std::forward<U>(u)), std::forward<T>(args)...) {}
  52. };
  53. // ---------------------------------------------------------------------------
  54. /** FOR IMPORTER PLUGINS ONLY: Simple exception class to be thrown if an
  55. * unrecoverable error occurs while importing. Loading APIs return
  56. * nullptr instead of a valid aiScene then. */
  57. class ASSIMP_API DeadlyImportError : public DeadlyErrorBase {
  58. public:
  59. DeadlyImportError(const char *message) :
  60. DeadlyErrorBase(Assimp::Formatter::format(), std::forward<const char*>(message)) {}
  61. /** Constructor with arguments */
  62. template<typename... T>
  63. explicit DeadlyImportError(T&&... args) :
  64. DeadlyErrorBase(Assimp::Formatter::format(), std::forward<T>(args)...) {}
  65. #if defined(_MSC_VER) && defined(__clang__)
  66. DeadlyImportError(DeadlyImportError& other) = delete;
  67. #endif
  68. };
  69. class ASSIMP_API DeadlyExportError : public DeadlyErrorBase {
  70. public:
  71. /** Constructor with arguments */
  72. template<typename... T>
  73. explicit DeadlyExportError(T&&... args) :
  74. DeadlyErrorBase(Assimp::Formatter::format(), std::forward<T>(args)...) {}
  75. #if defined(_MSC_VER) && defined(__clang__)
  76. DeadlyExportError(DeadlyExportError& other) = delete;
  77. #endif
  78. };
  79. #ifdef _MSC_VER
  80. #pragma warning(default : 4275)
  81. #endif
  82. // ---------------------------------------------------------------------------
  83. template <typename T>
  84. struct ExceptionSwallower {
  85. T operator()() const {
  86. return T();
  87. }
  88. };
  89. // ---------------------------------------------------------------------------
  90. template <typename T>
  91. struct ExceptionSwallower<T *> {
  92. T *operator()() const {
  93. return nullptr;
  94. }
  95. };
  96. // ---------------------------------------------------------------------------
  97. template <>
  98. struct ExceptionSwallower<aiReturn> {
  99. aiReturn operator()() const {
  100. try {
  101. throw;
  102. } catch (std::bad_alloc &) {
  103. return aiReturn_OUTOFMEMORY;
  104. } catch (...) {
  105. return aiReturn_FAILURE;
  106. }
  107. }
  108. };
  109. // ---------------------------------------------------------------------------
  110. template <>
  111. struct ExceptionSwallower<void> {
  112. void operator()() const {
  113. return;
  114. }
  115. };
  116. #define ASSIMP_BEGIN_EXCEPTION_REGION() \
  117. { \
  118. try {
  119. #define ASSIMP_END_EXCEPTION_REGION_WITH_ERROR_STRING(type, ASSIMP_END_EXCEPTION_REGION_errorString, ASSIMP_END_EXCEPTION_REGION_exception) \
  120. } \
  121. catch (const DeadlyImportError &e) { \
  122. ASSIMP_END_EXCEPTION_REGION_errorString = e.what(); \
  123. ASSIMP_END_EXCEPTION_REGION_exception = std::current_exception(); \
  124. return ExceptionSwallower<type>()(); \
  125. } \
  126. catch (...) { \
  127. ASSIMP_END_EXCEPTION_REGION_errorString = "Unknown exception"; \
  128. ASSIMP_END_EXCEPTION_REGION_exception = std::current_exception(); \
  129. return ExceptionSwallower<type>()(); \
  130. } \
  131. }
  132. #define ASSIMP_END_EXCEPTION_REGION(type) \
  133. } \
  134. catch (...) { \
  135. return ExceptionSwallower<type>()(); \
  136. } \
  137. }
  138. #endif // AI_INCLUDED_EXCEPTIONAL_H