Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

feat(windows_foundation): add GuidHelper APIs #375

Merged
merged 1 commit into from
Sep 14, 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
1 change: 1 addition & 0 deletions packages/windows_foundation/lib/src/exports.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export 'collections/stringmap.dart';
export 'collections/valueset.dart';
export 'extensions/extensions.dart';
export 'extensions/iunknown_helpers.dart';
export 'guidhelper.dart';
export 'helpers.dart';
export 'iclosable.dart';
export 'imemorybuffer.dart';
Expand Down
40 changes: 40 additions & 0 deletions packages/windows_foundation/lib/src/guidhelper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023, Dart | Windows. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// THIS FILE IS GENERATED AUTOMATICALLY AND SHOULD NOT BE EDITED DIRECTLY.

// ignore_for_file: constant_identifier_names, non_constant_identifier_names
// ignore_for_file: unnecessary_import, unused_import

import 'dart:async';
import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart'
hide DocumentProperties, WinRTStringConversion;

import '../internal.dart';
import 'collections/iiterator.dart';
import 'extensions/extensions.dart';
import 'helpers.dart';
import 'iguidhelperstatics.dart';

/// A class containing static helper methods for working with the Guid type.
class GuidHelper extends IInspectable {
GuidHelper.fromPtr(super.ptr);

static const _className = 'Windows.Foundation.GuidHelper';

static Guid createNewGuid() => createActivationFactory(
IGuidHelperStatics.fromPtr, _className, IID_IGuidHelperStatics)
.createNewGuid();

static Guid get empty => createActivationFactory(
IGuidHelperStatics.fromPtr, _className, IID_IGuidHelperStatics)
.empty;

static bool equals(Guid target, Guid value) => createActivationFactory(
IGuidHelperStatics.fromPtr, _className, IID_IGuidHelperStatics)
.equals(target, value);
}
113 changes: 113 additions & 0 deletions packages/windows_foundation/lib/src/iguidhelperstatics.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) 2023, Dart | Windows. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// THIS FILE IS GENERATED AUTOMATICALLY AND SHOULD NOT BE EDITED DIRECTLY.

// ignore_for_file: constant_identifier_names, non_constant_identifier_names
// ignore_for_file: unnecessary_import, unused_import

import 'dart:async';
import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart'
hide DocumentProperties, WinRTStringConversion;

import '../internal.dart';
import 'collections/iiterator.dart';
import 'extensions/extensions.dart';
import 'helpers.dart';

/// @nodoc
const IID_IGuidHelperStatics = '{59c7966b-ae52-5283-ad7f-a1b9e9678add}';

class IGuidHelperStatics extends IInspectable {
// vtable begins at 6, is 3 entries long.
IGuidHelperStatics.fromPtr(super.ptr);

factory IGuidHelperStatics.from(IInspectable interface) =>
interface.cast(IGuidHelperStatics.fromPtr, IID_IGuidHelperStatics);

Guid createNewGuid() {
final result = calloc<GUID>();

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

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

return result.toDartGuid();
} finally {
free(result);
}
}

Guid get empty {
final value = calloc<GUID>();

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

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

return value.toDartGuid();
} finally {
free(value);
}
}

bool equals(Guid target, Guid value) {
final result = calloc<Bool>();

try {
final targetNativeStructPtr = target.toNativeGUID();
final valueNativeStructPtr = value.toNativeGUID();

final hr = ptr.ref.vtable
.elementAt(8)
.cast<
Pointer<
NativeFunction<
HRESULT Function(
VTablePointer lpVtbl,
Pointer<GUID> target,
Pointer<GUID> value,
Pointer<Bool> result)>>>()
.value
.asFunction<
int Function(VTablePointer lpVtbl, Pointer<GUID> target,
Pointer<GUID> value, Pointer<Bool> result)>()(
ptr.ref.lpVtbl, targetNativeStructPtr, valueNativeStructPtr, result);

free(targetNativeStructPtr);
free(valueNativeStructPtr);

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

return result.value;
} finally {
free(result);
}
}
}
40 changes: 40 additions & 0 deletions packages/windows_foundation/test/guidhelper_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023, Dart | Windows. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

@TestOn('windows')

import 'package:test/test.dart';
import 'package:win32/win32.dart' show isWindowsRuntimeAvailable;
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';

void main() {
if (!isWindowsRuntimeAvailable()) {
print('Skipping tests because Windows Runtime is not available.');
return;
}

group('GuidHelper', () {
test('createNewGuid', () {
final guid = GuidHelper.createNewGuid();
final guidAsString = guid.toString();
expect(guidAsString.length, equals(38));
expect(guidAsString, startsWith('{'));
expect(guidAsString, endsWith('}'));
});

test('empty', () {
final guid = GuidHelper.empty;
expect(guid.toString(), equals('{00000000-0000-0000-0000-000000000000}'));
});

test('equals', () {
final emptyGuid = GuidHelper.empty;
expect(GuidHelper.equals(emptyGuid, emptyGuid), isTrue);
expect(GuidHelper.equals(GuidHelper.createNewGuid(), emptyGuid), isFalse);
});
});

tearDownAll(forceGC);
}
1 change: 1 addition & 0 deletions packages/winrtgen/assets/objects.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"Windows.Foundation.Collections.PropertySet": "Represents a property set, which is a set of [PropertyValue] objects with string keys.",
"Windows.Foundation.Collections.StringMap": "An associative collection, also known as a map or a dictionary.",
"Windows.Foundation.Collections.ValueSet": "Implements a map with keys of type String and values of type Object. Object must be a WinRT [PropertyValue] or [ValueSet]. As a [PropertyValue], it can be any type except [PropertyType] `InspectableArray`. This limitation exists to ensure that the value can be serialized; passed by value across a process boundary.",
"Windows.Foundation.GuidHelper": "A class containing static helper methods for working with the Guid type.",
"Windows.Foundation.IAsyncAction": "Represents an asynchronous action. This is the return type for many Windows Runtime asynchronous methods that don't have a result object, and don't report ongoing progress.",
"Windows.Foundation.IAsyncInfo": "Supports asynchronous actions and operations. `IAsyncInfo` is a base interface for `IAsyncAction`, `IAsyncActionWithProgress<TProgress>`, `IAsyncOperation<TResult>` and `IAsyncOperationWithProgress<TResult,TProgress>`, each of which support combinations of return type and progress for an asynchronous method.",
"Windows.Foundation.IClosable": "Defines a method to release allocated resources.",
Expand Down