CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
async.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UTIL_ASYNC_H_
6 #define CVMFS_UTIL_ASYNC_H_
7 
8 #ifdef CVMFS_NAMESPACE_GUARD
9 namespace CVMFS_NAMESPACE_GUARD {
10 #endif
11 
20 template <typename ParamT>
21 class CallbackBase {
22  public:
23  virtual ~CallbackBase() {}
24  virtual void operator()(const ParamT &value) const = 0;
25 };
26 
27 template <>
28 class CallbackBase<void> {
29  public:
30  virtual ~CallbackBase() {}
31  virtual void operator()() const = 0;
32 };
33 
44 template <typename ParamT>
45 class Callback : public CallbackBase<ParamT> {
46  public:
47  typedef void (*CallbackFunction)(const ParamT &value);
48 
49  explicit Callback(CallbackFunction function) : function_(function) {}
50  void operator()(const ParamT &value) const { function_(value); }
51 
52  private:
53  CallbackFunction function_;
54 };
55 
56 template <>
57 class Callback<void> : public CallbackBase<void> {
58  public:
59  typedef void (*CallbackFunction)();
60 
61  explicit Callback(CallbackFunction function) : function_(function) {}
62  void operator()() const { function_(); }
63 
64  private:
65  CallbackFunction function_;
66 };
67 
68 
83 template <typename ParamT, class DelegateT>
84 class BoundCallback : public CallbackBase<ParamT> {
85  public:
86  typedef void (DelegateT::*CallbackMethod)(const ParamT &value);
87 
88  BoundCallback(CallbackMethod method, DelegateT *delegate) :
89  delegate_(delegate),
90  method_(method) {}
91 
92  void operator()(const ParamT &value) const { (delegate_->*method_)(value); }
93 
94  private:
95  DelegateT* delegate_;
96  CallbackMethod method_;
97 };
98 
99 template <class DelegateT>
100 class BoundCallback<void, DelegateT> : public CallbackBase<void> {
101  public:
102  typedef void (DelegateT::*CallbackMethod)();
103 
104  BoundCallback(CallbackMethod method, DelegateT *delegate) :
105  delegate_(delegate), method_(method) {}
106 
107  void operator()() const { (delegate_->*method_)(); }
108 
109  private:
110  DelegateT* delegate_;
111  CallbackMethod method_;
112 };
113 
114 
128 template <typename ParamT, class DelegateT, typename ClosureDataT>
129 class BoundClosure : public CallbackBase<ParamT> {
130  public:
131  typedef void (DelegateT::*CallbackMethod)(const ParamT &value,
132  const ClosureDataT closure_data);
133 
134  public:
135  BoundClosure(CallbackMethod method,
136  DelegateT *delegate,
137  ClosureDataT data) :
138  delegate_(delegate),
139  method_(method),
140  closure_data_(data) {}
141 
142  void operator()(const ParamT &value) const {
143  (delegate_->*method_)(value, closure_data_);
144  }
145 
146  private:
147  DelegateT* delegate_;
148  CallbackMethod method_;
149  const ClosureDataT closure_data_;
150 };
151 
152 template <class DelegateT, typename ClosureDataT>
153 class BoundClosure<void, DelegateT, ClosureDataT> : public CallbackBase<void> {
154  public:
155  typedef void (DelegateT::*CallbackMethod)(const ClosureDataT closure_data);
156 
157  public:
158  BoundClosure(CallbackMethod method,
159  DelegateT *delegate,
160  ClosureDataT data) :
161  delegate_(delegate), method_(method), closure_data_(data) {}
162 
163  void operator()() const { (delegate_->*method_)(closure_data_); }
164 
165  private:
166  DelegateT* delegate_;
167  CallbackMethod method_;
168  const ClosureDataT closure_data_;
169 };
170 
171 
189 template <class ParamT>
191  public:
193 
194  public:
203  template <class DelegateT, typename ClosureDataT>
206  CallbackMethod method,
207  DelegateT *delegate,
208  const ClosureDataT &closure_data)
209  {
211  delegate,
212  closure_data);
213  }
214 
221  template <class DelegateT>
224  DelegateT *delegate) {
225  return new BoundCallback<ParamT, DelegateT>(method, delegate);
226  }
227 
235  typename Callback<ParamT>::CallbackFunction function) {
236  return new Callback<ParamT>(function);
237  }
238 };
239 
240 
247 template <class DelegateT>
248 void ThreadProxy(DelegateT *delegate,
249  void (DelegateT::*method)()) {
250  (*delegate.*method)();
251 }
252 
253 
254 
255 #ifdef CVMFS_NAMESPACE_GUARD
256 } // namespace CVMFS_NAMESPACE_GUARD
257 #endif
258 
259 #endif // CVMFS_UTIL_ASYNC_H_
virtual ~CallbackBase()
Definition: async.h:23
CallbackFunction function_
Definition: async.h:53
DelegateT * delegate_
Definition: async.h:95
void operator()(const ParamT &value) const
Definition: async.h:142
void operator()() const
Definition: async.h:62
static CallbackTN * MakeCallback(typename BoundCallback< ParamT, DelegateT >::CallbackMethod method, DelegateT *delegate)
Definition: async.h:222
Definition: async.h:45
Callback(CallbackFunction function)
Definition: async.h:61
const ClosureDataT closure_data_
Definition: async.h:149
void operator()(const ParamT &value) const
Definition: async.h:50
BoundCallback(CallbackMethod method, DelegateT *delegate)
Definition: async.h:104
Callback(CallbackFunction function)
Definition: async.h:49
CallbackMethod method_
Definition: async.h:148
void operator()(const ParamT &value) const
Definition: async.h:92
DelegateT * delegate_
Definition: async.h:147
BoundCallback(CallbackMethod method, DelegateT *delegate)
Definition: async.h:88
BoundClosure(CallbackMethod method, DelegateT *delegate, ClosureDataT data)
Definition: async.h:135
static CallbackTN * MakeCallback(typename Callback< ParamT >::CallbackFunction function)
Definition: async.h:234
virtual ~CallbackBase()
Definition: async.h:30
void ThreadProxy(DelegateT *delegate, void(DelegateT::*method)())
Definition: async.h:248
CallbackBase< ParamT > CallbackTN
Definition: async.h:192
CallbackMethod method_
Definition: async.h:96
CallbackFunction function_
Definition: async.h:65
BoundClosure(CallbackMethod method, DelegateT *delegate, ClosureDataT data)
Definition: async.h:158
static CallbackTN * MakeClosure(typename BoundClosure< ParamT, DelegateT, ClosureDataT >::CallbackMethod method, DelegateT *delegate, const ClosureDataT &closure_data)
Definition: async.h:204