Skip to content

Commit

Permalink
fix assigning proper variable
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbrix committed Jan 23, 2024
1 parent 8ba4b84 commit ce0ac09
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 27 deletions.
4 changes: 2 additions & 2 deletions swiftwinrt/Resources/CWinRT/MemoryBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ typedef struct IMemoryBufferByteAccessVtbl
_COM_Outptr_ void** ppvObject);
ULONG (STDMETHODCALLTYPE* AddRef)(__RPC__in IMemoryBufferByteAccess* This);
ULONG (STDMETHODCALLTYPE* Release)(__RPC__in IMemoryBufferByteAccess* This);
HRESULT (STDMETHODCALLTYPE* Buffer)(__RPC__in IMemoryBufferByteAccess* This,
BYTE** value);
HRESULT (STDMETHODCALLTYPE* GetBuffer)(__RPC__in IMemoryBufferByteAccess* This,
BYTE** value, _Out_ UINT32* capacity);

END_INTERFACE
} IMemoryBufferByteAccessVtbl;
Expand Down
3 changes: 2 additions & 1 deletion swiftwinrt/Resources/Support/IBufferByteAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ extension __ABI_ {
public class IBufferByteAccess: IUnknown {
override public class var IID: IID { IID_IBufferByteAccess}

public func Buffer(_ bytes: UnsafeMutablePointer<UInt8>?) throws {
public func Buffer() throws -> UnsafeMutablePointer<UInt8>? {
var buffer: UnsafeMutablePointer<UInt8>?
try perform(as: C_BINDINGS_MODULE.C_IBufferByteAccess.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.Buffer(pThis, &buffer))
}
return buffer
}
static fileprivate func Buffer(_ this: UnsafeMutablePointer<C_BINDINGS_MODULE.C_IBufferByteAccess>?, _ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?) -> HRESULT {
return E_NOTIMPL
Expand Down
10 changes: 6 additions & 4 deletions swiftwinrt/Resources/Support/IMemoryBufferByteAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ extension __ABI_ {
public class IMemoryBufferByteAccess: IUnknown {
override public class var IID: IID { IID_IMemoryBufferByteAccess}

public func Buffer(_ bytes: UnsafeMutablePointer<UInt8>?) throws {
public func Buffer() throws -> UnsafeMutablePointer<UInt8>? {
var buffer: UnsafeMutablePointer<UInt8>?
var capacity: UInt32 = 0
try perform(as: C_BINDINGS_MODULE.IMemoryBufferByteAccess.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.Buffer(pThis, &buffer))
try CHECKED(pThis.pointee.lpVtbl.pointee.GetBuffer(pThis, &buffer, &capacity))
}
return buffer
}

fileprivate static func Buffer(_ this: UnsafeMutablePointer<C_BINDINGS_MODULE.IMemoryBufferByteAccess>?, _ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?) -> HRESULT {
fileprivate static func GetBuffer(_ this: UnsafeMutablePointer<C_BINDINGS_MODULE.IMemoryBufferByteAccess>?, _ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?, _ count: UnsafeMutablePointer<UInt32>?) -> HRESULT {
return E_NOTIMPL
}
}
Expand Down Expand Up @@ -60,7 +62,7 @@ fileprivate var IMemoryBufferByteAccessVTable: C_BINDINGS_MODULE.IMemoryBufferBy
QueryInterface: { __ABI_.IMemoryBufferByteAccessWrapper.queryInterface($0, $1, $2) },
AddRef: { __ABI_.IMemoryBufferByteAccessWrapper.addRef($0) },
Release: { __ABI_.IMemoryBufferByteAccessWrapper.release($0) },
Buffer: { __ABI_.IMemoryBufferByteAccess.Buffer($0, $1) }
GetBuffer: { __ABI_.IMemoryBufferByteAccess.GetBuffer($0, $1, $2) }
)

extension IMemoryBufferByteAccess {
Expand Down
7 changes: 2 additions & 5 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1001,11 +1001,8 @@ bind_bridge_fullname(type));
w.write(R"(% var data: Data {
get throws {
let bufferByteAccess: %.__ABI_.% = try %.QueryInterface()
var data = Data(count: Int(capacity))
try data.withUnsafeMutableBytes { (bytes: UnsafeMutableRawBufferPointer) in
try bufferByteAccess.Buffer(bytes.baseAddress?.assumingMemoryBound(to: UInt8.self))
}
return data
guard let buffer = try bufferByteAccess.Buffer() else { return Data() }
return Data(bytesNoCopy: buffer, count: Int(capacity), deallocator: .none)
}
}
)", modifier_for(type_definition, info), w.support, type.swift_type_name(), get_swift_name(info));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,8 @@ public enum __IMPL_Windows_Foundation {
fileprivate var data: Data {
get throws {
let bufferByteAccess: test_component.__ABI_.IMemoryBufferByteAccess = try _IMemoryBufferByteAccess.QueryInterface()
var data = Data(count: Int(capacity))
try data.withUnsafeMutableBytes { (bytes: UnsafeMutableRawBufferPointer) in
try bufferByteAccess.Buffer(bytes.baseAddress?.assumingMemoryBound(to: UInt8.self))
}
return data
guard let buffer = try bufferByteAccess.Buffer() else { return Data() }
return Data(bytesNoCopy: buffer, count: Int(capacity), deallocator: .none)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ public enum __IMPL_Windows_Storage_Streams {
fileprivate var data: Data {
get throws {
let bufferByteAccess: test_component.__ABI_.IBufferByteAccess = try _IBufferByteAccess.QueryInterface()
var data = Data(count: Int(capacity))
try data.withUnsafeMutableBytes { (bytes: UnsafeMutableRawBufferPointer) in
try bufferByteAccess.Buffer(bytes.baseAddress?.assumingMemoryBound(to: UInt8.self))
}
return data
guard let buffer = try bufferByteAccess.Buffer() else { return Data() }
return Data(bytesNoCopy: buffer, count: Int(capacity), deallocator: .none)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ public final class Buffer : WinRTClass, IBufferByteAccess, IBuffer {
public var data: Data {
get throws {
let bufferByteAccess: test_component.__ABI_.IBufferByteAccess = try _IBufferByteAccess.QueryInterface()
var data = Data(count: Int(capacity))
try data.withUnsafeMutableBytes { (bytes: UnsafeMutableRawBufferPointer) in
try bufferByteAccess.Buffer(bytes.baseAddress?.assumingMemoryBound(to: UInt8.self))
}
return data
guard let buffer = try bufferByteAccess.Buffer() else { return Data() }
return Data(bytesNoCopy: buffer, count: Int(capacity), deallocator: .none)
}
}
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.storage.streams.buffer.capacity)
Expand Down

0 comments on commit ce0ac09

Please sign in to comment.