1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8 
9 /** \file
10  * C++ exception support for DNG SDK.
11 */
12 
13 /*****************************************************************************/
14 
15 #ifndef __dng_exceptions__
16 #define __dng_exceptions__
17 
18 /*****************************************************************************/
19 
20 #include "dng_errors.h"
21 #include "dng_flags.h"
22 
23 /*****************************************************************************/
24 
25 #ifndef DNG_NO_RETURN
26 #ifdef __GNUC__
27 #define DNG_NO_RETURN __attribute__((noreturn))
28 #else
29 #define DNG_NO_RETURN
30 #endif
31 #endif
32 
33 /*****************************************************************************/
34 
35 /// Display a warning message. Note that this may just eat the message.
36 
37 void ReportWarning (const char *message,
38 				    const char *sub_message = NULL);
39 
40 /*****************************************************************************/
41 
42 /// Display an error message. Note that this may just eat the message.
43 
44 void ReportError (const char *message,
45 				  const char *sub_message = NULL);
46 
47 /*****************************************************************************/
48 
49 /// \brief All exceptions thrown by the DNG SDK use this exception class.
50 
51 class dng_exception
52 	{
53 
54 	private:
55 
56 		dng_error_code fErrorCode;
57 
58 	public:
59 
60 		/// Construct an exception representing the given error code.
61 		/// \param code Error code this exception is for.
62 
dng_exception(dng_error_code code)63 		dng_exception (dng_error_code code)
64 
65 			: fErrorCode (code)
66 
67 			{
68 			}
69 
~dng_exception()70 		virtual ~dng_exception ()
71 			{
72 			}
73 
74 		/// Getter for error code of this exception
75 		/// \retval The error code of this exception.
76 
ErrorCode()77 		dng_error_code ErrorCode () const
78 			{
79 			return fErrorCode;
80 			}
81 
82 	};
83 
84 /******************************************************************************/
85 
86 /// \brief Throw an exception based on an arbitrary error code.
87 
88 void Throw_dng_error (dng_error_code err,
89 					  const char * message = NULL,
90 					  const char * sub_message = NULL,
91 					  bool silent = false) DNG_NO_RETURN;
92 
93 /******************************************************************************/
94 
95 /// \brief Convenience function to throw dng_exception with error code if
96 /// error_code is not dng_error_none .
97 
Fail_dng_error(dng_error_code err)98 inline void Fail_dng_error (dng_error_code err)
99 	{
100 
101 	if (err != dng_error_none)
102 		{
103 
104 		Throw_dng_error (err);
105 
106 		}
107 
108 	}
109 
110 /*****************************************************************************/
111 
112 /// \brief Convenience function to throw dng_exception with error code
113 /// dng_error_unknown .
114 
115 inline void ThrowProgramError (const char * sub_message = NULL)
116 	{
117 
118 	Throw_dng_error (dng_error_unknown, NULL, sub_message);
119 
120 	}
121 
122 /*****************************************************************************/
123 
124 /// \brief Convenience function to throw dng_exception with error code
125 /// dng_error_overflow.
126 
127 inline void ThrowOverflow (const char * sub_message = NULL)
128 	{
129 
130 	Throw_dng_error (dng_error_overflow, NULL, sub_message);
131 
132 	}
133 
134 /*****************************************************************************/
135 
136 /// \brief Convenience function to throw dng_exception with error code
137 /// dng_error_not_yet_implemented .
138 
139 inline void ThrowNotYetImplemented (const char * sub_message = NULL)
140 	{
141 
142 	Throw_dng_error (dng_error_not_yet_implemented, NULL, sub_message);
143 
144 	}
145 
146 /*****************************************************************************/
147 
148 /// \brief Convenience function to throw dng_exception with error code
149 /// dng_error_silent .
150 
ThrowSilentError()151 inline void ThrowSilentError ()
152 	{
153 
154 	Throw_dng_error (dng_error_silent);
155 
156 	}
157 
158 /*****************************************************************************/
159 
160 /// \brief Convenience function to throw dng_exception with error code
161 /// dng_error_user_canceled .
162 
ThrowUserCanceled()163 inline void ThrowUserCanceled ()
164 	{
165 
166 	Throw_dng_error (dng_error_user_canceled);
167 
168 	}
169 
170 /*****************************************************************************/
171 
172 /// \brief Convenience function to throw dng_exception with error code
173 /// dng_error_host_insufficient .
174 
175 inline void ThrowHostInsufficient (const char * sub_message = NULL,
176                                    bool silent = false)
177 	{
178 
179 	Throw_dng_error (dng_error_host_insufficient, NULL, sub_message, silent);
180 
181 	}
182 
183 /*****************************************************************************/
184 
185 /// \brief Convenience function to throw dng_exception with error code
186 /// dng_error_memory .
187 
188 inline void ThrowMemoryFull (const char * sub_message = NULL)
189 	{
190 
191 	Throw_dng_error (dng_error_memory, NULL, sub_message);
192 
193 	}
194 
195 /*****************************************************************************/
196 
197 /// \brief Convenience function to throw dng_exception with error code
198 /// dng_error_bad_format .
199 
200 inline void ThrowBadFormat (const char * sub_message = NULL)
201 	{
202 
203 	Throw_dng_error (dng_error_bad_format, NULL, sub_message);
204 
205 	}
206 
207 /*****************************************************************************/
208 
209 /// \brief Convenience function to throw dng_exception with error code
210 /// dng_error_matrix_math .
211 
212 inline void ThrowMatrixMath (const char * sub_message = NULL)
213 	{
214 
215 	Throw_dng_error (dng_error_matrix_math, NULL, sub_message);
216 
217 	}
218 
219 /*****************************************************************************/
220 
221 /// \brief Convenience function to throw dng_exception with error code
222 /// dng_error_open_file .
223 
224 inline void ThrowOpenFile (const char * sub_message = NULL, bool silent = false)
225 	{
226 
227 	Throw_dng_error (dng_error_open_file, NULL, sub_message, silent);
228 
229 	}
230 
231 /*****************************************************************************/
232 
233 /// \brief Convenience function to throw dng_exception with error code
234 /// dng_error_read_file .
235 
236 inline void ThrowReadFile (const char *sub_message = NULL)
237 	{
238 
239 	Throw_dng_error (dng_error_read_file, NULL, sub_message);
240 
241 	}
242 
243 /*****************************************************************************/
244 
245 /// \brief Convenience function to throw dng_exception with error code
246 /// dng_error_write_file .
247 
248 inline void ThrowWriteFile (const char *sub_message = NULL)
249 	{
250 
251 	Throw_dng_error (dng_error_write_file, NULL, sub_message);
252 
253 	}
254 
255 /*****************************************************************************/
256 
257 /// \brief Convenience function to throw dng_exception with error code
258 /// dng_error_end_of_file .
259 
260 inline void ThrowEndOfFile (const char *sub_message = NULL)
261 	{
262 
263 	Throw_dng_error (dng_error_end_of_file, NULL, sub_message);
264 
265 	}
266 
267 /*****************************************************************************/
268 
269 /// \brief Convenience function to throw dng_exception with error code
270 /// dng_error_file_is_damaged .
271 
ThrowFileIsDamaged()272 inline void ThrowFileIsDamaged ()
273 	{
274 
275 	Throw_dng_error (dng_error_file_is_damaged);
276 
277 	}
278 
279 /*****************************************************************************/
280 
281 /// \brief Convenience function to throw dng_exception with error code
282 /// dng_error_image_too_big_dng .
283 
ThrowImageTooBigDNG()284 inline void ThrowImageTooBigDNG ()
285 	{
286 
287 	Throw_dng_error (dng_error_image_too_big_dng);
288 
289 	}
290 
291 /*****************************************************************************/
292 
293 /// \brief Convenience function to throw dng_exception with error code
294 /// dng_error_image_too_big_tiff .
295 
ThrowImageTooBigTIFF()296 inline void ThrowImageTooBigTIFF ()
297 	{
298 
299 	Throw_dng_error (dng_error_image_too_big_tiff);
300 
301 	}
302 
303 /*****************************************************************************/
304 
305 /// \brief Convenience function to throw dng_exception with error code
306 /// dng_error_unsupported_dng .
307 
ThrowUnsupportedDNG()308 inline void ThrowUnsupportedDNG ()
309 	{
310 
311 	Throw_dng_error (dng_error_unsupported_dng);
312 
313 	}
314 
315 /*****************************************************************************/
316 
317 #endif
318 
319 /*****************************************************************************/
320