Skip to content

Commit

Permalink
feat: support (Uri, String) and (Object, Object?) key-value pairs in …
Browse files Browse the repository at this point in the history
…Map (#319)
  • Loading branch information
halildurmus committed Jul 19, 2023
1 parent 8a8ef2a commit c72958d
Show file tree
Hide file tree
Showing 17 changed files with 1,102 additions and 148 deletions.
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

0 comments on commit c72958d

Please sign in to comment.