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