3 Abstract: Part of CoreAudio Utility Classes
6 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7 Inc. ("Apple") in consideration of your agreement to the following
8 terms, and your use, installation, modification or redistribution of
9 this Apple software constitutes acceptance of these terms. If you do
10 not agree with these terms, please do not use, install, modify or
11 redistribute this Apple software.
13 In consideration of your agreement to abide by the following terms, and
14 subject to these terms, Apple grants you a personal, non-exclusive
15 license, under Apple's copyrights in this original Apple software (the
16 "Apple Software"), to use, reproduce, modify and redistribute the Apple
17 Software, with or without modifications, in source and/or binary forms;
18 provided that if you redistribute the Apple Software in its entirety and
19 without modifications, you must retain this notice and the following
20 text and disclaimers in all such redistributions of the Apple Software.
21 Neither the name, trademarks, service marks or logos of Apple Inc. may
22 be used to endorse or promote products derived from the Apple Software
23 without specific prior written permission from Apple. Except as
24 expressly stated in this notice, no other rights or licenses, express or
25 implied, are granted by Apple herein, including but not limited to any
26 patent rights that may be infringed by your derivative works or by other
27 works in which the Apple Software may be incorporated.
29 The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42 POSSIBILITY OF SUCH DAMAGE.
44 Copyright (C) 2014 Apple Inc. All Rights Reserved.
47 #ifndef __CAXException_h__
48 #define __CAXException_h__
50 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
51 #include <CoreFoundation/CoreFoundation.h>
53 #include <ConditionalMacros.h>
54 #include <CoreFoundation.h>
56 #include "CADebugMacros.h"
64 CAX4CCString(OSStatus error) {
65 // see if it appears to be a 4-char-code
66 UInt32 beErr = CFSwapInt32HostToBig(error);
68 memcpy(str + 1, &beErr, 4);
69 if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
70 str[0] = str[5] = '\'';
72 } else if (error > -200000 && error < 200000)
73 // no, format it as an integer
74 snprintf(str, sizeof(mStr), "%d", (int)error);
76 snprintf(str, sizeof(mStr), "0x%x", (int)error);
78 const char *get() const { return mStr; }
79 operator const char *() const { return mStr; }
84 class CAX4CCStringNoQuote {
86 CAX4CCStringNoQuote(OSStatus error) {
87 // see if it appears to be a 4-char-code
88 UInt32 beErr = CFSwapInt32HostToBig(error);
90 memcpy(str, &beErr, 4);
91 if (isprint(str[0]) && isprint(str[1]) && isprint(str[2]) && isprint(str[3])) {
93 } else if (error > -200000 && error < 200000)
94 // no, format it as an integer
95 snprintf(str, sizeof(mStr), "%d", (int)error);
97 snprintf(str, sizeof(mStr), "0x%x", (int)error);
99 const char *get() const { return mStr; }
100 operator const char *() const { return mStr; }
106 // An extended exception class that includes the name of the failed operation
109 CAXException(const char *operation, OSStatus err) :
112 if (operation == NULL)
113 mOperation[0] = '\0';
114 else if (strlen(operation) >= sizeof(mOperation)) {
115 memcpy(mOperation, operation, sizeof(mOperation) - 1);
116 mOperation[sizeof(mOperation) - 1] = '\0';
119 strlcpy(mOperation, operation, sizeof(mOperation));
122 char *FormatError(char *str, size_t strsize) const
124 return FormatError(str, strsize, mError);
127 char mOperation[256];
128 const OSStatus mError;
130 // -------------------------------------------------
132 typedef void (*WarningHandler)(const char *msg, OSStatus err);
134 static char *FormatError(char *str, size_t strsize, OSStatus error)
136 strlcpy(str, CAX4CCString(error), strsize);
140 static void Warning(const char *s, OSStatus error)
143 (*sWarningHandler)(s, error);
146 static void SetWarningHandler(WarningHandler f) { sWarningHandler = f; }
148 static WarningHandler sWarningHandler;
151 #if DEBUG || CoreAudio_Debug
152 #define XThrowIfError(error, operation) \
154 OSStatus __err = error; \
156 DebugMessageN4("%s:%d: about to throw %s: %s", __FILE__, __LINE__, CAX4CCString(__err).get(), operation);\
158 throw CAXException(operation, __err); \
162 #define XThrowIf(condition, error, operation) \
165 OSStatus __err = error; \
166 DebugMessageN4("%s:%d: about to throw %s: %s", __FILE__, __LINE__, CAX4CCString(__err).get(), operation);\
168 throw CAXException(operation, __err); \
172 #define XRequireNoError(error, label) \
174 OSStatus __err = error; \
176 DebugMessageN4("%s:%d: about to throw %s: %s", __FILE__, __LINE__, CAX4CCString(__err).get(), #error);\
182 #define XAssert(assertion) \
184 if (!(assertion)) { \
185 DebugMessageN3("%s:%d: error: failed assertion: %s", __FILE__, __LINE__, #assertion); \
190 #define XAssertNoError(error) \
192 OSStatus __err = error; \
194 DebugMessageN4("%s:%d: error %s: %s", __FILE__, __LINE__, CAX4CCString(__err).get(), #error);\
199 #define ca_require_noerr(errorCode, exceptionLabel) \
202 int evalOnceErrorCode = (errorCode); \
203 if ( __builtin_expect(0 != evalOnceErrorCode, 0) ) \
205 DebugMessageN5("ca_require_noerr: [%s, %d] (goto %s;) %s:%d", \
206 #errorCode, evalOnceErrorCode, \
210 goto exceptionLabel; \
214 #define ca_verify_noerr(errorCode) \
217 int evalOnceErrorCode = (errorCode); \
218 if ( __builtin_expect(0 != evalOnceErrorCode, 0) ) \
220 DebugMessageN4("ca_verify_noerr: [%s, %d] %s:%d", \
221 #errorCode, evalOnceErrorCode, \
227 #define ca_debug_string(message) \
230 DebugMessageN3("ca_debug_string: %s %s:%d", \
237 #define ca_verify(assertion) \
240 if ( __builtin_expect(!(assertion), 0) ) \
242 DebugMessageN3("ca_verify: %s %s:%d", \
249 #define ca_require(assertion, exceptionLabel) \
252 if ( __builtin_expect(!(assertion), 0) ) \
254 DebugMessageN4("ca_require: %s %s %s:%d", \
259 goto exceptionLabel; \
263 #define ca_check(assertion) \
266 if ( __builtin_expect(!(assertion), 0) ) \
268 DebugMessageN3("ca_check: %s %s:%d", \
276 #define XThrowIfError(error, operation) \
278 OSStatus __err = error; \
280 throw CAXException(operation, __err); \
284 #define XThrowIf(condition, error, operation) \
287 OSStatus __err = error; \
288 throw CAXException(operation, __err); \
292 #define XRequireNoError(error, label) \
294 OSStatus __err = error; \
300 #define XAssert(assertion) \
302 if (!(assertion)) { \
306 #define XAssertNoError(error) \
308 /*OSStatus __err =*/ error; \
311 #define ca_require_noerr(errorCode, exceptionLabel) \
314 if ( __builtin_expect(0 != (errorCode), 0) ) \
316 goto exceptionLabel; \
320 #define ca_verify_noerr(errorCode) \
323 if ( 0 != (errorCode) ) \
328 #define ca_debug_string(message)
330 #define ca_verify(assertion) \
333 if ( !(assertion) ) \
338 #define ca_require(assertion, exceptionLabel) \
341 if ( __builtin_expect(!(assertion), 0) ) \
343 goto exceptionLabel; \
347 #define ca_check(assertion) \
350 if ( !(assertion) ) \
358 #define XThrow(error, operation) XThrowIf(true, error, operation)
359 #define XThrowIfErr(error) XThrowIfError(error, #error)
361 #endif // __CAXException_h__