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. /// Get the opaque pointer.
public var opaquePointer: OpaquePointer? { public var opaquePointer: OpaquePointer? {
.init(bitPattern: bitPattern) get {
.init(bitPattern: bitPattern)
}
set {
bitPattern = .init(bitPattern: newValue)
}
} }
/// Initialize the pointer. /// Initialize the pointer.
/// - Parameter pointer: The opaque pointer. /// - Parameter pointer: The opaque pointer.
public init(_ pointer: OpaquePointer) { public init(_ pointer: OpaquePointer?) {
bitPattern = .init(bitPattern: pointer) bitPattern = .init(bitPattern: pointer)
} }

View File

@ -21,8 +21,8 @@ public actor ViewStorage: Sendable {
/// The previous state of the widget. /// The previous state of the widget.
public var previousState: Widget? public var previousState: Widget?
/// The pointer as an opaque pointer, as this is needed with backends interoperating with C or C++. /// The pointer as an actual pointer, e.g. for interoperating with C or C++.
public var opaquePointer: Pointer? { public var actualPointer: Pointer? {
get { get {
pointer as? Pointer 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. /// Initialize a view storage.
/// - Parameters: /// - Parameters:
/// - pointer: The pointer to the widget, its type depends on the backend. /// - pointer: The pointer to the widget, its type depends on the backend.
@ -45,6 +55,20 @@ public actor ViewStorage: Sendable {
self.previousState = state 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. /// Set the state under a certain key.
/// - Parameters: /// - Parameters:
/// - key: The key. /// - key: The key.