C++ ScopeGuard

2023-01-09 08:36:24

一般情况下申请对象资源时候使用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++风格)和快速迭代式开发

  • 作者:degawong
  • 原文链接:https://blog.csdn.net/degawong/article/details/111634572
    更新时间:2023-01-09 08:36:24