Facilitate usage of opaque pointers

This commit is contained in:
david-swift 2024-09-20 13:27:40 +02:00
parent 80c430c546
commit d6a8f180ef
2 changed files with 33 additions and 4 deletions

View File

@ -13,12 +13,17 @@ public struct Pointer: Sendable {
/// Get the opaque pointer.
public var opaquePointer: OpaquePointer? {
get {
.init(bitPattern: bitPattern)
}
set {
bitPattern = .init(bitPattern: newValue)
}
}
/// Initialize the pointer.
/// - Parameter pointer: The opaque pointer.
public init(_ pointer: OpaquePointer) {
public init(_ pointer: OpaquePointer?) {
bitPattern = .init(bitPattern: pointer)
}

View File

@ -21,8 +21,8 @@ public actor ViewStorage: Sendable {
/// The previous state of the widget.
public var previousState: Widget?
/// The pointer as an opaque pointer, as this is needed with backends interoperating with C or C++.
public var opaquePointer: Pointer? {
/// The pointer as an actual pointer, e.g. for interoperating with C or C++.
public var actualPointer: Pointer? {
get {
pointer as? Pointer
}
@ -31,6 +31,16 @@ public actor ViewStorage: Sendable {
}
}
/// The pointer as an opaque pointer, as this is needed with backends interoperating with C or C++.
public var opaquePointer: OpaquePointer? {
get {
actualPointer?.opaquePointer
}
set {
actualPointer?.opaquePointer = newValue
}
}
/// Initialize a view storage.
/// - Parameters:
/// - pointer: The pointer to the widget, its type depends on the backend.
@ -45,6 +55,20 @@ public actor ViewStorage: Sendable {
self.previousState = state
}
/// Initialize a view storage.
/// - Parameters:
/// - pointer: The opaque pointer.
/// - content: The view's content for container widgets.
public init(
_ pointer: OpaquePointer?,
content: [String: [ViewStorage]] = [:],
state: Widget? = nil
) {
self.pointer = Pointer(pointer)
self.content = content
self.previousState = state
}
/// Set the state under a certain key.
/// - Parameters:
/// - key: The key.