⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scopeguard.h

📁 一些unix下的c/c++的util包
💻 H
字号:
/**
 * 请添加说明
 * @file ScopeGuard.h
 * @date 2006年11月1日
 * @author 周志武
 * @version 1.0.0: 初始版本
 **/

#ifndef SCOPEGUARD_H_
#define SCOPEGUARD_H_

namespace nlkit
{
template <class T>
class RefHolder
{
	T& m_ref;
public:
	RefHolder(T& ref) : m_ref(ref) {}
	operator T& () const 
	{
		return m_ref;
	}
private:
    // Disable assignment - not implemented
    RefHolder& operator=(const RefHolder&);
};

template <class T>
inline RefHolder<T> byRef(T& t)
{
	return RefHolder<T>(t);
}

class ScopeGuardImplBase
{
	ScopeGuardImplBase& operator =(const ScopeGuardImplBase&);
protected:
	~ScopeGuardImplBase()
	{
	}
	ScopeGuardImplBase(const ScopeGuardImplBase& other) throw() 
		: m_dismissed(other.m_dismissed)
	{
		other.Dismiss();
	}
	template <typename J>
	static void safeExecute(J& j) throw() 
	{
		if (!j.m_dismissed)
			try
			{
				j.execute();
			}
			catch(...)
			{
			}
	}
	
	mutable bool m_dismissed;
public:
	ScopeGuardImplBase() throw() : m_dismissed(false) 
	{
	}
	void dismiss() const throw() 
	{
		m_dismissed = true;
	}
};

typedef const ScopeGuardImplBase& ScopeGuard;

template <typename F>
class ScopeGuardImpl0 : public ScopeGuardImplBase
{
public:
	static ScopeGuardImpl0<F> MakeGuard(F fun)
	{
		return ScopeGuardImpl0<F>(fun);
	}
	~ScopeGuardImpl0() throw() 
	{
		safeExecute(*this);
	}
	void execute() 
	{
		m_fun();
	}
protected:
	ScopeGuardImpl0(F fun) : m_fun(fun) 
	{
	}
	F m_fun;
};

template <typename F> 
inline ScopeGuardImpl0<F> MakeGuard(F fun)
{
	return ScopeGuardImpl0<F>::MakeGuard(fun);
}

template <typename F, typename P1>
class ScopeGuardImpl1 : public ScopeGuardImplBase
{
public:
	static ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
	{
		return ScopeGuardImpl1<F, P1>(fun, p1);
	}
	~ScopeGuardImpl1() throw() 
	{
		safeExecute(*this);
	}
	void execute()
	{
		m_fun(m_p1);
	}
protected:
	ScopeGuardImpl1(F fun, P1 p1) : m_fun(fun), m_p1(p1) 
	{
	}
	F m_fun;
	const P1 m_p1;
};

template <typename F, typename P1> 
inline ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
{
	return ScopeGuardImpl1<F, P1>::MakeGuard(fun, p1);
}

template <typename F, typename P1, typename P2>
class ScopeGuardImpl2: public ScopeGuardImplBase
{
public:
	static ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
	{
		return ScopeGuardImpl2<F, P1, P2>(fun, p1, p2);
	}
	~ScopeGuardImpl2() throw() 
	{
		safeExecute(*this);
	}
	void execute()
	{
		m_fun(m_p1, m_p2);
	}
protected:
	ScopeGuardImpl2(F fun, P1 p1, P2 p2) : m_fun(fun), m_p1(p1), m_p2(p2) 
	{
	}
	F m_fun;
	const P1 m_p1;
	const P2 m_p2;
};

template <typename F, typename P1, typename P2>
inline ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
{
	return ScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2);
}

template <typename F, typename P1, typename P2, typename P3>
class ScopeGuardImpl3 : public ScopeGuardImplBase
{
public:
	static ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
	{
		return ScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3);
	}
	~ScopeGuardImpl3() throw() 
	{
		safeExecute(*this);
	}
	void execute()
	{
		m_fun(m_p1, m_p2, m_p3);
	}
protected:
	ScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3) : m_fun(fun), m_p1(p1), m_p2(p2), m_p3(p3) 
	{
	}
	F m_fun;
	const P1 m_p1;
	const P2 m_p2;
	const P3 m_p3;
};

template <typename F, typename P1, typename P2, typename P3>
inline ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
{
	return ScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3);
}

//************************************************************

template <class Obj, typename MemFun>
class ObjScopeGuardImpl0 : public ScopeGuardImplBase
{
public:
	static ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun)
	{
		return ObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
	}
	~ObjScopeGuardImpl0() throw() 
	{
		safeExecute(*this);
	}
	void execute() 
	{
		(m_obj.*m_memFun)();
	}
protected:
	ObjScopeGuardImpl0(Obj& obj, MemFun memFun) 
		: m_obj(obj), m_memFun(memFun) {}
	Obj& m_obj;
	MemFun m_memFun;
};

template <class Obj, typename MemFun>
inline ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun)
{
	return ObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun);
}

template <class Obj, typename MemFun, typename P1>
class ObjScopeGuardImpl1 : public ScopeGuardImplBase
{
public:
	static ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
	{
		return ObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1);
	}
	~ObjScopeGuardImpl1() throw() 
	{
		safeExecute(*this);
	}
	void execute() 
	{
		(m_obj.*m_memFun)(m_p1);
	}
protected:
	ObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1) 
		: m_obj(obj), m_memFun(memFun), m_p1(p1) {}
	Obj& m_obj;
	MemFun m_memFun;
	const P1 m_p1;
};

template <class Obj, typename MemFun, typename P1>
inline ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
{
	return ObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1);
}

template <class Obj, typename MemFun, typename P1, typename P2>
class ObjScopeGuardImpl2 : public ScopeGuardImplBase
{
public:
	static ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
	{
		return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2);
	}
	~ObjScopeGuardImpl2() throw() 
	{
		safeExecute(*this);
	}
	void execute() 
	{
		(m_obj.*m_memFun)(m_p1, m_p2);
	}
protected:
	ObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2) 
		: m_obj(obj), m_memFun(memFun), m_p1(p1), m_p2(p2) {}
	Obj& m_obj;
	MemFun m_memFun;
	const P1 m_p1;
	const P2 m_p2;
};

template <class Obj, typename MemFun, typename P1, typename P2>
inline ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
{
	return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>::MakeObjGuard(obj, memFun, p1, p2);
}

#define CONCATENATE_DIRECT(s1, s2) s1##s2
#define CONCATENATE(s1, s2) CONCATENATE_DIRECT(s1, s2)
#define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __LINE__)

#define ON_BLOCK_EXIT ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) = MakeGuard
#define ON_BLOCK_EXIT_OBJ ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) = MakeObjGuard
}

#endif //SCOPEGUARD_H_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -