发博文
个人资料
  • 博客访问:105990
  • 博文数量:2
  • 博客积分:120
  • 博客等级:民兵
  • 注册时间:2009-03-26 17:04:19
订阅我的博客
  • 订阅
  • 订阅到鲜果
  • 订阅到抓虾
  • 订阅到Google
字体大小: 博文
利用宏自动生成代码 (2009-04-09 12:12)
分类: C++


/************************************************************************/
/* 从一串字符串当中,自动提取字段,并添加到类的相应字段上。 */
/************************************************************************/

//将下面的数据作为Debugging--Command Arguments 传入
//4表明后面有4个字段

//4 1 abc 2 ddd

//上面的数据相当于
//D d[2] = {{1, "abc"}, {2, "ddd"}};

//////////////////////////////////////////////////////////////////

//声明类当中的int型变量//__index_ 第几个字段(从0开始)

//__name_ 字段名称
#define SFS_DECLARE_ITEM_INT(__index_, __name_) \
void SetItemValue##__index_(char * pstr) \
{ \
  __name_ = atoi(pstr); \
  printf("[%d]\n", __name_); \
} \
int __name_;

//声明类当中的字符型变量
//__index_ 第几个字段(从0开始)
//__name_ 字段名称
//__len_ 字符的长度
#define SFS_DECLARE_ITEM_TEXT(__index_, __name_, __len_) \
void SetItemValue##__index_(char * pstr) \
{ \
  sprintf_s((char*)&__name_[0], __len_, (char*)pstr); \
  printf("[%s]\n", __name_); \
} \
unsigned char __name_[__len_];

//////////////////////////////////////////////////////////////////////////

//从字符串构造的基类
//nSize_T 表明 TYPE有多少个字段
template <typename TYPE, int nSize_T>
class CStructFromStr
{
public:
  CStructFromStr()
  :m_nDataCount(0)
  ,m_pDepot(NULL)
  {

  }

  virtual ~CStructFromStr(void)
  {
  DeleteDataSpace();
  }

  //回收分配的空间
  BOOL DeleteDataSpace()
  {
  if (m_pDepot)
  {
  delete [] m_pDepot;

  m_pDepot = NULL;
  }

  return TRUE;
  }

  BOOL Construct(char *ppcmd[])
  {
  BOOL bRet = FALSE;

  DeleteDataSpace();

  int nColCount = atoi(ppcmd[1]);

  if (nColCount > 0)
  {
  m_nDataCount = nColCount / nSize_T;

  m_pDepot = new TYPE[m_nDataCount];

  if (m_pDepot)
  {
  int nCmdIndex = 2;

  for (int i = 0; i < m_nDataCount; i++)
  {
  bRet = m_pDepot[i].FillStruct(nCmdIndex, ppcmd);
  }
  }
  }

  return bRet;
  }

  //存储的数量
  int m_nDataCount;

  //数据存储位置
  TYPE * m_pDepot;
};

//////////////////////////////////////////////////////////////////////////

//可以生成递归效果的宏
//每次处理后nCmdIndex都会加1操作
#define SFS_V_FILL_STRUCT(__index_) \
SetItemValue##__index_(ppcmd[nCmdIndex]);nCmdIndex++;

#define SFS_V_FILL_STRUCT0 SFS_V_FILL_STRUCT(0)
#define SFS_V_FILL_STRUCT1 SFS_V_FILL_STRUCT0; SFS_V_FILL_STRUCT(1)

//////////////////////////////////////////////////////////////////////////

//声明类(自动将类的基类指向CStructFromStr)
//__strct_name_ 类的名称
//__item_num 类的字段个数(从0开始)
//传递给CStructFromStr的 nSize_T应该是__item_num + 1
//提供了两个构造函数,第二个直接传入将字符串序列。
#define SFS_DECLARE_BEGIN(__strct_name_, __item_num) \
class __strct_name_:public CStructFromStr<__strct_name_, __item_num + 1> \
{ \
public: \
  __strct_name_() \
  { \
  memset(this, 0x00, sizeof(__strct_name_)); \
  } \
  explicit __strct_name_(char *ppcmd[]) \
  { \
  m_nDataCount = 0; \
  m_pDepot = NULL; \
  Construct(ppcmd); \
  } \
  BOOL FillStruct(int & nCmdIndex, char *ppcmd[]) \
  { \
  SFS_V_FILL_STRUCT##__item_num; \
  return TRUE; \
  }

//声明类的结束
#define SFS_DECLARE_END() \
};

//////////////////////////////////////////////////////////////////////////

//一个简单的测试类
SFS_DECLARE_BEGIN(D, 1)
  SFS_DECLARE_ITEM_INT(0, FLD_ID);
  SFS_DECLARE_ITEM_TEXT(1, FLD_NAME, 30);
SFS_DECLARE_END()

int main(int argc, char * argv[])
{
  D d(argv);

  for (int i = 0; i < 2; i++)
  {
  printf("d.m_pDepot[%d].FLD_ID=[%d]\n", i, d.m_pDepot[i].FLD_ID);
  printf("d.m_pDepot[%d].FLD_NAME=[%s]\n", i, d.m_pDepot[i].FLD_NAME);
  }

  return 0;
}


博客推荐文章
亲,您还没有登录,请[登录][注册]后再进行评论