-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made struct/enum boxing ABI-compatible and added tests.
- Loading branch information
1 parent
bcc55e6
commit a4c5e95
Showing
11 changed files
with
205 additions
and
24 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import WindowsRuntime | ||
import WinRTComponent | ||
import XCTest | ||
|
||
class BoxingTests: WinRTTestCase { | ||
typealias CppBoxing = WinRTComponent.Boxing | ||
typealias SwiftBoxing = WindowsRuntime.IInspectableBoxing | ||
|
||
func testRoundTripOfPrimitiveTypeWithIdentityProjection() throws { | ||
let original = Int32(42) | ||
XCTAssertEqual(try SwiftBoxing.unboxInt32(SwiftBoxing.box(original)), original) | ||
XCTAssertEqual(try SwiftBoxing.unboxInt32(CppBoxing.boxInt32(original)), original) | ||
XCTAssertEqual(try CppBoxing.unboxInt32(SwiftBoxing.box(original)), original) | ||
} | ||
|
||
func testRoundTripOfPrimitiveTypeWithAllocatingProjection() throws { | ||
let original = "Hello" | ||
XCTAssertEqual(try SwiftBoxing.unboxString(SwiftBoxing.box(original)), original) | ||
XCTAssertEqual(try SwiftBoxing.unboxString(CppBoxing.boxString(original)), original) | ||
XCTAssertEqual(try CppBoxing.unboxString(SwiftBoxing.box(original)), original) | ||
} | ||
|
||
func testRoundTripOfEnumType() throws { | ||
let original = MinimalEnum.one | ||
XCTAssertEqual(try SwiftBoxing.unbox(SwiftBoxing.box(original), projection: MinimalEnum.self), original) | ||
XCTAssertEqual(try SwiftBoxing.unbox(CppBoxing.boxMinimalEnum(original), projection: MinimalEnum.self), original) | ||
XCTAssertEqual(try CppBoxing.unboxMinimalEnum(SwiftBoxing.box(original)), original) | ||
} | ||
|
||
func testRoundTripOfStructType() throws { | ||
let original = MinimalStruct(field: 42) | ||
XCTAssertEqual(try SwiftBoxing.unbox(SwiftBoxing.box(original), projection: MinimalStruct.self), original) | ||
XCTAssertEqual(try SwiftBoxing.unbox(CppBoxing.boxMinimalStruct(original), projection: MinimalStruct.self), original) | ||
XCTAssertEqual(try CppBoxing.unboxMinimalStruct(SwiftBoxing.box(original)), original) | ||
} | ||
} |
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,39 @@ | ||
#include "pch.h" | ||
#include "Boxing.h" | ||
#include "Boxing.g.cpp" | ||
|
||
namespace winrt::WinRTComponent::implementation | ||
{ | ||
winrt::Windows::Foundation::IInspectable Boxing::BoxInt32(int32_t value) | ||
{ | ||
return winrt::box_value(value); | ||
} | ||
int32_t Boxing::UnboxInt32(winrt::Windows::Foundation::IInspectable const& value) | ||
{ | ||
return winrt::unbox_value<int32_t>(value); | ||
} | ||
winrt::Windows::Foundation::IInspectable Boxing::BoxString(hstring const& value) | ||
{ | ||
return winrt::box_value(value); | ||
} | ||
hstring Boxing::UnboxString(winrt::Windows::Foundation::IInspectable const& value) | ||
{ | ||
return winrt::unbox_value<hstring>(value); | ||
} | ||
winrt::Windows::Foundation::IInspectable Boxing::BoxMinimalEnum(winrt::WinRTComponent::MinimalEnum const& value) | ||
{ | ||
return winrt::box_value(value); | ||
} | ||
winrt::WinRTComponent::MinimalEnum Boxing::UnboxMinimalEnum(winrt::Windows::Foundation::IInspectable const& value) | ||
{ | ||
return winrt::unbox_value<winrt::WinRTComponent::MinimalEnum>(value); | ||
} | ||
winrt::Windows::Foundation::IInspectable Boxing::BoxMinimalStruct(winrt::WinRTComponent::MinimalStruct const& value) | ||
{ | ||
return winrt::box_value(value); | ||
} | ||
winrt::WinRTComponent::MinimalStruct Boxing::UnboxMinimalStruct(winrt::Windows::Foundation::IInspectable const& value) | ||
{ | ||
return winrt::unbox_value<winrt::WinRTComponent::MinimalStruct>(value); | ||
} | ||
} |
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,25 @@ | ||
#pragma once | ||
#include "Boxing.g.h" | ||
|
||
namespace winrt::WinRTComponent::implementation | ||
{ | ||
struct Boxing | ||
{ | ||
Boxing() = default; | ||
|
||
static winrt::Windows::Foundation::IInspectable BoxInt32(int32_t value); | ||
static int32_t UnboxInt32(winrt::Windows::Foundation::IInspectable const& value); | ||
static winrt::Windows::Foundation::IInspectable BoxString(hstring const& value); | ||
static hstring UnboxString(winrt::Windows::Foundation::IInspectable const& value); | ||
static winrt::Windows::Foundation::IInspectable BoxMinimalEnum(winrt::WinRTComponent::MinimalEnum const& value); | ||
static winrt::WinRTComponent::MinimalEnum UnboxMinimalEnum(winrt::Windows::Foundation::IInspectable const& value); | ||
static winrt::Windows::Foundation::IInspectable BoxMinimalStruct(winrt::WinRTComponent::MinimalStruct const& value); | ||
static winrt::WinRTComponent::MinimalStruct UnboxMinimalStruct(winrt::Windows::Foundation::IInspectable const& value); | ||
}; | ||
} | ||
namespace winrt::WinRTComponent::factory_implementation | ||
{ | ||
struct Boxing : BoxingT<Boxing, implementation::Boxing> | ||
{ | ||
}; | ||
} |
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,16 @@ | ||
import "MinimalTypes.idl"; | ||
|
||
namespace WinRTComponent | ||
{ | ||
static runtimeclass Boxing | ||
{ | ||
static IInspectable BoxInt32(Int32 value); | ||
static Int32 UnboxInt32(IInspectable value); | ||
static IInspectable BoxString(String value); | ||
static String UnboxString(IInspectable value); | ||
static IInspectable BoxMinimalEnum(MinimalEnum value); | ||
static MinimalEnum UnboxMinimalEnum(IInspectable value); | ||
static IInspectable BoxMinimalStruct(MinimalStruct value); | ||
static MinimalStruct UnboxMinimalStruct(IInspectable value); | ||
}; | ||
} |
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