类成员变量的自定义顺序输出问题
伪代码如下:
#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(); // 按照配置文件中定义的顺序打印
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;
}