// // 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* 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,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 ); } } } 总结
以上是内存溢出为你收集整理的cocos2d-x 3.2线程安全的消息中心全部内容,希望文章能够帮你解决cocos2d-x 3.2线程安全的消息中心所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)