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