本文章参考了某个博友的文章,具体的记不清楚了,望见谅!
//
//
// 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__) */
//
// 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 );
}
}
}
// 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 );
}
}
}