forked from decarbonization/PlayerKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBException.h
147 lines (126 loc) · 4.1 KB
/
RBException.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* RBException.h
* PlayerKit
*
* Created by Peter MacWhinnie on 5/18/09.
* Copyright 2009 Roundabout Software. All rights reserved.
*
*/
#ifndef RBException_h
#define RBException_h 1
#include <CoreFoundation/CoreFoundation.h>
#include "RBObject.h"
/*!
@defined RBAssert
@abstract Generates an exception if a given condition is false.
@param condition The condition to evaluate for truthiness.
@param description The message to print if the assertion fails.
@param ... The format data associated with the message.
*/
#define RBAssert(condition, description, ...) ({ if(!condition) RBException::HandleFailureInFunction(__PRETTY_FUNCTION__, __FILE__, __LINE__, RBException::DefaultErrorCode, description, ##__VA_ARGS__); })
/*!
@defined RBAssertNoErr
@abstract Generates an exception if a given error code does not equal noErr.
@param error The error code to check.
@param description The message to print if the assertion fails.
@param ... The foramt data associated with the message.
*/
#define RBAssertNoErr(error, description, ...) ({ if(error != noErr) RBException::HandleFailureInFunction(__PRETTY_FUNCTION__, __FILE__, __LINE__, error, description, ##__VA_ARGS__); })
/*!
@defined RBParameterAssert
@abstract Generates an exception if the parameter given is null.
*/
#define RBParameterAssert(parameter) RBAssert(parameter, CFSTR("Invalid parameter not satisfying: %s"), #parameter);
#pragma mark -
/*!
@class RBException
@abstract This is the exception used by any C++ PlayerKit class when it needs
to report a fatal error.
*/
class RBException : public RBObject
{
protected:
//ivars
/* retain */ CFStringRef mDomain;
/* retain */ CFStringRef mReason;
OSStatus mCode;
public:
/*!
@const DefaultErrorCode
@abstract The default error code used by RBException.
*/
static OSStatus const DefaultErrorCode = (-1);
/*!
@const InternalInconsistencyDomain
@abstract Domain of an exception that occurs when an internal assertion
fails and implies an unexpected condition within the called code.
*/
static CFStringRef InternalInconsistencyDomain;
/*! @ignore */ //Used by RBAssert
static void HandleFailureInFunction(const char *function, const char *file, int lineNumber, OSStatus erorrCode, CFStringRef description, ...);
/*!
@method RBException
@abstract The default constructor for RBException.
@param domain The domain of the exception. Defaults to internal inconsistency.
@param reason The reason for the exception. Defaults to an empty string.
*/
RBException(CFStringRef domain = InternalInconsistencyDomain,
CFStringRef reason = CFSTR("(No Reason Given)"),
OSStatus code = DefaultErrorCode) :
RBObject("RBException")
{
//C++ sucks, so these explicit casts
//are actually necessary.
mDomain = (CFStringRef)CFRetain(domain);
mReason = (CFStringRef)CFRetain(reason);
mCode = code;
}
//! @abstract The destructor
~RBException()
{
CFRelease(mDomain);
CFRelease(mReason);
}
//
// These incredibly ugly methods are the only way
// I could get this exception class to work when
// thrown on the stack. Yay C++.
//
RBException(const RBException &inException) :
RBObject("RBException")
{
//C++ sucks, so these explicit casts
//are actually necessary.
mDomain = (CFStringRef)CFRetain(inException.mDomain);
mReason = (CFStringRef)CFRetain(inException.mReason);
mCode = inException.mCode;
}
RBException& operator=(const RBException &inException)
{
mDomain = (CFStringRef)CFRetain(inException.mDomain);
mReason = (CFStringRef)CFRetain(inException.mReason);
mCode = inException.mCode;
return *this;
}
/*!
@method
@abstract Get the domain of an exception.
*/
CFStringRef GetDomain() const { return mDomain; }
/*!
@method
@abstract Get the reason for an exception to be thrown.
*/
CFStringRef GetReason() const { return mReason; }
/*!
@method
@abstract Get the code associated with the exception.
*/
OSStatus GetCode() const { return mCode; }
//! @abstract Copy out a CFErrorRef
CFErrorRef CopyError() const;
private:
/*! @ignore */
RBException() : RBObject("RBException") {};
};
#endif /* RBException_h */