• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_RESULT_H_
7 
8 #include <string>
9 
10 namespace flutter {
11 
12 // Encapsulates a result sent back to the Flutter engine in response to a
13 // MethodCall. Only one method should be called on any given instance.
14 template <typename T>
15 class MethodResult {
16  public:
17   MethodResult() = default;
18 
19   virtual ~MethodResult() = default;
20 
21   // Prevent copying.
22   MethodResult(MethodResult const&) = delete;
23   MethodResult& operator=(MethodResult const&) = delete;
24 
25   // Sends a success response, indicating that the call completed successfully.
26   // An optional value can be provided as part of the success message.
27   void Success(const T* result = nullptr) { SuccessInternal(result); }
28 
29   // Sends an error response, indicating that the call was understood but
30   // handling failed in some way. A string error code must be provided, and in
31   // addition an optional user-readable error_message and/or details object can
32   // be included.
33   void Error(const std::string& error_code,
34              const std::string& error_message = "",
35              const T* error_details = nullptr) {
36     ErrorInternal(error_code, error_message, error_details);
37   }
38 
39   // Sends a not-implemented response, indicating that the method either was not
40   // recognized, or has not been implemented.
NotImplemented()41   void NotImplemented() { NotImplementedInternal(); }
42 
43  protected:
44   // Implementation of the public interface, to be provided by subclasses.
45   virtual void SuccessInternal(const T* result) = 0;
46 
47   // Implementation of the public interface, to be provided by subclasses.
48   virtual void ErrorInternal(const std::string& error_code,
49                              const std::string& error_message,
50                              const T* error_details) = 0;
51 
52   // Implementation of the public interface, to be provided by subclasses.
53   virtual void NotImplementedInternal() = 0;
54 };
55 
56 }  // namespace flutter
57 
58 #endif  // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_RESULT_H_
59