Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // basic_binary_iprimitive.ipp: Chris@16: Chris@16: // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org for updates, documentation, and revision history. Chris@16: Chris@16: #include Chris@16: #include // size_t, NULL Chris@16: #include // memcpy Chris@16: Chris@16: #include Chris@16: #if defined(BOOST_NO_STDC_NAMESPACE) Chris@16: namespace std{ Chris@16: using ::size_t; Chris@16: using ::memcpy; Chris@16: } // namespace std Chris@16: #endif Chris@16: Chris@16: #include // fixup for RogueWave Chris@16: Chris@16: #include Chris@16: Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace archive { Chris@16: Chris@16: ////////////////////////////////////////////////////////////////////// Chris@16: // implementation of basic_binary_iprimitive Chris@16: Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: basic_binary_iprimitive::init() Chris@16: { Chris@16: // Detect attempts to pass native binary archives across Chris@16: // incompatible platforms. This is not fool proof but its Chris@16: // better than nothing. Chris@16: unsigned char size; Chris@16: this->This()->load(size); Chris@16: if(sizeof(int) != size) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception( Chris@16: archive_exception::incompatible_native_format, Chris@16: "size of int" Chris@16: ) Chris@16: ); Chris@16: this->This()->load(size); Chris@16: if(sizeof(long) != size) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception( Chris@16: archive_exception::incompatible_native_format, Chris@16: "size of long" Chris@16: ) Chris@16: ); Chris@16: this->This()->load(size); Chris@16: if(sizeof(float) != size) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception( Chris@16: archive_exception::incompatible_native_format, Chris@16: "size of float" Chris@16: ) Chris@16: ); Chris@16: this->This()->load(size); Chris@16: if(sizeof(double) != size) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception( Chris@16: archive_exception::incompatible_native_format, Chris@16: "size of double" Chris@16: ) Chris@16: ); Chris@16: Chris@16: // for checking endian Chris@16: int i; Chris@16: this->This()->load(i); Chris@16: if(1 != i) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception( Chris@16: archive_exception::incompatible_native_format, Chris@16: "endian setting" Chris@16: ) Chris@16: ); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: basic_binary_iprimitive::load(wchar_t * ws) Chris@16: { Chris@16: std::size_t l; // number of wchar_t !!! Chris@16: this->This()->load(l); Chris@16: load_binary(ws, l * sizeof(wchar_t) / sizeof(char)); Chris@16: ws[l] = L'\0'; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: basic_binary_iprimitive::load(std::string & s) Chris@16: { Chris@16: std::size_t l; Chris@16: this->This()->load(l); Chris@16: // borland de-allocator fixup Chris@16: #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101)) Chris@16: if(NULL != s.data()) Chris@16: #endif Chris@16: s.resize(l); Chris@16: // note breaking a rule here - could be a problem on some platform Chris@16: if(0 < l) Chris@16: load_binary(&(*s.begin()), l); Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_CWCHAR Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: basic_binary_iprimitive::load(char * s) Chris@16: { Chris@16: std::size_t l; Chris@16: this->This()->load(l); Chris@16: load_binary(s, l); Chris@16: s[l] = '\0'; Chris@16: } Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_NO_STD_WSTRING Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: basic_binary_iprimitive::load(std::wstring & ws) Chris@16: { Chris@16: std::size_t l; Chris@16: this->This()->load(l); Chris@16: // borland de-allocator fixup Chris@16: #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101)) Chris@16: if(NULL != ws.data()) Chris@16: #endif Chris@16: ws.resize(l); Chris@16: // note breaking a rule here - is could be a problem on some platform Chris@16: load_binary(const_cast(ws.data()), l * sizeof(wchar_t) / sizeof(char)); Chris@16: } Chris@16: #endif Chris@16: Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) Chris@16: basic_binary_iprimitive::basic_binary_iprimitive( Chris@16: std::basic_streambuf & sb, Chris@16: bool no_codecvt Chris@16: ) : Chris@16: #ifndef BOOST_NO_STD_LOCALE Chris@16: m_sb(sb), Chris@16: locale_saver(m_sb) Chris@16: { Chris@16: if(! no_codecvt){ Chris@16: archive_locale.reset( Chris@101: add_facet( Chris@16: std::locale::classic(), Chris@16: new codecvt_null Chris@16: ) Chris@16: ); Chris@101: //m_sb.pubimbue(* archive_locale); Chris@16: } Chris@16: } Chris@16: #else Chris@16: m_sb(sb) Chris@16: {} Chris@16: #endif Chris@16: Chris@16: // some libraries including stl and libcomo fail if the Chris@16: // buffer isn't flushed before the code_cvt facet is changed. Chris@16: // I think this is a bug. We explicity invoke sync to when Chris@16: // we're done with the streambuf to work around this problem. Chris@16: // Note that sync is a protected member of stream buff so we Chris@16: // have to invoke it through a contrived derived class. Chris@16: namespace detail { Chris@16: // note: use "using" to get past msvc bug Chris@16: using namespace std; Chris@16: template Chris@16: class input_streambuf_access : public std::basic_streambuf { Chris@16: public: Chris@16: virtual int sync(){ Chris@16: #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) Chris@16: return this->basic_streambuf::sync(); Chris@16: #else Chris@16: return this->basic_streambuf::sync(); Chris@16: #endif Chris@16: } Chris@16: }; Chris@16: } // detail Chris@16: Chris@16: // scoped_ptr requires that archive_locale be a complete type at time of Chris@16: // destruction so define destructor here rather than in the header Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) Chris@16: basic_binary_iprimitive::~basic_binary_iprimitive(){ Chris@16: // push back unread characters Chris@16: //destructor can't throw ! Chris@101: BOOST_TRY{ Chris@16: static_cast &>(m_sb).sync(); Chris@16: } Chris@101: BOOST_CATCH(...){ Chris@16: } Chris@101: BOOST_CATCH_END Chris@16: } Chris@16: Chris@16: } // namespace archive Chris@16: } // namespace boost