@@ -3,28 +3,41 @@ public protocol SDLObjectProtocol: AnyObject {
3
3
var pointer : Pointer { get }
4
4
}
5
5
6
- /*
7
- Explore this
8
- https://github.com/swiftlang/swift/blob/main/docs/OptimizationTips.rst#advice-use-unmanaged-references-to-avoid-reference-counting-overhead
9
- */
10
6
11
7
public final class SDLObject < Pointer: Hashable > : SDLObjectProtocol , @unchecked Sendable {
12
- enum Tag {
8
+ /// Used for associating a tag with an SDLObject instance, primarily for debugging and memory-allocation tracking.
9
+ @available ( * , deprecated, message: " Will be removed in a future release " )
10
+ public enum Tag {
11
+ /// A user-defined string for debugging purposes.
13
12
case custom( String )
13
+
14
+ /// A default, no-tag state.
15
+ case empty
14
16
}
15
17
18
+ /// The underlying resource reference managed by this object.
16
19
public let pointer : Pointer
17
20
21
+ /// A callback invoked during deinitialization to clean up the resource.
18
22
private let destroy : ( Pointer ) -> Void
23
+
24
+ /// A debugging or tracking tag for the instance.
19
25
private let tag : Tag
20
26
21
- required init ( _ pointer: Pointer , tag: Tag , destroy: @escaping ( Pointer ) -> Void = { _ in } ) {
27
+
28
+ /// Creates an instance of an SDL object from an underlying pointer reference.
29
+ /// - Parameters:
30
+ /// - pointer: The resource pointer being managed.
31
+ /// - tag: A debugging or memory-allocation tag (default: .empty).
32
+ /// - destroy: A closure invoked during deinitialization to clean up the resource (default: a no-op closure).
33
+ public required init ( _ pointer: Pointer , tag: Tag = . empty, destroy: @escaping ( Pointer ) -> Void = { _ in } ) {
22
34
// print("\(type(of: Pointer.self)): \(#function), \(tag)")
23
35
self . destroy = destroy
24
36
self . pointer = pointer
25
37
self . tag = tag
26
38
}
27
39
40
+ /// Ensures the destroy callback is called with the managed pointer when the SDLObject instance is deallocated.
28
41
deinit {
29
42
/*
30
43
#if DEBUG
@@ -35,16 +48,6 @@ public final class SDLObject<Pointer: Hashable>: SDLObjectProtocol, @unchecked S
35
48
}
36
49
}
37
50
38
- public func SDL_BufferPointer< Value> ( _ allocate: ( UnsafeMutablePointer < Int32 > ) -> UnsafeMutablePointer < Value > ? ) throws ( SDL_Error) -> [ Value ] {
39
- var count : Int32 = 0
40
- guard let pointer = allocate ( & count) else {
41
- throw . error
42
- }
43
- defer { SDL_free ( pointer) }
44
- let bufferPtr = UnsafeMutableBufferPointer . init ( start: pointer, count: Int ( count) )
45
- return Array ( bufferPtr)
46
- }
47
-
48
51
extension SDLObjectProtocol {
49
52
@discardableResult
50
53
@inlinable
@@ -82,3 +85,31 @@ extension SDLObjectProtocol {
82
85
return . success( self )
83
86
}
84
87
}
88
+
89
+ /**
90
+ This function facilitates the allocation and conversion of a buffer pointer
91
+ to an array of values, handling resource cleanup and error management seamlessly.
92
+
93
+ **Example:**
94
+
95
+ ```
96
+ let joysticks = try SDL_BufferPointer(SDL_GetJoysticks)
97
+ ```
98
+
99
+ The closure is responsible for:
100
+ - Modifying the Int32 pointer to indicate the number of elements allocated.
101
+ - Returning a pointer to the allocated buffer or nil on failure.
102
+
103
+ - parameter allocate: A closure that takes an `UnsafeMutablePointer<Int32>` and returns an optional `UnsafeMutablePointer<Value>`.
104
+ - returns: A Swift array containing the elements of the allocated buffer.
105
+ - throws: Throw `SDL_Error.error` when the allocate closure fails to return a valid pointer.
106
+ */
107
+ public func SDL_BufferPointer< Value> ( _ allocate: ( UnsafeMutablePointer < Int32 > ) -> UnsafeMutablePointer < Value > ? ) throws ( SDL_Error) -> [ Value ] {
108
+ var count : Int32 = 0
109
+ guard let pointer = allocate ( & count) else {
110
+ throw . error
111
+ }
112
+ defer { SDL_free ( pointer) }
113
+ let bufferPtr = UnsafeMutableBufferPointer . init ( start: pointer, count: Int ( count) )
114
+ return Array ( bufferPtr)
115
+ }
0 commit comments