Flutter Engine
method_result_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_METHOD_RESULT_FUNCTIONS_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_RESULT_FUNCTIONS_H_
7 
8 #include <functional>
9 #include <string>
10 
11 #include "method_result.h"
12 
13 namespace flutter {
14 
15 class EncodableValue;
16 
17 // Handler types for each of the MethodResult outcomes.
18 template <typename T>
19 using ResultHandlerSuccess = std::function<void(const T* result)>;
20 template <typename T>
21 using ResultHandlerError = std::function<void(const std::string& error_code,
22  const std::string& error_message,
23  const T* error_details)>;
24 template <typename T>
25 using ResultHandlerNotImplemented = std::function<void()>;
26 
27 // An implementation of MethodResult that pass calls through to provided
28 // function objects, for ease of constructing one-off result handlers.
29 template <typename T = EncodableValue>
31  public:
32  // Creates a result object that calls the provided functions for the
33  // corresponding MethodResult outcomes.
35  ResultHandlerError<T> on_error,
36  ResultHandlerNotImplemented<T> on_not_implemented)
37  : on_success_(on_success),
38  on_error_(on_error),
39  on_not_implemented_(on_not_implemented) {}
40 
41  virtual ~MethodResultFunctions() = default;
42 
43  // Prevent copying.
46 
47  protected:
48  // |flutter::MethodResult|
49  void SuccessInternal(const T* result) override {
50  if (on_success_) {
51  on_success_(result);
52  }
53  }
54 
55  // |flutter::MethodResult|
56  void ErrorInternal(const std::string& error_code,
57  const std::string& error_message,
58  const T* error_details) override {
59  if (on_error_) {
60  on_error_(error_code, error_message, error_details);
61  }
62  }
63 
64  // |flutter::MethodResult|
65  void NotImplementedInternal() override {
66  if (on_not_implemented_) {
67  on_not_implemented_();
68  }
69  }
70 
71  private:
72  ResultHandlerSuccess<T> on_success_;
73  ResultHandlerError<T> on_error_;
74  ResultHandlerNotImplemented<T> on_not_implemented_;
75 };
76 
77 } // namespace flutter
78 
79 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_RESULT_FUNCTIONS_H_
Dart_NativeFunction function
Definition: fuchsia.cc:51
void ErrorInternal(const std::string &error_code, const std::string &error_message, const T *error_details) override
MethodResultFunctions & operator=(MethodResultFunctions const &)=delete
virtual ~MethodResultFunctions()=default
void SuccessInternal(const T *result) override
std::function< void()> ResultHandlerNotImplemented
std::function< void(const T *result)> ResultHandlerSuccess
MethodResultFunctions(ResultHandlerSuccess< T > on_success, ResultHandlerError< T > on_error, ResultHandlerNotImplemented< T > on_not_implemented)
std::function< void(const std::string &error_code, const std::string &error_message, const T *error_details)> ResultHandlerError