~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/appleutility/CAXException.h

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
Import upstream version 3.4~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      Copyright:      � Copyright 2005 Apple Computer, Inc. All rights reserved.
 
2
 
 
3
        Disclaimer:     IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
 
4
                        ("Apple") in consideration of your agreement to the following terms, and your
 
5
                        use, installation, modification or redistribution of this Apple software
 
6
                        constitutes acceptance of these terms.  If you do not agree with these terms,
 
7
                        please do not use, install, modify or redistribute this Apple software.
 
8
 
 
9
                        In consideration of your agreement to abide by the following terms, and subject
 
10
                        to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
 
11
                        copyrights in this original Apple software (the "Apple Software"), to use,
 
12
                        reproduce, modify and redistribute the Apple Software, with or without
 
13
                        modifications, in source and/or binary forms; provided that if you redistribute
 
14
                        the Apple Software in its entirety and without modifications, you must retain
 
15
                        this notice and the following text and disclaimers in all such redistributions of
 
16
                        the Apple Software.  Neither the name, trademarks, service marks or logos of
 
17
                        Apple Computer, Inc. may be used to endorse or promote products derived from the
 
18
                        Apple Software without specific prior written permission from Apple.  Except as
 
19
                        expressly stated in this notice, no other rights or licenses, express or implied,
 
20
                        are granted by Apple herein, including but not limited to any patent rights that
 
21
                        may be infringed by your derivative works or by other works in which the Apple
 
22
                        Software may be incorporated.
 
23
 
 
24
                        The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
 
25
                        WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
 
26
                        WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
27
                        PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
 
28
                        COMBINATION WITH YOUR PRODUCTS.
 
29
 
 
30
                        IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
 
31
                        CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 
32
                        GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
33
                        ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
 
34
                        OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
 
35
                        (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
 
36
                        ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
37
*/
 
38
/*=============================================================================
 
39
        CAXException.h
 
40
        
 
41
=============================================================================*/
 
42
 
 
43
#ifndef __CAXException_h__
 
44
#define __CAXException_h__
 
45
 
 
46
#if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
 
47
        #include <CoreServices/CoreServices.h>
 
48
#else
 
49
        #include <ConditionalMacros.h>
 
50
        #include <CoreServices.h>
 
51
#endif
 
52
#include "CADebugMacros.h"
 
53
#include <ctype.h>
 
54
#include <stdio.h>
 
55
#include <string.h>
 
56
 
 
57
// An extended exception class that includes the name of the failed operation
 
58
class CAXException {
 
59
public:
 
60
        CAXException(const char *operation, OSStatus err) :
 
61
                mError(err)
 
62
                {
 
63
                        if (operation == NULL)
 
64
                                mOperation[0] = '\0';
 
65
                        else if (strlen(operation) >= sizeof(mOperation)) {
 
66
                                memcpy(mOperation, operation, sizeof(mOperation) - 1);
 
67
                                mOperation[sizeof(mOperation) - 1] = '\0';
 
68
                        } else
 
69
                                strcpy(mOperation, operation);
 
70
                }
 
71
        
 
72
        char *FormatError(char *str) const
 
73
        {
 
74
                return FormatError(str, mError);
 
75
        }
 
76
        
 
77
        char                            mOperation[256];
 
78
        const OSStatus          mError;
 
79
        
 
80
        // -------------------------------------------------
 
81
        
 
82
        typedef void (*WarningHandler)(const char *msg, OSStatus err);
 
83
        
 
84
        /*static void Throw(const char *operation, OSStatus err)
 
85
        {
 
86
                throw CAXException(operation, err);
 
87
        }*/
 
88
        
 
89
        static char *FormatError(char *str, OSStatus error)
 
90
        {
 
91
                // see if it appears to be a 4-char-code
 
92
                *(UInt32 *)(str + 1) = EndianU32_NtoB(error);
 
93
                if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
 
94
                        str[0] = str[5] = '\'';
 
95
                        str[6] = '\0';
 
96
                } else
 
97
                        // no, format it as an integer
 
98
                        sprintf(str, "%ld", error);
 
99
                return str;
 
100
        }
 
101
        
 
102
        static void Warning(const char *s, OSStatus error)
 
103
        {
 
104
                if (sWarningHandler)
 
105
                        (*sWarningHandler)(s, error);
 
106
        }
 
107
        
 
108
        static void SetWarningHandler(WarningHandler f) { sWarningHandler = f; }
 
109
private:
 
110
        static WarningHandler   sWarningHandler;
 
111
};
 
112
 
 
113
#if     DEBUG || CoreAudio_Debug
 
114
        #define XThrowIfError(error, operation) \
 
115
                do {                                                                                                                                    \
 
116
                        OSStatus __err = error;                                                                                         \
 
117
                        if (__err) {                                                                                                                    \
 
118
                                char __buf[12];                                                                                                 \
 
119
                                DebugMessageN2("error %s: %4s\n", CAXException::FormatError(__buf, __err), operation);\
 
120
                                STOP;                                                                                                                   \
 
121
                                throw CAXException(operation, __err);           \
 
122
                        }                                                                                                                                       \
 
123
                } while (0)
 
124
 
 
125
        #define XThrowIf(condition, error, operation) \
 
126
                do {                                                                                                                                    \
 
127
                        if (condition) {                                                                                                        \
 
128
                                OSStatus __err = error;                                                                                 \
 
129
                                char __buf[12];                                                                                                 \
 
130
                                DebugMessageN2("error %s: %4s\n", CAXException::FormatError(__buf, __err), operation);\
 
131
                                STOP;                                                                                                                   \
 
132
                                throw CAXException(operation, __err);           \
 
133
                        }                                                                                                                                       \
 
134
                } while (0)
 
135
 
 
136
#else
 
137
        #define XThrowIfError(error, operation) \
 
138
                do {                                                                                                                                    \
 
139
                        OSStatus __err = error;                                                                                         \
 
140
                        if (__err) {                                                                                                                    \
 
141
                                throw CAXException(operation, __err);           \
 
142
                        }                                                                                                                                       \
 
143
                } while (0)
 
144
 
 
145
        #define XThrowIf(condition, error, operation) \
 
146
                do {                                                                                                                                    \
 
147
                        if (condition) {                                                                                                        \
 
148
                                OSStatus __err = error;                                                                                 \
 
149
                                throw CAXException(operation, __err);           \
 
150
                        }                                                                                                                                       \
 
151
                } while (0)
 
152
 
 
153
#endif
 
154
 
 
155
#define XThrow(error, operation) XThrowIf(true, error, operation)
 
156
#define XThrowIfErr(error) XThrowIfError(error, #error)
 
157
 
 
158
#endif // __CAXException_h__