Path Tracer
Exceptional.h
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2020, 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 class ASSIMP_API DeadlyErrorBase : public runtime_error {
60 protected:
62 
63  template<typename... T, typename U>
64  DeadlyErrorBase(Assimp::Formatter::format f, U&& u, T&&... args) :
65  DeadlyErrorBase(std::move(f << std::forward<U>(u)), std::forward<T>(args)...) {}
66 };
67 
68 // ---------------------------------------------------------------------------
72 class ASSIMP_API DeadlyImportError : public DeadlyErrorBase {
73 public:
75  template<typename... T>
76  explicit DeadlyImportError(T&&... args) :
77  DeadlyErrorBase(Assimp::Formatter::format(), std::forward<T>(args)...) {}
78 };
79 
80 class ASSIMP_API DeadlyExportError : public DeadlyErrorBase {
81 public:
83  template<typename... T>
84  explicit DeadlyExportError(T&&... args) :
85  DeadlyErrorBase(Assimp::Formatter::format(), std::forward<T>(args)...) {}
86 };
87 
88 #ifdef _MSC_VER
89 #pragma warning(default : 4275)
90 #endif
91 
92 // ---------------------------------------------------------------------------
93 template <typename T>
95  T operator()() const {
96  return T();
97  }
98 };
99 
100 // ---------------------------------------------------------------------------
101 template <typename T>
102 struct ExceptionSwallower<T *> {
103  T *operator()() const {
104  return nullptr;
105  }
106 };
107 
108 // ---------------------------------------------------------------------------
109 template <>
111  aiReturn operator()() const {
112  try {
113  throw;
114  } catch (std::bad_alloc &) {
115  return aiReturn_OUTOFMEMORY;
116  } catch (...) {
117  return aiReturn_FAILURE;
118  }
119  }
120 };
121 
122 // ---------------------------------------------------------------------------
123 template <>
124 struct ExceptionSwallower<void> {
125  void operator()() const {
126  return;
127  }
128 };
129 
130 #define ASSIMP_BEGIN_EXCEPTION_REGION() \
131  { \
132  try {
133 
134 #define ASSIMP_END_EXCEPTION_REGION_WITH_ERROR_STRING(type, ASSIMP_END_EXCEPTION_REGION_errorString, ASSIMP_END_EXCEPTION_REGION_exception) \
135  } \
136  catch (const DeadlyImportError &e) { \
137  ASSIMP_END_EXCEPTION_REGION_errorString = e.what(); \
138  ASSIMP_END_EXCEPTION_REGION_exception = std::current_exception(); \
139  return ExceptionSwallower<type>()(); \
140  } \
141  catch (...) { \
142  ASSIMP_END_EXCEPTION_REGION_errorString = "Unknown exception"; \
143  ASSIMP_END_EXCEPTION_REGION_exception = std::current_exception(); \
144  return ExceptionSwallower<type>()(); \
145  } \
146 }
147 
148 #define ASSIMP_END_EXCEPTION_REGION(type) \
149  } \
150  catch (...) { \
151  return ExceptionSwallower<type>()(); \
152  } \
153  }
154 
155 #endif // AI_INCLUDED_EXCEPTIONAL_H
DeadlyExportError::DeadlyExportError
DeadlyExportError(T &&... args)
Definition: Exceptional.h:84
aiReturn
aiReturn
Definition: types.h:397
Assimp::Formatter::basic_formatter
Definition: TinyFormatter.h:74
DeadlyExportError
Definition: Exceptional.h:80
DeadlyImportError
Definition: Exceptional.h:72
ExceptionSwallower
Definition: Exceptional.h:94
aiReturn_OUTOFMEMORY
@ aiReturn_OUTOFMEMORY
Definition: types.h:407
TinyFormatter.h
Utility to format log messages more easily. Introduced to get rid of the boost::format dependency....
DeadlyErrorBase
Definition: Exceptional.h:59
Assimp
Definition: ai_assert.h:50
DeadlyImportError::DeadlyImportError
DeadlyImportError(T &&... args)
Definition: Exceptional.h:76
aiReturn_FAILURE
@ aiReturn_FAILURE
Definition: types.h:402