中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

觀察者工具類NotificationUtil

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
1.   .h文件

#ifndef _NotificationUtil_H_
#define _NotificationUtil_H_
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
class NotificationUtil : public Ref
{
public:
virtual bool init();
CREATE_FUNC(NotificationUtil);
static NotificationUtil * getInstance();//實(shí)現(xiàn)單例


//訂閱消息
void addObserver(const std::string &sMsgName, std::function<void(Ref*)>func);


//發(fā)布消息
void postNotification(const std::string & sMsgName, Ref * data);
private:
static NotificationUtil * m_NotifiactionUtil;


std::map<std::string, std::vector<std::function<void(Ref*)>>> m_funcMap;


};
#endif

2.  .cpp

#include "NotificationUtil.h"
NotificationUtil * NotificationUtil::m_NotifiactionUtil = NULL;
bool NotificationUtil::init()
{
return true;
}


NotificationUtil * NotificationUtil::getInstance()
{
if (m_NotifiactionUtil == NULL)


{
m_NotifiactionUtil = NotificationUtil::create();
m_NotifiactionUtil->retain();
}
return m_NotifiactionUtil;
}


void NotificationUtil::addObserver(const std::string &sMsgName, std::function<void(Ref*)>func)
{
//查找是否有已經(jīng)存在該消息的回調(diào)列表
if (m_funcMap.find(sMsgName)!=m_funcMap.end())
{
//已經(jīng)存在該回調(diào)列表(換句話說,已經(jīng)有人訂閱過同樣的消息)
std::vector<std::function<void(Ref*)>> & funcList = m_funcMap.at(sMsgName);


funcList.push_back(func);
}
else
{
//不存在該回調(diào)列表,表示沒有人訂閱過這種消息,新建一個(gè)列表
std::vector<std::function<void(Ref *)>> funcList;


//將新的訂閱者添加到回調(diào)列表里
funcList.push_back(func);


//將新建的列表保存到map中


m_funcMap[sMsgName] = funcList;


}


}


void NotificationUtil::postNotification(const std::string & sMsgName, Ref * data)
{
//查找是否有人訂閱過此消息
if (m_funcMap.find(sMsgName) != m_funcMap.end())
{
//獲取回調(diào)列表
std::vector<std::function<void(Ref*)>> funcList = m_funcMap.at(sMsgName);
//遍歷列表,回調(diào)函數(shù),并保存數(shù)據(jù)
for (auto func:funcList)
{
func(data);
}
}
}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android監(jiān)聽?wèi)?yīng)用程序安裝和卸載

下一篇:SQL Server2008中通過SQL獲取表結(jié)構(gòu)