hi leute hab da ein Problem oder besser eine Frage.
Ich arbeite mit xml(mit der tinyxml lib) und vreusch gerade eine kleine hilfs funktion zu schreiben.
Die 1. ist mir gelungen (war ja auch nicht schwer)
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
template<typename T>
void setElement(TiXmlElement* pElem,std::string elemName,std::string valueName,T value){
if(!pElem)
return;
TiXmlElement pElemChild = TiXmlElement (elemName);
pElemChild.SetAttribute(valueName,typeToStr<T>(value));
pElem->InsertEndChild(pElemChild);
}
void getElement(TiXmlElement* pElem,std::string elemName,std::string valueName,double& value);
void getElement(TiXmlElement* pElem,std::string elemName,std::string valueName,float& value);
void getElement(TiXmlElement* pElem,std::string elemName,std::string valueName,int& value);
void getElement(TiXmlElement* pElem,std::string elemName,std::string valueName,std::string& value);
void getElement(TiXmlElement* pElem,std::string elemName,std::string valueName,double& value){
TiXmlElement* pElemChild = pElem->FirstChildElement(elemName.c_str());
if(pElemChild)
pElemChild->QueryDoubleAttribute(valueName.c_str(),&value);
}
|
so weit so gut
Mein Problem ist jetzt aber was ist wenn ich ein Element hab das 2,3,4 oder 5 Attribute hat, will ich nicht jetzt 30 Funktionen schreiben
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
|
template<typename T,typename T2>
void setElement(TiXmlElement* pElem,std::string elemName,std::string valueName,std::string valueName2,T value,T2 value2){
if(!pElem)
return;
TiXmlElement pElemChild = TiXmlElement (elemName);
pElemChild.SetAttribute(valueName,typeToStr<T>(value));
pElemChild.SetAttribute(valueName2,typeToStr<T2>(value2));
pElem->InsertEndChild(pElemChild);
}
//...
|
könnte die Funktion nicht so aussehen
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Foo{
public:
int a;
double b;
float c;
}
void setElement(TiXmlElement* pElem,std::string elemName,std::string valueNames,...){
...
}
void getElement(TiXmlElement* pElem,std::string elemName,std::string valueNames,...){
...
}
setElement(pElem,"Foo","a b c",foo.a,foo.b,foo.c);
getElement(pElem,"Foo","a b c",&foo.a,&foo.b,&foo.c);
|
|
Quellcode
|
1
|
<Foo a="5" b="25.6589" c="12.56"/>
|
Hab auch schon nach stdarg.h geguckt, weis aber nicht wie ich das umsetzen kann.
So wie ich das verstanden hab braucht va_arg() noch den datentypen als parameter
aber woher soll ich wissen welche datentype das aktuelle argument hat.
Den Datentyp braucht ich ja auch für T(vom template).