source: NonGTP/Boost/boost/multi_index/detail/scope_guard.hpp @ 857

Revision 857, 7.8 KB checked in by igarcia, 18 years ago (diff)
Line 
1/* Copyright 2003-2005 Joaquín M López Muñoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
5 *
6 * See http://www.boost.org/libs/multi_index for library home page.
7 */
8
9#ifndef BOOST_MULTI_INDEX_DETAIL_SCOPE_GUARD_HPP
10#define BOOST_MULTI_INDEX_DETAIL_SCOPE_GUARD_HPP
11
12#if defined(_MSC_VER)&&(_MSC_VER>=1200)
13#pragma once
14#endif
15
16namespace boost{
17
18namespace multi_index{
19
20namespace detail{
21
22/* Until some official version of the ScopeGuard idiom makes it into Boost,
23 * we locally define our own. This is a merely reformated version of
24 * ScopeGuard.h as defined in:
25 *   Alexandrescu, A., Marginean, P.:"Generic<Programming>: Change the Way You
26 *     Write Exception-Safe Code - Forever", C/C++ Users Jornal, Dec 2000,
27 *     http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/
28 * with the following modifications:
29 *   - General pretty formatting (pretty to my taste at least.)
30 *   - Naming style changed to standard C++ library requirements.
31 *   - safe_execute does not feature a try-catch protection, so we can
32 *     use this even if BOOST_NO_EXCEPTIONS is defined.
33 *   - Added scope_guard_impl4 and obj_scope_guard_impl3, (Boost.MultiIndex
34 *     needs them). A better design would provide guards for many more
35 *     arguments through the Boost Preprocessor Library.
36 *   - Added scope_guard_impl_base::touch (see below.)
37 *   - Removed RefHolder and ByRef, whose functionality is provided
38 *     already by Boost.Ref.
39 *   - Removed static make_guard's and make_obj_guard's, so that the code
40 *     will work even if BOOST_NO_MEMBER_TEMPLATES is defined. This forces
41 *     us to move some private ctors to public, though.
42 *
43 * NB: CodeWarrior Pro 8 seems to have problems looking up safe_execute
44 * without an explicit qualification.
45 */
46
47class scope_guard_impl_base
48{
49public:
50  scope_guard_impl_base():dismissed_(false){}
51  void dismiss()const{dismissed_=true;}
52
53  /* This helps prevent some "unused variable" warnings under, for instance,
54   * GCC 3.2.
55   */
56  void touch()const{}
57
58protected:
59  ~scope_guard_impl_base(){}
60
61  scope_guard_impl_base(const scope_guard_impl_base& other):
62    dismissed_(other.dismissed_)
63  {
64    other.dismiss();
65  }
66
67  template<typename J>
68  static void safe_execute(J& j){if(!j.dismissed_)j.execute();}
69 
70  mutable bool dismissed_;
71
72private:
73  scope_guard_impl_base& operator=(const scope_guard_impl_base&);
74};
75
76typedef const scope_guard_impl_base& scope_guard;
77
78template<typename F>
79class scope_guard_impl0:public scope_guard_impl_base
80{
81public:
82  scope_guard_impl0(F fun):fun_(fun){}
83  ~scope_guard_impl0(){scope_guard_impl_base::safe_execute(*this);}
84  void execute(){fun_();}
85
86protected:
87
88  F fun_;
89};
90
91template<typename F>
92inline scope_guard_impl0<F> make_guard(F fun)
93{
94  return scope_guard_impl0<F>(fun);
95}
96
97template<typename F,typename P1>
98class scope_guard_impl1:public scope_guard_impl_base
99{
100public:
101  scope_guard_impl1(F fun,P1 p1):fun_(fun),p1_(p1){}
102  ~scope_guard_impl1(){scope_guard_impl_base::safe_execute(*this);}
103  void execute(){fun_(p1_);}
104
105protected:
106  F        fun_;
107  const P1 p1_;
108};
109
110template<typename F,typename P1>
111inline scope_guard_impl1<F,P1> make_guard(F fun,P1 p1)
112{
113  return scope_guard_impl1<F,P1>(fun,p1);
114}
115
116template<typename F,typename P1,typename P2>
117class scope_guard_impl2:public scope_guard_impl_base
118{
119public:
120  scope_guard_impl2(F fun,P1 p1,P2 p2):fun_(fun),p1_(p1),p2_(p2){}
121  ~scope_guard_impl2(){scope_guard_impl_base::safe_execute(*this);}
122  void execute(){fun_(p1_,p2_);}
123
124protected:
125  F        fun_;
126  const P1 p1_;
127  const P2 p2_;
128};
129
130template<typename F,typename P1,typename P2>
131inline scope_guard_impl2<F,P1,P2> make_guard(F fun,P1 p1,P2 p2)
132{
133  return scope_guard_impl2<F,P1,P2>(fun,p1,p2);
134}
135
136template<typename F,typename P1,typename P2,typename P3>
137class scope_guard_impl3:public scope_guard_impl_base
138{
139public:
140  scope_guard_impl3(F fun,P1 p1,P2 p2,P3 p3):fun_(fun),p1_(p1),p2_(p2),p3_(p3){}
141  ~scope_guard_impl3(){scope_guard_impl_base::safe_execute(*this);}
142  void execute(){fun_(p1_,p2_,p3_);}
143
144protected:
145  F        fun_;
146  const P1 p1_;
147  const P2 p2_;
148  const P3 p3_;
149};
150
151template<typename F,typename P1,typename P2,typename P3>
152inline scope_guard_impl3<F,P1,P2,P3> make_guard(F fun,P1 p1,P2 p2,P3 p3)
153{
154  return scope_guard_impl3<F,P1,P2,P3>(fun,p1,p2,p3);
155}
156
157template<typename F,typename P1,typename P2,typename P3,typename P4>
158class scope_guard_impl4:public scope_guard_impl_base
159{
160public:
161  scope_guard_impl4(F fun,P1 p1,P2 p2,P3 p3,P4 p4):
162    fun_(fun),p1_(p1),p2_(p2),p3_(p3),p4_(p4){}
163  ~scope_guard_impl4(){scope_guard_impl_base::safe_execute(*this);}
164  void execute(){fun_(p1_,p2_,p3_,p4_);}
165
166protected:
167  F        fun_;
168  const P1 p1_;
169  const P2 p2_;
170  const P3 p3_;
171  const P4 p4_;
172};
173
174template<typename F,typename P1,typename P2,typename P3,typename P4>
175inline scope_guard_impl4<F,P1,P2,P3,P4> make_guard(
176  F fun,P1 p1,P2 p2,P3 p3,P4 p4)
177{
178  return scope_guard_impl4<F,P1,P2,P3,P4>(fun,p1,p2,p3,p4);
179}
180
181template<class Obj,typename MemFun>
182class obj_scope_guard_impl0:public scope_guard_impl_base
183{
184public:
185  obj_scope_guard_impl0(Obj& obj,MemFun mem_fun):obj_(obj),mem_fun_(mem_fun){}
186  ~obj_scope_guard_impl0(){scope_guard_impl_base::safe_execute(*this);}
187  void execute(){(obj_.*mem_fun_)();}
188
189protected:
190  Obj&   obj_;
191  MemFun mem_fun_;
192};
193
194template<class Obj,typename MemFun>
195inline obj_scope_guard_impl0<Obj,MemFun> make_obj_guard(Obj& obj,MemFun mem_fun)
196{
197  return obj_scope_guard_impl0<Obj,MemFun>(obj,mem_fun);
198}
199
200template<class Obj,typename MemFun,typename P1>
201class obj_scope_guard_impl1:public scope_guard_impl_base
202{
203public:
204  obj_scope_guard_impl1(Obj& obj,MemFun mem_fun,P1 p1):
205    obj_(obj),mem_fun_(mem_fun),p1_(p1){}
206  ~obj_scope_guard_impl1(){scope_guard_impl_base::safe_execute(*this);}
207  void execute(){(obj_.*mem_fun_)(p1_);}
208
209protected:
210  Obj&     obj_;
211  MemFun   mem_fun_;
212  const P1 p1_;
213};
214
215template<class Obj,typename MemFun,typename P1>
216inline obj_scope_guard_impl1<Obj,MemFun,P1> make_obj_guard(
217  Obj& obj,MemFun mem_fun,P1 p1)
218{
219  return obj_scope_guard_impl1<Obj,MemFun,P1>(obj,mem_fun,p1);
220}
221
222template<class Obj,typename MemFun,typename P1,typename P2>
223class obj_scope_guard_impl2:public scope_guard_impl_base
224{
225public:
226  obj_scope_guard_impl2(Obj& obj,MemFun mem_fun,P1 p1,P2 p2):
227    obj_(obj),mem_fun_(mem_fun),p1_(p1),p2_(p2)
228  {}
229  ~obj_scope_guard_impl2(){scope_guard_impl_base::safe_execute(*this);}
230  void execute(){(obj_.*mem_fun_)(p1_,p2_);}
231
232protected:
233  Obj&     obj_;
234  MemFun   mem_fun_;
235  const P1 p1_;
236  const P2 p2_;
237};
238
239template<class Obj,typename MemFun,typename P1,typename P2>
240inline obj_scope_guard_impl2<Obj,MemFun,P1,P2>
241make_obj_guard(Obj& obj,MemFun mem_fun,P1 p1,P2 p2)
242{
243  return obj_scope_guard_impl2<Obj,MemFun,P1,P2>(obj,mem_fun,p1,p2);
244}
245
246template<class Obj,typename MemFun,typename P1,typename P2,typename P3>
247class obj_scope_guard_impl3:public scope_guard_impl_base
248{
249public:
250  obj_scope_guard_impl3(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3):
251    obj_(obj),mem_fun_(mem_fun),p1_(p1),p2_(p2),p3_(p3)
252  {}
253  ~obj_scope_guard_impl3(){scope_guard_impl_base::safe_execute(*this);}
254  void execute(){(obj_.*mem_fun_)(p1_,p2_,p3_);}
255
256protected:
257  Obj&     obj_;
258  MemFun   mem_fun_;
259  const P1 p1_;
260  const P2 p2_;
261  const P3 p3_;
262};
263
264template<class Obj,typename MemFun,typename P1,typename P2,typename P3>
265inline obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3>
266make_obj_guard(Obj& obj,MemFun mem_fun,P1 p1,P2 p2,P3 p3)
267{
268  return obj_scope_guard_impl3<Obj,MemFun,P1,P2,P3>(obj,mem_fun,p1,p2,p3);
269}
270
271} /* namespace multi_index::detail */
272
273} /* namespace multi_index */
274
275} /* namespace boost */
276
277#endif
Note: See TracBrowser for help on using the repository browser.