OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client 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 CALLBACK_H_ | |
6 #define CALLBACK_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 #include "ppapi/cpp/var.h" | |
12 | |
13 namespace scripting { | |
14 | |
15 class ScriptingBridge; | |
16 | |
17 // Templates used to support method call-backs when a method or property is | |
18 // accessed from the browser code. | |
19 | |
20 // Class suite used to publish a method name to Javascript. Typical use is | |
21 // like this: | |
22 // photo::MethodCallback<Calculator>* calculate_callback_; | |
23 // calculate_callback_ = | |
24 // new scripting::MethodCallback<Calculator>(this, | |
25 // &Calculator::Calculate); | |
26 // bridge->AddMethodNamed("calculate", calculate_callback_); | |
27 // ... | |
28 // delete calculate_callback_; | |
29 // | |
30 // The caller must delete the callback. | |
31 | |
32 // Methods get parameters as a dictionary that maps parameter names to values. | |
33 typedef std::map<std::string, std::string> MethodParameter; | |
34 | |
35 // Pure virtual class used in STL containers. | |
36 class MethodCallbackExecutor { | |
37 public: | |
38 virtual ~MethodCallbackExecutor() {} | |
39 virtual void Execute( | |
40 const ScriptingBridge& bridge, | |
41 const MethodParameter& parameters) = 0; | |
42 }; | |
43 | |
44 template <class T> | |
45 class MethodCallback : public MethodCallbackExecutor { | |
46 public: | |
47 typedef void (T::*Method)( | |
48 const ScriptingBridge& bridge, | |
49 const MethodParameter& parameters); | |
50 | |
51 MethodCallback(T* instance, Method method) | |
52 : instance_(instance), method_(method) {} | |
53 virtual ~MethodCallback() {} | |
54 virtual void Execute( | |
55 const ScriptingBridge& bridge, | |
56 const MethodParameter& parameters) { | |
57 // Use "this->" to force C++ to look inside our templatized base class; see | |
58 // Effective C++, 3rd Ed, item 43, p210 for details. | |
59 ((this->instance_)->*(this->method_))(bridge, parameters); | |
60 } | |
61 | |
62 private: | |
63 T* instance_; | |
64 Method method_; | |
65 }; | |
66 | |
67 template <class T> | |
68 class ConstMethodCallback : public MethodCallbackExecutor { | |
69 public: | |
70 typedef void (T::*ConstMethod)( | |
71 const ScriptingBridge& bridge, | |
72 const MethodParameter& parameters) const; | |
73 | |
74 ConstMethodCallback(const T* instance, ConstMethod method) | |
75 : instance_(instance), const_method_(method) {} | |
76 virtual ~ConstMethodCallback() {} | |
77 virtual void Execute( | |
78 const ScriptingBridge& bridge, | |
79 const MethodParameter& parameters) { | |
80 // Use "this->" to force C++ to look inside our templatized base class; see | |
81 // Effective C++, 3rd Ed, item 43, p210 for details. | |
82 ((this->instance_)->*(this->const_method_))(bridge, parameters); | |
83 } | |
84 | |
85 private: | |
86 const T* instance_; | |
87 ConstMethod const_method_; | |
88 }; | |
89 | |
90 } // namespace scripting | |
91 | |
92 #endif // CALLBACK_H_ | |
93 | |
OLD | NEW |