/src/alembic/lib/Alembic/Abc/ErrorHandler.h
Line | Count | Source |
1 | | //-***************************************************************************** |
2 | | // |
3 | | // Copyright (c) 2009-2013, |
4 | | // Sony Pictures Imageworks, Inc. and |
5 | | // Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd. |
6 | | // |
7 | | // All rights reserved. |
8 | | // |
9 | | // Redistribution and use in source and binary forms, with or without |
10 | | // modification, are permitted provided that the following conditions are |
11 | | // met: |
12 | | // * Redistributions of source code must retain the above copyright |
13 | | // notice, this list of conditions and the following disclaimer. |
14 | | // * Redistributions in binary form must reproduce the above |
15 | | // copyright notice, this list of conditions and the following disclaimer |
16 | | // in the documentation and/or other materials provided with the |
17 | | // distribution. |
18 | | // * Neither the name of Sony Pictures Imageworks, nor |
19 | | // Industrial Light & Magic nor the names of their contributors may be used |
20 | | // to endorse or promote products derived from this software without specific |
21 | | // prior written permission. |
22 | | // |
23 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
26 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
27 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
28 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
29 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
30 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
31 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
32 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
33 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
34 | | // |
35 | | //-***************************************************************************** |
36 | | |
37 | | #ifndef Alembic_Abc_ErrorHandler_h |
38 | | #define Alembic_Abc_ErrorHandler_h |
39 | | |
40 | | #include <Alembic/Util/Export.h> |
41 | | #include <Alembic/Abc/Foundation.h> |
42 | | |
43 | | namespace Alembic { |
44 | | namespace Abc { |
45 | | namespace ALEMBIC_VERSION_NS { |
46 | | |
47 | | //-***************************************************************************** |
48 | | class ALEMBIC_EXPORT ErrorHandler |
49 | | { |
50 | | public: |
51 | | enum Policy |
52 | | { |
53 | | kQuietNoopPolicy, |
54 | | kNoisyNoopPolicy, |
55 | | kThrowPolicy |
56 | | }; |
57 | | |
58 | | enum UnknownExceptionFlag |
59 | | { |
60 | | kUnknownException |
61 | | }; |
62 | | |
63 | | ErrorHandler() |
64 | 396 | : m_policy( kThrowPolicy ) |
65 | 396 | , m_errorLog( "" ) {} |
66 | | |
67 | | ErrorHandler( Policy iPolicy ) |
68 | | : m_policy( iPolicy ) |
69 | 0 | , m_errorLog( "" ) {} |
70 | | |
71 | | //! Default copy constructor |
72 | | //! Default assignment operator |
73 | | |
74 | | void operator()( std::exception &iExc, |
75 | | const std::string &iCtx = "" ); |
76 | | |
77 | | void operator()( const std::string &iErrMsg, |
78 | | const std::string &iCtx = "" ); |
79 | | |
80 | | void operator()( UnknownExceptionFlag iUef, |
81 | | const std::string &iCtx = "" ); |
82 | | |
83 | 10 | Policy getPolicy() const { return m_policy; } |
84 | 297 | void setPolicy( Policy iPolicy ) { m_policy = iPolicy; } |
85 | | |
86 | 0 | const std::string getErrorLog() const { return m_errorLog; } |
87 | | |
88 | 379 | bool valid() const { return ( m_errorLog == "" ); } |
89 | | |
90 | 139 | void clear() { m_errorLog = ""; } |
91 | | |
92 | | class Context |
93 | | { |
94 | | public: |
95 | | Context( ErrorHandler &iEhnd, const char *iCtxMsg ) |
96 | 413 | : m_handler( iEhnd ), |
97 | 413 | m_message( iCtxMsg ) {} |
98 | | |
99 | | void operator()( std::exception &iExc ) |
100 | 139 | { |
101 | 139 | m_handler( iExc, m_message ); |
102 | 139 | } |
103 | | |
104 | | void operator()( const std::string &iMsg ) |
105 | 0 | { |
106 | 0 | m_handler( iMsg, m_message ); |
107 | 0 | } |
108 | | |
109 | | void operator()( UnknownExceptionFlag iUef ) |
110 | 0 | { |
111 | 0 | m_handler( iUef, m_message ); |
112 | 0 | } |
113 | | |
114 | | private: |
115 | | const Context& operator= (const Context&); |
116 | | ErrorHandler &m_handler; |
117 | | const char *m_message; |
118 | | }; |
119 | | |
120 | | private: |
121 | | void handleIt( const std::string &iErr ); |
122 | | |
123 | | Policy m_policy; |
124 | | std::string m_errorLog; |
125 | | }; |
126 | | |
127 | | //-***************************************************************************** |
128 | | |
129 | | //-***************************************************************************** |
130 | | inline ErrorHandler::Policy |
131 | | GetErrorHandlerPolicy( AbcA::ArchiveWriterPtr /* iClass */ ) |
132 | 0 | { return ErrorHandler::kThrowPolicy; } |
133 | | |
134 | | inline ErrorHandler::Policy |
135 | | GetErrorHandlerPolicy( AbcA::ObjectWriterPtr /* iClass */ ) |
136 | 0 | { return ErrorHandler::kThrowPolicy; } |
137 | | |
138 | | inline ErrorHandler::Policy |
139 | | GetErrorHandlerPolicy( AbcA::CompoundPropertyWriterPtr /* iClass */ ) |
140 | 0 | { return ErrorHandler::kThrowPolicy; } |
141 | | |
142 | | inline ErrorHandler::Policy |
143 | | GetErrorHandlerPolicy( AbcA::ScalarPropertyWriterPtr /* iClass */ ) |
144 | 0 | { return ErrorHandler::kThrowPolicy; } |
145 | | |
146 | | inline ErrorHandler::Policy |
147 | | GetErrorHandlerPolicy( AbcA::ArrayPropertyWriterPtr /* iClass */ ) |
148 | 0 | { return ErrorHandler::kThrowPolicy; } |
149 | | |
150 | | //-***************************************************************************** |
151 | | inline ErrorHandler::Policy |
152 | | GetErrorHandlerPolicy( AbcA::ArchiveReaderPtr /* iClass */ ) |
153 | 0 | { return ErrorHandler::kThrowPolicy; } |
154 | | |
155 | | inline ErrorHandler::Policy |
156 | | GetErrorHandlerPolicy( AbcA::ObjectReaderPtr /* iClass */ ) |
157 | 57 | { return ErrorHandler::kThrowPolicy; } |
158 | | |
159 | | inline ErrorHandler::Policy |
160 | | GetErrorHandlerPolicy( AbcA::CompoundPropertyReaderPtr /* iClass */ ) |
161 | 1 | { return ErrorHandler::kThrowPolicy; } |
162 | | |
163 | | inline ErrorHandler::Policy |
164 | | GetErrorHandlerPolicy( AbcA::ScalarPropertyReaderPtr /* iClass */ ) |
165 | 0 | { return ErrorHandler::kThrowPolicy; } |
166 | | |
167 | | inline ErrorHandler::Policy |
168 | | GetErrorHandlerPolicy( AbcA::ArrayPropertyReaderPtr /* iClass */ ) |
169 | 0 | { return ErrorHandler::kThrowPolicy; } |
170 | | |
171 | | //-***************************************************************************** |
172 | 413 | #define ALEMBIC_ABC_SAFE_CALL_BEGIN( CONTEXT ) \ |
173 | 413 | do \ |
174 | 413 | { \ |
175 | 413 | ::Alembic::Abc::ErrorHandler::Context \ |
176 | 413 | __err( this->getErrorHandler(), ( CONTEXT ) ); \ |
177 | 413 | try \ |
178 | 413 | { |
179 | | |
180 | | //-***************************************************************************** |
181 | | #define ALEMBIC_ABC_SAFE_CALL_END_RESET() \ |
182 | 191 | } \ |
183 | 191 | catch ( std::exception &exc ) \ |
184 | 191 | { \ |
185 | 139 | this->reset(); \ |
186 | 139 | __err( exc ); \ |
187 | 139 | } \ |
188 | 191 | catch ( ... ) \ |
189 | 191 | { \ |
190 | 0 | this->reset(); \ |
191 | 0 | __err( ::Alembic::Abc:: \ |
192 | 0 | ErrorHandler::kUnknownException ); \ |
193 | 0 | } \ |
194 | 191 | } \ |
195 | 191 | while( 0 ) |
196 | | |
197 | | //-***************************************************************************** |
198 | | #define ALEMBIC_ABC_SAFE_CALL_END() \ |
199 | 222 | } \ |
200 | 222 | catch ( std::exception &exc ) \ |
201 | 222 | { \ |
202 | 0 | __err( exc ); \ |
203 | 0 | } \ |
204 | 222 | catch ( ... ) \ |
205 | 222 | { \ |
206 | 0 | __err( ::Alembic::Abc:: \ |
207 | 0 | ErrorHandler::kUnknownException ); \ |
208 | 0 | } \ |
209 | 222 | } \ |
210 | 222 | while( 0 ) |
211 | | |
212 | | } // End namespace ALEMBIC_VERSION_NS |
213 | | |
214 | | using namespace ALEMBIC_VERSION_NS; |
215 | | |
216 | | } // End namespace Abc |
217 | | } // End namespace Alembic |
218 | | |
219 | | #endif |