zoukankan      html  css  js  c++  java
  • 关于成员函数的Command模式的简单实现

    看到Fox发表关于成员函数的消息映射的文章,也忍不住发表的一点自己的观点,希望对大家有所帮助。
    其实也就是COMMAND模式的简单实现,看代码吧。
    1. XGUIEventHandlerPointer.h
    namespace XGUI
    {
        
    class EventHandlerSlot
        {
        
    public:
            
    virtual ~EventHandlerSlot() {};
            
    virtual void execute(const EventArgs& args) = 0;
            
    virtual void* getClass() = 0;
        };

        template
    <typename T>
        
    class EventHandlerPointer :
            
    public EventHandlerSlot
        {
        
    public:
            typedef 
    void (T::*EventHandler)(const EventArgs&);
        
    public:
            EventHandlerPointer() :
                d_undefined(
    true),
                d_object(NULL)
            {}
            EventHandlerPointer(EventHandler func, T
    * obj) :
                d_function(func),
                d_object(obj),
                d_undefined(
    false)
            {}
            
    virtual ~EventHandlerPointer() {}

            
    void execute(const EventArgs& args)
            {
                
    if(!d_undefined) 
                    (d_object
    ->*d_function)(args);
            }

            
    void* getClass()
            {
                
    return static_cast<void*>(d_object);
            }

        
    protected:
            EventHandler    d_function;
            T
    *                d_object;
            
    bool            d_undefined;
        };
    }

    2. XGUIWidget.h
     
    namespace XGUI
    {
        
    // 
        class Widget
        {
        
    public:
            template
    <typename T> void addWidgetEventHandler(WidgetEvent EVENT, void (T::*function)(const EventArgs&), T* obj)
            {
                mWidgetEventHandlers[EVENT].push_back(
    new EventHandlerPointer<T>(function,obj));
            }
            
    void addWidgetEventHandler(WidgetEvent EVENT, EventHandlerSlot* function)
                    {
                mWidgetEventHandlers[EVENT].push_back(
    new EventHandlerPointer<T>(function,obj));
            }
                    
    bool Widget::fireWidgetEvent(WidgetEvent EVENT, EventArgs& args)
                    {
                          
    // If there are no User defined event handlers we are done.
                          if(mWidgetEventHandlers[EVENT].empty())
                           
    return false;
                        
                          
    // Execute registered handlers
                          std::vector<EventHandlerSlot*>* userEventHandlers = &(mWidgetEventHandlers[EVENT]);
                          
    for(std::vector<EventHandlerSlot*>::iterator it = userEventHandlers->begin(); it != userEventHandlers->end(); ++it )
                           (
    *it)->execute(args);
                        
                          
    return true;
                 }
            
    // .
        };
    }

    以上只为部分代码。
    我最擅长从零开始创造世界,所以从来不怕失败,它最多也就让我一无所有。
  • 相关阅读:
    C++ 将对象写入文件 并读取
    IronPython fail to add reference to WebDriver.dll
    How to Capture and Decrypt Lync Server 2010 TLS Traffic Using Microsoft Tools
    .net code injection
    数学系学生应该知道的十个学术网站
    Difference Between Currency Swap and FX Swap
    Swift开源parser
    谈谈我对证券公司一些部门的理解(前、中、后台)[z]
    JDK8记FullGC时候Metaspace内存不会被垃圾回收
    JVM源码分析之JDK8下的僵尸(无法回收)类加载器[z]
  • 原文地址:https://www.cnblogs.com/flying_bat/p/1314403.html
Copyright © 2011-2022 走看看