summaryrefslogtreecommitdiffstats
path: root/base/call_wrapper.h
blob: ae9738d0c7355879beb283f9fec5702dff5e8739 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef BASE_CALL_WRAPPER_H_
#define BASE_CALL_WRAPPER_H_

// NOTE: If you want a callback that takes at-call-time parameters, you should
// use Callback (see task.h).  CallWrapper only supports creation-time binding.
//
// A function / method call invocation wrapper.  This creates a "closure" of
// sorts, storing a function pointer (or method pointer), along with a possible
// list of arguments.  The objects have a single method Run(), which will call
// the function / method with the arguments unpacked.  The arguments to the
// wrapped call are supplied at CallWrapper creation time, and no arguments can
// be supplied to the Run() method.
//
// All wrappers should be constructed through the two factory methods:
//   CallWrapper* NewFunctionCallWrapper(funcptr, ...);
//   CallWrapper* NewMethodCallWrapper(obj, &Klass::Method, ...);
//
// The classes memory should be managed like any other dynamically allocated
// object, and deleted when no longer in use.  It is of course wrong to destroy
// the object while a Run() is in progress, or to call Run() after the object
// has been destroyed.  Arguments are copied by value, and kept within the
// CallWrapper object.  You should only pass-by-value simple parameters, or
// pointers to more complex types.  If your parameters need custom memory
// management, then you should probably do this at the same point you destroy
// the CallWrapper.  You should be aware of how your CallWrapper is used, and
// it is valid for Run() to be called 0 or more times.
//
// Some example usage:
//   CallWrapper* wrapper = NewFunctionCallWrapper(&my_func);
//   wrapper->Run();  // my_func();
//   delete wrapper;
//
//   scoped_ptr<CallWrapper> wrapper(NewFunctionCallWrapper(&my_func, 10));
//   wrapper->Run();  // my_func(10);
//
//   MyObject obj;
//   scoped_ptr<CallWrapper> wrapper(
//      NewMethodCallWrapper(&obj, &MyObject::Foo, 1, 2));
//   wrapper->Run();  // obj->Foo(1, 2);

#include "base/tuple.h"

namespace base {

class CallWrapper {
 public:
  virtual ~CallWrapper() { }
  virtual void Run() = 0;
 protected:
  CallWrapper() { }
};

// Wraps a function / static method call.
template <typename FuncType, typename TupleType>
class FunctionCallWrapper : public CallWrapper {
 public:
  FunctionCallWrapper(FuncType func, TupleType params)
      : func_(func), params_(params) { }
  virtual void Run() {
    DispatchToFunction(func_, params_);
  }
 private:
  FuncType func_;
  // We copy from the const& constructor argument to our own copy here.
  TupleType params_;
};

// Wraps a method invocation on an object.
template <typename ObjType, typename MethType, typename TupleType>
class MethodCallWrapper : public CallWrapper {
 public:
  MethodCallWrapper(ObjType* obj, MethType meth, TupleType params)
      : obj_(obj), meth_(meth), params_(params) { }
  virtual void Run() {
    DispatchToMethod(obj_, meth_, params_);
  }
 private:
  ObjType* obj_;
  MethType meth_;
  // We copy from the const& constructor argument to our own copy here.
  TupleType params_;
};

// Factory functions, conveniently creating a Tuple for 0-5 arguments.
template <typename FuncType>
inline CallWrapper* NewFunctionCallWrapper(FuncType func) {
  typedef Tuple0 TupleType;
  return new FunctionCallWrapper<FuncType, TupleType>(
      func, MakeTuple());
}

template <typename FuncType, typename Arg0Type>
inline CallWrapper* NewFunctionCallWrapper(FuncType func,
                                           const Arg0Type& arg0) {
  typedef Tuple1<Arg0Type> TupleType;
  return new FunctionCallWrapper<FuncType, TupleType>(
      func, MakeTuple(arg0));
}

template <typename FuncType, typename Arg0Type, typename Arg1Type>
inline CallWrapper* NewFunctionCallWrapper(FuncType func,
                                           const Arg0Type& arg0,
                                           const Arg1Type& arg1) {
  typedef Tuple2<Arg0Type, Arg1Type> TupleType;
  return new FunctionCallWrapper<FuncType, TupleType>(
      func, MakeTuple(arg0, arg1));
}

template <typename FuncType, typename Arg0Type, typename Arg1Type,
          typename Arg2Type>
inline CallWrapper* NewFunctionCallWrapper(FuncType func,
                                           const Arg0Type& arg0,
                                           const Arg1Type& arg1,
                                           const Arg2Type& arg2) {
  typedef Tuple3<Arg0Type, Arg1Type, Arg2Type> TupleType;
  return new FunctionCallWrapper<FuncType, TupleType>(
      func, MakeTuple(arg0, arg1, arg2));
}

template <typename FuncType, typename Arg0Type, typename Arg1Type,
          typename Arg2Type, typename Arg3Type>
inline CallWrapper* NewFunctionCallWrapper(FuncType func,
                                           const Arg0Type& arg0,
                                           const Arg1Type& arg1,
                                           const Arg2Type& arg2,
                                           const Arg3Type& arg3) {
  typedef Tuple4<Arg0Type, Arg1Type, Arg2Type, Arg3Type> TupleType;
  return new FunctionCallWrapper<FuncType, TupleType>(
      func, MakeTuple(arg0, arg1, arg2, arg3));
}

template <typename FuncType, typename Arg0Type, typename Arg1Type,
          typename Arg2Type, typename Arg3Type, typename Arg4Type>
inline CallWrapper* NewFunctionCallWrapper(FuncType func,
                                           const Arg0Type& arg0,
                                           const Arg1Type& arg1,
                                           const Arg2Type& arg2,
                                           const Arg3Type& arg3,
                                           const Arg4Type& arg4) {
  typedef Tuple5<Arg0Type, Arg1Type, Arg2Type, Arg3Type, Arg4Type> TupleType;
  return new FunctionCallWrapper<FuncType, TupleType>(
      func, MakeTuple(arg0, arg1, arg2, arg3, arg4));
}


template <typename ObjType, typename MethType>
inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth) {
  typedef Tuple0 TupleType;
  return new MethodCallWrapper<ObjType, MethType, TupleType>(
      obj, meth, MakeTuple());
}

template <typename ObjType, typename MethType, typename Arg0Type>
inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth,
                                         const Arg0Type& arg0) {
  typedef Tuple1<Arg0Type> TupleType;
  return new MethodCallWrapper<ObjType, MethType, TupleType>(
      obj, meth, MakeTuple(arg0));
}

template <typename ObjType, typename MethType, typename Arg0Type,
          typename Arg1Type>
inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth,
                                         const Arg0Type& arg0,
                                         const Arg1Type& arg1) {
  typedef Tuple2<Arg0Type, Arg1Type> TupleType;
  return new MethodCallWrapper<ObjType, MethType, TupleType>(
      obj, meth, MakeTuple(arg0, arg1));
}

template <typename ObjType, typename MethType, typename Arg0Type,
          typename Arg1Type, typename Arg2Type>
inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth,
                                         const Arg0Type& arg0,
                                         const Arg1Type& arg1,
                                         const Arg2Type& arg2) {
  typedef Tuple3<Arg0Type, Arg1Type, Arg2Type> TupleType;
  return new MethodCallWrapper<ObjType, MethType, TupleType>(
      obj, meth, MakeTuple(arg0, arg1, arg2));
}

template <typename ObjType, typename MethType, typename Arg0Type,
          typename Arg1Type, typename Arg2Type, typename Arg3Type>
inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth,
                                         const Arg0Type& arg0,
                                         const Arg1Type& arg1,
                                         const Arg2Type& arg2,
                                         const Arg3Type& arg3) {
  typedef Tuple4<Arg0Type, Arg1Type, Arg2Type, Arg3Type> TupleType;
  return new MethodCallWrapper<ObjType, MethType, TupleType>(
      obj, meth, MakeTuple(arg0, arg1, arg2, arg3));
}

template <typename ObjType, typename MethType, typename Arg0Type,
          typename Arg1Type, typename Arg2Type, typename Arg3Type,
          typename Arg4Type>
inline CallWrapper* NewMethodCallWrapper(ObjType* obj, MethType meth,
                                         const Arg0Type& arg0,
                                         const Arg1Type& arg1,
                                         const Arg2Type& arg2,
                                         const Arg3Type& arg3,
                                         const Arg4Type& arg4) {
  typedef Tuple5<Arg0Type, Arg1Type, Arg2Type, Arg3Type, Arg4Type> TupleType;
  return new MethodCallWrapper<ObjType, MethType, TupleType>(
      obj, meth, MakeTuple(arg0, arg1, arg2, arg3, arg4));
}

}  // namespace base

#endif  // BASE_CALL_WRAPPER_H_