Re: [Boost-bugs] [Boost C++ Libraries] #3234: XML Serialization error when loading class that inherits from wstring

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #3234: XML Serialization error when loading class that inherits from wstring
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2009-07-10 23:39:43


#3234: XML Serialization error when loading class that inherits from wstring
-------------------------------------------------+--------------------------
 Reporter: nitram.cero@… | Owner: ramey
     Type: Bugs | Status: new
Milestone: Boost 1.40.0 | Component: serialization
  Version: Boost Development Trunk | Severity: Problem
 Keywords: serialization wstring derived class |
-------------------------------------------------+--------------------------

Comment(by nitram.cero@…):

 No problem.
 (Using MSVC++ 2008 express)

 Important excerpts:
 {{{
 namespace primitive {
         class wstring : public std::wstring {


 ...



 namespace object {
         class wstring : public std::wstring {


 ...


 BOOST_CLASS_IMPLEMENTATION(object::wstring,
 boost::serialization::object_serializable)
 BOOST_CLASS_IMPLEMENTATION(primitive::wstring,
 boost::serialization::primitive_type)


 ...

 template<class Archive>
 void serialize(Archive & ar, object::wstring &s, const unsigned int
 version)
 {
         ar & boost::serialization::make_nvp("base",
 static_cast<std::wstring &>(s));
         //ar & boost::serialization::base_object<std::wstring>(s);
 }
 }}}


 Code:
 {{{

 #define _CRT_SECURE_NO_WARNINGS

 #include <boost/archive/xml_woarchive.hpp>
 #include <boost/archive/xml_wiarchive.hpp>
 #include <boost/archive/xml_archive_exception.hpp>

 #include <boost/serialization/string.hpp>


 #include <string>
 #include <fstream>
 #include <iostream>



 //Utility defines for better readability
 #define SERIALIZE_FUNCTION
 \
     friend class boost::serialization::access;
 \
     template<class Archive>
 \
     void serialize(Archive & serializable_archive, const unsigned int
 serializable_version)

 #define serializable_base(BASECLASS)
 boost::serialization::make_nvp(#BASECLASS,
 boost::serialization::base_object< BASECLASS >(*this))
 #define serializable_variable(VAR)
 boost::serialization::make_nvp(#VAR, VAR)


 namespace primitive {

         class wstring : public std::wstring {
         public:
                 wstring()
 { }
                 wstring(const wchar_t *str) :
 std::wstring(str) { }
                 wstring(const wstring &wstr) :
 std::wstring(wstr) { }
                 wstring(const std::wstring &wstr) :
 std::wstring(wstr) { }

                 wstring &operator =(const wstring &wstr)
 { operator =(static_cast<const std::wstring &>(wstr)); return *this; }
                 wstring &operator =(const std::wstring &wstr) {
 std::wstring::operator =(wstr);
 return *this; }
                 wstring &operator =(const wchar_t *str)
 { std::wstring::operator =(str);
 return *this; }

                 //...custom stuff...
         };

 }

 namespace object {

         class wstring : public std::wstring {
         public:
                 wstring()
 { }
                 wstring(const wchar_t *str) :
 std::wstring(str) { }
                 wstring(const wstring &wstr) :
 std::wstring(wstr) { }
                 wstring(const std::wstring &wstr) :
 std::wstring(wstr) { }

                 wstring &operator =(const wstring &wstr)
 { operator =(static_cast<const std::wstring &>(wstr)); return *this; }
                 wstring &operator =(const std::wstring &wstr) {
 std::wstring::operator =(wstr);
 return *this; }
                 wstring &operator =(const wchar_t *str)
 { std::wstring::operator =(str);
 return *this; }

                 //...custom stuff...
         };

 }

 //BOOST_CLASS_IMPLEMENTATION(nt::wstring,
 boost::serialization::primitive_type)
 BOOST_CLASS_IMPLEMENTATION(object::wstring,
 boost::serialization::object_serializable)
 BOOST_CLASS_IMPLEMENTATION(primitive::wstring,
 boost::serialization::primitive_type)

 namespace boost { namespace serialization {

 template<class Archive>
 void serialize(Archive & ar, object::wstring &s, const unsigned int
 version)
 {
         ar & boost::serialization::make_nvp("base",
 static_cast<std::wstring &>(s));
         //ar & boost::serialization::base_object<std::wstring>(s);
 }

 } } // namespace serialization // namespace boost


 using std::wcout;
 using std::endl;

 template <typename T>
 void do_stuff(char *filename, const T &value)
 {
         try {
                 //save
                 {
                         T std_wstr = value;
                         wcout << "saving: " << std_wstr << endl;

                         std::wofstream os(filename);
                         boost::archive::xml_woarchive oxml(os);

                         oxml & serializable_variable(std_wstr);
                 }
                 //load
                 {
                         T std_wstr;

                         std::wifstream os(filename);
                         boost::archive::xml_wiarchive oxml(os);

                         oxml & serializable_variable(std_wstr);
                         wcout << "loaded: " << std_wstr << " " <<
 (std_wstr == value ? "(EQUAL)" : "(NOT EQUAL)") << endl;
                 }

                 wcout << "*** Success" << endl;
         }
         catch(boost::archive::xml_archive_exception &e) {
                 wcout << "*** Archive failure: " << e.what() << "***" <<
 endl;
         }
         catch(std::exception &e) {
                 wcout << "*** Failure: " << e.what() << "***" << endl;
         }
 }


 int main(int argc, char **argv)
 {
         wcout << L"---------- std::wstring ----------" << endl;
         //"official" wstring [OK]
         do_stuff<std::wstring >("_std1.xml", L"This is
 a test");
         do_stuff<std::wstring >("_std2.xml", L"...");

         wcout << L"---------- object::wstring (SEE _object*.xml)
 ----------" << endl;
         //derived as object [OK], BUT has TAGS surrounding the
 string in XML
         do_stuff<object::wstring >("_object1.xml", L"This is
 a test");
         do_stuff<object::wstring >("_object2.xml", L"...");

         wcout << L"---------- primitive::wstring ----------" << endl;
         //derived as primitive [FAILS]
         do_stuff<primitive::wstring >("_prim1.xml", L"This is
 a test");
         do_stuff<primitive::wstring >("_prim2.xml", L"...");

         return 0;
 }
 }}}

 Output:

 {{{
 ---------- std::wstring ----------
 saving: This is a test
 loaded: This is a test (EQUAL)
 *** Success
 saving: ...
 loaded: ... (EQUAL)
 *** Success
 ---------- object::wstring (SEE _object*.xml) ----------
 saving: This is a test
 loaded: This is a test (EQUAL)
 *** Success
 saving: ...
 loaded: ... (EQUAL)
 *** Success
 ---------- primitive::wstring ----------
 saving: This is a test
 *** Failure: stream error***
 saving: ...
 loaded: ...</std_wstr> (NOT EQUAL)
 *** Success
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/3234#comment:7>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:00 UTC