CernVM-FS  2.13.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), method_(method) { }
90 
91  void operator()(const ParamT &value) const { (delegate_->*method_)(value); }
92 
93  private:
94  DelegateT *delegate_;
95  CallbackMethod method_;
96 };
97 
98 template<class DelegateT>
99 class BoundCallback<void, DelegateT> : public CallbackBase<void> {
100  public:
101  typedef void (DelegateT::*CallbackMethod)();
102 
103  BoundCallback(CallbackMethod method, DelegateT *delegate)
104  : delegate_(delegate), method_(method) { }
105 
106  void operator()() const { (delegate_->*method_)(); }
107 
108  private:
109  DelegateT *delegate_;
110  CallbackMethod method_;
111 };
112 
113 
127 template<typename ParamT, class DelegateT, typename ClosureDataT>
128 class BoundClosure : public CallbackBase<ParamT> {
129  public:
130  typedef void (DelegateT::*CallbackMethod)(const ParamT &value,
131  const ClosureDataT closure_data);
132 
133  public:
134  BoundClosure(CallbackMethod method, DelegateT *delegate, ClosureDataT data)
135  : delegate_(delegate), method_(method), closure_data_(data) { }
136 
137  void operator()(const ParamT &value) const {
138  (delegate_->*method_)(value, closure_data_);
139  }
140 
141  private:
142  DelegateT *delegate_;
143  CallbackMethod method_;
144  const ClosureDataT closure_data_;
145 };
146 
147 template<class DelegateT, typename ClosureDataT>
148 class BoundClosure<void, DelegateT, ClosureDataT> : public CallbackBase<void> {
149  public:
150  typedef void (DelegateT::*CallbackMethod)(const ClosureDataT closure_data);
151 
152  public:
153  BoundClosure(CallbackMethod method, DelegateT *delegate, ClosureDataT data)
154  : delegate_(delegate), method_(method), closure_data_(data) { }
155 
156  void operator()() const { (delegate_->*method_)(closure_data_); }
157 
158  private:
159  DelegateT *delegate_;
160  CallbackMethod method_;
161  const ClosureDataT closure_data_;
162 };
163 
164 
182 template<class ParamT>
184  public:
186 
187  public:
196  template<class DelegateT, typename ClosureDataT>
199  method,
200  DelegateT *delegate,
201  const ClosureDataT &closure_data) {
203  method, delegate, closure_data);
204  }
205 
212  template<class DelegateT>
215  DelegateT *delegate) {
216  return new BoundCallback<ParamT, DelegateT>(method, delegate);
217  }
218 
226  typename Callback<ParamT>::CallbackFunction function) {
227  return new Callback<ParamT>(function);
228  }
229 };
230 
231 
238 template<class DelegateT>
239 void ThreadProxy(DelegateT *delegate, void (DelegateT::*method)()) {
240  (*delegate.*method)();
241 }
242 
243 
244 #ifdef CVMFS_NAMESPACE_GUARD
245 } // namespace CVMFS_NAMESPACE_GUARD
246 #endif
247 
248 #endif // CVMFS_UTIL_ASYNC_H_
virtual ~CallbackBase()
Definition: async.h:23
CallbackFunction function_
Definition: async.h:53
DelegateT * delegate_
Definition: async.h:94
void operator()(const ParamT &value) const
Definition: async.h:137
void operator()() const
Definition: async.h:62
static CallbackTN * MakeCallback(typename BoundCallback< ParamT, DelegateT >::CallbackMethod method, DelegateT *delegate)
Definition: async.h:213
Definition: async.h:45
Callback(CallbackFunction function)
Definition: async.h:61
const ClosureDataT closure_data_
Definition: async.h:144
void operator()(const ParamT &value) const
Definition: async.h:50
BoundCallback(CallbackMethod method, DelegateT *delegate)
Definition: async.h:103
Callback(CallbackFunction function)
Definition: async.h:49
CallbackMethod method_
Definition: async.h:143
void operator()(const ParamT &value) const
Definition: async.h:91
DelegateT * delegate_
Definition: async.h:142
BoundCallback(CallbackMethod method, DelegateT *delegate)
Definition: async.h:88
BoundClosure(CallbackMethod method, DelegateT *delegate, ClosureDataT data)
Definition: async.h:134
static CallbackTN * MakeCallback(typename Callback< ParamT >::CallbackFunction function)
Definition: async.h:225
virtual ~CallbackBase()
Definition: async.h:30
void ThreadProxy(DelegateT *delegate, void(DelegateT::*method)())
Definition: async.h:239
CallbackBase< ParamT > CallbackTN
Definition: async.h:185
CallbackMethod method_
Definition: async.h:95
CallbackFunction function_
Definition: async.h:65
BoundClosure(CallbackMethod method, DelegateT *delegate, ClosureDataT data)
Definition: async.h:153
static CallbackTN * MakeClosure(typename BoundClosure< ParamT, DelegateT, ClosureDataT >::CallbackMethod method, DelegateT *delegate, const ClosureDataT &closure_data)
Definition: async.h:197