Event Mode, an implmentation @ vs2005

来源:互联网 发布:e数据恢复软件破解版 编辑:程序博客网 时间:2024/06/04 01:16

Hello mates, Long time no see. Sorry for the delay, too many stuffs have to deal with.

But, dont wanna talk about 3D at the moment, how about the Event Mode?? 

While I was developing softs with vs2005/c++,  I found that the event mode was very very diffecult to use.

So, I tried to implement an Event class which similar the C# language.

Finially, I did it. Pasted the codes as following:

#pragma once
#ifndef B2EE0847_5BD6_488b_A920_CAA0518A7227_EVENT_H_
#define B2EE0847_5BD6_488b_A920_CAA0518A7227_EVENT_H_

#include <vector>
#include <list>

namespace COMPANYNAME
{
    namespace PLATFORM
    {
        template <typename EventSource, typename EventArgsPtr, bool MultiCast = true>
        class Event
        {
        public:
            typedef void(EventSource::*EventHandler)(EventSource*, EventArgsPtr);
        private:
            std::list<EventHandler> m_arrObserver;
        public:
            Event& operator += (EventHandler FuncPtr)
            {
                WriteLog("NEW EVENT TRY TO ADD IN, EVENT COUNT:[%d -->  ", m_arrObserver.size());
                if(std::find(m_arrObserver.begin(), m_arrObserver.end(), FuncPtr) == m_arrObserver.end())// if already exist, will not add this in.
                {
                    if(MultiCast == true)
                    {
                        m_arrObserver.push_back(FuncPtr);
                    }
                    else
                    {
                        if(m_arrObserver.empty() == true)
                        {
                            m_arrObserver.push_back(FuncPtr);
                        }
                    }
                }     
                WriteLog("%d]/n", m_arrObserver.size());
                return *this;
            }
            Event& operator -= (EventHandler FuncPtr)
            {
                WriteLog("EVENT TRY TO ERASE, EVENT COUNT:[%d --> ", m_arrObserver.size());
                m_arrObserver.erase(std::find(m_arrObserver.begin(), m_arrObserver.end(), FuncPtr));
                WriteLog("%d]/n", m_arrObserver.size());
                return *this;
            }
            void operator () (EventSource *source, EventArgsPtr args)
            {
                for(std::list<EventHandler>::iterator itc=m_arrObserver.begin();
                    itc!=m_arrObserver.end();
                    itc++)
                {
                    EventHandler pHandler = const_cast<EventHandler>(*itc);
                    (source->*pHandler)(source, args);
                }
            }
            void Clear()
            {
                m_arrObserver.clear();
            }
        };
    }
}

 

Here, you must care two points, which are caused by vs2005's new security features.

1 The function pointer, we have to write as:  NOTE:if do not write like following, an error C2440 must occur.

class XXX {public: void func(){}};   

typedef void(XXX::*PFunc)();

class YYY

{

public:

PFunc pf = &XXX::func;

void FunctionCall()

{

XXX xxx;

(xxx.*pf)(); //call the function

}

};

see the calling to function pointer, it's looks strange, but , it's work :)

2 Avoid multi-inherit.

class XXX:public AAA, public BBB

{

void func(){}

}; 

typedef void(XXX::*PFunc)();

class YYY

{

public:

PFunc pf = &XXX::func; // Error C2440, it's illegal, not security cast, different signature.

};

 

原创粉丝点击