41 #ifndef __CAXException_h__
42 #define __CAXException_h__
44 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
45 #include <CoreFoundation/CoreFoundation.h>
47 #include <ConditionalMacros.h>
48 #include <CoreFoundation.h>
50 #include "CADebugMacros.h"
58 CAX4CCString(OSStatus error) {
61 *(UInt32 *)(str + 1) = CFSwapInt32HostToBig(error);
62 if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
63 str[0] = str[5] =
'\'';
65 }
else if (error > -200000 && error < 200000)
67 sprintf(str,
"%d", (
int)error);
69 sprintf(str,
"0x%x", (
int)error);
71 const char *
get()
const {
return mStr; }
72 operator const char *()
const {
return mStr; }
80 CAXException(
const char *operation, OSStatus err) :
83 if (operation == NULL)
85 else if (strlen(operation) >=
sizeof(mOperation)) {
86 memcpy(mOperation, operation,
sizeof(mOperation) - 1);
87 mOperation[
sizeof(mOperation) - 1] =
'\0';
90 strlcpy(mOperation, operation,
sizeof(mOperation));
93 char *FormatError(
char *str)
const
95 return FormatError(str, mError);
99 const OSStatus mError;
103 typedef void (*WarningHandler)(
const char *msg, OSStatus err);
105 static char *FormatError(
char *str, OSStatus error)
107 strcpy(str, CAX4CCString(error));
111 static void Warning(
const char *s, OSStatus error)
114 (*sWarningHandler)(s, error);
117 static void SetWarningHandler(WarningHandler f) { sWarningHandler = f; }
119 static WarningHandler sWarningHandler;
122 #if DEBUG || CoreAudio_Debug
123 #define XThrowIfError(error, operation) \
125 OSStatus __err = error; \
127 DebugMessageN2("about to throw %s: %s", CAX4CCString(__err).get(), operation);\
129 throw CAXException(operation, __err); \
133 #define XThrowIf(condition, error, operation) \
136 OSStatus __err = error; \
137 DebugMessageN2("about to throw %s: %s", CAX4CCString(__err).get(), operation);\
139 throw CAXException(operation, __err); \
143 #define XRequireNoError(error, label) \
145 OSStatus __err = error; \
147 DebugMessageN2("about to throw %s: %s", CAX4CCString(__err).get(), #error);\
153 #define XAssert(assertion) \
155 if (!(assertion)) { \
156 DebugMessageN1("error: failed assertion: %s", #assertion); \
161 #define XAssertNoError(error) \
163 OSStatus __err = error; \
165 DebugMessageN2("error %s: %s", CAX4CCString(__err).get(), #error);\
170 #define ca_require_noerr(errorCode, exceptionLabel) \
173 int evalOnceErrorCode = (errorCode); \
174 if ( __builtin_expect(0 != evalOnceErrorCode, 0) ) \
176 DebugMessageN5("ca_require_noerr: [%s, %d] (goto %s;) %s:%d", \
177 #errorCode, evalOnceErrorCode, \
181 goto exceptionLabel; \
185 #define ca_verify_noerr(errorCode) \
188 int evalOnceErrorCode = (errorCode); \
189 if ( __builtin_expect(0 != evalOnceErrorCode, 0) ) \
191 DebugMessageN4("ca_verify_noerr: [%s, %d] %s:%d", \
192 #errorCode, evalOnceErrorCode, \
198 #define ca_debug_string(message) \
201 DebugMessageN3("ca_debug_string: %s %s:%d", \
208 #define ca_verify(assertion) \
211 if ( __builtin_expect(!(assertion), 0) ) \
213 DebugMessageN3("ca_verify: %s %s:%d", \
220 #define ca_require(assertion, exceptionLabel) \
223 if ( __builtin_expect(!(assertion), 0) ) \
225 DebugMessageN4("ca_require: %s %s %s:%d", \
230 goto exceptionLabel; \
234 #define ca_check(assertion) \
237 if ( __builtin_expect(!(assertion), 0) ) \
239 DebugMessageN3("ca_check: %s %s:%d", \
247 #define XThrowIfError(error, operation) \
249 OSStatus __err = error; \
251 throw CAXException(operation, __err); \
255 #define XThrowIf(condition, error, operation) \
258 OSStatus __err = error; \
259 throw CAXException(operation, __err); \
263 #define XRequireNoError(error, label) \
265 OSStatus __err = error; \
271 #define XAssert(assertion) \
275 #define XAssertNoError(error) \
280 #define ca_require_noerr(errorCode, exceptionLabel) \
283 if ( __builtin_expect(0 != (errorCode), 0) ) \
285 goto exceptionLabel; \
289 #define ca_verify_noerr(errorCode) \
292 if ( 0 != (errorCode) ) \
297 #define ca_debug_string(message)
299 #define ca_verify(assertion) \
302 if ( !(assertion) ) \
307 #define ca_require(assertion, exceptionLabel) \
310 if ( __builtin_expect(!(assertion), 0) ) \
312 goto exceptionLabel; \
316 #define ca_check(assertion) \
319 if ( !(assertion) ) \
327 #define XThrow(error, operation) XThrowIf(true, error, operation)
328 #define XThrowIfErr(error) XThrowIfError(error, #error)
330 #endif // __CAXException_h__