我的消息循环

来源:互联网 发布:程序员 年薪 编辑:程序博客网 时间:2024/04/29 03:22

#pragma once
#include "WXMessage.h"
#include <list>
#include <vector>
#include "MacroCode.h"


class CWXMsger
{
public:
    CWXMsger(void);
    virtual ~CWXMsger(void);
protected:
    CWXMsger(CWXMsger& msg);

public:
    bool SendMessage(CWXMessage& msg);
    template<class TMsgInput>
    void PostMessage(CWXMessage& msg)
    {
        CWXMessage* pMsg = new CWXMessage;
        pMsg->Copy<TMsgInput>(msg);
        this->m_vecMsg.push_back(pMsg);
    }

    virtual void Dispose(void);
    virtual void AddMsger(CWXMsger* pMsger);
    virtual void MoveMsger(CWXMsger* pMsger);
    virtual bool IsFindMsger(CWXMsger* pMsger);
protected:
    virtual bool DisposeInAllMsger(CWXMessage& msg);
    virtual void DisposeListInAllMsger(std::vector<CWXMessage*>& vecMsg);
    virtual bool BNSProc(CWXMessage& msg);
    virtual bool BNSListProc(CWXMessage& msg);
private:
    std::vector<CWXMsger*> m_vecMsger;
    std::vector<CWXMessage*> m_vecMsg;
};

 

 

#include "StdAfx.h"
#include "WXMessage.h"
#include "WXMsger.h"
#include <algorithm>
#include <iostream>
#include <functional>

CWXMsger::CWXMsger(void)
{
}

CWXMsger::CWXMsger(CWXMsger& msg)
{
}

CWXMsger::~CWXMsger(void)
{
}

bool CWXMsger::SendMessage(CWXMessage& msg)
{
    return this->DisposeInAllMsger(msg);
}


void CWXMsger::Dispose(void)
{
    this->DisposeListInAllMsger(this->m_vecMsg);
}

void CWXMsger::AddMsger(CWXMsger* pMsger)
{
    this->m_vecMsger.push_back(pMsger);
}

void CWXMsger::MoveMsger(CWXMsger* pMsger)
{
}

bool CWXMsger::IsFindMsger(CWXMsger* pMsger)
{
    return false;
}

bool CWXMsger::DisposeInAllMsger(CWXMessage& msg)
{
    //当前消息链已处理了该消息
    if(this->BNSProc(msg))
    {
        return true;
    }

    //下属消息链进行处理
    std::vector<CWXMsger*>::iterator iter = this->m_vecMsger.begin();
    for(;iter<this->m_vecMsger.end(); iter++)
    {
        if((*iter)->SendMessage(msg))
        {
            return true;
        }
    }

    return false;
}


void CWXMsger::DisposeListInAllMsger(std::vector<CWXMessage*>& vecMsg)
{
    //对每一个消息的默认处理函数
    WX_COMM_FUN_IN_FUN_BY_CLASS_BEGIN;
    static bool DisposMessage(CWXMessage* msg, CWXMsger* msger)
    {
        msger->BNSListProc(*msg);
        //释放消息空间
        delete msg;
        return true;
    }
    WX_COMM_FUN_IN_FUN_BY_CLASS_END;

    //循环处理每一个消息
    std::for_each(this->m_vecMsg.begin(), this->m_vecMsg.end(), std::bind2nd(std::ptr_fun(FUN::DisposMessage), this));
    //清空列表
    this->m_vecMsg.clear();
}

bool CWXMsger::BNSProc(CWXMessage& msg)
{
    std::cout <<msg.GetModelNo() << std::endl;
    return false;
}

bool CWXMsger::BNSListProc(CWXMessage& msg)
{
    return this->DisposeInAllMsger(msg);   
}

 

 

 

#pragma once
#include "MacroCode.h"

//输入参数
class CWXMsgInput
{
public:
    CWXMsgInput(){}
    ~CWXMsgInput(){}
    CWXMsgInput& operator =(const CWXMsgInput& msgInput)
    {
        this->Copy(msgInput);

        return *this;
    }
    CWXMsgInput(const CWXMsgInput& msgInput)
    {
        this->Copy(msgInput);
    }
private:
    virtual void Copy(const CWXMsgInput& msgInput){};
};
//输出参数
class CWXMsgOutput
{
public:
    CWXMsgOutput(){}
    ~CWXMsgOutput(){}
    CWXMsgOutput& operator =(const CWXMsgOutput& msgOutput)
    {
        this->Copy(msgOutput);

        return *this;
    }
    CWXMsgOutput(const CWXMsgOutput& msgOutput)
    {
        this->Copy(msgOutput);
    }
private:
    virtual void Copy(const CWXMsgOutput& msgOutput){};
};

class CWXMessage
{
public:
    CWXMessage(void) : m_nMsgNo(0), m_nModelNo(0), m_pmsgInput(NULL), m_pmsgOutput(NULL)
    {
    }

    virtual ~CWXMessage(void)
    {
        if(NULL!=this->m_pmsgInput)
        {
            delete this->m_pmsgInput;
        }
    }

private:
    template<class TMsgInput>
    CWXMessage(long nModelNo, long nMsgNo, const CWXMsgInput* msgInput, CWXMsgOutput* pmsgOutput) :
    m_nModelNo(nModelNo),
        m_nMsgNo(nMsgNo),
        m_pmsgInput(msgInput),
        m_pmsgOutput(pmsgOutput)
    {
        if(NULL!=msgInput)
        {
            this->SetNewInput(*msgInput);
        }
    }

    template<class TMsgInput>
    CWXMessage(const CWXMessage& msg)
    {
        if(this != &msg)
        {
            this->Copy<TMsgInput>(msg);
        }
    }

    template<class TMsgInput>
    CWXMessage& operator =(const CWXMessage& msg)
    {
        this->Copy<TMsgInput>(msg);

        return *this;
    }

public:
    //new出新的数据存放输入参数
    template<class TMsgInput>
    void SetNewInput(const CWXMsgInput& pmsgInput)
    {
        if(NULL!=this->m_pmsgInput)
        {
            delete this->m_pmsgInput;
        }
        this->m_pmsgInput = new TMsgInput(*((const TMsgInput*)&pmsgInput));
    }

    template<class TMsgInput>
    void Copy(const CWXMessage& msg)
    {
        this->m_nModelNo = msg.m_nModelNo;
        this->m_nMsgNo = msg.m_nMsgNo;
        if(NULL!=msg.m_pmsgInput)
        {
            this->SetNewInput<TMsgInput>(*msg.m_pmsgInput);
        }
        this->m_pmsgOutput = msg.m_pmsgOutput;
    }

private:
    //指定处理消息的模块编号
    WX_COMM_ADD_ATTRIBUTE_0(long, ModelNo, n);
    //消息编号
    WX_COMM_ADD_ATTRIBUTE_0(long, MsgNo, n);
    //输入参数
    WX_COMM_ADD_ATTRIBUTE_0(CWXMsgInput*, Input, pmsg);
    //输出参数
    WX_COMM_ADD_ATTRIBUTE_0(CWXMsgOutput*, Output, pmsg);
};

 

//WxCommMacroCode.h

#pragma once

//功能处理类
//WX_COMM_FUN_IN_FUN_BY_CLASS_BEGIN;
//static bool xxx(CWXMsger* msger)
//{
//    std::cout << msger->GetNo() << std::endl;
//    return true;
//}
//WX_COMM_FUN_IN_FUN_BY_CLASS_END;
////下属消息链进行处理
//std::for_each(this->m_lstMsger.begin(), this->m_lstMsger.end(),FUN::xxx);
#define WX_COMM_FUN_IN_FUN_BY_CLASS_BEGIN class FUN/
    {/
    public:
#define WX_COMM_FUN_IN_FUN_BY_CLASS_END };

//给类加属性 值传递 WX_COMM_ADD_ATTRIBUTE_0(int, No, n);
#define WX_COMM_ADD_ATTRIBUTE_0(mtype, pn, pp) /
private:/
    mtype m_##pp##pn; /
public:/
    void Set##pn(mtype pp##pn) /
    {/
    m_##pp##pn = pp##pn;/
}/
    mtype Get##pn()/
    {/
    return m_##pp##pn;/
}
//给类加属性 引用传递 WX_COMM_ADD_ATTRIBUTE_1(std::string, Name, Str);
#define WX_COMM_ADD_ATTRIBUTE_1(mtype, pn, pp) /
private:/
    mtype m_##pp##pn; /
public:/
    void Set##pn(mtype& pp##pn) /
    {/
    m_##pp##pn = pp##pn;/
}/
    mtype* Get##pn()/
    {/
    return &m_##pp##pn;/
}
//给类加属性 数组 WX_COMM_ADD_ATTRIBUTE_2(char, Arr, ch, 1024);
#define WX_COMM_ADD_ATTRIBUTE_2(mtype, pn, pp, count) /
private:/
    mtype m_##pp##pn[count]; /
public:/
    void Set##pn(mtype pp##pn, int nPos) /
    {/
    m_##pp##pn[nPos] = pp##pn;/
}/
    void Set##pn(mtype* pp##pn, mtype* (*cpy)(mtype* pObj, const mtype* pSource)) /
    {/
    cpy(m_##pp##pn, pp##pn);/
}/
    void Set##pn(mtype* pp##pn, unsigned int nCount, /
    void* (*cpy)(void* pObj, const void* pSource, unsigned int nCount)) /
    {/
    cpy(m_##pp##pn, pp##pn, nCount);/
}/
    mtype* Get##pn()/
    {/
    return m_##pp##pn;/
}

原创粉丝点击