comparison dstep/foundation/NSException.d @ 14:89f3c3ef1fd2

Added the Foundation framework
author Jacob Carlborg <doob@me.com>
date Mon, 03 Aug 2009 15:23:15 +0200
parents
children 7ff919f595d5
comparison
equal deleted inserted replaced
13:4f583f7e242e 14:89f3c3ef1fd2
1 /**
2 * Copyright: Copyright (c) 2009 Jacob Carlborg.
3 * Authors: Jacob Carlborg
4 * Version: Initial created: Aug 3, 2009
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6 */
7 module dstep.foundation.NSException;
8
9 import dstep.foundation.NSArray;
10 import dstep.foundation.NSAssertionHandler;
11 import dstep.foundation.NSDictionary;
12 import dstep.foundation.NSObject;
13 import dstep.foundation.NSString;
14 import dstep.objc.bridge.Bridge;
15 import dstep.objc.objc : id;
16 import dstep.setjmp;
17 import dstep.stdarg;
18
19 import bindings = dstep.foundation.NSException_bindings;
20
21 extern (C)
22 {
23 alias void function (id) NSUncaughtExceptionHandler *;
24 }
25
26 const NSString NSGenericException;
27 const NSString NSRangeException;
28 const NSString NSInvalidArgumentException;
29 const NSString NSInternalInconsistencyException;
30 const NSString NSMallocException;
31 const NSString NSObjectInaccessibleException;
32 const NSString NSObjectNotAvailableException;
33 const NSString NSDestinationInvalidException;
34 const NSString NSPortTimeoutException;
35 const NSString NSInvalidSendPortException;
36 const NSString NSInvalidReceivePortException;
37 const NSString NSPortSendException;
38 const NSString NSPortReceiveException;
39 const NSString NSOldStyleException;
40
41 static this ()
42 {
43 NSGenericException = new NSString(bindings.NSGenericException);
44 NSRangeException = new NSString(bindings.NSRangeException);
45 NSInvalidArgumentException = new NSString(bindings.NSInvalidArgumentException);
46 NSInternalInconsistencyException = new NSString(bindings.NSInternalInconsistencyException);
47 NSMallocException = new NSString(bindings.NSMallocException);
48 NSObjectInaccessibleException = new NSString(bindings.NSObjectInaccessibleException);
49 NSObjectNotAvailableException = new NSString(bindings.NSObjectNotAvailableException);
50 NSDestinationInvalidException = new NSString(bindings.NSDestinationInvalidException);
51 NSPortTimeoutException = new NSString(bindings.NSPortTimeoutException);
52 NSInvalidSendPortException = new NSString(bindings.NSInvalidSendPortException);
53 NSInvalidReceivePortException = new NSString(bindings.NSInvalidReceivePortException);
54 NSPortSendException = new NSString(bindings.NSPortSendException);
55 NSPortReceiveException = new NSString(bindings.NSPortReceiveException);
56 NSOldStyleException = new NSString(bindings.NSOldStyleException);
57 }
58
59 class NSAssertionHandler : NSObject
60 {
61 mixin ObjcWrap;
62
63 static NSAssertionHandler currentHandler ()
64 {
65 return invokeObjcSelfClass!(NSAssertionHandler, "currentHandler"return result is this.objcObject ? this : (result !is null ? new NSAssertionHandler(result) : null); }
66
67 void handleFailureInMethod (SEL selector, Object object, NSString fileName, NSInteger line, NSString description, ...)
68 {
69 return invokeObjcSelf!(void, "handleFailureInMethod:object:file:lineNumber:description:", SEL, Object, NSString, NSInteger, NSString)(selector, object, fileName, line, description);
70 }
71
72 void handleFailureInFunction (NSString functionName, NSString fileName, NSInteger line, NSString description, ...)
73 {
74 return invokeObjcSelf!(void, "handleFailureInFunction:file:lineNumber:description:", NSString, NSString, NSInteger, NSString)(functionName, fileName, line, description);
75 }
76 }
77
78 class NSException : NSObject, INSCopying, INSCoding
79 {
80 mixin ObjcWrap;
81 mixin TNSExceptionRaisingConveniences;
82
83 static NSException exceptionWithName (NSString name, NSString reason, NSDictionary userInfo)
84 {
85 return invokeObjcSelfClass!(NSException, "exceptionWithName:reason:userInfo:", NSString, NSString, NSDictionary)(name, reason, userInforeturn result is this.objcObject ? this : (result !is null ? new NSException(result) : null); }
86
87 Object initWithName (NSString aName, NSString aReason, NSDictionary aUserInfo)
88 {
89 return invokeObjcSelf!(Object, "initWithName:reason:userInfo:", NSString, NSString, NSDictionary)(aName, aReason, aUserInfo);
90 }
91
92 this (NSString aName, NSString aReason, NSDictionary aUserInfo)
93 {
94 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
95 id result = Bridge.invokeObjcMethod!(id, "initWithName:reason:userInfo:", NSString, NSString, NSDictionary)(objcObject, aName, aReason, aUserInfo);
96
97 if (result)
98 objcObject = ret;
99
100 dObject = this;
101 }
102
103 NSString name ()
104 {
105 return invokeObjcSelf!(NSString, "name");
106 }
107
108 NSString reason ()
109 {
110 return invokeObjcSelf!(NSString, "reason");
111 }
112
113 NSDictionary userInfo ()
114 {
115 return invokeObjcSelf!(NSDictionary, "userInfo");
116 }
117
118 NSArray callStackReturnAddresses ()
119 {
120 return invokeObjcSelf!(NSArray, "callStackReturnAddresses");
121 }
122
123 void raise ()
124 {
125 return invokeObjcSelf!(void, "raise");
126 }
127
128 Object copyWithZone (NSZone* zone)
129 {
130 return invokeObjcSelf!(Object, "copyWithZone:", NSZone*)(zone);
131 }
132
133 void encodeWithCoder (NSCoder aCoder)
134 {
135 return invokeObjcSelf!(void, "encodeWithCoder:", NSCoder)(aCoder);
136 }
137
138 Object initWithCoder (NSCoder aDecoder)
139 {
140 return invokeObjcSelf!(Object, "initWithCoder:", NSCoder)(aDecoder);
141 }
142
143 this (NSCoder aDecoder)
144 {
145 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
146 id result = Bridge.invokeObjcMethod!(id, "initWithCoder:", NSCoder)(objcObject, aDecoder);
147
148 if (result)
149 objcObject = ret;
150
151 dObject = this;
152 }
153 }
154
155 template TNSExceptionRaisingConveniences ()
156 {
157 static void raise (NSString name, NSString format, ...)
158 {
159 return invokeObjcSelfClass!(void, "raise:format:", NSString, NSString)(name, format);
160 }
161
162 static void raise (NSString name, NSString format, char* argList)
163 {
164 return invokeObjcSelfClass!(void, "raise:format:arguments:", NSString, NSString, char*)(name, format, argList);
165 }
166 }
167
168 extern (C)
169 {
170 NSUncaughtExceptionHandler* NSGetUncaughtExceptionHandler ();
171 void NSSetUncaughtExceptionHandler (NSUncaughtExceptionHandler* dummy);
172 }