开发者

How to save type information of template vector

开发者 https://www.devze.com 2023-01-27 09:01 出处:网络
I am trying to implement a class for serialization (XML for now). The idea is that any derived class can registers its members with the base class and base can write the members in form of XML.

I am trying to implement a class for serialization (XML for now). The idea is that any derived class can registers its members with the base class and base can write the members in form of XML.

The code looks something like this

class IXMLINF
{

protected:

struct INFObj
{
union MemPtr
{
  int* piMem;
  char* pstrMem;
  IXMLINF* pINFMem;
}

MemPtr memObj;
};

vec<INFObj*> m_INFObjVec;
void addMemToINF(int* piMem)
{
INFObj* pObj = new INFObj;
pObj->memObj.piMem = piMem;
m_INFObjVec.append(pObj);
}
void addMemToINF(char* pstrMem);
void addMemToINF(IXMLINF* pINFMem);

void writeToXML()
开发者_如何转开发{
for_each_element_in_m_INFObjVec
{
//if int or char process to XML
//else if IXMINF call its writeToXML
}
}
}

So far so good . However I also want to to be able to write vectors of types to XML. For int and char* it is easy but how to do it for vectors of IXMLINF derived class in a generic way (vec is a different type from vec)

One possible way might be

<class T>void addMemToINF(vec<T*>* pXMem)
{

//T is a class derived from IXMLINF
void* pvMem = (void*)pXMem
//Somehow store type of T

Type = T

}

void writeToXML()
{
....

vec<Type*>* pVec = (vec<Type*>*)pvMem ;

}

I will appreciate any suggestions on how to store Type informatio (Type = T step) or any alternate method for doing what I want to do.


FWIW this answer (by @Phillip) to a related question also answers this question with a little bit of tweaking . If anybody wants I can put the soln.

0

精彩评论

暂无评论...
验证码 换一张
取 消