1 module objc.runtime;
2 
3 version(D_ObjectiveC):
4 
5 NSString ns(string str)
6 {
7     import core.memory;
8     str~= '\0';
9     // scope(exit) GC.free(cast(void*)str.ptr);
10     return NSString.alloc.initWithUTF8String(str.ptr);
11 }
12 
13 
14 extern(Objective-C):
15 
16 
17 public import core.attribute:selector;
18 alias BOOL = bool;
19 enum YES = true;
20 enum NO = false;
21 
22 ///Type used to represent elapsed time in seconds.
23 alias CFTimeInterval = double;
24 
25 version(watchOS)
26 {
27     alias NSUInteger = uint;
28     alias NSInteger = int;
29 } 
30 else
31 {
32     alias NSInteger = long;
33     alias NSUInteger = ulong;
34 }
35 extern class NSObject
36 {
37     static NSObject alloc() @selector("alloc");
38     NSObject initialize() @selector("init");
39     ///Increments the receiver’s reference count.
40     NSObject retain() @selector("retain");
41     ///Decrements the receiver’s reference count.
42     void release() @selector("release");
43     ///Decrements the receiver’s retain count at the end of the current autorelease pool block.
44     NSObject autorelease() @selector("autorelease");
45 
46 }
47 
48 /** 
49  * Used for NSObjects. It will define a new
50  *  alloc, init and an opCall for that.
51  */
52 mixin template ObjectiveCOpCall()
53 {
54     extern(Objective-C)
55     {
56         override static typeof(this) alloc() @selector("alloc");
57         override typeof(this) initialize() @selector("init");
58     }
59     extern(D) final static typeof(this) opCall()
60     {
61         return alloc.initialize;
62     }
63 }
64 
65 extern(C) void NSLog(NSString str, ...);
66 
67 
68 extern(Objective-C)
69 extern class NSString
70 {
71     static NSString alloc() @selector("alloc");
72     NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:");
73 
74     static NSString stringWithCString(char* stringWithCString, size_t encoding);
75     static size_t defaultCStringEncoding();
76     void release() @selector("release");
77 }
78 
79 private extern(C) void* _D4objc7runtime7NSArray7__ClassZ = null;
80 extern class NSArray : NSObject
81 {
82     NSArray init() @selector("init");
83     ///Creates and returns an empty array.
84     @selector("array")
85     static NSArray array();
86 
87     ///Creates and returns an array containing the objects in another given array.
88     @selector("arrayWithArray:")
89     static NSArray array(NSArray array);
90 
91     ///Creates and returns an array containing a given object.
92     @selector("arrayWithObject:")
93     static NSArray array(NSObject object);  
94 
95     ///Creates and returns an array containing the objects in the argument list.
96     @selector("arrayWithObjects:")
97     static NSArray array(NSObject objects, ...);
98 
99     ///Creates and returns an array that includes a given number of objects from a given C array.
100     @selector("arrayWithObjects:count:")
101     static NSArray array(NSObject* objects, NSUInteger count); 
102 
103     ///The number of objects in the array.
104     @selector("count")
105     NSUInteger count();
106     alias length = count;
107 
108     ///Returns the object located at the specified index.
109     @selector("objectAtIndex:")
110     NSObject objectAtIndex(NSUInteger index);
111 }
112 
113 alias NSArray_(T) = NSArray;
114 
115 struct NSArrayD(T)
116 {
117     NSArray arr = void;
118     alias arr this;
119 
120     auto opAssign(NSArray arr)
121     {
122         this.arr = arr;
123         return this;
124     }
125 
126     extern(D) pragma(inline, true) T opIndex(size_t index)
127     {
128         return cast(T)cast(void*)arr.objectAtIndex(index);
129     }
130     extern(D) final int opApply(scope int delegate(T) dg)
131     {
132         int result = 0;
133         NSUInteger l = arr.count;
134         for(int i = 0; i < l; i++)
135         {
136             T item = opIndex(i);
137             result = dg(item);
138             if (result)
139                 break;
140         }
141         return result;
142     }
143 }
144 
145 extern class NSDictionary : NSObject
146 {
147     static NSDictionary dictionary();
148 }
149 
150 extern class NSError{}
151 struct NSRange
152 {
153     NSUInteger length;
154     NSUInteger location;
155 }
156 extern class NSData : NSObject{}