Skip to content

Commit af82803

Browse files
More Foundation types
1 parent 0911812 commit af82803

File tree

8 files changed

+169
-27
lines changed

8 files changed

+169
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dub.selections.json
44

55
# Auto-generated docs
66
docs/
7+
__dummy_docs/
78

89
# Output directory.
910
out/

dub.sdl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ toolchainRequirements gdc="no" frontend=">=2.111" ldc=">=1.40.0"
99

1010
targetPath "out/"
1111

12-
dependency "numem" version=">=1.1.4"
12+
dependency "numem" version=">=1.2.2"
1313

1414
# Default configuration.
1515
configuration "static" {
@@ -22,6 +22,7 @@ configuration "unittest" {
2222
platforms "osx"
2323

2424
lflags "-framework" "Foundation" "-framework" "CoreFoundation" "-all_load"
25+
2526
dependency "silly" version=">=1.1.1"
2627
dependency "numem:hookset-libc" version="*"
2728
}

source/foundation/interop.d

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
Helpful Interop functionality for Foundation
3+
4+
Copyright: Copyright © 2024-2025, Kitsunebi Games EMV
5+
License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6+
Authors: Luna Nielsen
7+
*/
8+
module foundation.interop;
9+
10+
/**
11+
Converts a basic type to a retained Foundation type.
12+
13+
Params:
14+
value = The value to convert
15+
16+
Returns:
17+
The converted value.
18+
*/
19+
auto ns(T)(T value) @nogc {
20+
static if (is(T : string)) {
21+
22+
import foundation.nsstring : NSString;
23+
return NSString.create(value);
24+
} else static if (__traits(isScalar, T)) {
25+
26+
import foundation.nsvalue : NSNumber;
27+
return NSNumber.create(value);
28+
} else static if (is(T : ubyte[]) || is (T == void[])) {
29+
30+
import foundation.nsdata : NSData;
31+
return NSData.create(value.ptr, value.length);
32+
} else static assert(0, T.stringof~" is not a basic D type.");
33+
}
34+
35+
// Tests that all .ns conversions convert to the correct types.
36+
@("ns")
37+
unittest {
38+
import foundation.nsstring : NSString;
39+
import foundation.nsvalue : NSNumber;
40+
import foundation.nsdata : NSData;
41+
42+
assert(("Hello, world!".ns()).isCompatibleWith(NSString.class_));
43+
assert(((cast(byte)1).ns()).isCompatibleWith(NSNumber.class_));
44+
assert(((cast(short)1).ns()).isCompatibleWith(NSNumber.class_));
45+
assert(((cast(int)1).ns()).isCompatibleWith(NSNumber.class_));
46+
assert(((cast(long)1).ns()).isCompatibleWith(NSNumber.class_));
47+
assert(((cast(ubyte)1).ns()).isCompatibleWith(NSNumber.class_));
48+
assert(((cast(ushort)1).ns()).isCompatibleWith(NSNumber.class_));
49+
assert(((cast(uint)1).ns()).isCompatibleWith(NSNumber.class_));
50+
assert(((cast(ulong)1).ns()).isCompatibleWith(NSNumber.class_));
51+
assert(((cast(float)1).ns()).isCompatibleWith(NSNumber.class_));
52+
assert(((cast(double)1).ns()).isCompatibleWith(NSNumber.class_));
53+
assert((true.ns()).isCompatibleWith(NSNumber.class_));
54+
assert(((cast(ubyte[])[0x0, 0x0, 0x0, 0x0]).ns()).isCompatibleWith(NSData.class_));
55+
}

source/foundation/nsdata.d

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ extern(Objective-C)
2121
extern class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
2222
@nogc:
2323

24+
/**
25+
Creates an NSData Instance
26+
*/
27+
static NSData create(void* bytes, NSUInteger length) @selector("dataWithBytes:length:");
28+
2429
/**
2530
A Boolean value that indicates whether or not the class
2631
supports secure coding.

source/foundation/nsobject.d

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ public:
8383
*/
8484
bool isEqual(inout(NSObject) obj) inout @selector("isEqual:");
8585

86-
/**
87-
Returns the class object for the receiver’s class.
88-
*/
89-
Class class_() @selector("class");
90-
91-
/**
92-
Returns the class object for the receiver’s superclass.
93-
*/
94-
Class superclass() @selector("superclass");
95-
9686
/**
9787
Returns a Boolean value that indicates whether the receiver
9888
is an instance of given class or an instance of any class that
@@ -123,6 +113,26 @@ public:
123113
*/
124114
@property NSUInteger hash() @selector("hash");
125115

116+
/**
117+
Returns the class object for the receiver’s class.
118+
*/
119+
static @property Class class_() @selector("class");
120+
121+
/**
122+
Returns the class object for the receiver’s class.
123+
*/
124+
static @property Class superclass() @selector("superclass");
125+
126+
/**
127+
Returns the class object for the receiver’s class.
128+
*/
129+
@property Class selfClass() @selector("class");
130+
131+
/**
132+
Returns the class object for the receiver’s superclass.
133+
*/
134+
@property Class selfSuperclass() @selector("superclass");
135+
126136
/**
127137
Returns the receiver.
128138
*/

source/foundation/nsproto.d

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@ public:
2525
/**
2626
Returns the class object for the receiver’s class.
2727
*/
28-
@property Class class_() @selector("class");
28+
static @property Class class_() @selector("class");
29+
30+
/**
31+
Returns the class object for the receiver’s class.
32+
*/
33+
static @property Class superclass() @selector("superclass");
34+
35+
/**
36+
Returns the class object for the receiver’s class.
37+
*/
38+
@property Class selfClass() @selector("class");
2939

3040
/**
3141
Returns the class object for the receiver’s superclass.
3242
*/
33-
@property Class superclass() @selector("superclass");
43+
@property Class selfSuperclass() @selector("superclass");
3444

3545
/**
3646
Returns a Boolean value that indicates whether the receiver

source/foundation/nsvalue.d

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,61 @@ public:
7373
*/
7474
override NSNumber init() @selector("init");
7575

76+
/**
77+
Creates a NSNumber instance with a bool value.
78+
*/
79+
static NSNumber create(bool value) @selector("numberWithBool:");
80+
81+
/**
82+
Creates a NSNumber instance with a double value.
83+
*/
84+
static NSNumber create(double value) @selector("numberWithDouble:");
85+
86+
/**
87+
Creates a NSNumber instance with a float value.
88+
*/
89+
static NSNumber create(float value) @selector("numberWithFloat:");
90+
91+
/**
92+
Creates a NSNumber instance with a byte value.
93+
*/
94+
static NSNumber create(byte value) @selector("numberWithChar:");
95+
96+
/**
97+
Creates a NSNumber instance with a short value.
98+
*/
99+
static NSNumber create(short value) @selector("numberWithShort:");
100+
101+
/**
102+
Creates a NSNumber instance with a int value.
103+
*/
104+
static NSNumber create(int value) @selector("numberWithInt:");
105+
106+
/**
107+
Creates a NSNumber instance with a long value.
108+
*/
109+
static NSNumber create(long value) @selector("numberWithLongLong:");
110+
111+
/**
112+
Creates a NSNumber instance with a ubyte value.
113+
*/
114+
static NSNumber create(ubyte value) @selector("numberWithUnsignedChar:");
115+
116+
/**
117+
Creates a NSNumber instance with a ushort value.
118+
*/
119+
static NSNumber create(ushort value) @selector("numberWithUnsignedShort:");
120+
121+
/**
122+
Creates a NSNumber instance with a uint value.
123+
*/
124+
static NSNumber create(uint value) @selector("numberWithUnsignedInt:");
125+
126+
/**
127+
Creates a NSNumber instance with a ulong value.
128+
*/
129+
static NSNumber create(ulong value) @selector("numberWithUnsignedLongLong:");
130+
76131
/**
77132
The number object's value expressed as a Boolean value.
78133
*/
@@ -146,62 +201,62 @@ public:
146201
/**
147202
Creates and returns an NSNumber object containing a given value.
148203
*/
149-
this(bool value) @selector("initWithBool:");
204+
NSNumber initWith(bool value) @selector("initWithBool:");
150205

151206
/**
152207
Creates and returns an NSNumber object containing a given value.
153208
*/
154-
this(char value) @selector("initWithChar:");
209+
NSNumber initWith(char value) @selector("initWithChar:");
155210

156211
/**
157212
Creates and returns an NSNumber object containing a given value.
158213
*/
159-
this(double value) @selector("initWithDouble:");
214+
NSNumber initWith(double value) @selector("initWithDouble:");
160215

161216
/**
162217
Creates and returns an NSNumber object containing a given value.
163218
*/
164-
this(float value) @selector("initWithFloat:");
219+
NSNumber initWith(float value) @selector("initWithFloat:");
165220

166221
/**
167222
Creates and returns an NSNumber object containing a given value.
168223
*/
169-
this(float value) @selector("initWithFloat:");
224+
NSNumber initWith(float value) @selector("initWithFloat:");
170225

171226
/**
172227
Creates and returns an NSNumber object containing a given value.
173228
*/
174-
this(int value) @selector("initWithInt:");
229+
NSNumber initWith(int value) @selector("initWithInt:");
175230

176231
/**
177232
Creates and returns an NSNumber object containing a given value.
178233
*/
179-
this(short value) @selector("initWithShort:");
234+
NSNumber initWith(short value) @selector("initWithShort:");
180235

181236
/**
182237
Creates and returns an NSNumber object containing a given value.
183238
*/
184-
this(NSInteger value) @selector("initWithInteger:");
239+
NSNumber initWith(NSInteger value) @selector("initWithInteger:");
185240

186241
/**
187242
Creates and returns an NSNumber object containing a given value.
188243
*/
189-
this(ubyte value) @selector("initWithUnsignedChar:");
244+
NSNumber initWith(ubyte value) @selector("initWithUnsignedChar:");
190245

191246
/**
192247
Creates and returns an NSNumber object containing a given value.
193248
*/
194-
this(ushort value) @selector("initWithUnsignedShort:");
249+
NSNumber initWith(ushort value) @selector("initWithUnsignedShort:");
195250

196251
/**
197252
Creates and returns an NSNumber object containing a given value.
198253
*/
199-
this(uint value) @selector("initWithUnsignedInt:");
254+
NSNumber initWith(uint value) @selector("initWithUnsignedInt:");
200255

201256
/**
202257
Creates and returns an NSNumber object containing a given value.
203258
*/
204-
this(NSUInteger value) @selector("initWithUnsignedInteger:");
259+
NSNumber initWith(NSUInteger value) @selector("initWithUnsignedInteger:");
205260

206261
/**
207262
Gets whether this number is equal to another.

source/foundation/package.d

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
module foundation;
99
import objc;
1010

11+
// Helpers
12+
public import foundation.interop;
13+
public import objc.bridge;
14+
public import objc.block;
15+
1116
// Core Types
1217
public import foundation.core;
1318
public import foundation.nserror;
1419
public import foundation.nsobject;
20+
public import foundation.nsstring;
21+
public import foundation.nsvalue;
1522
public import foundation.nscoder;
1623
public import foundation.nszone;
1724

@@ -20,10 +27,8 @@ public import foundation.nsenumerator;
2027
public import foundation.nsdictionary;
2128
public import foundation.nsset;
2229
public import foundation.nsarray;
23-
public import foundation.nsstring;
2430

2531
// Other
26-
public import foundation.nsvalue;
2732
public import foundation.nsbundle;
2833
public import foundation.nsurl;
2934

0 commit comments

Comments
 (0)