Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / NativeClient / callback.h
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 EXAMPLES_TUMBLER_CALLBACK_H_
6 #define EXAMPLES_TUMBLER_CALLBACK_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 namespace tumbler {
13
14 class ScriptingBridge;
15
16 // Templates used to support method call-backs when a method or property is
17 // accessed from the browser code.
18
19 // Class suite used to publish a method name to Javascript.  Typical use is
20 // like this:
21 //     photo::MethodCallback<Calculator>* calculate_callback_;
22 //     calculate_callback_ =
23 //        new scripting::MethodCallback<Calculator>(this,
24 //                                                  &Calculator::Calculate);
25 //     bridge->AddMethodNamed("calculate", calculate_callback_);
26 //     ...
27 //     delete calculate_callback_;
28 //
29 // The caller must delete the callback.
30
31 // Methods get parameters as a dictionary that maps parameter names to values.
32 typedef std::map<std::string, std::string> MethodParameter;
33
34 // Pure virtual class used in STL containers.
35 class MethodCallbackExecutor {
36  public:
37   virtual ~MethodCallbackExecutor() {}
38   virtual void Execute(
39       const ScriptingBridge& bridge,
40       const MethodParameter& parameters) = 0;
41 };
42
43 template <class T>
44 class MethodCallback : public MethodCallbackExecutor {
45  public:
46   typedef void (T::*Method)(
47       const ScriptingBridge& bridge,
48       const MethodParameter& parameters);
49
50   MethodCallback(T* instance, Method method)
51       : instance_(instance), method_(method) {}
52   virtual ~MethodCallback() {}
53   virtual void Execute(
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);
59   }
60
61  private:
62   T* instance_;
63   Method method_;
64 };
65
66 template <class T>
67 class ConstMethodCallback : public MethodCallbackExecutor {
68  public:
69   typedef void (T::*ConstMethod)(
70       const ScriptingBridge& bridge,
71       const MethodParameter& parameters) const;
72
73   ConstMethodCallback(const T* instance, ConstMethod method)
74       : instance_(instance), const_method_(method) {}
75   virtual ~ConstMethodCallback() {}
76   virtual void Execute(
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);
82   }
83
84  private:
85   const T* instance_;
86   ConstMethod const_method_;
87 };
88
89 }  // namespace tumbler
90
91 #endif  // EXAMPLES_TUMBLER_CALLBACK_H_
92