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

feat: support (Uri, String) and (Object, Object?) key-value pairs in Map #319

Merged
merged 1 commit into from
Jul 19, 2023
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
12 changes: 10 additions & 2 deletions packages/windows_foundation/lib/src/collections/ikeyvaluepair.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ abstract interface class IKeyValuePair<K, V> extends IInspectable {

/// Creates an instance of [IKeyValuePair] from the given [ptr].
///
/// [K] must be of type `Guid`, `int`, `String`, or `WinRTEnum` (e.g.
/// `PedometerStepKind`).
/// [K] must be of type `Guid`, `int`, `Object`, `String`, `Uri`, or
/// `WinRTEnum` (e.g. `PedometerStepKind`).
///
/// [V] must be of type `Object?`, `String`, `IInspectable?` (e.g.
/// `IJsonValue?`), or `WinRTEnum` (e.g. `ChatMessageStatus`).
Expand Down Expand Up @@ -130,6 +130,14 @@ abstract interface class IKeyValuePair<K, V> extends IInspectable {
creator: creator, enumKeyCreator: enumKeyCreator);
}

if (K == Uri && V == String) {
return _IKeyValuePairUriString.fromPtr(ptr) as IKeyValuePair<K, V>;
}

if (K == Object && isNullableObjectType<V>()) {
return _IKeyValuePairObjectObject.fromPtr(ptr) as IKeyValuePair<K, V>;
}

throw UnsupportedError('Unsupported key-value pair: ($K, $V)');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,63 @@ final class _IKeyValuePairInt64Inspectable<V> extends IKeyValuePair<int, V> {
}
}

final class _IKeyValuePairObjectObject extends IKeyValuePair<Object, Object?> {
_IKeyValuePairObjectObject.fromPtr(super.ptr);

@override
Object get key {
final retValuePtr = calloc<COMObject>();

final hr = ptr.ref.vtable
.elementAt(6)
.cast<
Pointer<
NativeFunction<
HRESULT Function(VTablePointer lpVtbl,
Pointer<COMObject> retValuePtr)>>>()
.value
.asFunction<
int Function(VTablePointer lpVtbl,
Pointer<COMObject> retValuePtr)>()(ptr.ref.lpVtbl, retValuePtr);

if (FAILED(hr)) {
free(retValuePtr);
throwWindowsException(hr);
}

return IPropertyValue.fromPtr(retValuePtr).value as Object;
}

@override
Object? get value {
final retValuePtr = calloc<COMObject>();

final hr = ptr.ref.vtable
.elementAt(7)
.cast<
Pointer<
NativeFunction<
HRESULT Function(VTablePointer lpVtbl,
Pointer<COMObject> retValuePtr)>>>()
.value
.asFunction<
int Function(VTablePointer lpVtbl,
Pointer<COMObject> retValuePtr)>()(ptr.ref.lpVtbl, retValuePtr);

if (FAILED(hr)) {
free(retValuePtr);
throwWindowsException(hr);
}

if (retValuePtr.isNull) {
free(retValuePtr);
return null;
}

return IPropertyValue.fromPtr(retValuePtr).value;
}
}

final class _IKeyValuePairStringInspectable<V>
extends IKeyValuePair<String, V> {
_IKeyValuePairStringInspectable.fromPtr(super.ptr, {required this.creator});
Expand Down Expand Up @@ -840,6 +897,60 @@ final class _IKeyValuePairUint64Inspectable<V> extends IKeyValuePair<int, V> {
}
}

final class _IKeyValuePairUriString extends IKeyValuePair<Uri, String> {
_IKeyValuePairUriString.fromPtr(super.ptr);

@override
Uri get key {
final retValuePtr = calloc<COMObject>();

final hr = ptr.ref.vtable
.elementAt(6)
.cast<
Pointer<
NativeFunction<
HRESULT Function(VTablePointer lpVtbl,
Pointer<COMObject> retValuePtr)>>>()
.value
.asFunction<
int Function(VTablePointer lpVtbl,
Pointer<COMObject> retValuePtr)>()(ptr.ref.lpVtbl, retValuePtr);

if (FAILED(hr)) {
free(retValuePtr);
throwWindowsException(hr);
}

return retValuePtr.toWinRTUri().toDartUri();
}

@override
String get value {
final retValuePtr = calloc<IntPtr>();

try {
final hr = ptr.ref.vtable
.elementAt(7)
.cast<
Pointer<
NativeFunction<
HRESULT Function(VTablePointer lpVtbl,
Pointer<IntPtr> retValuePtr)>>>()
.value
.asFunction<
int Function(VTablePointer lpVtbl,
Pointer<IntPtr> retValuePtr)>()(ptr.ref.lpVtbl, retValuePtr);

if (FAILED(hr)) throwWindowsException(hr);

return retValuePtr.toDartString();
} finally {
WindowsDeleteString(retValuePtr.value);
free(retValuePtr);
}
}
}

final class _IKeyValuePairWinRTEnumInspectable<K, V>
extends IKeyValuePair<K, V> {
_IKeyValuePairWinRTEnumInspectable.fromPtr(super.ptr,
Expand Down
14 changes: 12 additions & 2 deletions packages/windows_foundation/lib/src/collections/imap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ abstract interface class IMap<K, V> extends IInspectable
/// [iterableIid] must be the IID of the `IIterable<IKeyValuePair<K, V>>`
/// interface (e.g. `'{fe2f3d47-5d47-5499-8374-430c7cda0204}'`).
///
/// [K] must be of type `Guid`, `int`, `String`, or `WinRTEnum` (e.g.
/// `PedometerStepKind`).
/// [K] must be of type `Guid`, `int`, `Object`, `String`, `Uri`, or
/// `WinRTEnum` (e.g. `PedometerStepKind`).
///
/// [V] must be of type `Object?`, `String`, `IInspectable?` (e.g.
/// `IJsonValue?`), or `WinRTEnum` (e.g. `ChatMessageStatus`).
Expand Down Expand Up @@ -202,6 +202,16 @@ abstract interface class IMap<K, V> extends IInspectable
iterableIid: iterableIid);
}

if (K == Uri && V == String) {
return _IMapUriString.fromPtr(ptr, iterableIid: iterableIid)
as IMap<K, V>;
}

if (K == Object && isNullableObjectType<V>()) {
return _IMapObjectObject.fromPtr(ptr, iterableIid: iterableIid)
as IMap<K, V>;
}

throw UnsupportedError('Unsupported key-value pair: ($K, $V)');
}

Expand Down
Loading
Loading