diff dstep/foundation/NSException.d @ 16:19885b43130e

Huge update, the bridge actually works now
author Jacob Carlborg <doob@me.com>
date Sun, 03 Jan 2010 22:06:11 +0100
parents 7ff919f595d5
children b9de51448c6b
line wrap: on
line diff
--- a/dstep/foundation/NSException.d	Mon Aug 03 15:31:48 2009 +0200
+++ b/dstep/foundation/NSException.d	Sun Jan 03 22:06:11 2010 +0100
@@ -7,62 +7,184 @@
 module dstep.foundation.NSException;
 
 import dstep.foundation.NSArray;
-import dstep.foundation.NSAssertionHandler;
+import dstep.foundation.NSCoder;
 import dstep.foundation.NSDictionary;
+import dstep.foundation.NSObjCRuntime;
 import dstep.foundation.NSObject;
 import dstep.foundation.NSString;
+import dstep.foundation.NSZone;
 import dstep.objc.bridge.Bridge;
-import dstep.objc.objc : id;
-import dstep.setjmp;
-import dstep.stdarg;
+import dstep.objc.objc;
+
+
 
 import bindings = dstep.foundation.NSException_bindings;
 
 extern (C)
 {
-	alias void function (id) NSUncaughtExceptionHandler *;
+	alias void function (id) NSUncaughtExceptionHandler;
+}
+
+private
+{
+	NSString NSGenericException_;
+	NSString NSRangeException_;
+	NSString NSInvalidArgumentException_;
+	NSString NSInternalInconsistencyException_;
+	NSString NSMallocException_;
+	NSString NSObjectInaccessibleException_;
+	NSString NSObjectNotAvailableException_;
+	NSString NSDestinationInvalidException_;
+	NSString NSPortTimeoutException_;
+	NSString NSInvalidSendPortException_;
+	NSString NSInvalidReceivePortException_;
+	NSString NSPortSendException_;
+	NSString NSPortReceiveException_;
+	NSString NSOldStyleException_;
+}
+
+NSString NSGenericException ()
+{
+	if (NSGenericException_)
+		return NSGenericException_;
+		
+	return NSGenericException_ = new NSString(bindings.NSGenericException);
+}
+
+NSString NSRangeException ()
+{
+	if (NSRangeException_)
+		return NSRangeException_;
+		
+	return NSRangeException_ = new NSString(bindings.NSRangeException);
+}
+
+NSString NSInvalidArgumentException ()
+{
+	if (NSInvalidArgumentException_)
+		return NSInvalidArgumentException_;
+		
+	return NSInvalidArgumentException_ = new NSString(bindings.NSInvalidArgumentException);
+}
+
+NSString NSInternalInconsistencyException ()
+{
+	if (NSInternalInconsistencyException_)
+		return NSInternalInconsistencyException_;
+		
+	return NSInternalInconsistencyException_ = new NSString(bindings.NSInternalInconsistencyException);
+}
+
+NSString NSMallocException ()
+{
+	if (NSMallocException_)
+		return NSMallocException_;
+		
+	return NSMallocException_ = new NSString(bindings.NSMallocException);
+}
+
+NSString NSObjectInaccessibleException ()
+{
+	if (NSObjectInaccessibleException_)
+		return NSObjectInaccessibleException_;
+		
+	return NSObjectInaccessibleException_ = new NSString(bindings.NSObjectInaccessibleException);
 }
 
-const NSString NSGenericException;
-const NSString NSRangeException;
-const NSString NSInvalidArgumentException;
-const NSString NSInternalInconsistencyException;
-const NSString NSMallocException;
-const NSString NSObjectInaccessibleException;
-const NSString NSObjectNotAvailableException;
-const NSString NSDestinationInvalidException;
-const NSString NSPortTimeoutException;
-const NSString NSInvalidSendPortException;
-const NSString NSInvalidReceivePortException;
-const NSString NSPortSendException;
-const NSString NSPortReceiveException;
-const NSString NSOldStyleException;
+NSString NSObjectNotAvailableException ()
+{
+	if (NSObjectNotAvailableException_)
+		return NSObjectNotAvailableException_;
+		
+	return NSObjectNotAvailableException_ = new NSString(bindings.NSObjectNotAvailableException);
+}
+
+NSString NSDestinationInvalidException ()
+{
+	if (NSDestinationInvalidException_)
+		return NSDestinationInvalidException_;
+		
+	return NSDestinationInvalidException_ = new NSString(bindings.NSDestinationInvalidException);
+}
+
+NSString NSPortTimeoutException ()
+{
+	if (NSPortTimeoutException_)
+		return NSPortTimeoutException_;
+		
+	return NSPortTimeoutException_ = new NSString(bindings.NSPortTimeoutException);
+}
+
+NSString NSInvalidSendPortException ()
+{
+	if (NSInvalidSendPortException_)
+		return NSInvalidSendPortException_;
+		
+	return NSInvalidSendPortException_ = new NSString(bindings.NSInvalidSendPortException);
+}
 
-static this ()
+NSString NSInvalidReceivePortException ()
+{
+	if (NSInvalidReceivePortException_)
+		return NSInvalidReceivePortException_;
+		
+	return NSInvalidReceivePortException_ = new NSString(bindings.NSInvalidReceivePortException);
+}
+
+NSString NSPortSendException ()
+{
+	if (NSPortSendException_)
+		return NSPortSendException_;
+		
+	return NSPortSendException_ = new NSString(bindings.NSPortSendException);
+}
+
+NSString NSPortReceiveException ()
 {
-	NSGenericException = new NSString(bindings.NSGenericException);
-	NSRangeException = new NSString(bindings.NSRangeException);
-	NSInvalidArgumentException = new NSString(bindings.NSInvalidArgumentException);
-	NSInternalInconsistencyException = new NSString(bindings.NSInternalInconsistencyException);
-	NSMallocException = new NSString(bindings.NSMallocException);
-	NSObjectInaccessibleException = new NSString(bindings.NSObjectInaccessibleException);
-	NSObjectNotAvailableException = new NSString(bindings.NSObjectNotAvailableException);
-	NSDestinationInvalidException = new NSString(bindings.NSDestinationInvalidException);
-	NSPortTimeoutException = new NSString(bindings.NSPortTimeoutException);
-	NSInvalidSendPortException = new NSString(bindings.NSInvalidSendPortException);
-	NSInvalidReceivePortException = new NSString(bindings.NSInvalidReceivePortException);
-	NSPortSendException = new NSString(bindings.NSPortSendException);
-	NSPortReceiveException = new NSString(bindings.NSPortReceiveException);
-	NSOldStyleException = new NSString(bindings.NSOldStyleException);
+	if (NSPortReceiveException_)
+		return NSPortReceiveException_;
+		
+	return NSPortReceiveException_ = new NSString(bindings.NSPortReceiveException);
+}
+
+NSString NSOldStyleException ()
+{
+	if (NSOldStyleException_)
+		return NSOldStyleException_;
+		
+	return NSOldStyleException_ = new NSString(bindings.NSOldStyleException);
 }
 
+const TNSExceptionRaisingConveniences = `
+
+	static void raise (NSString name, NSString format, ...)
+	{
+		return invokeObjcSuperClass!(void, "raise:format:", NSString, NSString)(name, format);
+	}
+
+	static void raise (NSString name, NSString format, char* argList)
+	{
+		return invokeObjcSuperClass!(void, "raise:format:arguments:", NSString, NSString, char*)(name, format, argList);
+	}
+`;
+
 class NSAssertionHandler : NSObject
 {
-	mixin ObjcWrap;
+	mixin (ObjcWrap);
+	
+	this ()
+	{
+		super(typeof(this).alloc.init.objcObject);
+	}
+	
+	typeof(this) init ()
+	{
+		return invokeObjcSelf!(typeof(this), "init");
+	}
 
 	static NSAssertionHandler currentHandler ()
 	{
-		return invokeObjcSelfClass!(NSAssertionHandler, "currentHandler");
+		return invokeObjcSuperClass!(NSAssertionHandler, "currentHandler");
 	}
 
 	void handleFailureInMethod (SEL selector, Object object, NSString fileName, NSInteger line, NSString description, ...)
@@ -78,12 +200,21 @@
 
 class NSException : NSObject, INSCopying, INSCoding
 {
-	mixin ObjcWrap;
-	mixin TNSExceptionRaisingConveniences;
+	mixin (ObjcWrap);
+	
+	this ()
+	{
+		super(typeof(this).alloc.init.objcObject);
+	}
+	
+	typeof(this) init ()
+	{
+		return invokeObjcSelf!(typeof(this), "init");
+	}
 
 	static NSException exceptionWithName (NSString name, NSString reason, NSDictionary userInfo)
 	{
-		return invokeObjcSelfClass!(NSException, "exceptionWithName:reason:userInfo:", NSString, NSString, NSDictionary)(name, reason, userInfo);
+		return invokeObjcSuperClass!(NSException, "exceptionWithName:reason:userInfo:", NSString, NSString, NSDictionary)(name, reason, userInfo);
 	}
 
 	Object initWithName (NSString aName, NSString aReason, NSDictionary aUserInfo)
@@ -93,13 +224,7 @@
 
 	this (NSString aName, NSString aReason, NSDictionary aUserInfo)
 	{
-		objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
-		id result = Bridge.invokeObjcMethod!(id, "initWithName:reason:userInfo:", NSString, NSString, NSDictionary)(objcObject, aName, aReason, aUserInfo);
-
-		if (result)
-			objcObject = ret;
-
-		dObject = this;
+		typeof(this).alloc.initWithName(aName, aReason, aUserInfo);
 	}
 
 	NSString name ()
@@ -144,26 +269,18 @@
 
 	this (NSCoder aDecoder)
 	{
-		objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
-		id result = Bridge.invokeObjcMethod!(id, "initWithCoder:", NSCoder)(objcObject, aDecoder);
-
-		if (result)
-			objcObject = ret;
-
-		dObject = this;
+		typeof(this).alloc.initWithCoder(aDecoder);
 	}
-}
-
-template TNSExceptionRaisingConveniences ()
-{
+	
+	// TNSExceptionRaisingConveniences
 	static void raise (NSString name, NSString format, ...)
 	{
-		return invokeObjcSelfClass!(void, "raise:format:", NSString, NSString)(name, format);
+		return invokeObjcSuperClass!(void, "raise:format:", NSString, NSString)(name, format);
 	}
-
+	
 	static void raise (NSString name, NSString format, char* argList)
 	{
-		return invokeObjcSelfClass!(void, "raise:format:arguments:", NSString, NSString, char*)(name, format, argList);
+		return invokeObjcSuperClass!(void, "raise:format:arguments:", NSString, NSString, char*)(name, format, argList);
 	}
 }