C++ ScopeGuard

一般情况下申请对象资源时候使用RAII应该可以满足需求,但总有些情况下不需要深度包装的对象申请时候是不太合适这些方案的,如果有需求的时候,可以参考下如下代码。

class noncopyable {
protected:
	noncopyable() = default;
	~noncopyable() = default;
	noncopyable(const noncopyable& _) = delete;
	noncopyable& operator=(const noncopyable& _) = delete;
};

template<typename _lambda>
class scope_guard : public noncopyable {
public:
	explicit scope_guard(_lambda&& exit_operation) :
		__dismissed(false),
		__exit_operation(std::forward<_lambda>(exit_operation)) {
	}

	explicit scope_guard(scope_guard&& anther) :
		__dismissed(anther._dismissed)
		__exit_operation(std::move(anther.__exit_operation)) {
	}

	~scope_guard() {
		if (!__dismissed) __exit_operation();
	}

public:
	void dismiss() {
		__dismissed = true;
	}
private:
	bool __dismissed;
	_lambda __exit_operation;
};

template<typename _lambda>
auto make_guard(_lambda&& lambda) {
	return scope_guard<_lambda>(std::forward<_lambda>(lambda));
}

下面几个宏定义可以方便scope_guard的定义与使用

// make the unique name of the defered operation
#define make_name(index, v) _##index##_##v
// replace variable value of their marco name
#define forward(_1, _2) make_name(_1, _2)
// define the auto variable of the defer operation
#define defer(operation) auto forward(__COUNTER__, scope_guard) = make_guard([&](){ operation; });

测试代码如下

int main() {
	auto i = 0;
	auto c = [&](auto i) {
		cout << "lambda " << i << endl;
	};
	scope_guard sg([=]() {
		c(i);
	});
	auto mg = make_guard([&]() {
		c(i);
	});
	defer(c(i));
	i += 1;
	auto make_name(1, 2) = 1;
}

输入结果

lambda 1
lambda 1
lambda 0

注意到sg使用传值方式调用函数,所以其调用时候输出了0,另外两个变量使用了引用,所以输出结果为1,有点闭包的味道似的。同理,把输出提示语句的lambda函数替换成资源分配与回收的语句即可。

参考

qicosmos : 用 C++11 实现简洁的ScopeGuard
刘未鹏 : C++11(及现代C++风格)和快速迭代式开发

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值