Flutter macOS Embedder
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_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 teardown
18 // requests.
19 template <typename T>
21  std::function<std::unique_ptr<StreamHandlerError<T>>(
22  const T* arguments,
23  std::unique_ptr<EventSink<T>>&& events)>;
24 
25 template <typename T>
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_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_
virtual ~StreamHandlerFunctions()=default
StreamHandlerFunctions & operator=(StreamHandlerFunctions const &)=delete
std::unique_ptr< StreamHandlerError< T > > OnCancelInternal(const T *arguments) override
std::unique_ptr< StreamHandlerError< T > > OnListenInternal(const T *arguments, std::unique_ptr< EventSink< T >> &&events) override
StreamHandlerFunctions(StreamHandlerFunctions const &)=delete
StreamHandlerFunctions(StreamHandlerListen< T > on_listen, StreamHandlerCancel< T > on_cancel)
std::function< std::unique_ptr< StreamHandlerError< T > >(const T *arguments, std::unique_ptr< EventSink< T > > &&events)> StreamHandlerListen
std::function< std::unique_ptr< StreamHandlerError< T > >(const T *arguments)> StreamHandlerCancel