Chinaunix首页 | 论坛 | 博客
  • 博客访问: 339018
  • 博文数量: 88
  • 博客积分: 1695
  • 博客等级: 上尉
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-06 15:48
个人简介

喜欢美食, 旅行..

文章分类

全部博文(88)

文章存档

2014年(2)

2013年(12)

2012年(14)

2010年(8)

2009年(52)

我的朋友

分类: C/C++

2013-07-25 20:37:32


这里只是简单些个序列化的逻辑. 做个笔记而已.
字符串转换成结构体类似. 读者如果有兴趣可以采用 Json 的字符串格式.

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;

  5. template<class T>
  6. string to_string( T t )
  7. {
  8.     ostringstream oss;
  9.     oss << t;
  10.     result = oss.str();
  11. }


  12. #define TransBegin( cls ) \
  13.     template<>\
  14.     string to_string ( cls t )\
  15. {\
  16.     string strContent("{\n");\

  17. #define TransMenber(value) \
  18. {\
  19.     stringstream sio;\
  20.     sio << #value << " : " << t.value << "\n" ;\
  21.     strContent += sio.str();\
  22. }\

  23. #define TransEnd() \
  24.     strContent += "}";\
  25.     return strContent;\
  26. }\

  27. struct Data
  28. {
  29.     int numberData;
  30.     string strData;
  31. };
  32. TransBegin(Data)
  33. TransMenber(numberData)
  34. TransMenber(strData)
  35. TransEnd()

  36. void main()
  37. {

  38.     Data data;
  39.     string str = to_string(data);

  40.     cout << str << endl;

  41. }

阅读(976) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~