Skip to content

Commit 8019058

Browse files
Default C++ wrapper templates to EncodableValue (flutter#20760)
The C++ wrapper makes heavy use of templates to support arbitrary types in the platform channel classes, but in practice EncodableValue is what essentially all code will use. This defaults those template types to reduce boilerplate in plugin code (e.g., allowing the use of MethodChannel<> instead of MethodChannel<EncodableValue>).
1 parent 1f52ec3 commit 8019058

12 files changed

+78
-81
lines changed

shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h"
6-
75
#include <memory>
86
#include <string>
97

108
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h"
9+
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h"
1110
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h"
1211
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h"
1312
#include "gtest/gtest.h"
@@ -51,25 +50,21 @@ TEST(EventChannelTest, Registration) {
5150
EventChannel channel(&messenger, channel_name, &codec);
5251

5352
bool on_listen_called = false;
54-
auto handler = std::make_unique<
55-
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
56-
[&on_listen_called](
57-
const flutter::EncodableValue* arguments,
58-
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events)
59-
-> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> {
53+
auto handler = std::make_unique<StreamHandlerFunctions<>>(
54+
[&on_listen_called](const EncodableValue* arguments,
55+
std::unique_ptr<EventSink<>>&& events)
56+
-> std::unique_ptr<StreamHandlerError<>> {
6057
on_listen_called = true;
6158
return nullptr;
6259
},
63-
[](const flutter::EncodableValue* arguments)
64-
-> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> {
65-
return nullptr;
66-
});
60+
[](const EncodableValue* arguments)
61+
-> std::unique_ptr<StreamHandlerError<>> { return nullptr; });
6762
channel.SetStreamHandler(std::move(handler));
6863
EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
6964
EXPECT_NE(messenger.last_message_handler(), nullptr);
7065

7166
// Send dummy listen message.
72-
MethodCall<flutter::EncodableValue> call("listen", nullptr);
67+
MethodCall<> call("listen", nullptr);
7368
auto message = codec.EncodeMethodCall(call);
7469
messenger.last_message_handler()(
7570
message->data(), message->size(),
@@ -86,17 +81,11 @@ TEST(EventChannelTest, Unregistration) {
8681
const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
8782
EventChannel channel(&messenger, channel_name, &codec);
8883

89-
auto handler = std::make_unique<
90-
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
91-
[](const flutter::EncodableValue* arguments,
92-
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events)
93-
-> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> {
94-
return nullptr;
95-
},
96-
[](const flutter::EncodableValue* arguments)
97-
-> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> {
98-
return nullptr;
99-
});
84+
auto handler = std::make_unique<StreamHandlerFunctions<>>(
85+
[](const EncodableValue* arguments, std::unique_ptr<EventSink<>>&& events)
86+
-> std::unique_ptr<StreamHandlerError<>> { return nullptr; },
87+
[](const EncodableValue* arguments)
88+
-> std::unique_ptr<StreamHandlerError<>> { return nullptr; });
10089
channel.SetStreamHandler(std::move(handler));
10190
EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
10291
EXPECT_NE(messenger.last_message_handler(), nullptr);
@@ -115,17 +104,15 @@ TEST(EventChannelTest, Cancel) {
115104

116105
bool on_listen_called = false;
117106
bool on_cancel_called = false;
118-
auto handler = std::make_unique<
119-
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
120-
[&on_listen_called](
121-
const flutter::EncodableValue* arguments,
122-
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events)
123-
-> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> {
107+
auto handler = std::make_unique<StreamHandlerFunctions<>>(
108+
[&on_listen_called](const EncodableValue* arguments,
109+
std::unique_ptr<EventSink<>>&& events)
110+
-> std::unique_ptr<StreamHandlerError<>> {
124111
on_listen_called = true;
125112
return nullptr;
126113
},
127-
[&on_cancel_called](const flutter::EncodableValue* arguments)
128-
-> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> {
114+
[&on_cancel_called](const EncodableValue* arguments)
115+
-> std::unique_ptr<StreamHandlerError<>> {
129116
on_cancel_called = true;
130117
return nullptr;
131118
});
@@ -134,15 +121,15 @@ TEST(EventChannelTest, Cancel) {
134121
EXPECT_NE(messenger.last_message_handler(), nullptr);
135122

136123
// Send dummy listen message.
137-
MethodCall<flutter::EncodableValue> call_listen("listen", nullptr);
124+
MethodCall<> call_listen("listen", nullptr);
138125
auto message = codec.EncodeMethodCall(call_listen);
139126
messenger.last_message_handler()(
140127
message->data(), message->size(),
141128
[](const uint8_t* reply, const size_t reply_size) {});
142129
EXPECT_EQ(on_listen_called, true);
143130

144131
// Send dummy cancel message.
145-
MethodCall<flutter::EncodableValue> call_cancel("cancel", nullptr);
132+
MethodCall<> call_cancel("cancel", nullptr);
146133
message = codec.EncodeMethodCall(call_cancel);
147134
messenger.last_message_handler()(
148135
message->data(), message->size(),
@@ -164,17 +151,15 @@ TEST(EventChannelTest, ReRegistration) {
164151

165152
bool on_listen_called = false;
166153
bool on_cancel_called = false;
167-
auto handler = std::make_unique<
168-
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
169-
[&on_listen_called](
170-
const flutter::EncodableValue* arguments,
171-
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events)
172-
-> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> {
154+
auto handler = std::make_unique<StreamHandlerFunctions<>>(
155+
[&on_listen_called](const EncodableValue* arguments,
156+
std::unique_ptr<EventSink<>>&& events)
157+
-> std::unique_ptr<StreamHandlerError<>> {
173158
on_listen_called = true;
174159
return nullptr;
175160
},
176-
[&on_cancel_called](const flutter::EncodableValue* arguments)
177-
-> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> {
161+
[&on_cancel_called](const EncodableValue* arguments)
162+
-> std::unique_ptr<StreamHandlerError<>> {
178163
on_cancel_called = true;
179164
return nullptr;
180165
});
@@ -183,7 +168,7 @@ TEST(EventChannelTest, ReRegistration) {
183168
EXPECT_NE(messenger.last_message_handler(), nullptr);
184169

185170
// Send dummy listen message.
186-
MethodCall<flutter::EncodableValue> call("listen", nullptr);
171+
MethodCall<> call("listen", nullptr);
187172
auto message = codec.EncodeMethodCall(call);
188173
messenger.last_message_handler()(
189174
message->data(), message->size(),

shell/platform/common/cpp/client_wrapper/include/flutter/basic_message_channel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace flutter {
1515

16+
class EncodableValue;
17+
1618
// A message reply callback.
1719
//
1820
// Used for submitting a reply back to a Flutter message sender.
@@ -29,7 +31,7 @@ using MessageHandler =
2931

3032
// A channel for communicating with the Flutter engine by sending asynchronous
3133
// messages.
32-
template <typename T>
34+
template <typename T = EncodableValue>
3335
class BasicMessageChannel {
3436
public:
3537
// Creates an instance that sends and receives method calls on the channel

shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace flutter {
1818

19+
class EncodableValue;
20+
1921
// A named channel for communicating with the Flutter application using
2022
// asynchronous event streams. Incoming requests for event stream setup are
2123
// decoded from binary on receipt, and C++ responses and events are encoded into
@@ -27,7 +29,7 @@ namespace flutter {
2729
// The C++ type of stream configuration arguments, events, and error details are
2830
// templated, but only values supported by the specified MethodCodec can be
2931
// used.
30-
template <typename T>
32+
template <typename T = EncodableValue>
3133
class EventChannel {
3234
public:
3335
// Creates an instance that sends and receives event handler on the channel

shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
namespace flutter {
99

10+
class EncodableValue;
11+
1012
// Event callback. Events to be sent to Flutter application
1113
// act as clients of this interface for sending events.
12-
template <typename T>
14+
template <typename T = EncodableValue>
1315
class EventSink {
1416
public:
1517
EventSink() = default;

shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
namespace flutter {
1111

12-
template <typename T>
12+
class EncodableValue;
13+
14+
template <typename T = EncodableValue>
1315
struct StreamHandlerError {
1416
const std::string& error_code;
1517
const std::string& error_message;
@@ -29,7 +31,7 @@ struct StreamHandlerError {
2931
// resources when the last such call is not OnListen(). In typical situations,
3032
// this means that the implementation should register itself with
3133
// platform-specific event sources OnListen() and deregister again OnCancel().
32-
template <typename T>
34+
template <typename T = EncodableValue>
3335
class StreamHandler {
3436
public:
3537
StreamHandler() = default;

shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace flutter {
1414

15+
class EncodableValue;
16+
1517
// Handler types for each of the StreamHandler setup and tear-down
1618
// requests.
1719
template <typename T>
@@ -26,7 +28,7 @@ using StreamHandlerCancel =
2628

2729
// An implementation of StreamHandler that pass calls through to
2830
// provided function objects.
29-
template <typename T>
31+
template <typename T = EncodableValue>
3032
class StreamHandlerFunctions : public StreamHandler<T> {
3133
public:
3234
// Creates a handler object that calls the provided functions

shell/platform/common/cpp/client_wrapper/include/flutter/method_call.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
namespace flutter {
1212

13+
class EncodableValue;
14+
1315
// An object encapsulating a method call from Flutter whose arguments are of
1416
// type T.
15-
template <typename T>
17+
template <typename T = EncodableValue>
1618
class MethodCall {
1719
public:
1820
// Creates a MethodCall with the given name and arguments.

shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace flutter {
1818

19+
class EncodableValue;
20+
1921
// A handler for receiving a method call from the Flutter engine.
2022
//
2123
// Implementations must asynchronously call exactly one of the methods on
@@ -27,7 +29,7 @@ using MethodCallHandler =
2729

2830
// A channel for communicating with the Flutter engine using invocation of
2931
// asynchronous methods.
30-
template <typename T>
32+
template <typename T = EncodableValue>
3133
class MethodChannel {
3234
public:
3335
// Creates an instance that sends and receives method calls on the channel

shell/platform/common/cpp/client_wrapper/include/flutter/method_result.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
namespace flutter {
1111

12+
class EncodableValue;
13+
1214
// Encapsulates a result returned from a MethodCall. Only one method should be
1315
// called on any given instance.
14-
template <typename T>
16+
template <typename T = EncodableValue>
1517
class MethodResult {
1618
public:
1719
MethodResult() = default;

shell/platform/common/cpp/client_wrapper/include/flutter/method_result_functions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace flutter {
1414

15+
class EncodableValue;
16+
1517
// Handler types for each of the MethodResult outcomes.
1618
template <typename T>
1719
using ResultHandlerSuccess = std::function<void(const T* result)>;
@@ -24,7 +26,7 @@ using ResultHandlerNotImplemented = std::function<void()>;
2426

2527
// An implementation of MethodResult that pass calls through to provided
2628
// function objects, for ease of constructing one-off result handlers.
27-
template <typename T>
29+
template <typename T = EncodableValue>
2830
class MethodResultFunctions : public MethodResult<T> {
2931
public:
3032
// Creates a result object that calls the provided functions for the

shell/platform/common/cpp/client_wrapper/method_channel_unittests.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ TEST(MethodChannelTest, Registration) {
7272
EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
7373
EXPECT_NE(messenger.last_message_handler(), nullptr);
7474
// Send a test message to trigger the handler test assertions.
75-
MethodCall<EncodableValue> call(method_name, nullptr);
75+
MethodCall<> call(method_name, nullptr);
7676
auto message = codec.EncodeMethodCall(call);
7777

7878
messenger.last_message_handler()(
@@ -86,7 +86,7 @@ TEST(MethodChannelTest, Unregistration) {
8686
TestBinaryMessenger messenger;
8787
const std::string channel_name("some_channel");
8888
MethodChannel channel(&messenger, channel_name,
89-
&flutter::StandardMethodCodec::GetInstance());
89+
&StandardMethodCodec::GetInstance());
9090

9191
channel.SetMethodCallHandler([](const auto& call, auto result) {});
9292
EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
@@ -101,7 +101,7 @@ TEST(MethodChannelTest, InvokeWithoutResponse) {
101101
TestBinaryMessenger messenger;
102102
const std::string channel_name("some_channel");
103103
MethodChannel channel(&messenger, channel_name,
104-
&flutter::StandardMethodCodec::GetInstance());
104+
&StandardMethodCodec::GetInstance());
105105

106106
channel.InvokeMethod("foo", nullptr);
107107
EXPECT_TRUE(messenger.send_called());
@@ -112,11 +112,11 @@ TEST(MethodChannelTest, InvokeWithResponse) {
112112
TestBinaryMessenger messenger;
113113
const std::string channel_name("some_channel");
114114
MethodChannel channel(&messenger, channel_name,
115-
&flutter::StandardMethodCodec::GetInstance());
115+
&StandardMethodCodec::GetInstance());
116116

117117
bool received_reply = false;
118118
const std::string reply = "bar";
119-
auto result_handler = std::make_unique<MethodResultFunctions<EncodableValue>>(
119+
auto result_handler = std::make_unique<MethodResultFunctions<>>(
120120
[&received_reply, reply](const EncodableValue* success_value) {
121121
received_reply = true;
122122
EXPECT_EQ(std::get<std::string>(*success_value), reply);
@@ -130,8 +130,7 @@ TEST(MethodChannelTest, InvokeWithResponse) {
130130
// Call the underlying reply handler to ensure it's processed correctly.
131131
EncodableValue reply_value(reply);
132132
std::unique_ptr<std::vector<uint8_t>> encoded_reply =
133-
flutter::StandardMethodCodec::GetInstance().EncodeSuccessEnvelope(
134-
&reply_value);
133+
StandardMethodCodec::GetInstance().EncodeSuccessEnvelope(&reply_value);
135134
messenger.last_reply_handler()(encoded_reply->data(), encoded_reply->size());
136135
EXPECT_TRUE(received_reply);
137136
}
@@ -140,10 +139,10 @@ TEST(MethodChannelTest, InvokeNotImplemented) {
140139
TestBinaryMessenger messenger;
141140
const std::string channel_name("some_channel");
142141
MethodChannel channel(&messenger, channel_name,
143-
&flutter::StandardMethodCodec::GetInstance());
142+
&StandardMethodCodec::GetInstance());
144143

145144
bool received_not_implemented = false;
146-
auto result_handler = std::make_unique<MethodResultFunctions<EncodableValue>>(
145+
auto result_handler = std::make_unique<MethodResultFunctions<>>(
147146
nullptr, nullptr,
148147
[&received_not_implemented]() { received_not_implemented = true; });
149148

0 commit comments

Comments
 (0)