回滚操作

 
/******************************************************************** 
 file name : Tool.h 
 author  :   Clark/陈泽丹 
 created :   2011-11-30
 工具包:
 参照Loki库提供指令封装和提供回滚操作
*********************************************************************/ 
#pragma once

#define TYPELIST_1(T1) TOOL::Typelist<T1, ::TOOL::NullType>
#define TYPELIST_2(T1, T2) TOOL::Typelist<T1, TYPELIST_1(T2) >

namespace TOOL
{
	class NullType {};
	template <class T, class U>
	struct Typelist
	{
		typedef T Head;
		typedef U Tail;
	};

	namespace Private
	{
		template <class R>
		struct FunctorImplBase
		{
			typedef R ResultType;
			typedef int Parm1;
			typedef int Parm2;
		};

		template <class R, class TList>
		class FunctorImpl;
		template <class R>
		class FunctorImpl<R, NullType>: public FunctorImplBase<R>
		{
		public:
			typedef R ResultType;
			virtual R operator()() = 0;
		};
		template <class R, class P1>
		class FunctorImpl<R, TYPELIST_1(P1)>: public FunctorImplBase<R>
		{
		public:
			typedef R ResultType;
			typedef P1 Parm1;
			virtual R operator()(Parm1) = 0;
		};
		template <class R, class P1, class P2>
		class FunctorImpl<R, TYPELIST_2(P1, P2)>: public FunctorImplBase<R>
		{
		public:
			typedef R ResultType;
			typedef P1 Parm1;
			typedef P2 Parm2;
			virtual R operator()(Parm1, Parm2) = 0;
		};

		//普通函数和仿函数的对外接口
		template <class Impl, class Fun>
		class FunctorHandler: public Impl
		{
		private:
			typedef typename Impl Base;
			typedef typename Base::ResultType ResultType;
			typedef typename Base::Parm1 Parm1;
			typedef typename Base::Parm2 Parm2;
			Fun m_fun;

		public:
			FunctorHandler(const Fun& fun) : m_fun(fun) {}
			ResultType operator()(){ return m_fun(); }
			ResultType operator()(Parm1 p1){ return m_fun(p1); }
			ResultType operator()(Parm1 p1, Parm2 p2){ return m_fun(p1, p2); }
		};

		//成员函数的对外接口
		template <class Impl, class PointerToObj, class PointerToMemFn>
		class MemFunHandler : public Impl
		{
		private:
			typedef typename Impl Base;
			typedef typename Base::ResultType ResultType;
			typedef typename Base::Parm1 Parm1;
			typedef typename Base::Parm2 Parm2;
			PointerToObj m_pObj;
			PointerToMemFn m_pMemFn;

		public:
			MemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn): m_pObj(pObj), m_pMemFn(pMemFn){}
			ResultType operator()(){ return ((*m_pObj).*m_pMemFn)(); }
			ResultType operator()(Parm1 p1){ return ((*m_pObj).*m_pMemFn)(p1); }
			ResultType operator()(Parm1 p1, Parm2 p2){ return ((*m_pObj).*m_pMemFn)(p1, p2); }
		};
	}

	//指令封装
	//将普通函数,仿函数和成员函数的对外接口整合在一起
	template <class R, class TList = NullType>
	class Functor
	{
	private:
		typedef Private::FunctorImpl<R, TList> Impl;
		typedef R ResultType;
		typedef TList ParmList;
		typedef typename Impl::Parm1 Parm1;
		typedef typename Impl::Parm2 Parm2;
		Impl* m_spImpl;
		//不提供拷贝构造
		Functor(const Functor& other){}
		//不提供赋值语句
		Functor& operator=(const Functor& other){}

	public:
		template <class Fun>
		Functor(Fun fun):m_spImpl(NULL)
		{
			m_spImpl = new Private::FunctorHandler<Impl, Fun>(fun);
			if( NULL == m_spImpl){ /*Log语句*/ }
		}
		template <class PtrObj, class MemFn>
		Functor(const PtrObj& p, MemFn memFn):m_spImpl(NULL)
		{
			m_spImpl = new Private::MemFunHandler<Impl, PtrObj, MemFn>(p, memFn);
			if( NULL == m_spImpl){ /*Log语句*/ }
		}
		ResultType operator()(){ return (*m_spImpl)(); }
		ResultType operator()(Parm1 p1){ return (*m_spImpl)(p1); }
		ResultType operator()(Parm1 p1, Parm2 p2){ return (*m_spImpl)(p1, p2); }
	};

	//回滚操作
	//回滚的触发条件:本个体操作已成功执行,但群体操作未成功,导致本个体操作回滚
	template <class R>
	class Rall
	{
	public:
		Rall():cmd(NULL),m_bIsOK(false){}
		~Rall()
		{
			if( NULL != cmd)
			{
				if( !m_bIsOK )
				{ 
					(*cmd)();
				}
				delete cmd;
				cmd = NULL;
			}
		}

		template <class Fun>
		void SetRall(Fun fun)
		{ 
			if( NULL != cmd){ delete cmd; cmd = NULL; }
			cmd = new Functor<R>(fun); 
		}
		template <class PtrObj, class MemFn>
		void SetRall(const PtrObj& p, MemFn memFn)
		{
			if( NULL != cmd){ delete cmd; cmd = NULL; }
			cmd = new Functor<R>(p, memFn);
		}
		void OK(){ m_bIsOK = true; }
	private:
		bool m_bIsOK;
		Functor<R>* cmd;
	};
	/*
	使用:
	TOOL::Rall<void> r2;
	DataBase db;
	if( db.doDB(true)) 
	r2.SetRall(&db,&DataBase::UndoDB);
	else return;

	TOOL::Rall<void> r1;
	if( doFun(true)) 
	r1.SetRall(UndoFun);
	else return;

	r1.OK();
	r2.OK();
	*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值