Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
14namespace flutter {
15
16class EncodableValue;
17
18// Handler types for each of the MethodResult outcomes.
19template <typename T>
20using ResultHandlerSuccess = std::function<void(const T* result)>;
21template <typename T>
22using ResultHandlerError = std::function<void(const std::string& error_code,
23 const std::string& error_message,
24 const T* error_details)>;
25template <typename T>
26using 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.
30template <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_
virtual ~MethodResultFunctions()=default
void SuccessInternal(const T *result) override
MethodResultFunctions(ResultHandlerSuccess< T > on_success, ResultHandlerError< T > on_error, ResultHandlerNotImplemented< T > on_not_implemented)
MethodResultFunctions(MethodResultFunctions const &)=delete
void ErrorInternal(const std::string &error_code, const std::string &error_message, const T *error_details) override
MethodResultFunctions & operator=(MethodResultFunctions const &)=delete
GAsyncResult * result
std::function< void()> ResultHandlerNotImplemented
std::function< void(const std::string &error_code, const std::string &error_message, const T *error_details)> ResultHandlerError
std::function< void(const T *result)> ResultHandlerSuccess
Definition ref_ptr.h:256
#define T