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.
5 #ifndef EXAMPLES_TUMBLER_CALLBACK_H_
6 #define EXAMPLES_TUMBLER_CALLBACK_H_
14 class ScriptingBridge;
16 // Templates used to support method call-backs when a method or property is
17 // accessed from the browser code.
19 // Class suite used to publish a method name to Javascript. Typical use is
21 // photo::MethodCallback<Calculator>* calculate_callback_;
22 // calculate_callback_ =
23 // new scripting::MethodCallback<Calculator>(this,
24 // &Calculator::Calculate);
25 // bridge->AddMethodNamed("calculate", calculate_callback_);
27 // delete calculate_callback_;
29 // The caller must delete the callback.
31 // Methods get parameters as a dictionary that maps parameter names to values.
32 typedef std::map<std::string, std::string> MethodParameter;
34 // Pure virtual class used in STL containers.
35 class MethodCallbackExecutor {
37 virtual ~MethodCallbackExecutor() {}
39 const ScriptingBridge& bridge,
40 const MethodParameter& parameters) = 0;
44 class MethodCallback : public MethodCallbackExecutor {
46 typedef void (T::*Method)(
47 const ScriptingBridge& bridge,
48 const MethodParameter& parameters);
50 MethodCallback(T* instance, Method method)
51 : instance_(instance), method_(method) {}
52 virtual ~MethodCallback() {}
54 const ScriptingBridge& bridge,
55 const MethodParameter& parameters) {
56 // Use "this->" to force C++ to look inside our templatized base class; see
57 // Effective C++, 3rd Ed, item 43, p210 for details.
58 ((this->instance_)->*(this->method_))(bridge, parameters);
67 class ConstMethodCallback : public MethodCallbackExecutor {
69 typedef void (T::*ConstMethod)(
70 const ScriptingBridge& bridge,
71 const MethodParameter& parameters) const;
73 ConstMethodCallback(const T* instance, ConstMethod method)
74 : instance_(instance), const_method_(method) {}
75 virtual ~ConstMethodCallback() {}
77 const ScriptingBridge& bridge,
78 const MethodParameter& parameters) {
79 // Use "this->" to force C++ to look inside our templatized base class; see
80 // Effective C++, 3rd Ed, item 43, p210 for details.
81 ((this->instance_)->*(this->const_method_))(bridge, parameters);
86 ConstMethod const_method_;
89 } // namespace tumbler
91 #endif // EXAMPLES_TUMBLER_CALLBACK_H_