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