首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

类成员变量的自定义顺序输出有关问题

2012-07-15 
类成员变量的自定义顺序输出问题伪代码如下:C/C++ code#include iostreamusing namespace stdclass A{p

类成员变量的自定义顺序输出问题
伪代码如下:

C/C++ code
#include <iostream>using namespace std;class A{public:    int i;    double d;    char* c;    string s;    int print(); // 按照配置文件中定义的顺序打印};/* * 我想在配置文件中定义输出变量及其顺序,比如: * format=${i}|${s}|${d}| * 注意:某些变量${c}没有出现在配置里,表示不打印。*/LOAD_CONFIG();  // 加载配置A a;a.print();      // 按照配置文件中定义的顺序打印




请问该如何实现?

谢谢!

[解决办法]
探讨

顶一下
期待解决方案。。。

[解决办法]
应该是这样的:
不知道有米有解决楼主的问题?
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
enum ItemType
{
TYPE_INT,
TYPE_DOUBLE,
TYPE_STLSTRING,
};
template<class T>
struct ItemFormat
{
const char *name; // 变量名
ItemType tp; // 变量类型
const char *fmt; // 格式化符号
//size_t offset; // 类型偏移
char T::* poAdress; // 类型偏移
};
#define PREFIX '$'
#define BUFFSZ 1024
class A
{
public:
int i;
double d;
std::string s;
//
// 所有可用的配置变量
//
static ItemFormat<A> FormatDef[];
//
// format=hello|${i}|${s}|${d}|world
//
static char FormatBuff[BUFFSZ]; // 加载上述配置后变成:hello|$|$|$|world
static std::vector<int> ItemOrder; // 加载上述配置后变成:[0,1,2], 数字分别表示上述 $ 符号对应在 FormatDef 里的下标
static int SetFormat(const char* fmt);
int printfA(char* buff, int bufflen, ItemFormat<A> *fmt, int sz);
};
ItemFormat<A> A::FormatDef[] = {
{"i", TYPE_INT, "%d", (char A::*)&A::i},
{"s", TYPE_STLSTRING, "%s", (char A::*)&A::s},
{"d", TYPE_DOUBLE, "%f", (char A::*)&A::d},
{0,TYPE_INT,0,0},
};
//
// format=hello|${i}|${s}|${d}|world
//
char A::FormatBuff[BUFFSZ] = {0};
std::vector<int> A::ItemOrder;
//
// 格式配置加载、转换由 SetFormat 完成
//
int A::SetFormat(const char* fmt)
{
int i = 0;
int k = 0;
for(i=0; (i<sizeof(FormatBuff)) && (0!=fmt[i]); i++)
{
FormatBuff[k++] = fmt[i];
if(PREFIX==fmt[i]) // xxxx${varname}zzzz
{
++i;
if( (i<sizeof(FormatBuff)) && ('{'==fmt[i]) )
{
++i; // begin pos of 'varname'
char varname[32] = {0};
int j = 0;
for(j=0; (j<sizeof(varname)) && (0!=fmt[i]) && ('}'!=fmt[i]); j++)
{
varname[j] = fmt[i];
++i;
}
if('}'!=fmt[i]) // 最后的字符必定是'}', 否则格式有误
return -1;
int itemid = -1;
// 找到该变量在 FormatDef 中的位置
for(j=0; 0!=FormatDef[j].name; j++)
{
if( 0==strcmp(FormatDef[j].name, varname) )
{
itemid = j;
break;
}
}
if(itemid>=0)
ItemOrder.push_back(itemid);
}
}
}
return 0;
}
int A::printfA(char* buff, int bufflen, ItemFormat<A> *fmt, int sz)
{
char *p = buff;
int i = 0;
std::vector<int>::const_iterator it = ItemOrder.begin();
for(i=0; ((i<sizeof(FormatBuff)) && (0!=FormatBuff[i]) && (p-buff)<bufflen); i++)
{


if( PREFIX==FormatBuff[i] )
{
if(it!=ItemOrder.end())
{
int len = 0;
int idx = *it;
if( idx>=0 && idx<=sz && NULL!=fmt[idx].name )
{
switch(fmt[idx].tp)
{
case TYPE_INT:
//len = sprintf(p, fmt[idx].fmt, *((int*)((char*)this+fmt[idx].offset)));
len = sprintf(p, fmt[idx].fmt, this->*(fmt[idx].poAdress));
break;
case TYPE_DOUBLE:
//len = sprintf(p, fmt[idx].fmt, *((double*)((char*)this+fmt[idx].offset)));
len = sprintf(p, fmt[idx].fmt, this->*((double A::*)(fmt[idx].poAdress)));
break;
case TYPE_STLSTRING:
//len = sprintf(p, fmt[idx].fmt, ((std::string*)((char*)this+fmt[idx].offset))->c_str());
len = sprintf(p, fmt[idx].fmt, (this->*((std::string A::*)(fmt[idx].poAdress))).c_str());
break;
default:
break;
}
if( len>0 )
{
p += len;
}
else // failed when do sprintf
{
memset(buff, 0, bufflen);
return -1;
}
}
else
{
return -1;
}
it++;
}
else
{// $, 但是没有格式化信息,即 $ 的数量太多
memset(buff, 0, bufflen);
return -1;
}
}
else
{
*p = FormatBuff[i];
++p;
}
}
if((0!=FormatBuff[i]) || (p-buff)>bufflen)
{
memset(buff, 0, bufflen);
return -1;
}
return 0;
}
int main(int argc, char* argv[])
{
A::SetFormat("hello|${i}|${s}|${d}|world");
A a;
a.i = 1;
a.s = "s";
a.d = 3.1415;
char buff[1024] = {0};
a.printfA(buff, 1024, A::FormatDef, sizeof(A::FormatDef)/sizeof(ItemFormat<A>));
cout << buff << endl;
}
 

热点排行