view dstep/foundation/NSThread.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.NSThread;

import dstep.foundation.NSArray;
import dstep.foundation.NSDate;
import dstep.foundation.NSDictionary;
import dstep.foundation.NSObjCRuntime;
import dstep.foundation.NSObject;
import dstep.foundation.NSString;
import dstep.objc.bridge.Bridge;
import dstep.objc.objc;


import bindings = dstep.foundation.NSThread_bindings;

private
{
	NSString NSWillBecomeMultiThreadedNotification_;
	NSString NSDidBecomeSingleThreadedNotification_;
	NSString NSThreadWillExitNotification_;
}

NSString NSWillBecomeMultiThreadedNotification ()
{
	if (NSWillBecomeMultiThreadedNotification_)
		return NSWillBecomeMultiThreadedNotification_;
		
	return NSWillBecomeMultiThreadedNotification_ = new NSString(bindings.NSWillBecomeMultiThreadedNotification);
}

NSString NSDidBecomeSingleThreadedNotification ()
{
	if (NSDidBecomeSingleThreadedNotification_)
		return NSDidBecomeSingleThreadedNotification_;
		
	return NSDidBecomeSingleThreadedNotification_ = new NSString(bindings.NSDidBecomeSingleThreadedNotification);
}

NSString NSThreadWillExitNotification ()
{
	if (NSThreadWillExitNotification_)
		return NSThreadWillExitNotification_;
		
	return NSThreadWillExitNotification_ = new NSString(bindings.NSThreadWillExitNotification);
}


const TNSThreadPerformAdditions = `

	void performSelectorOnMainThread (SEL aSelector, Object arg, bool wait, NSArray array)
	{
		return invokeObjcSelf!(void, "performSelectorOnMainThread:withObject:waitUntilDone:modes", SEL, Object, bool, NSArray)(aSelector, arg, wait, array);
	}
	
	void performSelectorOnMainThread (SEL aSelector, Object arg, bool wait)
	{
		return invokeObjcSelf!(void, "performSelectorOnMainThread:withObject:waitUntilDone:", SEL, Object, bool)(aSelector, arg, wait);
	}
	
	void performSelector (SEL aSelector, NSThread thr, Object arg, bool wait, NSArray array)
	{
		return invokeObjcSelf!(void, "performSelector:onThread:withObject:waitUntilDone:modes:", SEL, NSThread, Object, bool, NSArray)(aSelector, thr, arg, wait, array);
	}
	
	void performSelector (SEL aSelector, NSThread thr, Object arg, bool wait)
	{
		return invokeObjcSelf!(void, "performSelector:onThread:withObject:waitUntilDone:", SEL, NSThread, Object, bool)(aSelector, thr, arg, wait);
	}
	
	void performSelectorInBackground (SEL aSelector, Object arg)
	{
		return invokeObjcSelf!(void, "performSelectorInBackground:withObject:", SEL, Object)(aSelector, arg);
	}
	
	//mixin ObjcBindMethod!(performSelectorOnMainThread, void, "performSelectorOnMainThread:withObject:waitUntilDone:modes:", SEL, Object, bool, NSArray);
	//mixin ObjcBindMethod!(performSelectorOnMainThread, void, "performSelectorOnMainThread:withObject:waitUntilDone:", SEL, Object, bool);
	//mixin ObjcBindMethod!(performSelector, void, "performSelector:onThread:withObject:waitUntilDone:modes:", SEL, NSThread, Object, bool, NSArray);
	//mixin ObjcBindMethod!(performSelector, void, "performSelector:onThread:withObject:waitUntilDone:", SEL, NSThread, Object, bool);
	//mixin ObjcBindMethod!(performSelectorInBackground, void, "performSelectorInBackground:withObject:", SEL, Object);
`;

class NSThread : NSObject
{
	mixin (ObjcWrap);
	
	this ()
	{
		super(typeof(this).alloc.init.objcObject);
	}
	
	typeof(this) init ()
	{
		return invokeObjcSelf!(typeof(this), "init");
	}

	static NSThread currentThread ()
	{
		return invokeObjcSuperClass!(NSThread, "currentThread");
	}

	static void detachNewThreadSelector (SEL selector, Object target, Object argument)
	{
		return invokeObjcSuperClass!(void, "detachNewThreadSelector:toTarget:withObject:", SEL, Object, Object)(selector, target, argument);
	}

	static bool isMultiThreaded ()
	{
		return invokeObjcSuperClass!(bool, "isMultiThreaded");
	}

	NSMutableDictionary threadDictionary ()
	{
		return invokeObjcSelf!(NSMutableDictionary, "threadDictionary");
	}

	static void sleepUntilDate (NSDate date)
	{
		return invokeObjcSuperClass!(void, "sleepUntilDate:", NSDate)(date);
	}

	static void sleepForTimeInterval (double ti)
	{
		return invokeObjcSuperClass!(void, "sleepForTimeInterval:", double)(ti);
	}

	static void exit ()
	{
		return invokeObjcSuperClass!(void, "exit");
	}

	static double threadPriority ()
	{
		return invokeObjcSuperClass!(double, "threadPriority");
	}

	static bool setThreadPriority (double p)
	{
		return invokeObjcSuperClass!(bool, "setThreadPriority:", double)(p);
	}

	static NSArray callStackReturnAddresses ()
	{
		return invokeObjcSuperClass!(NSArray, "callStackReturnAddresses");
	}

	void setName (NSString n)
	{
		return invokeObjcSelf!(void, "setName:", NSString)(n);
	}

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

	NSUInteger stackSize ()
	{
		return invokeObjcSelf!(NSUInteger, "stackSize");
	}

	void setStackSize (NSUInteger s)
	{
		return invokeObjcSelf!(void, "setStackSize:", NSUInteger)(s);
	}

	bool isMainThread ()
	{
		return invokeObjcSelf!(bool, "isMainThread");
	}

	static bool isMainThread_static ()
	{
		return invokeObjcSuperClass!(bool, "isMainThread");
	}

	static NSThread mainThread ()
	{
		return invokeObjcSuperClass!(NSThread, "mainThread");
	}

	Object initWithTarget (Object target, SEL selector, Object argument)
	{
		return invokeObjcSelf!(Object, "initWithTarget:selector:object:", Object, SEL, Object)(target, selector, argument);
	}

	this (Object target, SEL selector, Object argument)
	{
		typeof(this).alloc.initWithTarget(target, selector, argument);
	}

	bool isExecuting ()
	{
		return invokeObjcSelf!(bool, "isExecuting");
	}

	bool isFinished ()
	{
		return invokeObjcSelf!(bool, "isFinished");
	}

	bool isCancelled ()
	{
		return invokeObjcSelf!(bool, "isCancelled");
	}

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

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

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