嗯,我从事的项目是一个试验,试图在 C++ 中创建一种类似的事件处理机制——或多或少类似于 C# 事件和事件处理程序的方式。于是,在网上查阅了一些资料,找到了合适的方法后,我写了两个模板类:一个是为成员方法存储一个指针,并称之为'EventHandler'。另一种是存储事件处理程序的映射并在需要时调用它们;这是“事件”类。然后我写了两个“普通”类:一个是事件触发类,另一个是响应类或监听类。Event 和 EventHandler 类的初始版本如下:
#include <functional>
namespace eventhandling
{
#ifndef __BASIC_EVENT_HANDLER__
#define __BASIC_EVENT_HANDLER__
////////////////////////////////////////////////////////////////////////////
// the root event handler class
////////////////////////////////////////////////////////////////////////////
template <typename empty = int, empty = 0>
class BaseEventHandler
{
public:
virtual void Excute() = 0;
};
///////////////////////////////////////////////////////////////////////////
// the basic derived event handler class; the class which will wrap the
// methods of other classes which want to respond to specific event(s)..
///////////////////////////////////////////////////////////////////////////
template <typename ResponderType>
class EventHandler : public BaseEventHandler < >
{
typedef void (ResponderType::*MethodPointer());
ResponderType* responder;
MethodPointer methodPointer;
public:
EventHandler(ResponderType* resp, MethodPointer func) : responder(resp), methodPointer(func)
{}
void Excute()
{
return methodPointer();
}
};
#endif
}
#include "BasicEventHandler.h"
#include <map>
namespace eventhandling
{
#ifndef __BASIC_EVENT__
#define __BASIC_EVENT__
////////////////////////////////////////////////////////////////////////////////////////////////
// the event class which will receive these event handlers, stores them in a map object,
// and call them squentially when invoked from within the event firing method...
////////////////////////////////////////////////////////////////////////////////////////////////
// the template takes no parameters, so I added an empty parameter, just because
//it cannot ignore the parameter list, otherwise it will be considered template specialization
template <typename empty = int, empty = 0>
class BasicEvent
{
//store the eventhandlers in a map so that I can track them from outside the class by id
typedef std::map<int, BaseEventHandler<empty>* > Responders;
Responders responders;
int respondersCount;
public:
BasicEvent() : respondersCount(0)
{}
// classical add method templatized so that it accepts any object
template <typename Responder>
int Add(Responder* sender, void (Responder::*memberFunc)())
{
responders[respondersCount] = (new EventHandler<Responder>(sender, memberFunc));
respondersCount++;
return respondersCount - 1;
}
// simple method to clean up the map memory after done with the eventhandlers
void Remove(int responderID)
{
Responders::iterator it = responders.find(responderID);
if (it == responders.end())
return;
delete it->second;
responders.erase(it);
}
// method which invokes all the eventhandlers alltogether without control from the outside
void Invoke()
{
Responders::iterator it = responders.begin();
for (; it != responders.end(); ++it)
{
it->second->Excute();
}
}
// method which invokes only the eventhandler whose id has been passed to it
void Invoke(int id)
{
Responders::iterator it = responders.find(id);
if (it != responders.end())
it->second->Excute();
}
// overloaded operator+= to replace the functionality of method Add()
template <typename Responder>
void operator+=(EventHandler<Responder>* eventhandler)
{
responders[respondersCount] = eventhandler;
respondersCount++;
}
// overloaded operator -= to replace the functionality of method Remove()
void operator-=(int id)
{
Responders::iterator it = responders.find(id);
if (it == responders.end())
return;
delete it->second;
responders.erase(it);
}
//simple method which gives the size of the map object
int Size()
{
return respondersCount;
}
};
#endif
}
然后,我想在创建一个新的 EventHandler 对象时摆脱显式的 '<…..>' 模板语法,这显然是 C# 中的情况,所以,我将其简单如下:
typedef EventHandler<MyClass> SomeEventFired_EventHandler;
这样当我需要创建这个模板的新对象时,我只需要编写:
MyClass* myObject = new MyClass();
MyEventFiringClass* firingObject = new MyEventFiringClass();
firingObject->OnFired += new SomeEventFired_EventHandler(myObject, &MyClass::SomeMethod);
当然,后面的完整代码示例会更清楚!我的问题来了,我希望能够传递 MyClass 的派生类的对象。问题是如上所示的 EventHandler 的模板不接受这样的派生对象,因为它只期望基类的对象,编译器抱怨它无法从派生类转换为基类。当他向我展示了使 EventHandler 类的构造函数模板化的正确方法时,ecatmur 提供了宝贵的帮助。这样,当我使用 MyClass 作为基类键入定义的 SomeEventFired_EventHandler 时,我就能够将任何对象及其方法传递给它的构造函数,只要该对象来自从 MyClass 派生的类。这是我实现 EventHandler 多态特性的最终目标。我想要这个特性,因为如果你检查 C# 中的 EventHandlers,你会看到 System::EventHandler 是多态的,它接受来自类的不同对象,这些对象基本上是从类 Object 派生的,我猜。所以,这里是完整的例子,带有基于 ecatmur 解决方案的整改 EventHandler 类,供大家查看,希望对您有所帮助。最终,您可以从 BaseEventHandler 类派生,以便派生的 EventHandler 可以存储具有不同返回类型和不同参数参数的方法,因为这里显示的基本方法接受返回 void 和取 void 的方法(我相信您可以这样做更改 std::function<> 的声明
std::function<int(int)>
,等等)。
事件类同上……</p>
#include <functional>
namespace eventhandling
{
#ifndef __BASIC_EVENT_HANDLER__
#define __BASIC_EVENT_HANDLER__
////////////////////////////////////////////////////////////////////////////
// the root event handler class
////////////////////////////////////////////////////////////////////////////
template <typename empty = int, empty = 0>
class BaseEventHandler
{
public:
virtual void Excute() = 0;
};
///////////////////////////////////////////////////////////////////////////
// the basic derived event handler class; the class which will wrap the
// methods of other classes which want to respond to specific event(s)..
///////////////////////////////////////////////////////////////////////////
template <typename ResponderType>
class EventHandler : public BaseEventHandler < >
{
std::function<void ()> type_erased_method;
ResponderType* responder;
public:
template<typename T>
EventHandler(T* resp, void (T::*MethodPointer)()) : responder(resp), type_erased_method(std::bind(MethodPointer, resp))
{}
void Excute()
{
return type_erased_method();
}
};
#endif
}
事件触发类头文件……</p>
#include <iostream>
#include <string>
#include "BasicEvent.h"
namespace eventhandling
{
#ifndef __FONT_SIMULATOR__
#define __FONT_SIMULATOR__
typedef BasicEvent<> FontEvent;
typedef std::string s;
class FontSimulator
{
private:
s fontName;
s fontSize;
s fontStyle;
public:
FontSimulator();
FontSimulator(s name, s size, s style);
~FontSimulator();
FontEvent OnDraw;
void DrawText();
// the setting methods
void SetFontName(s n) {fontName = n;}
void SetFontSize(s si) {fontSize = si;}
void SetFontStyle(s st) {fontStyle = st;}
// the getting methods
s GetFontName() {return fontName;}
s GetFontSize() {return fontSize;}
s GetFontStyle() {return fontStyle;}
};
#endif
}
它的源文件,.cpp
#include "FontSimulator.h"
using namespace eventhandling;
FontSimulator::FontSimulator() : fontName("Default Name"), fontSize ("Default Size"), fontStyle("Default Style")
{
}
FontSimulator::FontSimulator(s fName, s fSize, s fStyle) : fontName(fName), fontSize(fSize), fontStyle(fStyle)
{
}
FontSimulator::~FontSimulator()
{
delete this;
}
void FontSimulator::DrawText()
{
std::cout << "Initialization of font done!" << std::endl << std::endl;
std::cout << fontName << std::endl;
std::cout << fontSize << std::endl;
std::cout << fontStyle << std::endl << std::endl;
for (int i = 0; i < OnDraw.Size(); ++i)
{
OnDraw.Invoke(i);
std::cout << "the #" << i + 1 << " responder method called!" << std::endl << std::endl;
std::cout << fontName << std::endl;
std::cout << fontSize << std::endl;
std::cout << fontStyle << std::endl << std::endl;
}
for (int j = 0; j < OnDraw.Size(); j++)
{
//OnDraw.Remove(j);
OnDraw -= j;
}
std::cout << "The finishing font work after all the event handler are called!" << std::endl <<std::endl;
}
处理字体类事件的抽象基类……</p>
#include "BasicEventHandler.h"
namespace eventhandling
{
#ifndef __IFONT_CLIENT__
#define __IFONT_CLIENT__
class IFontClient
{
public:
IFontClient(){};
~IFontClient(){delete this;}
virtual void SetupFont() = 0;
};
typedef EventHandler<IFontClient> FontEventHandler;
#endif
}
先从 IFontClient... 头文件派生的类
#include "BasicEventHandler.h"
#include "BasicEvent.h"
#include "FontSimulator.h"
#include "IFontClient.h"
namespace eventhandling
{
#ifndef __CONTROL_SIMULATOR__
#define __CONTROL_SIMULATOR__
class ControlSimulator : public IFontClient
{
protected:
std::string caption;
FontSimulator* font;
public:
ControlSimulator();
ControlSimulator(std::string theCaption, FontSimulator* theFont);
~ControlSimulator();
virtual void Draw();
virtual void SetupFont();
void SetCaption(std::string c) {caption = c;}
std::string GetCaption() {return caption;}
};
#endif
}
它的源文件 .cpp
#include "ControlSimulator.h"
namespace eventhandling
{
ControlSimulator::ControlSimulator() : caption("Default Caption"), font(new FontSimulator())
{
}
ControlSimulator::ControlSimulator(std::string c, FontSimulator* f) : caption(c), font(f)
{
}
ControlSimulator::~ControlSimulator()
{
delete this;
}
void ControlSimulator::Draw()
{
std::cout << "Drawing " << caption << " is done!" << std::endl << std::endl;
}
void ControlSimulator::SetupFont()
{
std::string costumProperty = caption;
font->SetFontName(costumProperty.append(", Costumized Font Name"));
costumProperty = caption;
font->SetFontSize(costumProperty.append(", Costumized Font Size"));
costumProperty = caption;
font->SetFontStyle(costumProperty.append(", Costumized Font Style"));
}
}
测试应用程序的主要入口
#include "ControlSimulator.h"
using namespace eventhandling;
int main(int argc, char** argv)
{
char c;
FontSimulator* font = new FontSimulator();
ControlSimulator* control1 = new ControlSimulator("Control one", font);
ControlSimulator* control2 = new ControlSimulator("Control two", font);
control1->Draw();
control2->Draw();
font->OnDraw += new FontEventHandler(control1, &ControlSimulator::SetupFont);
font->OnDraw += new FontEventHandler(control2, &ControlSimulator::SetupFont);
font->DrawText();
std::cout << "Enter any character to exit!" << std::endl;
std::cin >> c;
return 0;
}