Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project GUID to Foundation.UUID #134

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions swiftwinrt/Resources/Support/GUID.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import C_BINDINGS_MODULE

extension GUID: @retroactive CustomStringConvertible {
Expand Down Expand Up @@ -37,3 +38,47 @@ extension GUID: @retroactive Equatable {
}

public func ~=(_ lhs: GUID, _ rhs: GUID) -> Bool { lhs == rhs}

public extension Foundation.UUID {
init(from guid: GUID) {
let uuid: uuid_t = (
UInt8((guid.Data1 >> 24) & 0xff),
UInt8((guid.Data1 >> 16) & 0xff),
UInt8((guid.Data1 >> 8) & 0xff),
UInt8(guid.Data1 & 0xff),
UInt8((guid.Data2 >> 8) & 0xff),
UInt8(guid.Data2 & 0xff),
UInt8((guid.Data3 >> 8) & 0xff),
UInt8(guid.Data3 & 0xff),
guid.Data4.0,
guid.Data4.1,
guid.Data4.2,
guid.Data4.3,
guid.Data4.4,
guid.Data4.5,
guid.Data4.6,
guid.Data4.7
)
self.init(uuid: uuid)
}
}

public extension GUID {
init(from uuid: Foundation.UUID) {
self.init(
Data1: UInt32((UInt32(uuid.uuid.0) << 24) | (UInt32(uuid.uuid.1) << 16) | (UInt32(uuid.uuid.2) << 8) | UInt32(uuid.uuid.3)),
Data2: UInt16((UInt16(uuid.uuid.4) << 8) | UInt16(uuid.uuid.5)),
Data3: UInt16((UInt16(uuid.uuid.6) << 8) | UInt16(uuid.uuid.7)),
Data4: (
uuid.uuid.8,
uuid.uuid.9,
uuid.uuid.10,
uuid.uuid.11,
uuid.uuid.12,
uuid.uuid.13,
uuid.uuid.14,
uuid.uuid.15
)
)
}
}
4 changes: 2 additions & 2 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ bind_bridge_fullname(type));
public func getChar16() -> Character { _value as! Character }
public func getBoolean() -> Bool { _value as! Bool }
public func getString() -> String { _value as! String }
public func getGuid() -> %.GUID { _value as! %.GUID }
public func getGuid() -> Foundation.UUID { _value as! Foundation.UUID }
public func getDateTime() -> DateTime { _value as! DateTime }
public func getTimeSpan() -> TimeSpan { _value as! TimeSpan }
public func getPoint() -> Point { _value as! Point }
Expand All @@ -932,7 +932,7 @@ bind_bridge_fullname(type));
%
}

)", w.support, w.support, winrtInterfaceConformance);
)", winrtInterfaceConformance);

}

Expand Down
5 changes: 3 additions & 2 deletions swiftwinrt/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace swiftwinrt
}
if (swift_code && !w.type_namespace.empty() && !w.support.empty())
{
w.write("import Foundation\n");
auto module = w.swift_module;
if (!settings.test)
{
Expand Down Expand Up @@ -121,10 +122,10 @@ namespace swiftwinrt
inline std::string get_full_swift_type_name(writer const& w, const metadata_type* type)
{
auto swift_full_name = type->swift_full_name();
auto last_ns_index = swift_full_name.find_last_of('.');
bool use_full_name = w.full_type_names || !w.writing_generic;
if (last_ns_index != swift_full_name.npos)
if (auto typedefBase = dynamic_cast<const typedef_base*>(type))
{
auto last_ns_index = swift_full_name.find_last_of('.');
auto ns = swift_full_name.substr(0, last_ns_index);
auto typeName = swift_full_name.substr(last_ns_index + 1);
// writing a generic, don't include the '.' because the type name here should
Expand Down
3 changes: 1 addition & 2 deletions swiftwinrt/type_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ namespace swiftwinrt
switch (category) {
case param_category::enum_type:
case param_category::fundamental_type:
case param_category::guid_type:
return true;
default:
return false;
Expand Down Expand Up @@ -434,7 +433,7 @@ namespace swiftwinrt

if (get_full_type_name(type_ref) == "System.Guid")
{
result = true;
result = false;
return;
}
type_def = find_required(type.TypeRef());
Expand Down
2 changes: 1 addition & 1 deletion swiftwinrt/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ namespace swiftwinrt
{
if (typeName == "Guid"sv)
{
static system_type const guid_type{ "GUID"sv, "GUID"sv, "g16"sv };
static system_type const guid_type{ "Foundation.UUID"sv, "GUID"sv, "g16"sv };
return guid_type;
}

Expand Down
3 changes: 1 addition & 2 deletions swiftwinrt/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ namespace swiftwinrt

virtual std::string_view swift_logical_namespace() const override
{
// Currently all mapped types from the System namespace have no namespace
return {};
return "Foundation";
}

virtual std::string_view swift_full_name() const override
Expand Down
7 changes: 4 additions & 3 deletions tests/test_app/WinRTInterfaceImplementations.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import test_component
import WinSDK

Expand Down Expand Up @@ -35,7 +36,7 @@ class MySimpleDelegate : ISimpleDelegate {
func getThat() -> Int32 { that }
}


class MyImplementableDelegate: IIAmImplementable {
private var thisCount = 9
func inInt32(_ value: Int32) -> String {
Expand Down Expand Up @@ -81,7 +82,7 @@ class MyImplementableDelegate: IIAmImplementable {

var enumProperty: Fruit = .apple

var id: test_component.GUID?
var id: Foundation.UUID?
func fireEvent(_ data: String) {
_implementableEvent.invoke(data)
}
Expand All @@ -102,4 +103,4 @@ class MyImplementableDelegate: IIAmImplementable {
}

@EventSource<InDelegate> var implementableEvent: Event<InDelegate>
}
}
7 changes: 4 additions & 3 deletions tests/test_app/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,11 @@ class SwiftWinRTTests : XCTestCase {
print("value: ", classy.startValue ?? "N/A")
XCTAssertEqual(classy.startValue, 23)

let id: test_component.IID? = .init(parsingString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")
let uuidString = "E621E1F8-C36C-495A-93FC-0C247A3E6E5F"
let id: Foundation.UUID? = .init(uuidString: uuidString)
classy.id = id

print("ID: ", classy.id ?? "00000000-0000-0000-0000-0000000")
let unwrappedID = try XCTUnwrap(classy.id)
XCTAssertEqual("\(unwrappedID)", uuidString)
XCTAssertEqual(classy.id, id)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

private var IID___x_ABI_CWindows_CFoundation_CIAsyncAction: test_component.IID {
Expand Down Expand Up @@ -452,12 +453,12 @@ public enum __ABI_Windows_Foundation {
return .init(from: value)
}

open func GetGuidImpl() throws -> test_component.GUID {
open func GetGuidImpl() throws -> Foundation.UUID {
var value: test_component.GUID = .init()
_ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValue.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.GetGuid(pThis, &value))
}
return value
return .init(from: value)
}

open func GetDateTimeImpl() throws -> test_component.DateTime {
Expand Down Expand Up @@ -656,7 +657,7 @@ public enum __ABI_Windows_Foundation {
do {
guard let __unwrapped__instance = IPropertyValueWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let value = try __unwrapped__instance.getGuid()
$1?.initialize(to: value)
$1?.initialize(to: .init(from: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

public enum __IMPL_Windows_Foundation {
Expand Down Expand Up @@ -223,7 +224,7 @@ public enum __IMPL_Windows_Foundation {
public func getChar16() -> Character { _value as! Character }
public func getBoolean() -> Bool { _value as! Bool }
public func getString() -> String { _value as! String }
public func getGuid() -> test_component.GUID { _value as! test_component.GUID }
public func getGuid() -> Foundation.UUID { _value as! Foundation.UUID }
public func getDateTime() -> DateTime { _value as! DateTime }
public func getTimeSpan() -> TimeSpan { _value as! TimeSpan }
public func getPoint() -> Point { _value as! Point }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

private var IID___x_ABI_C__FIIterable_1_T: test_component.IID {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

public enum __IMPL_Windows_Foundation_Collections {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.collectionchange)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.asyncstatus)
Expand Down Expand Up @@ -300,7 +301,7 @@ public protocol IPropertyValue : WinRTInterface {
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.ipropertyvalue.getstring)
func getString() throws -> String
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.ipropertyvalue.getguid)
func getGuid() throws -> test_component.GUID
func getGuid() throws -> Foundation.UUID
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.ipropertyvalue.getdatetime)
func getDateTime() throws -> test_component.DateTime
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.ipropertyvalue.gettimespan)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

private var IID___x_ABI_Ctest__component_CIAsyncMethodsStatics: test_component.IID {
Expand Down Expand Up @@ -735,7 +736,7 @@ public enum __ABI_test_component {
}
}

internal func get_IdImpl() throws -> test_component.GUID? {
internal func get_IdImpl() throws -> Foundation.UUID? {
let (value) = try ComPtrs.initialize { valueAbi in
_ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.get_Id(pThis, &valueAbi))
Expand All @@ -744,7 +745,7 @@ public enum __ABI_test_component {
return test_component.__x_ABI_C__FIReference_1_GUIDWrapper.unwrapFrom(abi: value)
}

internal func put_IdImpl(_ value: test_component.GUID?) throws {
internal func put_IdImpl(_ value: Foundation.UUID?) throws {
let valueWrapper = test_component.__x_ABI_C__FIReference_1_GUIDWrapper(value)
let _value = try! valueWrapper?.toABI { $0 }
_ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in
Expand Down Expand Up @@ -1224,7 +1225,7 @@ public enum __ABI_test_component {
}
}

open func get_IdImpl() throws -> test_component.GUID? {
open func get_IdImpl() throws -> Foundation.UUID? {
let (value) = try ComPtrs.initialize { valueAbi in
_ = try perform(as: __x_ABI_Ctest__component_CIIAmImplementable.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.get_Id(pThis, &valueAbi))
Expand All @@ -1233,7 +1234,7 @@ public enum __ABI_test_component {
return test_component.__x_ABI_C__FIReference_1_GUIDWrapper.unwrapFrom(abi: value)
}

open func put_IdImpl(_ value: test_component.GUID?) throws {
open func put_IdImpl(_ value: Foundation.UUID?) throws {
let valueWrapper = test_component.__x_ABI_C__FIReference_1_GUIDWrapper(value)
let _value = try! valueWrapper?.toABI { $0 }
_ = try perform(as: __x_ABI_Ctest__component_CIIAmImplementable.self) { pThis in
Expand Down Expand Up @@ -1439,7 +1440,7 @@ public enum __ABI_test_component {

put_Id: {
guard let __unwrapped__instance = IIAmImplementableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let value: test_component.GUID? = test_component.__x_ABI_C__FIReference_1_GUIDWrapper.unwrapFrom(abi: ComPtr($1))
let value: Foundation.UUID? = test_component.__x_ABI_C__FIReference_1_GUIDWrapper.unwrapFrom(abi: ComPtr($1))
__unwrapped__instance.id = value
return S_OK
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

private var IID___x_ABI_C__FIAsyncOperationCompletedHandler_1_int: test_component.IID {
Expand Down Expand Up @@ -6442,14 +6443,14 @@ private var IID___x_ABI_C__FIReference_1_GUID: test_component.IID {

internal enum __x_ABI_C__FIReference_1_GUIDBridge: ReferenceBridge {
typealias CABI = __x_ABI_C__FIReference_1_GUID
typealias SwiftProjection = GUID
typealias SwiftProjection = Foundation.UUID
static var IID: test_component.IID { IID___x_ABI_C__FIReference_1_GUID }

static func from(abi: ComPtr<CABI>?) -> SwiftProjection? {
guard let val = abi else { return nil }
var result: GUID = .init()
try! CHECKED(val.get().pointee.lpVtbl.pointee.get_Value(val.get(), &result))
return result
return .init(from: result)
}

static func makeAbi() -> CABI {
Expand All @@ -6475,7 +6476,7 @@ internal var __x_ABI_C__FIReference_1_GUIDVTable: __x_ABI_C__FIReference_1_GUIDV

GetRuntimeClassName: {
_ = $0
let hstring = try! HString("Windows.Foundation.IReference`1<GUID>").detach()
let hstring = try! HString("Windows.Foundation.IReference`1<Foundation.UUID>").detach()
$1!.pointee = hstring
return S_OK
},
Expand All @@ -6489,7 +6490,7 @@ internal var __x_ABI_C__FIReference_1_GUIDVTable: __x_ABI_C__FIReference_1_GUIDV
get_Value: {
guard let __unwrapped__instance = __x_ABI_C__FIReference_1_GUIDWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let result = __unwrapped__instance
$1?.initialize(to: result)
$1?.initialize(to: .init(from: result))
return S_OK
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

public enum __IMPL_test_component {
Expand Down Expand Up @@ -174,7 +175,7 @@ public enum __IMPL_test_component {
set { try! _default.put_EnumPropertyImpl(newValue) }
}

fileprivate var id : test_component.GUID? {
fileprivate var id : Foundation.UUID? {
get { try! _default.get_IdImpl() }
set { try! _default.put_IdImpl(newValue) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

fileprivate func makeIAsyncActionFrom(abi: test_component.IInspectable) -> Any {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

private var IID___x_ABI_Ctest__component_CDelegates_CIInDelegate: test_component.IID {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

public enum __IMPL_test_component_Delegates {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1
// swiftlint:disable all
import Foundation
import Ctest_component

public typealias InDelegate = (String) -> ()
Expand Down
Loading