【转贴】Lua常用资源连接(来自lua-users)

posted @ 2010-04-21 21:48 avatar 阅读(223) | 评论 (0)编辑 收藏

[转载]低耦合模块间的通信组件:两个模板

用途

在一个UI与逻辑模块交互比较多的程序中,因为并不想让两个模块发生太大的耦合,基本目标是
可以完全不改代码地换一个UI。逻辑模块需要在产生一些事件后通知到UI模块,并且在这个通知
里携带足够多的信息(数据)给接收通知的模块,例如UI模块。逻辑模块还可能被放置于与UI模
块不同的线程里。

最初的结构

最开始我直接采用最简单的方法,逻辑模块保存一个UI模块传过来的listener。当有事件发生时,
就回调相应的接口将此通知传出去。大致结构如下:

 /// Logic
 class EventNotify
 
{
 
public:
  
virtual void OnEnterRgn( Player *player, long rgn_id );
 }
;

 
/// UI
 class EventNotifyImpl : public EventNotify
 
{
 }
;

 
/// Logic
 GetEventNotify()->OnEnterRgn( player, rgn_id );

 

但是,在代码越写越多之后,逻辑模块需要通知的事件越来越多之后,EventNotify这个类开始
膨胀:接口变多了、不同接口定义的参数看起来也越来越恶心了。

改进

于是我决定将各种事件通知统一化:

 

struct Event
{
 
long type; // 事件类型
  // 附属参数
}
;

 

这样,逻辑模块只需要创建事件结构,两个模块间的通信就只需要一个接口即可:

void OnNotify( const Event &event );

但是问题又来了,不同的事件类型携带的附属参数(数据)不一样。也许,可以使用一个序列化
的组件,将各种数据先序列化,然后在事件处理模块对应地取数据出来。这样做总感觉有点大动
干戈了。当然,也可以使用C语言里的不定参数去解决,如:

void OnNotify( long event_type, ... )

其实,我需要的就是一个可以表面上类型一样,但其内部保存的数据却多样的东西。这样一想,
模块就能让事情简单化:

 

template <typename P1, typename P2>
class Param
{
public:
 Param( P1 p1, P2 p2 ) : _p1( p1 ), _p2( p2 )
 
{
 }

 
 P1 _p1;
 P2 _p2;
}
;

template 
<typename P1, typename P2>
void OnNotify( long event_type, Param<P1, P2> param );

GetNotify()
->OnNotify( ET_ENTER_RGN, Param<Player*long>( player, rgn_id ) );
GetNotify()
->OnNotify( ET_MOVE, Param<longlong>( x, y ) );

 

在上面这个例子中,虽然通过Param的包装,逻辑模块可以在事件通知里放置任意类型的数据,但
毕竟只支持2个参数。实际上为了实现支持多个参数(起码得有15个),还是免不了自己实现多个
参数的Param。

幸亏我以前写过宏递归产生代码的东西,可以自动地生成这种情况下诸如Param1、Param2的代码。
如:

 

#define CREATE_PARAM( n ) \
 template 
<DEF_PARAM( n )> \
 
struct Param##n \
 
{ \
  DEF_PARAM_TYPE( n ); \
  Param##n( DEF_FUNC_PARAM( n ) ) \
  
{ \
   DEF_MEM_VAR_ASSIGN( n ); \
  }
 \
  DEF_VAR_DEF( n ); \
 }


 CREATE_PARAM( 
1 );
 CREATE_PARAM( 
2 );

 

即可生成Param1和Param2的版本。其实这样定义了Param1、Param2的东西之后,又使得OnNotify
的参数不是特定的了。虽然可以把Param也泛化,但是在逻辑层写过多的模板代码,总感觉不好。

于是又想到以前写的一个东西,可以把各种类型包装成一种类型---对于外界而言:any。any在
boost中有提到,我只是实现了个简单的版本。any的大致实现手法就是在内部通过多态机制将各
种类型在某种程度上隐藏,如:

 

        class base_type
        
{
        
public:
            
virtual ~base_type()
            
{
            }

            
virtual base_type *clone() const = 0;
        }
;
        
        template 
<typename _Tp>
        
class var_holder : public base_type
        
{
        
public:
            typedef _Tp type;
            typedef var_holder
<type> self;
        
public:
            var_holder( 
const type &t ) : _t( t )
            
{
            }


            base_type 
*clone() const
            
{
                
return new self( _t );
            }

        
public:
            type _t;
        }


这样,any类通过一个base_type类,利用C++多态机制即可将类型隐藏于var_holder里。那么,
最终的事件通知接口成为下面的样子:

void OnNotify( long type, any data );

OnNotify( ET_ENTER_RGN, any( create_param( player, rgn_id ) ) );其中,create_param
是一个辅助函数,用于创建各种Param对象。

事实上,实现各种ParamN版本,让其名字不一样其实有点不妥。还有一种方法可以让Param的名字
只有一个,那就是模板偏特化。例如:

 

template <typename _Tp>
struct Param;

template 
<>
struct Param<void()>;

template 
<typename P1>
struct Param<void(P1)>

template 
<typename P1, typename P2>
struct Param<void(P1,P2)>

 

这种方法主要是通过组合出一种函数类型,来实现偏特化。因为我觉得构造一个函数类型给主模版,
并不是一种合情理的事情。但是,即使使用偏特化来让Param名字看起来只有一个,但对于不同的
实例化版本,还是不同的类型,所以还是需要any来包装。

实际使用

实际使用起来让我觉得非常赏心悦目。上面做的这些事情,实际上是做了一个不同模块间零耦合
通信的通道(零耦合似乎有点过激)。现在逻辑模块通知UI模块,只需要定义新的事件类型,在
两边分别写通知和处理通知的代码即可。

PS:
针对一些评论,我再解释下。其实any只是用于包装Param列表而已,这里也可以用void*,再转成
Param*。在这里过多地关注是用any*还是用void*其实偏离了本文的重点。本文的重点其实是Param:

 

OnNotify( NT_ENTER_RGN, ang( create_param( player, rgn_id ) ) );

->
void OnNotify( long type, any data )
{
 Param2
<Player*long> ParamType;
 ParamType 
*= any_cast<ParamType>&data );
 Player 
*player = p->p1;
 
long rgn_id = p->p2;
}




下载相关代码

posted @ 2010-04-20 22:21 avatar 阅读(439) | 评论 (1)编辑 收藏

STL容器实现IniFileParser

IniFileParser.h

/*************************************************************************/
/* FileName:IniFileParser.cpp                                                                                                   */
/* Describe:IniFile@read、write                                                                                               */
/* Author  :Kagayaku                                                                                                               */
/* Date    :3.22.2010                                                                                                                */
/************************************************************************/

#ifndef _INIFILEPARSER_H_
#define _INIFILEPARSER_H_

#include <string>
#include <vector>
using namespace std;

 

struct CIniEntry
{
 CIniEntry(){}
 CIniEntry(char *szName,char *szValue):m_strIEName(szName),m_strIEValue(szValue){}
 string m_strIEName;
 string m_strIEValue;
};

struct CIniComment
{
 CIniComment(){}
 CIniComment(char *szIC):m_strIC(szIC){}
 string m_strIC;
};

struct CIniSection
{
 vector<CIniEntry>     m_vecIE;
 vector<CIniComment>   m_vecIC;
 string                m_strISName;
};


class CIniFile
{
public:
 CIniFile(const char *szIniFileFullPath);
 ~CIniFile();
 bool ReadIniFile(const char *szinifile);
 bool WriteIniFile(const char *szinifile);
 void TrimIniFile(char* &szinifile) const;
 void RemoveComment(char *szinifile) const;
 bool SearchMatchingIniFileSectionGetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname);
 bool SearchMatchingIniFileSectionSetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname,const char *szentryvalue);
   
private:
 vector<CIniSection> m_vecIS;
 string              m_strBufIEValue;
 string              m_strIniFileName;


};
#endif

IniFileParser.cpp

#include "IniFileParser.h"
#include <fstream>

CIniFile::CIniFile(const char *szIniFileFullPath):m_strIniFileName(szIniFileFullPath)
{
 ReadIniFile(szIniFileFullPath);
}

CIniFile::~CIniFile()
{

}

bool CIniFile::ReadIniFile(const char *szinifile)
{
 if (NULL==szinifile)
 {
  return false;
 }
 ifstream inifile(szinifile);
 if (NULL==inifile)
 {
  return false;
 }

 const int MAX_ROW_LENGTH=200;
 char chLineBufArray[MAX_ROW_LENGTH]={0};
 while(inifile.getline(chLineBufArray,MAX_ROW_LENGTH))
 {
  char *p=chLineBufArray;
  TrimIniFile(p);

  if (*p=='[')
  {
   RemoveComment(p);
   char *pEnd=strchr(p,']');
   if (NULL==pEnd || pEnd==p+1)
   {
    continue;
   }
   *pEnd = 0;
   CIniSection is;
   is.m_strISName=string(p+1);
   m_vecIS.push_back(is);
   continue;
  }

  if (m_vecIS.size()<1)
  {
   continue;
  }
  

  if (*p==';')
  {
   if (NULL==*(p+1))
   {
    continue;
   }
   else
   {
    CIniComment ic(p+1);
    m_vecIS[m_vecIS.size()-1].m_vecIC.push_back(ic);
    continue;
   }
   
  }
  
  char *pFlag=strchr(p,'=');
  if (NULL==pFlag || pFlag==p || NULL==*(pFlag+1))
  {
   continue;
  }
  else
  {
   *pFlag=0;
   CIniEntry ie;
   ie.m_strIEName=string(p);
   ie.m_strIEValue=string(pFlag+1);
   m_vecIS[m_vecIS.size()-1].m_vecIE.push_back(ie);
   continue;

  }

 }
 inifile.close();
 return true;

}

bool CIniFile::WriteIniFile(const char *szinifile)
{
 if (NULL==szinifile || m_strIniFileName!=szinifile)
 {
  return false;
 }

 ofstream inifile(szinifile);
 if (NULL==inifile)
 {
  return false;
 }

 for (int i=0;i!=m_vecIS.size();++i)
 {
  inifile.write("[",1);
  inifile.write(m_vecIS[i].m_strISName.c_str(),m_vecIS[i].m_strISName.size());
  inifile.write("]",1);
  inifile << endl;
  for (int j=0;j!=m_vecIS[i].m_vecIE.size();++j)
  {
   inifile.write(m_vecIS[i].m_vecIE[j].m_strIEName.c_str(),m_vecIS[i].m_vecIE[j].m_strIEName.size());
   inifile.write("=",1);
   inifile.write(m_vecIS[i].m_vecIE[j].m_strIEValue.c_str(),m_vecIS[i].m_vecIE[j].m_strIEValue.size());
   inifile << endl;
  }
 }
 inifile.close();
 return true;
}

void CIniFile::TrimIniFile(char* &szinifile) const
{
 if (NULL==szinifile)
 {
  return;
 }

 char *p=szinifile;

 while(*p==' ' || *p=='\t')
 {
  ++p;
 }

 szinifile=p;
 p=szinifile+strlen(szinifile)-1;

 while(*p==' ' || *p=='\t'|| *p=='\r'|| *p=='\n')
 {
  *p=0;
  --p;
 }

}

void CIniFile::RemoveComment(char *szinifile) const
{
 if (NULL==szinifile)
 {
  return;
 }
 char *p=strchr(szinifile,';');
 *p = 0;

}

bool CIniFile::SearchMatchingIniFileSectionGetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname)
{
 if (NULL==szinifile || NULL==szsectionname || NULL==szentryname)
 {
  return false;
 }

 if (m_strIniFileName!=szinifile)
 {
  return false;
 }

    bool temp=false;

 for (vector<CIniSection>::iterator iterIS=m_vecIS.begin();iterIS!=m_vecIS.end();++iterIS)
 {
  if ((*iterIS).m_strISName==szsectionname)
  {
   for (vector<CIniEntry>::iterator iterIE=(*iterIS).m_vecIE.begin();iterIE!=(*iterIS).m_vecIE.end();++iterIE)
   {
    if ((*iterIE).m_strIEName==szentryname)
    {
     m_strBufIEValue=(*iterIE).m_strIEValue;
     temp=true;
    }
   }
  }
 }
 return temp;

}

bool CIniFile::SearchMatchingIniFileSectionSetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname,const char *szentryvalue)
{
 if (NULL==szinifile || NULL==szsectionname || NULL==szentryname|| NULL==szentryvalue)
 {
  return false;
 }

 if (m_strIniFileName!=szinifile)
 {
  return false;
 }

 bool temp=false;

 for (vector<CIniSection>::iterator iterIS=m_vecIS.begin();iterIS!=m_vecIS.end();++iterIS)
 {
  if ((*iterIS).m_strISName==szsectionname)
  {
   for (vector<CIniEntry>::iterator iterIE=(*iterIS).m_vecIE.begin();iterIE!=(*iterIS).m_vecIE.end();++iterIE)
   {
    if ((*iterIE).m_strIEName==szentryname)
    {
     (*iterIE).m_strIEValue=szentryvalue;
     temp=true;
    }
   }
  }
 }
   
 
 return temp?WriteIniFile(szinifile):false;

}

posted @ 2010-03-22 23:01 avatar 阅读(1777) | 评论 (5)编辑 收藏

仅列出标题
共3页: 1 2 3 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜