Mobile API Reference  MicroStrategy 2019
ScopeGuard.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : ScopeGuard.h
3 // AUTHOR : Juan Pablo Muraira
4 // CREATION : 9/26/01
5 // Copyright (C) MicroStrategy Incorporated 2001
6 // All Rights Reserved
7 //==============================================================================================
8 #ifndef MBase_ScopeGuard_h
9 #define MBase_ScopeGuard_h
10 
42 namespace MBase
43 {
51  {
52  public:
57  void Dismiss() const throw()
58  {
59  mDismissed = true;
60  }
61 
66  void UnDismiss() const throw()
67  {
68  mDismissed = false;
69  }
70 
71  protected:
72  ScopeGuardImplBase() throw():
73  mDismissed(false)
74  {
75  }
76 
82  ScopeGuardImplBase(const ScopeGuardImplBase& iScopeGuardImplBase) throw():
83  mDismissed(iScopeGuardImplBase.mDismissed)
84  {
85  iScopeGuardImplBase.Dismiss();
86  }
87 
88  ~ScopeGuardImplBase() throw() // nonvirtual (see below why)
89  {
90  }
91 
92  mutable bool mDismissed;
93 
94  private:
95  // Disable assignment
96  ScopeGuardImplBase& operator=(const ScopeGuardImplBase&);
97  };
98 
103  template <class Function>
105  {
106  public:
107  ScopeGuardImpl0(Function iFunction) throw():
108  mFunction(iFunction)
109  {
110  }
112  {
113  if (!mDismissed)
114  {
115  (*mFunction)();
116  }
117  }
118  private:
119  Function mFunction;
120  };
121 
125  template <class Function>
127  {
128  return ScopeGuardImpl0<Function>(iFunction);
129  }
130 
135  template <class Function, class Parameter1>
137  {
138  public:
139  ScopeGuardImpl1(Function iFunction,const Parameter1& iParameter1) throw():
140  mFunction(iFunction),
141  mParameter1(iParameter1)
142  {
143  }
145  {
146  if (!mDismissed)
147  {
148  (*mFunction)(mParameter1);
149  }
150  }
151  private:
152  Function mFunction;
153  // TQMS 278351, xfan, 1/18/2011
154  const Parameter1& mParameter1;
155  };
156 
160  template <class Function, class Parameter1>
161  ScopeGuardImpl1<Function, Parameter1>
162  MakeMethodGuard(Function iFunction, const Parameter1& iParameter1) throw()
163  {
164  return ScopeGuardImpl1<Function, Parameter1>(iFunction, iParameter1);
165  }
166 
171  template <class Function, class Parameter1, class Parameter2>
173  {
174  public:
176  const Parameter1& iParameter1,
177  const Parameter2& iParameter2) throw():
178  mFunction(iFunction),
179  mParameter1(iParameter1),
180  mParameter2(iParameter2)
181  {
182  }
184  {
185  if (!mDismissed)
186  {
187  (*mFunction)(mParameter1,mParameter2);
188  }
189  }
190  private:
191  Function mFunction;
192  const Parameter1& mParameter1;
193  const Parameter2& mParameter2;
194  };
195 
199  template <class Function, class Parameter1, class Parameter2>
200  ScopeGuardImpl2<Function, Parameter1, Parameter2>
201  MakeMethodGuard(Function iFunction, const Parameter1& iParameter1,
202  const Parameter2& iParameter2) throw()
203  {
205  iFunction, iParameter1, iParameter2);
206  }
207 
212  template <class Function, class Parameter1, class Parameter2, class Parameter3>
214  {
215  public:
217  const Parameter1& iParameter1,
218  const Parameter2& iParameter2,
219  const Parameter3& iParameter3) throw():
220  mFunction(iFunction),
221  mParameter1(iParameter1),
222  mParameter2(iParameter2),
223  mParameter3(iParameter3)
224  {
225  }
227  {
228  if (!mDismissed)
229  {
230  (*mFunction)(mParameter1,mParameter2,mParameter3);
231  }
232  }
233  private:
234  Function mFunction;
235  const Parameter1& mParameter1;
236  const Parameter2& mParameter2;
237  const Parameter3& mParameter3;
238  };
239 
243  template <class Function, class Parameter1, class Parameter2, class Parameter3>
244  ScopeGuardImpl3<Function, Parameter1, Parameter2, Parameter3>
245  MakeMethodGuard(Function iFunction, const Parameter1& iParameter1,
246  const Parameter2& iParameter2,const Parameter3& iParameter3) throw()
247  {
249  iFunction, iParameter1, iParameter2, iParameter3);
250  }
251 
256  template <class Function, class Parameter1, class Parameter2, class Parameter3, class Parameter4>
258  {
259  public:
261  const Parameter1& iParameter1,
262  const Parameter2& iParameter2,
263  const Parameter3& iParameter3,
264  const Parameter4& iParameter4) throw():
265  mFunction(iFunction),
266  mParameter1(iParameter1),
267  mParameter2(iParameter2),
268  mParameter3(iParameter3),
269  mParameter4(iParameter4)
270  {
271  }
273  {
274  if (!mDismissed)
275  {
276  (*mFunction)(mParameter1,mParameter2,mParameter3,mParameter4);
277  }
278  }
279  private:
280  Function mFunction;
281  const Parameter1& mParameter1;
282  const Parameter2& mParameter2;
283  const Parameter3& mParameter3;
284  const Parameter4& mParameter4;
285  };
286 
290  template <class Function, class Parameter1, class Parameter2, class Parameter3, class Parameter4>
291  ScopeGuardImpl4<Function, Parameter1, Parameter2, Parameter3, Parameter4>
292  MakeMethodGuard(Function iFunction, const Parameter1& iParameter1,
293  const Parameter2& iParameter2,const Parameter3& iParameter3,const Parameter4& iParameter4) throw()
294  {
296  iFunction, iParameter1, iParameter2, iParameter3, iParameter4);
297  }
298 
299 
300 
305  template <class Object, class MemberFunction>
307  {
308  public:
309  ObjectScopeGuardImpl0(Object& iObject, MemberFunction iMemberFunction) throw():
310  mObject(iObject),
311  mMemberFunction(iMemberFunction)
312  {
313  }
315  {
316  if (!mDismissed)
317  {
318  (mObject.*mMemberFunction)();
319  }
320  }
321  private:
322  Object& mObject;
323  MemberFunction mMemberFunction;
324  };
325 
329  template <class Object, class MemberFunction>
330  ObjectScopeGuardImpl0<Object, MemberFunction>
331  MakeObjectGuard(Object& iObject, MemberFunction iMemberFunction) throw()
332  {
333  return ObjectScopeGuardImpl0<Object, MemberFunction>(iObject, iMemberFunction);
334  }
335 
340  template <class Object, class MemberFunction, class Parameter1>
342  {
343  public:
344  ObjectScopeGuardImpl1( Object& iObject,
345  MemberFunction iMemberFunction,
346  Parameter1 iParameter1) throw():
347  mObject(iObject),
348  mMemberFunction(iMemberFunction),
349  mParameter1(iParameter1)
350  {
351  }
353  {
354  if (!mDismissed)
355  {
356  (mObject.*mMemberFunction)(mParameter1);
357  }
358  }
359  private:
360  Object& mObject;
361  MemberFunction mMemberFunction;
362  Parameter1 mParameter1;
363  };
364 
368  template <class Object, class MemberFunction, class Parameter1>
369  ObjectScopeGuardImpl1<Object, MemberFunction, Parameter1>
370  MakeObjectGuard(Object& iObject,
371  MemberFunction iMemberFunction,
372  Parameter1 iParameter1) throw()
373  {
375  (iObject, iMemberFunction, iParameter1);
376  }
377 
382  template <class Object, class MemberFunction, class Parameter1, class Parameter2>
384  {
385  public:
386  ObjectScopeGuardImpl2( Object& iObject,
387  MemberFunction iMemberFunction,
388  Parameter1 iParameter1,
389  Parameter2 iParameter2) throw():
390  mObject(iObject),
391  mMemberFunction(iMemberFunction),
392  mParameter1(iParameter1),
393  mParameter2(iParameter2)
394  {
395  }
397  {
398  if (!mDismissed)
399  {
400  (mObject.*mMemberFunction)(mParameter1, mParameter2);
401  }
402  }
403  private:
404  Object& mObject;
405  MemberFunction mMemberFunction;
406  Parameter1 mParameter1;
407  Parameter2 mParameter2;
408  };
409 
413  template <class Object, class MemberFunction, class Parameter1, class Parameter2>
414  ObjectScopeGuardImpl2<Object, MemberFunction, Parameter1, Parameter2>
415  MakeObjectGuard(Object& iObject,MemberFunction iMemberFunction,
416  Parameter1 iParameter1, Parameter2 iParameter2) throw()
417  {
419  (iObject, iMemberFunction, iParameter1, iParameter2);
420  }
421 
422  // typedef ScopeGuard for ease of use (see top of the file)
424 }
425 
426 #endif // MBase_ScopeGuard_h
~ScopeGuardImplBase()
Definition: ScopeGuard.h:88
bool mDismissed
Definition: ScopeGuard.h:92
Definition: ScopeGuard.h:306
ScopeGuardImplBase(const ScopeGuardImplBase &iScopeGuardImplBase)
Definition: ScopeGuard.h:82
~ScopeGuardImpl0()
Definition: ScopeGuard.h:111
ObjectScopeGuardImpl1(Object &iObject, MemberFunction iMemberFunction, Parameter1 iParameter1)
Definition: ScopeGuard.h:344
~ScopeGuardImpl2()
Definition: ScopeGuard.h:183
ScopeGuardImpl1(Function iFunction, const Parameter1 &iParameter1)
Definition: ScopeGuard.h:139
~ScopeGuardImpl4()
Definition: ScopeGuard.h:272
Definition: ScopeGuard.h:213
ScopeGuardImpl0(Function iFunction)
Definition: ScopeGuard.h:107
Definition: ScopeGuard.h:341
ObjectScopeGuardImpl0< Object, MemberFunction > MakeObjectGuard(Object &iObject, MemberFunction iMemberFunction)
Definition: ScopeGuard.h:331
~ObjectScopeGuardImpl0()
Definition: ScopeGuard.h:314
ScopeGuardImpl0< Function > MakeMethodGuard(Function iFunction)
Definition: ScopeGuard.h:126
enum _Function Function
void UnDismiss() const
Definition: ScopeGuard.h:66
ObjectScopeGuardImpl0(Object &iObject, MemberFunction iMemberFunction)
Definition: ScopeGuard.h:309
Definition: ScopeGuard.h:104
~ScopeGuardImpl3()
Definition: ScopeGuard.h:226
~ScopeGuardImpl1()
Definition: ScopeGuard.h:144
Definition: ScopeGuard.h:383
ScopeGuardImpl4(Function iFunction, const Parameter1 &iParameter1, const Parameter2 &iParameter2, const Parameter3 &iParameter3, const Parameter4 &iParameter4)
Definition: ScopeGuard.h:260
ObjectScopeGuardImpl2(Object &iObject, MemberFunction iMemberFunction, Parameter1 iParameter1, Parameter2 iParameter2)
Definition: ScopeGuard.h:386
Definition: Allocator.h:47
~ObjectScopeGuardImpl1()
Definition: ScopeGuard.h:352
~ObjectScopeGuardImpl2()
Definition: ScopeGuard.h:396
Definition: ScopeGuard.h:172
const ScopeGuardImplBase & ScopeGuard
Definition: ScopeGuard.h:423
Definition: ScopeGuard.h:50
ScopeGuardImplBase()
Definition: ScopeGuard.h:72
ScopeGuardImpl3(Function iFunction, const Parameter1 &iParameter1, const Parameter2 &iParameter2, const Parameter3 &iParameter3)
Definition: ScopeGuard.h:216
ScopeGuardImpl2(Function iFunction, const Parameter1 &iParameter1, const Parameter2 &iParameter2)
Definition: ScopeGuard.h:175
void Dismiss() const
Definition: ScopeGuard.h:57
Definition: ScopeGuard.h:257
Definition: ScopeGuard.h:136