Skip to content

Commit f092626

Browse files
authored
Fix support for delegates returning arrays of structs (#510)
1 parent fcdbc24 commit f092626

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ packages
1111
Debug
1212
Release
1313
Generated Files
14+
obj

cppwinrt/code_writers.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ namespace cppwinrt
965965
}
966966
}
967967

968-
static void write_consume_return_type(writer& w, method_signature const& signature)
968+
static void write_consume_return_type(writer& w, method_signature const& signature, bool delegate_types)
969969
{
970970
if (!signature.return_signature())
971971
{
@@ -981,13 +981,15 @@ namespace cppwinrt
981981
%* %{};)";
982982

983983
w.abi_types = true;
984+
w.delegate_types = delegate_types;
984985

985986
w.write(format,
986987
signature.return_param_name(),
987988
signature.return_signature(),
988989
signature.return_param_name());
989990

990991
w.abi_types = false;
992+
w.delegate_types = false;
991993
}
992994
else if (category == param_category::object_type || category == param_category::string_type)
993995
{
@@ -1077,7 +1079,7 @@ namespace cppwinrt
10771079
bind<write_comma_generic_types>(generics),
10781080
method_name,
10791081
bind<write_consume_params>(signature),
1080-
bind<write_consume_return_type>(signature),
1082+
bind<write_consume_return_type>(signature, false),
10811083
type,
10821084
get_abi_name(method),
10831085
bind<write_abi_args>(signature),
@@ -2447,7 +2449,7 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
24472449
type_name,
24482450
bind_list(", ", generics),
24492451
bind<write_consume_params>(signature),
2450-
bind<write_consume_return_type>(signature),
2452+
bind<write_consume_return_type>(signature, true),
24512453
type_name,
24522454
bind_list(", ", generics),
24532455
bind<write_abi_args>(signature),
@@ -2500,7 +2502,7 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
25002502
type_name,
25012503
type_name,
25022504
bind<write_consume_params>(signature),
2503-
bind<write_consume_return_type>(signature),
2505+
bind<write_consume_return_type>(signature, true),
25042506
type_name,
25052507
bind<write_abi_args>(signature),
25062508
bind<write_consume_return_statement>(signature));

cppwinrt/type_writers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ namespace cppwinrt
106106

107107
std::string type_namespace;
108108
bool abi_types{};
109+
bool delegate_types{};
109110
bool param_names{};
110111
bool consume_types{};
111112
bool async_types{};
@@ -276,6 +277,10 @@ namespace cppwinrt
276277
{
277278
write("@::%", ns, name);
278279
}
280+
else if (delegate_types)
281+
{
282+
write("struct impl::struct_%_%", get_impl_name(ns), name);
283+
}
279284
else
280285
{
281286
write("struct struct_%_%", get_impl_name(ns), name);

test/test/struct_delegate.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "pch.h"
2+
#include "winrt/test_component.delegates.h"
3+
4+
using namespace winrt;
5+
using namespace test_component;
6+
7+
TEST_CASE("struct_delegate")
8+
{
9+
Delegates::StructDelegate d = []
10+
{
11+
return com_array{ Struct{ L"First", L"Second" }, Struct{ L"Third", L"Fourth" } };
12+
};
13+
14+
auto values = d();
15+
16+
REQUIRE(values.size() == 2);
17+
REQUIRE(values[0].First == L"First");
18+
REQUIRE(values[0].Second == L"Second");
19+
REQUIRE(values[1].First == L"Third");
20+
REQUIRE(values[1].Second == L"Fourth");
21+
}

test/test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@
417417
<ClCompile Include="return_params_abi.cpp" />
418418
<ClCompile Include="single_threaded_observable_vector.cpp" />
419419
<ClCompile Include="structs.cpp" />
420+
<ClCompile Include="struct_delegate.cpp" />
420421
<ClCompile Include="tearoff.cpp" />
421422
<ClCompile Include="thread_pool.cpp" />
422423
<ClCompile Include="uniform_in_params.cpp" />

test/test_component/test_component.idl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace Windows.Foundation.Metadata
1111

1212
namespace test_component
1313
{
14+
struct Struct
15+
{
16+
String First;
17+
String Second;
18+
};
19+
1420
namespace Delegates
1521
{
1622
delegate void AgileDelegate();
@@ -22,6 +28,7 @@ namespace test_component
2228
delegate String[] ReturnStringArrayDelegate();
2329
delegate void OutStringArrayDelegate(out String[] value);
2430
delegate void RefStringArrayDelegate(ref String[] value);
31+
delegate test_component.Struct[] StructDelegate();
2532
}
2633

2734
enum Signed
@@ -39,12 +46,6 @@ namespace test_component
3946
Third = 2
4047
};
4148

42-
struct Struct
43-
{
44-
String First;
45-
String Second;
46-
};
47-
4849
runtimeclass Simple
4950
{
5051
Simple();

0 commit comments

Comments
 (0)