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.
};
- Event Mode, an implmentation @ vs2005
- Event Kernel mode
- Access: run an update sql then got The action or event has been blocked by Disabled Mode.
- An keypress event example
- Fire an event manually
- Making an event module---reacting to an event
- Event 在kernal mode与user mode之间的通信
- Kernel-mode API spying - an ultimate hack
- Kernel-mode API spying - an ultimate hack
- Kernel-mode API spying - an ultimate hack
- An easy to use event---By duyanning
- Custom an event for DataGridView.ContextMenuStrip
- [DirectShow] 019 - Learning When an Event Occurs
- libevent – an event notification library
- Salesforce - Log an event with populated value
- GetInvocationList of an event in VB.NET
- An event-driven communication c# program
- Two Interview questions! about event and Observer mode
- DataTable,dataview和 datagrid之间的区别
- 什么是V Model(V模型)
- dataset的使用方法
- Prerequisites for Internet Client Classes (WININET)
- WPF是什么,为什么它那么重要呢?(转)
- Event Mode, an implmentation @ vs2005
- PC到PC的IP电话实现
- RGB与YUV图像视频格式的相互转换
- iptables学习研究(二)
- OleDbCommand 的用法
- what is good test case?
- 万年历的源代码
- 用xfire实现从服务器上传和下载文件
- Solaris 挂在windows,linux分区