惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - Clingingboy

冒个泡,刷刷存在感 使用文件映射和信号量来进程间通信 Xperf Basics: Recording a Trace (the easy way)(转) Xperf Basics: Recording a Trace(转) Xperf Analysis Basics(转) Android相关sdk使用 Uniscribe文字自动换行 Chrome RenderText分析(2) c++智能指针 codepage IMLangCodePages GUI 快捷键的实现思路 Menu实现逻辑 控件保持多种绘图状态的做法 绘图 Painter转接口封装的方式 DirectUI消息循环的简单封装 c++以代理的方式来实现接口化编程 c++对象工厂 使用模板来解决接口继承问题 VC++ 使用attributes定义接口
2个函数宏技巧
Clingingboy · 2013-11-21 · via 博客园 - Clingingboy

2013-11-21 13:56  Clingingboy  阅读(587)  评论()    收藏  举报

1.用宏调用对象函数

#define FOR_EACH_OBSERVER(ObserverType, observer_list, func) \
    do{ \
      CObserverListBase<ObserverType>::Iterator it(observer_list); \
      ObserverType* obs; \
      while((obs=it.GetNext()) != NULL) \
        obs->func; \
    } while(0)

调用方式

//     void NotifyBar(int x, int y) {
//       FOR_EACH_OBSERVER(Observer, observer_list_, OnBar(this, x, y));
//     }

2.用宏定义函数指针来转发

定义:

class TestDelegate
{
public:
    void Test(int i)
    {
        i=1;
    }

    void Test2(int i,int j)
    {
        i=1;
    }

    void (Test22)(int i,int j)
    {
        //((this)->*(&TestDelegate::Test))(i);
    }
};

#define __IMPLEMENT_COMSINK_FUNCTION(func, params, values) \
    typedef void (T::*F##func)params;\
    void Hook_##func(F##func pf##func) \
{ \
    m_pf##func = pf##func; \
} \
    void (func)params \
{ \
    ((m_pT)->*(m_pf##func))values; \
} \
private:\
    F##func m_pf##func;

template<typename T>
class DelegateHandler
{
public:
    DelegateHandler(T* pT,void (T::*pFunc)(int))
        :m_pT(pT),m_pFunc(pFunc)
    {

    }

    __IMPLEMENT_COMSINK_FUNCTION(OnEvent,(int a,int b),(a,b))

private:
    T* m_pT;
    void (T::*m_pFunc)(int);
};

调用

TestDelegate td;
DelegateHandler<TestDelegate> dh(&td,&TestDelegate::Test);
dh.Hook_OnEvent(&TestDelegate::Test2);
dh.OnEvent(4,3);