Skip to content

Commit 20b1e88

Browse files
committed
More pointer aliases; Unmanaged class (Cocoa)
1 parent 7230408 commit 20b1e88

7 files changed

+68
-0
lines changed

Source/Aliases.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public typealias CInt = Int32
6969
public typealias CLong = Int
7070
public typealias CLongLong = Int64
7171
#if COCOA || ISLAND
72+
//UnsafeMutablePointer
73+
//UnsafePointer
74+
public typealias UnsafeMutableBufferPointer<T> = UnsafeMutablePointer<T>
75+
public typealias UnsafeBufferPointer<T> = UnsafePointer<T>
76+
public typealias UnsafeMutableRawPointer<T> = UnsafeMutablePointer<T>
77+
public typealias UnsafeRawPointer<T> = UnsafePointer<T>
78+
public typealias UnsafeMutableRawBufferPointer<T> = UnsafeMutablePointer<T>
79+
public typealias UnsafeRawBufferPointer<T> = UnsafePointer<T>
80+
7281
public typealias OpaquePointer = UnsafePointer<Void>
7382
#endif
7483
public typealias CShort = Int16

Source/Swift.Island.Darwin.iOS.elements

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Reference Include="gc" />
5454
<Reference Include="Island" />
5555
<Reference Include="rtl" />
56+
<Reference Include="CoreFoundation" />
5657
</ItemGroup>
5758
<Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Elements\RemObjects.Elements.Island.Darwin.targets" />
5859
<Import Project="Swift.Shared.projitems" Label="Swift.Shared" />

Source/Swift.Island.Darwin.macOS.elements

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Reference Include="gc" />
5555
<Reference Include="Island" />
5656
<Reference Include="rtl" />
57+
<Reference Include="CoreFoundation" />
5758
</ItemGroup>
5859
<Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Elements\RemObjects.Elements.Island.Darwin.targets" />
5960
<Import Project="Swift.Shared.projitems" Label="Swift.Shared" />

Source/Swift.Island.Darwin.tvOS.elements

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Reference Include="gc" />
5454
<Reference Include="Island" />
5555
<Reference Include="rtl" />
56+
<Reference Include="CoreFoundation" />
5657
</ItemGroup>
5758
<Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Elements\RemObjects.Elements.Island.Darwin.targets" />
5859
<Import Project="Swift.Shared.projitems" Label="Swift.Shared" />

Source/Swift.Island.Darwin.watchOS.elements

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Reference Include="gc" />
5454
<Reference Include="Island" />
5555
<Reference Include="rtl" />
56+
<Reference Include="CoreFoundation" />
5657
</ItemGroup>
5758
<Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Elements\RemObjects.Elements.Island.Darwin.targets" />
5859
<Import Project="Swift.Shared.projitems" Label="Swift.Shared" />

Source/Swift.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<Compile Include="$(MSBuildThisFileDirectory)Dictionary.Java.swift" />
9595
<Compile Include="$(MSBuildThisFileDirectory)Operators.swift" />
9696
<Compile Include="$(MSBuildThisFileDirectory)Errors.swift" />
97+
<Compile Include="$(MSBuildThisFileDirectory)Unmanaged.swift" />
9798
</ItemGroup>
9899
<ItemGroup>
99100
<Folder Include="Sequences">

Source/Unmanaged.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#if DARWIN
2+
public struct Unmanaged<Instance> where Instance : AnyObject {
3+
4+
#if ISLAND
5+
func autorelease() -> Unmanaged<Instance> {
6+
CFAutorelease(opaque)
7+
return self
8+
}
9+
func release() {
10+
CFRelease(opaque)
11+
}
12+
func retain() -> Unmanaged<Instance> {
13+
CFRetain(opaque)
14+
return self
15+
}
16+
#endif
17+
18+
func takeRetainedValue() -> Instance {
19+
return bridge<Instance>(opaque, BridgeMode.Bridge)
20+
}
21+
22+
func takeUnretainedValue() -> Instance {
23+
return bridge<Instance>(opaque, BridgeMode.Transfer)
24+
}
25+
26+
func toOpaque() -> OpaquePointer {
27+
return opaque
28+
}
29+
30+
static func fromOpaque(_ value: OpaquePointer) -> Unmanaged<Instance> {
31+
Unmanaged<Instance>(with: value)
32+
}
33+
static func passRetained(_ value: Instance) -> Unmanaged<Instance> {
34+
Unmanaged<Instance>(retained: value)
35+
}
36+
static func passUnretained(_ value: Instance) -> Unmanaged<Instance> {
37+
Unmanaged<Instance>(unretained: value)
38+
}
39+
40+
private let opaque: OpaquePointer
41+
42+
private init(with value: OpaquePointer) {
43+
opaque = value
44+
}
45+
46+
private init(withRetained value: Instance) {
47+
opaque = bridge<OpaquePointer>(value, BridgeMode.Retained)
48+
}
49+
50+
private init(withUnretained value: Instance) {
51+
opaque = bridge<OpaquePointer>(value, BridgeMode.Bridge)
52+
}
53+
}
54+
#endif

0 commit comments

Comments
 (0)