博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x 3.2线程安全的消息中心
阅读量:4691 次
发布时间:2019-06-09

本文共 3782 字,大约阅读时间需要 12 分钟。

本文章参考了某个博友的文章,具体的记不清楚了,望见谅!
//
//  MsgManager.h
//  MsgManager
//
//  Created by sky on 14-11-21.
//
//   
线程安全的消息中心
#ifndef __MsgManager__MsgManager__
#define __MsgManager__MsgManager__
#include  
<stdio.h>
#include
 
"cocos2d.h"
USING_NS_CC;
//
消息体
class  
CC_DLL Message :
public  
Ref
{
public
:
    Message(
const  
std::string &msgName,Ref* target,SEL_CallFuncO selector,Ref* msgContent);
   
 
virtual  
~Message();
   
 
void  
handerMessage(
Ref
*msgContent);
   
 
CC_SYNTHESIZE_READONLY
(
std
::
string
,  
m_msgName
, MessageName);
   
 
CC_SYNTHESIZE_READONLY
(
Ref
*,  
m_target
, Target);
   
 
CC_SYNTHESIZE_READONLY
(
SEL_CallFuncO
,  
m_selector
, Selector);
   
 
CC_SYNTHESIZE_READONLY
(
Ref
*,  
m_msgContent
, MessageContent);  
//function args
};
//
消息管理
class
CC_DLL  
MsgManager:
public
Ref
{
public
:
    MsgManager();
   
   
 
virtual  
~MsgManager();
   
   
 
static
MsgManager
* getInstance();
   
   
 
//
多个对象可以监听同一个名字的消息
   
 
bool  
addObserver(
const
std
::
string  
&msgName,
Ref
*target,
SEL_CallFuncO  
selector,
Ref
* msgContent=
nullptr
);
   
   
 
void  
postMessage(
const
std
::
string
& msgName);
   
 
void  
postMessage(
const
std
::
string
& msgName,
Ref
*msgContent);
   
   
 
//
如果
target
为空则移除所有名字为
msgName
的消息
   
 
void  
removeObserverByName(
const
std
::
string
& msgName,
Ref
*target=
nullptr
);
   
   
 
//
移除一个对象的所有监听
   
 
void  
removeAllObserver(
Ref
* target);
   
protected
:
   
 
Message
* getMessageByNameAndTarget(
const
std
::
string  
&msgName,
Ref
* target)  
const
;
private
:
   
 
Vector
<
Message
*> m_msgContainer;
};
#endif  
/* defined(__MsgManager__MsgManager__) */
//
//  MsgManager.cpp
//  MsgManager
//
//  Created by sky on 14-11-21.
//
//
#include  
"MsgManager.h"
#pragma mark--
消息体
Message::Message(
const  
std::string &msgName,Ref* target,SEL_CallFuncO selector,Ref* msgContent):m_msgName(msgName),
                m_target(target),
                m_selector(selector),
                m_msgContent(msgContent)
               
{
}
Message::~Message()
{
}
void  
Message::handerMessage(cocos2d::Ref *msgContent)
{
   
 
if  
(m_target)
    {
        (m_target ->* m_selector)(msgContent);
    }
}
#pragma mark--
消息管理
static  
MsgManager* instance =  
nullptr
;
std::mutex containerMutex;
MsgManager::MsgManager()
{
}
MsgManager::~MsgManager()
{
    m_msgContainer.clear();
}
MsgManager* MsgManager::getInstance()
{
   
 
if  
(!instance)
    {
        instance =
 
new  
MsgManager();
    }
   
 
return  
instance;
}
bool  
MsgManager::addObserver(
const  
std::string &msgName, cocos2d::Ref *target, SEL_CallFuncO selector,Ref *msgContent)
{
    std::lock_guard<std::mutex> ul(containerMutex);
   
 
if  
(!getMessageByNameAndTarget(msgName,target))
    {
       
 
auto  
msg =  
new  
Message(msgName,target,selector,msgContent);
       
 
if  
(!msg)
        {
           
 
return
false
;
            CC_SAFE_DELETE(msg);
        }
        msg -> autorelease();
        m_msgContainer.pushBack(msg);
        CCLOG(
"msgCount:%lu"
,m_msgContainer.size());
       
       
 
return 
true
;
    }
   
 
return 
false
;
}
void  
MsgManager::postMessage(
const  
std::string &msgName, cocos2d::Ref *msgContent)
{
    std::lock_guard<std::mutex> ul(containerMutex);
   
 
for
(
auto  
&msg : m_msgContainer)
    {
       
 
if  
(msgName==msg->getMessageName())
        {
            msg -> handerMessage(msgContent);
        }
    }
}
void  
MsgManager::postMessage(
const  
std::string &msgName)
{
    postMessage(msgName,
nullptr
);
}
Message* MsgManager::getMessageByNameAndTarget(
const  
std::string &msgName, cocos2d::Ref *target)  
const
{
   
 
for
(
auto  
&msg : m_msgContainer)
    {
       
 
if  
(msgName==msg->getMessageName() && target==msg->getTarget())
        {
           
 
return  
msg;
        }
    }
   
 
return 
nullptr
;
}
void  
MsgManager::removeObserverByName(
const  
std::string &msgName,Ref *target)
{
    std::lock_guard<std::mutex> ui(containerMutex);
   
 
for
(
auto  
&msg : m_msgContainer)
    {
       
 
if  
(msgName == msg->getMessageName() && (target==msg->getTarget()||!target))
        {
            m_msgContainer.eraseObject(msg,
true
);   
//?
        }
    }
}
void  
MsgManager::removeAllObserver(cocos2d::Ref *target)
{
    std::lock_guard<std::mutex> ul(containerMutex);
   
 
for  
(
auto  
&msg : m_msgContainer)
    {
       
 
if  
(target==msg->getTarget())
        {
            m_msgContainer.eraseObject(msg,
true
);
        }
    }
}

转载于:https://www.cnblogs.com/skyxu123/p/9543819.html

你可能感兴趣的文章
查看手机已经记住的WIFI密码
查看>>
Linux实战教学笔记24:SSH连接原理及ssh-key
查看>>
最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
查看>>
Dynamic CRM 2013学习笔记(四十二)流程5 - 实时/同步工作流(Workflow)用法图解...
查看>>
Windows下命令(bat可用)
查看>>
我是怎么用缠论在商品里边抢钱之二 (2019-07-12 15:10:10)
查看>>
python入门之正则表达式
查看>>
SAS学习经验总结分享:篇五-过程步的应用
查看>>
Android创建文件夹及文件并写入数据
查看>>
file的getPath getAbsolutePath和getCanonicalPath的不同
查看>>
课时4—切入切出动画
查看>>
eclipse 编辑 python 中文乱码的解决方案
查看>>
gnome2 恢复默认 panel
查看>>
Python 爬虫的集中简单方式
查看>>
数据库MySQL/mariadb知识点——触发器
查看>>
Adaboost算法结合Haar-like特征
查看>>
Ubuntu做Tomcat服务:insserv: warning: script 'tomcat' missing LSB tags and overrides
查看>>
Binary Agents
查看>>
入门Webpack,看这篇就够了
查看>>
短信拦截马”黑色产业链与溯源取证研究
查看>>