纯C++版委托及Variant

#ifndef DELEGATION_H
#define DELEGATION_H

/********************************************************************************
** Copyright (C) 2011 Liuxb
**
** 创建标识:[Liuxb|20110711]
** 解决方法:C++版委托
**
********************************************************************************/



#include <iostream>
#include <list>
#include <cassert>




/********************************************************************************
** 委托接口
********************************************************************************/
struct DelegationInterface
{
    virtual ~DelegationInterface(){};
    virtual void Run() = 0;
};


/********************************************************************************
** 委托器,将某类型的函数委托给DelegationImpl的Run执行
********************************************************************************/
template<typename T>
struct DelegationImpl : public DelegationInterface
{
    typedef void (T::* _pF_t)(); // 指向类T成员函数的指针类型

    DelegationImpl(T* _PP, _pF_t pF) :_P(_PP), _PF(pF) {}
    virtual void Run()
    {
        if(_P)
        {
            (_P->*_PF)();

        } // 成员函数调用,很别扭的写法(_P->*_PF)();
    }

    T* _P; // Receiver类
    _pF_t _PF; // 指向Receiver类的某个成员函数
};

/********************************************************************************
** 信号处理器:ConnectSlot用来连接执行函数pF,operator()调用pF来处理信号。
********************************************************************************/
struct Signal
{
    DelegationInterface* _PI;
    Signal():_PI(0){}
    void operator()()
    {
        if(_PI)
        {
            _PI->Run();
        }
    }

    template<typename T>
    void ConnectSlot(T& recv,void(T::*pF)())
    {
        _PI = new DelegationImpl<T>(&recv,pF);
    }
};


/********************************************************************************
** 消息转发简便方式
** 例:
** struct ExecuteCentre {void AddStock();};
** ExecuteHelper<ExecuteCentre>::Invoke(&ExecuteCentre::AddStock);
********************************************************************************/
template<typename T>
struct ExecuteHelper
{
    typedef void (T::* _pF_t)(); // 指向类T成员函数的指针类型


    static void Invoke(_pF_t pF)
    {
        Signal signal;
        T fun;
        signal.ConnectSlot<T>(fun,pF);
        signal();
    }
};



/********************************************************************************
** 用于存储任意对象
** 例:
** Variant _any = any(8);
** int i = a.GetValue<int>();
********************************************************************************/
class Variant
{
public:
	
	//保存真正数据的接口类
	class placeholder
	{
	public:     
		virtual ~placeholder()
		{
		}
	public: 

		virtual const std::type_info & type() const = 0;
		virtual placeholder * clone() const = 0;    
	};

	//真正保存和获取数据的类。
	template<typename ValueType>
	class holder : public placeholder
	{
	public:         
		holder(const ValueType & value): held(value)
		{
		}

	public: 

		virtual const std::type_info & type() const
		{
			return typeid(ValueType);
		}

		virtual placeholder * clone() const
		{
			return new holder(held);//使用了原型模式
		}

	public: 

		//真正的数据,就保存在这里
		ValueType held;
	};

public:

	Variant(): content(NULL)   
	{       
	}

	//模板构造函数,参数可以是任意类型,真正的数据保存在content中
	template<typename ValueType>
	Variant(const ValueType & value): content(new holder<ValueType>(value))
	{
	}  

	//拷贝构造函数
	Variant(const Variant & other)
		: content(other.content ? other.content->clone() : 0)
	{
	}

	template<typename ValueType>
	Variant operator = (const ValueType value)
	{
		if(NULL != content)
		{
			delete content;
			content = NULL;
		}
		this->content = new holder<ValueType>(value);
		return *this;
	}

	//析构函数,删除保存数据的content对象
	~Variant()
	{
		if(NULL != content)
		{
			delete content;
			content = NULL;
		}
	}

private:
	//一个placeholde对象指针,指向其子类folder的一个实现
	// 即content( new holder<ValueType>(value) )语句
	placeholder* content;

	//查询真实数据的类型。
	const std::type_info & type() const
	{
		return content ? content->type() : typeid(void);
	}

public:
	template<typename ValueType> 
	ValueType GetValue()
	{
		assert( this->type() == typeid(ValueType) );
		return static_cast<Variant::holder<ValueType> *>(this->content)->held;
	}
};


/********************************************************************************
** Mission数据包,能存储任意数据类型
** 例:
** struct MyStruct {...};
** Mission mission;
** MyStruct stu;
** mission.SetValue<MyStruct>(stu);
** MyStruct stu2 = mission.GetValue<MyStruct>();
********************************************************************************/
class Mission
{
public:
    Mission():_variant(),_retCode(),_ret(true){}

    template<typename T>
    T GetValue()
    {
		return  _variant.GetValue<T>();
    }

    template<typename T>
    void SetValue(const T& value)
    {
        _variant = value;
    }

    template<typename T>
    T GetRetCode()
    {
		return _variant.GetValue<T>();
    }

    template<typename T>
    void SetRetCode(const T& value)
    {
        _retCode = Variant(value);
    }


    bool GetRet()
    {
        return _ret;
    }

    void SetRet(bool ret)
    {
        _ret = ret;
    }

private:
    Variant _variant;	      //传给执行函数的参数
    Variant _retCode;	      //返回结果
    bool _ret;              //消息处理是否成功
};

//Mission类工厂
class MissionFactory
{
public:
    static Mission* CreateMission();

private:
    static Mission* pMission;
};


#endif // DELEGATION_H

#include "stdafx.h"
#include "delegation.h"

Mission* MissionFactory::CreateMission()
{
    if(pMission)
     {
         return pMission;
     }
     else
     {
         pMission = new Mission;
         return pMission;
     }
}

Mission* MissionFactory::pMission = 0;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值