view 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 source

/**
 * Copyright: Copyright (c) 2009 Jacob Carlborg.
 * Authors: Jacob Carlborg
 * Version: Initial created: Aug 3, 2009 
 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
 */
module dstep.foundation.NSException;

import dstep.foundation.NSArray;
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;



import bindings = dstep.foundation.NSException_bindings;

extern (C)
{
	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);
}

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);
}

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 ()
{
	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);
	
	this ()
	{
		super(typeof(this).alloc.init.objcObject);
	}
	
	typeof(this) init ()
	{
		return invokeObjcSelf!(typeof(this), "init");
	}

	static NSAssertionHandler currentHandler ()
	{
		return invokeObjcSuperClass!(NSAssertionHandler, "currentHandler");
	}

	void handleFailureInMethod (SEL selector, Object object, NSString fileName, NSInteger line, NSString description, ...)
	{
		return invokeObjcSelf!(void, "handleFailureInMethod:object:file:lineNumber:description:", SEL, Object, NSString, NSInteger, NSString)(selector, object, fileName, line, description);
	}

	void handleFailureInFunction (NSString functionName, NSString fileName, NSInteger line, NSString description, ...)
	{
		return invokeObjcSelf!(void, "handleFailureInFunction:file:lineNumber:description:", NSString, NSString, NSInteger, NSString)(functionName, fileName, line, description);
	}
}

class NSException : NSObject, INSCopying, INSCoding
{
	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 invokeObjcSuperClass!(NSException, "exceptionWithName:reason:userInfo:", NSString, NSString, NSDictionary)(name, reason, userInfo);
	}

	Object initWithName (NSString aName, NSString aReason, NSDictionary aUserInfo)
	{
		return invokeObjcSelf!(Object, "initWithName:reason:userInfo:", NSString, NSString, NSDictionary)(aName, aReason, aUserInfo);
	}

	this (NSString aName, NSString aReason, NSDictionary aUserInfo)
	{
		typeof(this).alloc.initWithName(aName, aReason, aUserInfo);
	}

	NSString name ()
	{
		return invokeObjcSelf!(NSString, "name");
	}

	NSString reason ()
	{
		return invokeObjcSelf!(NSString, "reason");
	}

	NSDictionary userInfo ()
	{
		return invokeObjcSelf!(NSDictionary, "userInfo");
	}

	NSArray callStackReturnAddresses ()
	{
		return invokeObjcSelf!(NSArray, "callStackReturnAddresses");
	}

	void raise ()
	{
		return invokeObjcSelf!(void, "raise");
	}

	Object copyWithZone (NSZone* zone)
	{
		return invokeObjcSelf!(Object, "copyWithZone:", NSZone*)(zone);
	}

	void encodeWithCoder (NSCoder aCoder)
	{
		return invokeObjcSelf!(void, "encodeWithCoder:", NSCoder)(aCoder);
	}

	Object initWithCoder (NSCoder aDecoder)
	{
		return invokeObjcSelf!(Object, "initWithCoder:", NSCoder)(aDecoder);
	}

	this (NSCoder aDecoder)
	{
		typeof(this).alloc.initWithCoder(aDecoder);
	}
	
	// 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);
	}
}

extern (C)
{
	NSUncaughtExceptionHandler* NSGetUncaughtExceptionHandler ();
	void NSSetUncaughtExceptionHandler (NSUncaughtExceptionHandler* dummy);
}