comparison 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
comparison
equal deleted inserted replaced
15:7ff919f595d5 16:19885b43130e
6 */ 6 */
7 module dstep.foundation.NSThread; 7 module dstep.foundation.NSThread;
8 8
9 import dstep.foundation.NSArray; 9 import dstep.foundation.NSArray;
10 import dstep.foundation.NSDate; 10 import dstep.foundation.NSDate;
11 import dstep.foundation.NSMutableDictionary; 11 import dstep.foundation.NSDictionary;
12 import dstep.foundation.NSObjCRuntime;
12 import dstep.foundation.NSObject; 13 import dstep.foundation.NSObject;
14 import dstep.foundation.NSString;
13 import dstep.objc.bridge.Bridge; 15 import dstep.objc.bridge.Bridge;
14 import dstep.objc.objc : id; 16 import dstep.objc.objc;
17
15 18
16 import bindings = dstep.foundation.NSThread_bindings; 19 import bindings = dstep.foundation.NSThread_bindings;
17 20
18 const NSString NSWillBecomeMultiThreadedNotification; 21 private
19 const NSString NSDidBecomeSingleThreadedNotification; 22 {
20 const NSString NSThreadWillExitNotification; 23 NSString NSWillBecomeMultiThreadedNotification_;
21 24 NSString NSDidBecomeSingleThreadedNotification_;
22 static this () 25 NSString NSThreadWillExitNotification_;
23 { 26 }
24 NSWillBecomeMultiThreadedNotification = new NSString(bindings.NSWillBecomeMultiThreadedNotification); 27
25 NSDidBecomeSingleThreadedNotification = new NSString(bindings.NSDidBecomeSingleThreadedNotification); 28 NSString NSWillBecomeMultiThreadedNotification ()
26 NSThreadWillExitNotification = new NSString(bindings.NSThreadWillExitNotification); 29 {
27 } 30 if (NSWillBecomeMultiThreadedNotification_)
31 return NSWillBecomeMultiThreadedNotification_;
32
33 return NSWillBecomeMultiThreadedNotification_ = new NSString(bindings.NSWillBecomeMultiThreadedNotification);
34 }
35
36 NSString NSDidBecomeSingleThreadedNotification ()
37 {
38 if (NSDidBecomeSingleThreadedNotification_)
39 return NSDidBecomeSingleThreadedNotification_;
40
41 return NSDidBecomeSingleThreadedNotification_ = new NSString(bindings.NSDidBecomeSingleThreadedNotification);
42 }
43
44 NSString NSThreadWillExitNotification ()
45 {
46 if (NSThreadWillExitNotification_)
47 return NSThreadWillExitNotification_;
48
49 return NSThreadWillExitNotification_ = new NSString(bindings.NSThreadWillExitNotification);
50 }
51
52
53 const TNSThreadPerformAdditions = `
54
55 void performSelectorOnMainThread (SEL aSelector, Object arg, bool wait, NSArray array)
56 {
57 return invokeObjcSelf!(void, "performSelectorOnMainThread:withObject:waitUntilDone:modes", SEL, Object, bool, NSArray)(aSelector, arg, wait, array);
58 }
59
60 void performSelectorOnMainThread (SEL aSelector, Object arg, bool wait)
61 {
62 return invokeObjcSelf!(void, "performSelectorOnMainThread:withObject:waitUntilDone:", SEL, Object, bool)(aSelector, arg, wait);
63 }
64
65 void performSelector (SEL aSelector, NSThread thr, Object arg, bool wait, NSArray array)
66 {
67 return invokeObjcSelf!(void, "performSelector:onThread:withObject:waitUntilDone:modes:", SEL, NSThread, Object, bool, NSArray)(aSelector, thr, arg, wait, array);
68 }
69
70 void performSelector (SEL aSelector, NSThread thr, Object arg, bool wait)
71 {
72 return invokeObjcSelf!(void, "performSelector:onThread:withObject:waitUntilDone:", SEL, NSThread, Object, bool)(aSelector, thr, arg, wait);
73 }
74
75 void performSelectorInBackground (SEL aSelector, Object arg)
76 {
77 return invokeObjcSelf!(void, "performSelectorInBackground:withObject:", SEL, Object)(aSelector, arg);
78 }
79
80 //mixin ObjcBindMethod!(performSelectorOnMainThread, void, "performSelectorOnMainThread:withObject:waitUntilDone:modes:", SEL, Object, bool, NSArray);
81 //mixin ObjcBindMethod!(performSelectorOnMainThread, void, "performSelectorOnMainThread:withObject:waitUntilDone:", SEL, Object, bool);
82 //mixin ObjcBindMethod!(performSelector, void, "performSelector:onThread:withObject:waitUntilDone:modes:", SEL, NSThread, Object, bool, NSArray);
83 //mixin ObjcBindMethod!(performSelector, void, "performSelector:onThread:withObject:waitUntilDone:", SEL, NSThread, Object, bool);
84 //mixin ObjcBindMethod!(performSelectorInBackground, void, "performSelectorInBackground:withObject:", SEL, Object);
85 `;
28 86
29 class NSThread : NSObject 87 class NSThread : NSObject
30 { 88 {
31 mixin ObjcWrap; 89 mixin (ObjcWrap);
90
91 this ()
92 {
93 super(typeof(this).alloc.init.objcObject);
94 }
95
96 typeof(this) init ()
97 {
98 return invokeObjcSelf!(typeof(this), "init");
99 }
32 100
33 static NSThread currentThread () 101 static NSThread currentThread ()
34 { 102 {
35 return invokeObjcSelfClass!(NSThread, "currentThread"); 103 return invokeObjcSuperClass!(NSThread, "currentThread");
36 } 104 }
37 105
38 static void detachNewThreadSelector (SEL selector, Object target, Object argument) 106 static void detachNewThreadSelector (SEL selector, Object target, Object argument)
39 { 107 {
40 return invokeObjcSelfClass!(void, "detachNewThreadSelector:toTarget:withObject:", SEL, Object, Object)(selector, target, argument); 108 return invokeObjcSuperClass!(void, "detachNewThreadSelector:toTarget:withObject:", SEL, Object, Object)(selector, target, argument);
41 } 109 }
42 110
43 static bool isMultiThreaded () 111 static bool isMultiThreaded ()
44 { 112 {
45 return invokeObjcSelfClass!(bool, "isMultiThreaded"); 113 return invokeObjcSuperClass!(bool, "isMultiThreaded");
46 } 114 }
47 115
48 NSMutableDictionary threadDictionary () 116 NSMutableDictionary threadDictionary ()
49 { 117 {
50 return invokeObjcSelf!(NSMutableDictionary, "threadDictionary"); 118 return invokeObjcSelf!(NSMutableDictionary, "threadDictionary");
51 } 119 }
52 120
53 static void sleepUntilDate (NSDate date) 121 static void sleepUntilDate (NSDate date)
54 { 122 {
55 return invokeObjcSelfClass!(void, "sleepUntilDate:", NSDate)(date); 123 return invokeObjcSuperClass!(void, "sleepUntilDate:", NSDate)(date);
56 } 124 }
57 125
58 static void sleepForTimeInterval (double ti) 126 static void sleepForTimeInterval (double ti)
59 { 127 {
60 return invokeObjcSelfClass!(void, "sleepForTimeInterval:", double)(ti); 128 return invokeObjcSuperClass!(void, "sleepForTimeInterval:", double)(ti);
61 } 129 }
62 130
63 static void exit () 131 static void exit ()
64 { 132 {
65 return invokeObjcSelfClass!(void, "exit"); 133 return invokeObjcSuperClass!(void, "exit");
66 } 134 }
67 135
68 static double threadPriority () 136 static double threadPriority ()
69 { 137 {
70 return invokeObjcSelfClass!(double, "threadPriority"); 138 return invokeObjcSuperClass!(double, "threadPriority");
71 } 139 }
72 140
73 static bool setThreadPriority (double p) 141 static bool setThreadPriority (double p)
74 { 142 {
75 return invokeObjcSelfClass!(bool, "setThreadPriority:", double)(p); 143 return invokeObjcSuperClass!(bool, "setThreadPriority:", double)(p);
76 } 144 }
77 145
78 static NSArray callStackReturnAddresses () 146 static NSArray callStackReturnAddresses ()
79 { 147 {
80 return invokeObjcSelfClass!(NSArray, "callStackReturnAddresses"); 148 return invokeObjcSuperClass!(NSArray, "callStackReturnAddresses");
81 } 149 }
82 150
83 void setName (NSString n) 151 void setName (NSString n)
84 { 152 {
85 return invokeObjcSelf!(void, "setName:", NSString)(n); 153 return invokeObjcSelf!(void, "setName:", NSString)(n);
103 bool isMainThread () 171 bool isMainThread ()
104 { 172 {
105 return invokeObjcSelf!(bool, "isMainThread"); 173 return invokeObjcSelf!(bool, "isMainThread");
106 } 174 }
107 175
108 static bool isMainThread () 176 static bool isMainThread_static ()
109 { 177 {
110 return invokeObjcSelfClass!(bool, "isMainThread"); 178 return invokeObjcSuperClass!(bool, "isMainThread");
111 } 179 }
112 180
113 static NSThread mainThread () 181 static NSThread mainThread ()
114 { 182 {
115 return invokeObjcSelfClass!(NSThread, "mainThread"); 183 return invokeObjcSuperClass!(NSThread, "mainThread");
116 }
117
118 Object init ()
119 {
120 return invokeObjcSelf!(Object, "init");
121 }
122
123 this ()
124 {
125 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
126 id result = Bridge.invokeObjcMethod!(id, "init")(objcObject);
127
128 if (result)
129 objcObject = ret;
130
131 dObject = this;
132 } 184 }
133 185
134 Object initWithTarget (Object target, SEL selector, Object argument) 186 Object initWithTarget (Object target, SEL selector, Object argument)
135 { 187 {
136 return invokeObjcSelf!(Object, "initWithTarget:selector:object:", Object, SEL, Object)(target, selector, argument); 188 return invokeObjcSelf!(Object, "initWithTarget:selector:object:", Object, SEL, Object)(target, selector, argument);
137 } 189 }
138 190
139 this (Object target, SEL selector, Object argument) 191 this (Object target, SEL selector, Object argument)
140 { 192 {
141 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); 193 typeof(this).alloc.initWithTarget(target, selector, argument);
142 id result = Bridge.invokeObjcMethod!(id, "initWithTarget:selector:object:", Object, SEL, Object)(objcObject, target, selector, argument);
143
144 if (result)
145 objcObject = ret;
146
147 dObject = this;
148 } 194 }
149 195
150 bool isExecuting () 196 bool isExecuting ()
151 { 197 {
152 return invokeObjcSelf!(bool, "isExecuting"); 198 return invokeObjcSelf!(bool, "isExecuting");
175 void main () 221 void main ()
176 { 222 {
177 return invokeObjcSelf!(void, "main"); 223 return invokeObjcSelf!(void, "main");
178 } 224 }
179 } 225 }
180
181 template TNSThreadPerformAdditions ()
182 {
183 void performSelectorOnMainThread (SEL aSelector, Object arg, bool wait, NSArray array);
184 void performSelectorOnMainThread (SEL aSelector, Object arg, bool wait);
185 void performSelector (SEL aSelector, NSThread thr, Object arg, bool wait, NSArray array);
186 void performSelector (SEL aSelector, NSThread thr, Object arg, bool wait);
187 void performSelectorInBackground (SEL aSelector, Object arg);
188 }
189