Chinaunix

标题: 能帮我简化下这个破函数吗? [打印本页]

作者: jd808    时间: 2016-01-21 20:40
标题: 能帮我简化下这个破函数吗?

  1. int BaseTEngine::CallConnList(std::tr1::function<int(Conn *,Conn *,const char *,int,void*)> f,Conn * ActConn,const char *command,int cmdlen,void* n)
  2. {
  3.         list<Conn *> pconlist;
  4.         pconlist.clear();

  5.         Conn *pNext= NULL, *pcon= NULL;
  6.         ConnList *m_conlist=NULL;
  7.         ConnList* m_UserList = g_pServer->GetUserList();

  8.         for (int i=0; i<g_pServer->GetUserListCount(); i++) {
  9.                 m_conlist=&m_UserList[i];
  10.                 pthread_rwlock_rdlock(&m_conlist->ConnListLock);
  11.                 pcon = m_conlist->GetHead()->GetNext();
  12.                 while (pcon != NULL) {
  13.                         pNext = pcon->GetNext();
  14.                         if(pcon->is_login==1&& pcon->fd_type==1&&pcon->is_down==0)
  15.                                 pconlist.push_back(pcon);
  16.                         pcon = pNext;
  17.                 }
  18.                 pthread_rwlock_unlock(&m_conlist->ConnListLock);
  19.         }

  20.         Conn *xpconn = NULL;
  21.         list<Conn *>::iterator itor;
  22.         itor=pconlist.begin();
  23.         while(itor!=pconlist.end())
  24.         {
  25.                 xpconn=*itor++;
  26.                 if(xpconn!=NULL)
  27.                         f(xpconn,ActConn,command,cmdlen,n);
  28.         }

  29.         return 0;
  30. }

  31. int TServer::send_client(Conn *pcon,Conn *Build_conn,const char *command,int cmdlen,void *p_id)
  32. {
  33.         int mip=*(int *)p_id;

  34.         if(Build_conn->mip==mip)
  35.                 m_TimeThreadInfo->t_Engine->Base_Client_Lock(pcon,command,cmdlen, g_pConf->STRZIP);
  36.         return 0;
  37. }

  38. CallConnList(bind(&TServer::send_client, this, tr1::placeholders::_1, tr1::placeholders::_2, tr1::placeholders::_3, tr1::placeholders::_4, tr1::placeholders::_5),pcon,command,DataLen,(void*)&p_id);
复制代码
每次调用都要写这么长
  1. CallConnList(bind(&TServer::send_client, this, tr1::placeholders::_1, tr1::placeholders::_2, tr1::placeholders::_3, tr1::placeholders::_4, tr1::placeholders::_5),pcon,command,DataLen,(void*)&p_id);
复制代码
不方便啊,能不能吧bind移到CallConnList函数里呀
作者: windoze    时间: 2016-01-21 21:38
你就不能自己在外面包一层?

作者: jd808    时间: 2016-01-22 15:11
windoze 发表于 2016-01-21 21:38
你就不能自己在外面包一层?
TServer::send_client这函数我不会传,我吧他定义成
  1. auto *func=&TServer::send_client;
  2. CallConnList(func, pcon,command,DataLen,(void*)&p_id);

  3. int BaseTEngine::CallConnList(auto *func,Conn * ActConn,const char *command,int cmdlen,void* n)
  4. {
  5.            auto f=bind(func, tr1::placeholders::_1, tr1::placeholders::_2, tr1::placeholders::_3, tr1::placeholders::_4);
  6.           return f(ActConn,command,cmdlen,n);
  7. }
复制代码
这样,似乎行不通




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2