Robert Ramey
Markus Werle wrote:
Robert Ramey
writes: The easiest would be just to define non-untrusive serialize templates for these classes.
Unfortunately this approach still requires an extra nvp for xml files, so for the code below we obtain e.g.
<SomeTag><value>SomeText</value><SomeTag>
The extra tag is what I would like to avoid.
Note that usage of std::string rather than CString will have the same "extra" tag.
I disagree. Probably I did not explain my intention/problem precise enough. To store a std::string in an xml file you need 1 tag, not 2 like for CString. You store it by using std::string myStdString = "Test"; [...] ar & make_nvp("SomeTag", myStdString); and obtain an xml file with a line <SomeTag>Test<SomeTag> which is exactly what I want. But now I want this for any type which has a string representation, e.g. CString from MFC (the lib I hate most) As I showed in a previous post the classical approach, which you also mention in modified form bails out due to missing _extra_ nvp! Compiler fails on template<class Archive> inline void save(Archive & ar, CString const & S, unsigned int const /* file_version */) { using boost::serialization::make_nvp; std::string const s(S); ar << s; } with [...]basic_xml_oarchive.hpp(86) : error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE<> etc. even if you store the CString using an nvp like in CString myStdString = "Test"; [...] ar & make_nvp("SomeTag", myStdString); So there is an unnecessary (?) asymmetry between CString and std::string.
what's the matter with adding to MyType a common string cast operator
and using:
template<class Archive> void save(Archive &ar, const MyType &t, const unsigned version){ std::string s = t; ar << t; } and
std::ostream & operator<<(std::ostream &os, MyType const & T){ std::string s = t; *this << t; }
This is what I tried with CString. My approach yields an xml file with a line containing <SomeTag><value>Test</value><SomeTag> using template<class Archive> inline void save(Archive & ar, CString const & S, unsigned int const /* file_version */) { using boost::serialization::make_nvp; std::string const s(S); ar << make_nvp("value", s); } I still cannot see how to avoid this extra <value> tag. Did it become clear now? Markus