Skip to content

Commit

Permalink
feat: support DateTime, Duration, and TextRange type args in Vectors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
halildurmus committed Jul 18, 2023
1 parent c03183f commit 8a8ef2a
Show file tree
Hide file tree
Showing 15 changed files with 1,716 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ interface class IIterable<T> extends IInspectable {

/// Creates an instance of [IIterable] from the given [ptr].
///
/// [T] must be of type `bool`, `double`, `Guid`, `int`, `Object?`, `String`,
/// `Uri?`, `IInspectable?` (e.g.`StorageFile?`), `WinRTEnum` (e.g.
/// `DeviceClass`), or `WinRTStruct` (e.g. `BasicGeoposition`).
/// [T] must be of type `bool`, `DateTime`, `double`, `Duration`, `Guid`,
/// `int`, `Object?`, `String`, `Uri?`, `IInspectable?` (e.g. `StorageFile?`),
/// `WinRTEnum` (e.g. `DeviceClass`), or `WinRTStruct` (e.g.
/// `BasicGeoposition`).
///
/// [doubleType] must be specified if [T] is `double`.
/// ```dart
Expand Down
12 changes: 9 additions & 3 deletions packages/windows_foundation/lib/src/collections/iiterator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ abstract interface class IIterator<T> extends IInspectable {

/// Creates an instance of [IIterator] from the given [ptr].
///
/// [T] must be of type `bool`, `double`, `Guid`, `int`, `Object?`, `String`,
/// `Uri?`, `IInspectable?` (e.g.`StorageFile?`), `WinRTEnum` (e.g.
/// `DeviceClass`), or `WinRTStruct` (e.g. `BasicGeoposition`).
/// [T] must be of type `bool`, `DateTime`, `double`, `Duration`, `Guid`,
/// `int`, `Object?`, `String`, `Uri?`, `IInspectable?` (e.g. `StorageFile?`),
/// `WinRTEnum` (e.g. `DeviceClass`), or `WinRTStruct` (e.g.
/// `BasicGeoposition`).
///
/// [doubleType] must be specified if [T] is `double`.
/// ```dart
Expand Down Expand Up @@ -64,6 +65,8 @@ abstract interface class IIterator<T> extends IInspectable {
IntType? intType,
}) {
if (T == bool) return _IIteratorBool.fromPtr(ptr) as IIterator<T>;
if (T == DateTime) return _IIteratorDateTime.fromPtr(ptr) as IIterator<T>;
if (T == Duration) return _IIteratorDuration.fromPtr(ptr) as IIterator<T>;
if (T == Guid) return _IIteratorGuid.fromPtr(ptr) as IIterator<T>;

if (T == double) {
Expand Down Expand Up @@ -149,6 +152,9 @@ abstract interface class IIterator<T> extends IInspectable {
if (T == StorePackageUpdateStatus) {
return _IIteratorStorePackageUpdateStatus.fromPtr(ptr) as IIterator<T>;
}
if (T == TextRange) {
return _IIteratorTextRange.fromPtr(ptr) as IIterator<T>;
}
if (T == TextSegment) {
return _IIteratorTextSegment.fromPtr(ptr) as IIterator<T>;
}
Expand Down
193 changes: 193 additions & 0 deletions packages/windows_foundation/lib/src/collections/iiterator_part.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,69 @@ final class _IIteratorColor extends IIterator<Color> {
}
}

final class _IIteratorDateTime extends IIterator<DateTime> {
_IIteratorDateTime.fromPtr(super.ptr);

@override
DateTime get current {
final retValuePtr = calloc<Int64>();

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

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

return retValuePtr.toDartDateTime();
} finally {
free(retValuePtr);
}
}

@override
(int, {List<DateTime> items}) getMany(int itemsSize) {
final retValuePtr = calloc<Uint32>();
final items = calloc<Int64>(itemsSize);

try {
final hr = ptr.ref.vtable
.elementAt(9)
.cast<
Pointer<
NativeFunction<
HRESULT Function(
VTablePointer lpVtbl,
Uint32 itemsSize,
Pointer<Int64> items,
Pointer<Uint32> retValuePtr)>>>()
.value
.asFunction<
int Function(VTablePointer lpVtbl, int itemsSize,
Pointer<Int64> items, Pointer<Uint32> retValuePtr)>()(
ptr.ref.lpVtbl, itemsSize, items, retValuePtr);

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

return (
retValuePtr.value,
items: items.toDateTimeList(length: retValuePtr.value)
);
} finally {
free(items);
free(retValuePtr);
}
}
}

final class _IIteratorDouble extends IIterator<double> {
_IIteratorDouble.fromPtr(super.ptr);

Expand Down Expand Up @@ -408,6 +471,69 @@ final class _IIteratorDouble extends IIterator<double> {
}
}

final class _IIteratorDuration extends IIterator<Duration> {
_IIteratorDuration.fromPtr(super.ptr);

@override
Duration get current {
final retValuePtr = calloc<Int64>();

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

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

return retValuePtr.toDartDuration();
} finally {
free(retValuePtr);
}
}

@override
(int, {List<Duration> items}) getMany(int itemsSize) {
final retValuePtr = calloc<Uint32>();
final items = calloc<Int64>(itemsSize);

try {
final hr = ptr.ref.vtable
.elementAt(9)
.cast<
Pointer<
NativeFunction<
HRESULT Function(
VTablePointer lpVtbl,
Uint32 itemsSize,
Pointer<Int64> items,
Pointer<Uint32> retValuePtr)>>>()
.value
.asFunction<
int Function(VTablePointer lpVtbl, int itemsSize,
Pointer<Int64> items, Pointer<Uint32> retValuePtr)>()(
ptr.ref.lpVtbl, itemsSize, items, retValuePtr);

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

return (
retValuePtr.value,
items: items.toDurationList(length: retValuePtr.value)
);
} finally {
free(items);
free(retValuePtr);
}
}
}

final class _IIteratorFloat extends IIterator<double> {
_IIteratorFloat.fromPtr(super.ptr);

Expand Down Expand Up @@ -1799,6 +1925,73 @@ final class _IIteratorString extends IIterator<String> {
}
}

final class _IIteratorTextRange extends IIterator<TextRange> {
_IIteratorTextRange.fromPtr(super.ptr);

@override
TextRange get current {
final retValuePtr = calloc<NativeTextRange>();

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

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

return retValuePtr.toDart();
} finally {
free(retValuePtr);
}
}

@override
(int, {List<TextRange> items}) getMany(int itemsSize) {
final retValuePtr = calloc<Uint32>();
final items = calloc<NativeTextRange>(itemsSize);

try {
final hr = ptr.ref.vtable
.elementAt(9)
.cast<
Pointer<
NativeFunction<
HRESULT Function(
VTablePointer lpVtbl,
Uint32 itemsSize,
Pointer<NativeTextRange> items,
Pointer<Uint32> retValuePtr)>>>()
.value
.asFunction<
int Function(
VTablePointer lpVtbl,
int itemsSize,
Pointer<NativeTextRange> items,
Pointer<Uint32> retValuePtr)>()(
ptr.ref.lpVtbl, itemsSize, items, retValuePtr);

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

return (
retValuePtr.value,
items: items.toList(length: retValuePtr.value)
);
} finally {
free(items);
free(retValuePtr);
}
}
}

final class _IIteratorTextSegment extends IIterator<TextSegment> {
_IIteratorTextSegment.fromPtr(super.ptr);

Expand Down
21 changes: 18 additions & 3 deletions packages/windows_foundation/lib/src/collections/ivector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ abstract interface class IVector<T> extends IInspectable
/// [iterableIid] must be the IID of the `IIterable<T>` interface (e.g.
/// `'{9ac00304-83ea-5688-87b6-ae38aab65d0b}'`).
///
/// [T] must be of type `bool`, `double`, `Guid`, `int`, `Object?`, `String`,
/// `Uri?`, `IInspectable?` (e.g.`StorageFile?`), `WinRTEnum` (e.g.
/// `DeviceClass`), or `WinRTStruct` (e.g. `BasicGeoposition`).
/// [T] must be of type `bool`, `DateTime`, `double`, `Duration`, `Guid`,
/// `int`, `Object?`, `String`, `Uri?`, `IInspectable?` (e.g. `StorageFile?`),
/// `WinRTEnum` (e.g. `DeviceClass`), or `WinRTStruct` (e.g.
/// `BasicGeoposition`).
///
/// [doubleType] must be specified if [T] is `double`.
/// ```dart
Expand Down Expand Up @@ -98,6 +99,16 @@ abstract interface class IVector<T> extends IInspectable
return _IVectorBool.fromPtr(ptr, iterableIid: iterableIid) as IVector<T>;
}

if (T == DateTime) {
return _IVectorDateTime.fromPtr(ptr, iterableIid: iterableIid)
as IVector<T>;
}

if (T == Duration) {
return _IVectorDuration.fromPtr(ptr, iterableIid: iterableIid)
as IVector<T>;
}

if (T == Guid) {
return _IVectorGuid.fromPtr(ptr, iterableIid: iterableIid) as IVector<T>;
}
Expand Down Expand Up @@ -232,6 +243,10 @@ abstract interface class IVector<T> extends IInspectable
return _IVectorStorePackageUpdateStatus.fromPtr(ptr,
iterableIid: iterableIid) as IVector<T>;
}
if (T == TextRange) {
return _IVectorTextRange.fromPtr(ptr, iterableIid: iterableIid)
as IVector<T>;
}
if (T == TextSegment) {
return _IVectorTextSegment.fromPtr(ptr, iterableIid: iterableIid)
as IVector<T>;
Expand Down

0 comments on commit 8a8ef2a

Please sign in to comment.