Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // basic_binary_oprimitive.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 // NULL Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_NO_STDC_NAMESPACE) && ! defined(__LIBCOMO__) Chris@16: namespace std{ Chris@16: using ::strlen; Chris@16: } // namespace std Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_NO_CWCHAR Chris@16: #include Chris@16: #ifdef BOOST_NO_STDC_NAMESPACE Chris@16: namespace std{ using ::wcslen; } Chris@16: #endif Chris@16: #endif Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace archive { Chris@16: Chris@16: ////////////////////////////////////////////////////////////////////// Chris@16: // implementation of basic_binary_oprimitive Chris@16: Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: basic_binary_oprimitive::init() Chris@16: { Chris@16: // record native sizes of fundamental types Chris@16: // this is to permit detection of attempts to pass Chris@16: // native binary archives accross incompatible machines. Chris@16: // This is not foolproof but its better than nothing. Chris@16: this->This()->save(static_cast(sizeof(int))); Chris@16: this->This()->save(static_cast(sizeof(long))); Chris@16: this->This()->save(static_cast(sizeof(float))); Chris@16: this->This()->save(static_cast(sizeof(double))); Chris@16: // for checking endianness Chris@16: this->This()->save(int(1)); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: basic_binary_oprimitive::save(const char * s) Chris@16: { Chris@16: std::size_t l = std::strlen(s); Chris@16: this->This()->save(l); Chris@16: save_binary(s, l); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: basic_binary_oprimitive::save(const std::string &s) Chris@16: { Chris@16: std::size_t l = static_cast(s.size()); Chris@16: this->This()->save(l); Chris@16: save_binary(s.data(), 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_oprimitive::save(const wchar_t * ws) Chris@16: { Chris@16: std::size_t l = std::wcslen(ws); Chris@16: this->This()->save(l); Chris@16: save_binary(ws, l * sizeof(wchar_t) / sizeof(char)); 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_oprimitive::save(const std::wstring &ws) Chris@16: { Chris@16: std::size_t l = ws.size(); Chris@16: this->This()->save(l); Chris@16: save_binary(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_oprimitive::basic_binary_oprimitive( 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@16: add_facet( Chris@101: 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 output_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 g 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_oprimitive::~basic_binary_oprimitive(){ Chris@16: // flush buffer 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