Chris@16: #ifndef BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP Chris@16: #define BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP Chris@16: Chris@16: // MS compatible compilers support #pragma once Chris@101: #if defined(_MSC_VER) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // basic_binary_oprimitive.hpp 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: // archives stored as native binary - this should be the fastest way Chris@16: // to archive the state of a group of obects. It makes no attempt to Chris@16: // convert to any canonical form. Chris@16: Chris@16: // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE Chris@16: // ON PLATFORM APART FROM THE ONE THEY ARE CREATE ON Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // basic_streambuf Chris@16: #include Chris@16: #include // size_t Chris@16: Chris@16: #include Chris@16: #if defined(BOOST_NO_STDC_NAMESPACE) Chris@16: namespace std{ Chris@16: using ::size_t; Chris@16: } // namespace std Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // must be the last header Chris@16: Chris@16: namespace boost { Chris@16: namespace archive { Chris@16: Chris@101: template Chris@101: class codecvt_null; Chris@101: Chris@16: ///////////////////////////////////////////////////////////////////////// Chris@16: // class basic_binary_oprimitive - binary output of prmitives Chris@16: Chris@16: template Chris@101: class basic_binary_oprimitive { Chris@16: #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS Chris@16: friend class save_access; Chris@16: protected: Chris@16: #else Chris@16: public: Chris@16: #endif Chris@16: std::basic_streambuf & m_sb; Chris@16: // return a pointer to the most derived class Chris@16: Archive * This(){ Chris@16: return static_cast(this); Chris@16: } Chris@16: #ifndef BOOST_NO_STD_LOCALE Chris@101: boost::scoped_ptr > codecvt_facet; Chris@16: boost::scoped_ptr archive_locale; Chris@16: basic_streambuf_locale_saver locale_saver; Chris@16: #endif Chris@16: // default saving of primitives. Chris@16: template Chris@16: void save(const T & t) Chris@16: { Chris@16: save_binary(& t, sizeof(T)); Chris@16: } Chris@16: Chris@16: ///////////////////////////////////////////////////////// Chris@16: // fundamental types that need special treatment Chris@16: Chris@16: // trap usage of invalid uninitialized boolean which would Chris@16: // otherwise crash on load. Chris@16: void save(const bool t){ Chris@16: BOOST_ASSERT(0 == static_cast(t) || 1 == static_cast(t)); Chris@16: save_binary(& t, sizeof(t)); Chris@16: } Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: save(const std::string &s); Chris@16: #ifndef BOOST_NO_STD_WSTRING Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: save(const std::wstring &ws); Chris@16: #endif Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: save(const char * t); Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: save(const wchar_t * t); Chris@16: Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) Chris@16: init(); Chris@16: Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) Chris@16: basic_binary_oprimitive( Chris@16: std::basic_streambuf & sb, Chris@16: bool no_codecvt Chris@16: ); Chris@16: BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) Chris@16: ~basic_binary_oprimitive(); Chris@16: public: Chris@16: Chris@16: // we provide an optimized save for all fundamental types Chris@16: // typedef serialization::is_bitwise_serializable Chris@16: // use_array_optimization; Chris@16: // workaround without using mpl lambdas Chris@16: struct use_array_optimization { Chris@16: template Chris@16: #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS) Chris@16: struct apply { Chris@101: typedef typename boost::serialization::is_bitwise_serializable< T >::type type; Chris@16: }; Chris@16: #else Chris@16: struct apply : public boost::serialization::is_bitwise_serializable< T > {}; Chris@16: #endif Chris@16: }; Chris@16: Chris@16: Chris@16: // the optimized save_array dispatches to save_binary Chris@16: template Chris@16: void save_array(boost::serialization::array const& a, unsigned int) Chris@16: { Chris@16: save_binary(a.address(),a.count()*sizeof(ValueType)); Chris@16: } Chris@16: Chris@16: void save_binary(const void *address, std::size_t count); Chris@16: }; Chris@16: Chris@16: template Chris@16: inline void Chris@16: basic_binary_oprimitive::save_binary( Chris@16: const void *address, Chris@16: std::size_t count Chris@16: ){ Chris@16: //BOOST_ASSERT( Chris@16: // static_cast((std::numeric_limits::max)()) >= count Chris@16: //); Chris@16: // note: if the following assertions fail Chris@16: // a likely cause is that the output stream is set to "text" Chris@16: // mode where by cr characters recieve special treatment. Chris@16: // be sure that the output stream is opened with ios::binary Chris@16: //if(os.fail()) Chris@16: // boost::serialization::throw_exception( Chris@16: // archive_exception(archive_exception::output_stream_error) Chris@16: // ); Chris@16: // figure number of elements to output - round up Chris@16: count = ( count + sizeof(Elem) - 1) Chris@16: / sizeof(Elem); Chris@16: BOOST_ASSERT(count <= std::size_t(boost::integer_traits::const_max)); Chris@16: std::streamsize scount = m_sb.sputn( Chris@16: static_cast(address), Chris@16: static_cast(count) Chris@16: ); Chris@16: if(count != static_cast(scount)) Chris@16: boost::serialization::throw_exception( Chris@16: archive_exception(archive_exception::output_stream_error) Chris@16: ); Chris@16: //os.write( Chris@101: // static_cast(address), Chris@16: // count Chris@16: //); Chris@16: //BOOST_ASSERT(os.good()); Chris@16: } Chris@16: Chris@16: } //namespace boost Chris@16: } //namespace archive Chris@16: Chris@16: #include // pop pragmas Chris@16: Chris@16: #endif // BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP