zoukankan      html  css  js  c++  java
  • (原创)c++11改进我们的模式之改进代理模式,实现通用的AOP框架

    c++11 boost技术交流群:296561497,欢迎大家来交流技术。

    本次要讲的时候如何改进代理模式,具体来说是动态代理模式,动态代理模式一般实现AOP框架,不懂AOP的童鞋看这里。我前面的博文也实现了一个AOP框架(可以参考我前面的博文:(原创) C++ 轻量级AOP框架),但是那个AOP框架存在一个缺陷,就是不支持切面的组合,这一点大大降低了其通用性,本次通过c++11改进,使AOP框架更完善:支持切面组合,不要求切面必须派生于某个基类,不用虚函数调用,性能更好,功能更强大。

    上代码:

    struct Aspect : boost::noncopyable
    {
        template<typename Func>
        Aspect(const Func& f) : m_func(f)
        {
    
        }
    
        template<typename T>
        void Invoke(T&& value)
        {
            value.Before();
            m_func();
            value.After();
        }
    
        template<typename Head, typename... Tail>
        void Invoke(Head&& head, Tail&&... tail)
        {
            head.Before();
            Invoke(std::forward<Tail>(tail)...);
            head.After();
        }
    
    private:
        std::function<void()> m_func;
    };
    
    template<typename... AP>
    void Invoke(const std::function<void ()>& f)
    {
        Aspect msp(f);
        msp.Invoke(AP()...);
    }
    View Code

    切面有要求,切面类中必须要有Before/After方法,否则编译不过,允许空实现。

    测试代码:

    struct AA
    {
        void Before()
        {
            cout<<"Before from AA"<<endl;
        }
    
        void After()
        {
            cout<<"After from AA"<<endl;
        }
    };
    
    struct BB
    {
        void Before()
        {
            cout<<"Before from BB"<<endl;
        }
    
        void After()
        {
            cout<<"After from BB"<<endl;
        }
    };
    
    struct CC
    {
        void Before()
        {
            cout<<"Before from CC"<<endl;
        }
    
        void After()
        {
            cout<<"After from CC"<<endl;
        }
    };
    
    struct TT
    {
        void g()
        {
            cout<<"real g function"<<endl;
        }
    
        void h(int a)
        {
            cout<<"real h function: "<<a<<endl;
        }
    };
    
    struct DD
    {
        void Before()
        {
            
        }
    
        void After()
        {
            
        }
    };
    
    void GT()
    {
        cout<<"real GT function"<<endl;
    }
    
    void HT(int a)
    {
        cout<<"real HT function: "<<a<<endl;
    }
    
    void TestAOP()
    {
        TT tt; 
        std::function<void()> ff = std::bind(&TT::g, &tt);
            //组合了两个切面AA BB
        Invoke<AA,BB>([&ff](){ff();}); //织入成员函数
        Invoke<AA,BB>([&tt](){tt.g();}); //织入对象
    
        int aa = 3;
        Invoke<AA,BB>(&GT); //织入方法
        Invoke<AA,BB>([aa](){HT(aa);});//织入带参的方法
    
        //织入带参数的成员函数和对象
        std::function<void(int)> ff1 = std::bind(&TT::h, &tt, std::placeholders::_1);
        Invoke<AA,BB,CC,DD>([&ff1,aa](){ff1(aa);}); //组合了四个切面
        Invoke<AA,BB>([&tt,aa](){tt.h(aa);});
    }    
    View Code

    测试结果:

    更新:代码在GCC下编译没问题,但在vs2013下编译不过,是微软的bug,如果要在vs2013下编译通过,需要做一点修改:

    template<typename T> using identity_t = T;
    
    template<typename... AP>
    void Invoke(const std::function<void()>& f)
    {
        Aspect msp(f);
        msp.Invoke(identity_t<AP>()...);
    }
  • 相关阅读:
    PyMySQL TypeError: not enough arguments for format string
    使用python3抓取pinpoint应用信息入库
    JS 异步之 async await
    JS Null 空 判断
    Vue问题汇总
    pymysql DAO简单封装
    py可视化执行过程
    jenkins回滚之groovy动态获取版本号
    容器时间 容器乱码问题
    SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
  • 原文地址:https://www.cnblogs.com/qicosmos/p/3154174.html
Copyright © 2011-2022 走看看