資源釋放助手類,支持lambda
、指針、數組指針及自定義函數。
auto p = new int(0);
{
ON_SCOPE_EXIT(ScopeGuard::Delete, p);
}
assert(p == nullptr);
auto a = new int[10](0);
{
ON_SCOPE_EXIT(ScopeGuard::DeleteArray, a);
}
assert(a == nullptr);
int n = 0;
{
ON_SCOPE_EXIT([&n]()
{
n = 1;
});
}
assert(n == 1);
void set(int& n, int m)
{
n = m;
}
int n = 0;
{
ON_SCOPE_EXIT(set, n, 1);
}
assert(n == 1);
注意C++11之前传引用要使用boost::ref
,即上面需改為:
ON_SCOPE_EXIT(set, boost::ref(n), 1);
不可變引用需用boost::cref
,C++11後使用無需。
int add(int a, int b)
{
return a + b;
}
int n = 0;
{
ON_SCOPE_EXIT(std::ref(n), add, 1, 2);
}
assert(n == 3);
此處的std::ref
或boost::ref
不可缺,可用SCOPE_GUARD_REF
宏來作兼容,同理還有SCOPE_GUARD_CREF
。
int n = 0;
{
ScopeGuard set1([&n]()
{
n = 1;
});
set1.reset();
assert(n == 1);
}
int n = 0;
{
ScopeGuard set1([&n]()
{
n = 1;
});
set1.release();
}
assert(n == 0);