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

Commit

Permalink
refactor(winrtgen): rename ArrayStyle enum to ArrayPassingStyle (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
halildurmus authored Jul 11, 2023
1 parent 81584de commit 96d5a4e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
27 changes: 27 additions & 0 deletions packages/winrtgen/lib/src/models/array_passing_style.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.

/// Represents the various array-passing styles in WinRT.
/// See https://learn.microsoft.com/uwp/winrt-cref/winrt-type-system#array-parameters
enum ArrayPassingStyle {
/// Used when the caller provides an array for the method to fill, up to a
/// maximum array size.
///
/// In this style, the array size parameter is an `in` parameter, while the
/// array parameter is an `out` parameter.
fill,

/// Used when the caller provides an array to the method.
///
/// In this style, the array size parameter and the array parameter are both
/// `in` parameters.
pass,

/// Used when the caller receives an array that was allocated by the method.
///
/// In this style, the array size parameter and the array parameter are both
/// `out` parameters. Additionally, the array parameter is passed by
/// reference (that is, `ArrayType**`, rather than `ArrayType*`).
receive
}
18 changes: 0 additions & 18 deletions packages/winrtgen/lib/src/models/array_style.dart

This file was deleted.

2 changes: 1 addition & 1 deletion packages/winrtgen/lib/src/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

export 'array_style.dart';
export 'array_passing_style.dart';
export 'generic_type.dart';
export 'json.dart';
export 'projection_kind.dart';
Expand Down
8 changes: 5 additions & 3 deletions packages/winrtgen/lib/src/projections/parameter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ base class ParameterProjection {
// projected as List. See the ArrayParameterProjection class.
if (parameter.isSimpleArraySizeParam) {
final identifier = parameter.toArrayParamName();
return switch (parameter.arrayStyle) {
ArrayStyle.fill || ArrayStyle.receive => '${identifier}Size',
ArrayStyle.pass => '$identifier.length',
return switch (parameter.arrayPassingStyle) {
ArrayPassingStyle.fill ||
ArrayPassingStyle.receive =>
'${identifier}Size',
ArrayPassingStyle.pass => '$identifier.length',
};
}

Expand Down
16 changes: 9 additions & 7 deletions packages/winrtgen/lib/src/projections/types/array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ abstract base class ArrayParameterProjection extends ParameterProjection {

/// Returns the appropriate projection for the parameter.
factory ArrayParameterProjection.create(Parameter param) {
final Parameter(:arrayStyle, :parent, :projectionKind) = param;
final Parameter(:arrayPassingStyle, :parent, :projectionKind) = param;
try {
return switch (arrayStyle) {
ArrayStyle.fill => FillArrayParameterProjection(param),
ArrayStyle.pass when projectionKind == ProjectionKind.stringArray =>
return switch (arrayPassingStyle) {
ArrayPassingStyle.fill => FillArrayParameterProjection(param),
ArrayPassingStyle.pass
when projectionKind == ProjectionKind.stringArray =>
StringPassArrayParameterProjection(param),
ArrayStyle.pass when projectionKind == ProjectionKind.structArray =>
ArrayPassingStyle.pass
when projectionKind == ProjectionKind.structArray =>
StructPassArrayParameterProjection(param),
ArrayStyle.pass => PassArrayParameterProjection(param),
ArrayStyle.receive => ReceiveArrayParameterProjection(param),
ArrayPassingStyle.pass => PassArrayParameterProjection(param),
ArrayPassingStyle.receive => ReceiveArrayParameterProjection(param),
};
} catch (_) {
print("Failed to project parameter '$param' from '$parent'.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:winmd/winmd.dart';

import '../../exception/exception.dart';
import '../../models/models.dart';
import '../../projections/type.dart';
import 'type_identifier_helpers.dart';
Expand All @@ -15,7 +16,7 @@ extension ParameterHelpers on Parameter {
..attributes = attributes
..name = name;

ArrayStyle get arrayStyle {
ArrayPassingStyle get arrayPassingStyle {
assert(isReferenceType || isSimpleArraySizeParam || isSimpleArrayType);

var arrayParam = this;
Expand All @@ -33,12 +34,20 @@ extension ParameterHelpers on Parameter {
.firstOrNull;
}

if (arraySizeParam == null) return ArrayStyle.receive;
if (arraySizeParam.isInParam) {
if (arrayParam.isOutParam) return ArrayStyle.fill;
return ArrayStyle.pass;
if (arraySizeParam == null || arrayParam.isReferenceType) {
return ArrayPassingStyle.receive;
}
return ArrayStyle.receive;

if (arraySizeParam.isInParam && arrayParam.isOutParam) {
return ArrayPassingStyle.fill;
}

if (arraySizeParam.isInParam && arrayParam.isInParam) {
return ArrayPassingStyle.pass;
}

throw WinRTGenException(
'Failed to determine array-passing style for parameter $name');
}

bool get isClassVariableType => typeIdentifier.isClassVariableType;
Expand Down

0 comments on commit 96d5a4e

Please sign in to comment.