event_stream_handler_functions.h 2.5 KB
Newer Older
1 2 3 4
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_
#define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_
7 8 9 10 11 12 13 14

#include <memory>

#include "event_sink.h"
#include "event_stream_handler.h"

namespace flutter {

15 16
class EncodableValue;

17
// Handler types for each of the StreamHandler setup and teardown
18 19 20 21 22 23 24 25 26 27 28 29 30
// requests.
template <typename T>
using StreamHandlerListen =
    std::function<std::unique_ptr<StreamHandlerError<T>>(
        const T* arguments,
        std::unique_ptr<EventSink<T>>&& events)>;

template <typename T>
using StreamHandlerCancel =
    std::function<std::unique_ptr<StreamHandlerError<T>>(const T* arguments)>;

// An implementation of StreamHandler that pass calls through to
// provided function objects.
31
template <typename T = EncodableValue>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
class StreamHandlerFunctions : public StreamHandler<T> {
 public:
  // Creates a handler object that calls the provided functions
  // for the corresponding StreamHandler outcomes.
  StreamHandlerFunctions(StreamHandlerListen<T> on_listen,
                         StreamHandlerCancel<T> on_cancel)
      : on_listen_(on_listen), on_cancel_(on_cancel) {}

  virtual ~StreamHandlerFunctions() = default;

  // Prevent copying.
  StreamHandlerFunctions(StreamHandlerFunctions const&) = delete;
  StreamHandlerFunctions& operator=(StreamHandlerFunctions const&) = delete;

 protected:
  // |flutter::StreamHandler|
  std::unique_ptr<StreamHandlerError<T>> OnListenInternal(
      const T* arguments,
      std::unique_ptr<EventSink<T>>&& events) override {
    if (on_listen_) {
      return on_listen_(arguments, std::move(events));
    }

    auto error = std::make_unique<StreamHandlerError<T>>(
        "error", "No OnListen handler set", nullptr);
    return std::move(error);
  }

  // |flutter::StreamHandler|
  std::unique_ptr<StreamHandlerError<T>> OnCancelInternal(
      const T* arguments) override {
    if (on_cancel_) {
      return on_cancel_(arguments);
    }

    auto error = std::make_unique<StreamHandlerError<T>>(
        "error", "No OnCancel handler set", nullptr);
    return std::move(error);
  }

  StreamHandlerListen<T> on_listen_;
  StreamHandlerCancel<T> on_cancel_;
};

}  // namespace flutter

78
#endif  // FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_