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