xml_wiarchive_impl.ipp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // xml_wiprimitive.cpp:
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org for updates, documentation, and revision history.
  8. #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
  9. #include <cstring>
  10. #if defined(BOOST_NO_STDC_NAMESPACE)
  11. namespace std{
  12. using ::memcpy;
  13. } //std
  14. #endif
  15. #include <boost/config.hpp> // msvc 6.0 needs this to suppress warnings
  16. #ifndef BOOST_NO_STD_WSTREAMBUF
  17. #include <boost/assert.hpp>
  18. #include <algorithm>
  19. #include <boost/detail/workaround.hpp> // Dinkumware and RogueWave
  20. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  21. #include <boost/archive/dinkumware.hpp>
  22. #endif
  23. #include <boost/io/ios_state.hpp>
  24. #include <boost/detail/no_exceptions_support.hpp>
  25. #include <boost/serialization/pfto.hpp>
  26. #include <boost/serialization/string.hpp>
  27. #include <boost/archive/add_facet.hpp>
  28. #include <boost/archive/xml_archive_exception.hpp>
  29. #include <boost/archive/detail/utf8_codecvt_facet.hpp>
  30. #include <boost/archive/iterators/mb_from_wchar.hpp>
  31. #include <boost/archive/basic_xml_archive.hpp>
  32. #include <boost/archive/xml_wiarchive.hpp>
  33. #include "basic_xml_grammar.hpp"
  34. namespace boost {
  35. namespace archive {
  36. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  37. // implemenations of functions specific to wide char archives
  38. namespace { // anonymous
  39. void copy_to_ptr(char * s, const std::wstring & ws){
  40. std::copy(
  41. iterators::mb_from_wchar<std::wstring::const_iterator>(
  42. BOOST_MAKE_PFTO_WRAPPER(ws.begin())
  43. ),
  44. iterators::mb_from_wchar<std::wstring::const_iterator>(
  45. BOOST_MAKE_PFTO_WRAPPER(ws.end())
  46. ),
  47. s
  48. );
  49. s[ws.size()] = 0;
  50. }
  51. } // anonymous
  52. template<class Archive>
  53. BOOST_WARCHIVE_DECL(void)
  54. xml_wiarchive_impl<Archive>::load(std::string & s){
  55. std::wstring ws;
  56. bool result = gimpl->parse_string(is, ws);
  57. if(! result)
  58. boost::serialization::throw_exception(
  59. xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
  60. );
  61. #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
  62. if(NULL != s.data())
  63. #endif
  64. s.resize(0);
  65. s.reserve(ws.size());
  66. std::copy(
  67. iterators::mb_from_wchar<std::wstring::iterator>(
  68. BOOST_MAKE_PFTO_WRAPPER(ws.begin())
  69. ),
  70. iterators::mb_from_wchar<std::wstring::iterator>(
  71. BOOST_MAKE_PFTO_WRAPPER(ws.end())
  72. ),
  73. std::back_inserter(s)
  74. );
  75. }
  76. #ifndef BOOST_NO_STD_WSTRING
  77. template<class Archive>
  78. BOOST_WARCHIVE_DECL(void)
  79. xml_wiarchive_impl<Archive>::load(std::wstring & ws){
  80. bool result = gimpl->parse_string(is, ws);
  81. if(! result)
  82. boost::serialization::throw_exception(
  83. xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
  84. );
  85. }
  86. #endif
  87. template<class Archive>
  88. BOOST_WARCHIVE_DECL(void)
  89. xml_wiarchive_impl<Archive>::load(char * s){
  90. std::wstring ws;
  91. bool result = gimpl->parse_string(is, ws);
  92. if(! result)
  93. boost::serialization::throw_exception(
  94. xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
  95. );
  96. copy_to_ptr(s, ws);
  97. }
  98. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  99. template<class Archive>
  100. BOOST_WARCHIVE_DECL(void)
  101. xml_wiarchive_impl<Archive>::load(wchar_t * ws){
  102. std::wstring twstring;
  103. bool result = gimpl->parse_string(is, twstring);
  104. if(! result)
  105. boost::serialization::throw_exception(
  106. xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
  107. );
  108. std::memcpy(ws, twstring.c_str(), twstring.size());
  109. ws[twstring.size()] = L'\0';
  110. }
  111. #endif
  112. template<class Archive>
  113. BOOST_WARCHIVE_DECL(void)
  114. xml_wiarchive_impl<Archive>::load_override(class_name_type & t, int){
  115. const std::wstring & ws = gimpl->rv.class_name;
  116. if(ws.size() > BOOST_SERIALIZATION_MAX_KEY_SIZE - 1)
  117. boost::serialization::throw_exception(
  118. archive_exception(archive_exception::invalid_class_name)
  119. );
  120. copy_to_ptr(t, ws);
  121. }
  122. template<class Archive>
  123. BOOST_WARCHIVE_DECL(void)
  124. xml_wiarchive_impl<Archive>::init(){
  125. gimpl->init(is);
  126. this->set_library_version(
  127. library_version_type(gimpl->rv.version)
  128. );
  129. }
  130. template<class Archive>
  131. BOOST_WARCHIVE_DECL(BOOST_PP_EMPTY())
  132. xml_wiarchive_impl<Archive>::xml_wiarchive_impl(
  133. std::wistream &is_,
  134. unsigned int flags
  135. ) :
  136. basic_text_iprimitive<std::wistream>(
  137. is_,
  138. true // don't change the codecvt - use the one below
  139. ),
  140. basic_xml_iarchive<Archive>(flags),
  141. gimpl(new xml_wgrammar())
  142. {
  143. if(0 == (flags & no_codecvt)){
  144. archive_locale.reset(
  145. add_facet(
  146. std::locale::classic(),
  147. new boost::archive::detail::utf8_codecvt_facet
  148. )
  149. );
  150. is.imbue(* archive_locale);
  151. }
  152. if(0 == (flags & no_header)){
  153. BOOST_TRY{
  154. this->init();
  155. }
  156. BOOST_CATCH(...){
  157. delete gimpl;
  158. #ifndef BOOST_NO_EXCEPTIONS
  159. throw; // re-throw
  160. #endif
  161. }
  162. BOOST_CATCH_END
  163. }
  164. }
  165. template<class Archive>
  166. BOOST_WARCHIVE_DECL(BOOST_PP_EMPTY())
  167. xml_wiarchive_impl<Archive>::~xml_wiarchive_impl(){
  168. if(0 == (this->get_flags() & no_header)){
  169. BOOST_TRY{
  170. gimpl->windup(is);
  171. }
  172. BOOST_CATCH(...){}
  173. BOOST_CATCH_END
  174. }
  175. delete gimpl;
  176. }
  177. } // namespace archive
  178. } // namespace boost
  179. #endif // BOOST_NO_STD_WSTREAMBUF