Flutter Engine
event_stream_handler_functions.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_
7 
8 #include <memory>
9 
10 #include "event_sink.h"
11 #include "event_stream_handler.h"
12 
13 namespace flutter {
14 
15 class EncodableValue;
16 
17 // Handler types for each of the StreamHandler setup and tear-down
18 // requests.
19 template <typename T>
20 using StreamHandlerListen =
21  std::function<std::unique_ptr<StreamHandlerError<T>>(
22  const T* arguments,
23  std::unique_ptr<EventSink<T>>&& events)>;
24 
25 template <typename T>
26 using StreamHandlerCancel =
27  std::function<std::unique_ptr<StreamHandlerError<T>>(const T* arguments)>;
28 
29 // An implementation of StreamHandler that pass calls through to
30 // provided function objects.
31 template <typename T = EncodableValue>
33  public:
34  // Creates a handler object that calls the provided functions
35  // for the corresponding StreamHandler outcomes.
37  StreamHandlerCancel<T> on_cancel)
38  : on_listen_(on_listen), on_cancel_(on_cancel) {}
39 
40  virtual ~StreamHandlerFunctions() = default;
41 
42  // Prevent copying.
45 
46  protected:
47  // |flutter::StreamHandler|
48  std::unique_ptr<StreamHandlerError<T>> OnListenInternal(
49  const T* arguments,
50  std::unique_ptr<EventSink<T>>&& events) override {
51  if (on_listen_) {
52  return on_listen_(arguments, std::move(events));
53  }
54 
55  auto error = std::make_unique<StreamHandlerError<T>>(
56  "error", "No OnListen handler set", nullptr);
57  return std::move(error);
58  }
59 
60  // |flutter::StreamHandler|
61  std::unique_ptr<StreamHandlerError<T>> OnCancelInternal(
62  const T* arguments) override {
63  if (on_cancel_) {
64  return on_cancel_(arguments);
65  }
66 
67  auto error = std::make_unique<StreamHandlerError<T>>(
68  "error", "No OnCancel handler set", nullptr);
69  return std::move(error);
70  }
71 
74 };
75 
76 } // namespace flutter
77 
78 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_
FlMethodResponse GError ** error
std::unique_ptr< StreamHandlerError< T > > OnCancelInternal(const T *arguments) override
StreamHandlerFunctions(StreamHandlerListen< T > on_listen, StreamHandlerCancel< T > on_cancel)
virtual ~StreamHandlerFunctions()=default
StreamHandlerFunctions & operator=(StreamHandlerFunctions const &)=delete
std::unique_ptr< StreamHandlerError< T > > OnListenInternal(const T *arguments, std::unique_ptr< EventSink< T >> &&events) override
std::function< std::unique_ptr< StreamHandlerError< T > >(const T *arguments)> StreamHandlerCancel
std::function< std::unique_ptr< StreamHandlerError< T > >(const T *arguments, std::unique_ptr< EventSink< T > > &&events)> StreamHandlerListen