cocos2d-x 3.2线程安全的消息中心

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值