-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial implementation * move to c definitions and more swift stuff * building * build fixes * fix assigning proper variable * adding tests which verify buffer * fix indent * fix indent * fix comments * run all tests! * use let instead of var
- Loading branch information
1 parent
930217e
commit f5c3a60
Showing
45 changed files
with
24,599 additions
and
4,534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// The real MemoryBuffer.h doesn't provide C definitions, so we need to provide those here | ||
// No pragma once, we might include this twice (once for the regular definitions, once for the workaround) | ||
|
||
#include <objidl.h> | ||
|
||
#ifndef _IMEMORYBUFFERBYTEACCESS_DEFINED | ||
#define _IMEMORYBUFFERBYTEACCESS_DEFINED | ||
typedef interface IMemoryBufferByteAccess IMemoryBufferByteAccess; | ||
#endif | ||
|
||
typedef struct IMemoryBufferByteAccessVtbl | ||
{ | ||
BEGIN_INTERFACE | ||
|
||
HRESULT (STDMETHODCALLTYPE* QueryInterface)(__RPC__in IMemoryBufferByteAccess* This, | ||
REFIID riid, | ||
_COM_Outptr_ void** ppvObject); | ||
ULONG (STDMETHODCALLTYPE* AddRef)(__RPC__in IMemoryBufferByteAccess* This); | ||
ULONG (STDMETHODCALLTYPE* Release)(__RPC__in IMemoryBufferByteAccess* This); | ||
HRESULT (STDMETHODCALLTYPE* GetBuffer)(__RPC__in IMemoryBufferByteAccess* This, | ||
BYTE** value, | ||
_Out_ UINT32* capacity); | ||
|
||
END_INTERFACE | ||
} IMemoryBufferByteAccessVtbl; | ||
|
||
interface IMemoryBufferByteAccess | ||
{ | ||
CONST_VTBL struct IMemoryBufferByteAccessVtbl* lpVtbl; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// The real robuffer.h doesn't provide C definitions, so we need to provide those here | ||
|
||
// No pragma once, we might include this twice (once for the regular definitions, once for the workaround) | ||
|
||
#include <apiset.h> | ||
#include <apisetcconv.h> | ||
#include <objidl.h> | ||
|
||
#ifndef _IBufferByteAccess_DEFINED | ||
#define _IBufferByteAccess_DEFINED | ||
typedef interface C_IBufferByteAccess C_IBufferByteAccess; | ||
#endif /* _IBufferByteAccess_DEFINED */ | ||
|
||
typedef struct C_IBufferByteAccessVtbl | ||
{ | ||
BEGIN_INTERFACE | ||
|
||
HRESULT (STDMETHODCALLTYPE* QueryInterface)(__RPC__in C_IBufferByteAccess* This, | ||
REFIID riid, | ||
_COM_Outptr_ void** ppvObject); | ||
ULONG (STDMETHODCALLTYPE* AddRef)(__RPC__in C_IBufferByteAccess* This); | ||
ULONG (STDMETHODCALLTYPE* Release)(__RPC__in C_IBufferByteAccess* This); | ||
HRESULT (STDMETHODCALLTYPE* Buffer)(__RPC__in C_IBufferByteAccess* This, | ||
BYTE** value); | ||
|
||
END_INTERFACE | ||
} C_IBufferByteAccessVtbl; | ||
|
||
interface C_IBufferByteAccess | ||
{ | ||
CONST_VTBL struct C_IBufferByteAccessVtbl* lpVtbl; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import C_BINDINGS_MODULE | ||
import Foundation | ||
|
||
/// IBufferByteAccess provides direct access to the underlying bytes of an IBuffer. | ||
/// This buffer is only valid for the lifetime of the IBuffer. For a read-only representation | ||
/// of the buffer, access the bytes through the Data property of the IBuffer. | ||
/// [Open Microsoft Documentation](https://learn.microsoft.com/en-us/windows/win32/api/robuffer/ns-robuffer-ibufferbyteaccess) | ||
public protocol IBufferByteAccess: WinRTInterface { | ||
var buffer: UnsafeMutablePointer<UInt8>? { get throws } | ||
} | ||
|
||
public typealias AnyIBufferByteAccess = any IBufferByteAccess | ||
|
||
fileprivate let IID_IBufferByteAccess = IID(Data1: 0x905A0FEF, Data2: 0xBC53, Data3: 0x11DF, Data4: ( 0x8C, 0x49, 0x00, 0x1E, 0x4F, 0xC6, 0x86, 0xDA )) // 905a0fef-bc53-11df-8c49-001e4fc686da | ||
|
||
extension __ABI_ { | ||
public class IBufferByteAccess: IUnknown { | ||
override public class var IID: IID { IID_IBufferByteAccess} | ||
|
||
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 | ||
} | ||
} | ||
|
||
public typealias IBufferByteAccessWrapper = InterfaceWrapperBase<IBufferByteAccessBridge> | ||
} | ||
|
||
public enum IBufferByteAccessBridge: AbiInterfaceBridge { | ||
public static func makeAbi() -> CABI { | ||
return CABI(lpVtbl: &IBufferByteAccessVTable) | ||
} | ||
|
||
public static func from(abi: ComPtr<CABI>?) -> SwiftProjection? { | ||
// This code path is not actually reachable since IBufferByteAccess is not a WinRT interface. | ||
// It is a COM interface which is implemented by any object which implements the IBuffer interface. | ||
// And the IBufferImpl object will correctly have the implementation of this interface, so this isn't needed | ||
assertionFailure("IBufferByteAccessBridge.from not implemented") | ||
return nil | ||
} | ||
|
||
public typealias CABI = C_BINDINGS_MODULE.C_IBufferByteAccess | ||
public typealias SwiftABI = __ABI_.IBufferByteAccess | ||
public typealias SwiftProjection = AnyIBufferByteAccess | ||
} | ||
|
||
private var IBufferByteAccessVTable: C_BINDINGS_MODULE.C_IBufferByteAccessVtbl = .init( | ||
QueryInterface: { __ABI_.IBufferByteAccessWrapper.queryInterface($0, $1, $2) }, | ||
AddRef: { __ABI_.IBufferByteAccessWrapper.addRef($0) }, | ||
Release: { __ABI_.IBufferByteAccessWrapper.release($0) }, | ||
Buffer: { __ABI_.IBufferByteAccessWrapper.buffer($0, $1) } | ||
) | ||
|
||
extension __ABI_.IBufferByteAccessWrapper { | ||
fileprivate static func buffer(_ this: UnsafeMutablePointer<C_BINDINGS_MODULE.C_IBufferByteAccess>?, _ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?) -> HRESULT { | ||
guard let swiftObj = __ABI_.IBufferByteAccessWrapper.tryUnwrapFrom(raw: this) else { return E_INVALIDARG } | ||
guard let buffer else { return E_INVALIDARG } | ||
buffer.pointee = try? swiftObj.buffer | ||
return S_OK | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
swiftwinrt/Resources/Support/IMemoryBufferByteAccess.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import C_BINDINGS_MODULE | ||
import Foundation | ||
|
||
/// IMemoryBufferByteAccess provides direct access to the underlying bytes of an IMemoryBuffer. | ||
/// This buffer is only valid for the lifetime of the buffer. For a read-only representation | ||
/// of the buffer, access the bytes through the Data property of the IBuffer. | ||
/// [Open Microsoft Documentation](https://learn.microsoft.com/en-us/previous-versions/mt297505(v=vs.85)) | ||
public protocol IMemoryBufferByteAccess: WinRTInterface { | ||
var buffer: UnsafeMutableBufferPointer<UInt8>? { get throws } | ||
} | ||
|
||
public typealias AnyIMemoryBufferByteAccess = any IMemoryBufferByteAccess | ||
fileprivate let IID_IMemoryBufferByteAccess = IID(Data1: 0x5B0D3235, Data2: 0x4DBA, Data3: 0x4D44, Data4: ( 0x86, 0x5E, 0x8F, 0x1D, 0x0E, 0x4F, 0xD0, 0x4D )) // 5b0d3235-4dba-4d44-865e-8f1d0e4fd04d | ||
|
||
extension __ABI_ { | ||
public class IMemoryBufferByteAccess: IUnknown { | ||
override public class var IID: IID { IID_IMemoryBufferByteAccess} | ||
|
||
public func Buffer() throws -> UnsafeMutableBufferPointer<UInt8>? { | ||
var capacity: UInt32 = 0 | ||
let ptr = try GetBuffer(&capacity) | ||
return UnsafeMutableBufferPointer(start: ptr, count: Int(capacity)) | ||
} | ||
|
||
fileprivate func GetBuffer(_ capacity: inout UInt32) throws -> UnsafeMutablePointer<UInt8>? { | ||
var buffer: UnsafeMutablePointer<UInt8>? | ||
try perform(as: C_BINDINGS_MODULE.IMemoryBufferByteAccess.self) { pThis in | ||
try CHECKED(pThis.pointee.lpVtbl.pointee.GetBuffer(pThis, &buffer, &capacity)) | ||
} | ||
return buffer | ||
} | ||
} | ||
|
||
public typealias IMemoryBufferByteAccessWrapper = InterfaceWrapperBase<IMemoryBufferByteAccessBridge> | ||
} | ||
|
||
public enum IMemoryBufferByteAccessBridge: AbiInterfaceBridge { | ||
public static func makeAbi() -> CABI { | ||
return CABI(lpVtbl: &IMemoryBufferByteAccessVTable) | ||
} | ||
|
||
public static func from(abi: ComPtr<CABI>?) -> SwiftProjection? { | ||
// This code path is not actually reachable since IMemoryBufferByteAccess is not a WinRT interface. | ||
// It is a COM interface which is implemented by any object which implements the IMemoryBufferReference interface. | ||
// And the IMemoryBufferReferenceImpl object will correctly have the implementation of this interface, so this isn't needed | ||
assertionFailure("IMemoryBufferByteAccessBridge.from not implemented") | ||
return nil | ||
} | ||
|
||
public typealias CABI = C_BINDINGS_MODULE.IMemoryBufferByteAccess | ||
public typealias SwiftABI = __ABI_.IMemoryBufferByteAccess | ||
public typealias SwiftProjection = AnyIMemoryBufferByteAccess | ||
} | ||
|
||
fileprivate var IMemoryBufferByteAccessVTable: C_BINDINGS_MODULE.IMemoryBufferByteAccessVtbl = .init( | ||
QueryInterface: { __ABI_.IMemoryBufferByteAccessWrapper.queryInterface($0, $1, $2) }, | ||
AddRef: { __ABI_.IMemoryBufferByteAccessWrapper.addRef($0) }, | ||
Release: { __ABI_.IMemoryBufferByteAccessWrapper.release($0) }, | ||
GetBuffer: { __ABI_.IMemoryBufferByteAccessWrapper.getBuffer($0, $1, $2) } | ||
) | ||
|
||
extension __ABI_.IMemoryBufferByteAccessWrapper { | ||
fileprivate static func getBuffer(_ this: UnsafeMutablePointer<C_BINDINGS_MODULE.IMemoryBufferByteAccess>?, _ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?, _ count: UnsafeMutablePointer<UInt32>?) -> HRESULT { | ||
guard let swiftObj = __ABI_.IMemoryBufferByteAccessWrapper.tryUnwrapFrom(raw: this) else { return E_INVALIDARG } | ||
guard let buffer, let count else { return E_INVALIDARG } | ||
count.pointee = 0 | ||
buffer.pointee = nil | ||
|
||
guard let swiftBuffer = try? swiftObj.buffer else { return E_FAIL } | ||
count.pointee = UInt32(swiftBuffer.count) | ||
buffer.pointee = swiftBuffer.baseAddress | ||
return S_OK | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.