Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:29

0001 #ifndef BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
0002 #define BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
0003 
0004 // MS compatible compilers support #pragma once
0005 #if defined(_MSC_VER)
0006 # pragma once
0007 #endif
0008 
0009 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
0010 // basic_binary_oprimitive.hpp
0011 
0012 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
0013 // Use, modification and distribution is subject to the Boost Software
0014 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0015 // http://www.boost.org/LICENSE_1_0.txt)
0016 
0017 //  See http://www.boost.org for updates, documentation, and revision history.
0018 
0019 // archives stored as native binary - this should be the fastest way
0020 // to archive the state of a group of objects.  It makes no attempt to
0021 // convert to any canonical form.
0022 
0023 // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE
0024 // ON PLATFORM APART FROM THE ONE THEY ARE CREATE ON
0025 
0026 #include <iosfwd>
0027 #include <boost/assert.hpp>
0028 #include <locale>
0029 #include <streambuf> // basic_streambuf
0030 #include <string>
0031 #include <cstddef> // size_t
0032 
0033 #include <boost/config.hpp>
0034 #if defined(BOOST_NO_STDC_NAMESPACE)
0035 namespace std{
0036     using ::size_t;
0037 } // namespace std
0038 #endif
0039 
0040 #include <boost/cstdint.hpp>
0041 #include <boost/integer.hpp>
0042 #include <boost/integer_traits.hpp>
0043 #include <boost/scoped_ptr.hpp>
0044 #include <boost/serialization/throw_exception.hpp>
0045 
0046 #include <boost/serialization/is_bitwise_serializable.hpp>
0047 #include <boost/serialization/array_wrapper.hpp>
0048 
0049 #include <boost/archive/basic_streambuf_locale_saver.hpp>
0050 #include <boost/archive/codecvt_null.hpp>
0051 #include <boost/archive/archive_exception.hpp>
0052 #include <boost/archive/detail/auto_link_archive.hpp>
0053 #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
0054 
0055 namespace boost {
0056 namespace archive {
0057 
0058 /////////////////////////////////////////////////////////////////////////
0059 // class basic_binary_oprimitive - binary output of primitives
0060 
0061 template<class Archive, class Elem, class Tr>
0062 class BOOST_SYMBOL_VISIBLE basic_binary_oprimitive {
0063 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
0064     friend class save_access;
0065 protected:
0066 #else
0067 public:
0068 #endif
0069     std::basic_streambuf<Elem, Tr> & m_sb;
0070     // return a pointer to the most derived class
0071     Archive * This(){
0072         return static_cast<Archive *>(this);
0073     }
0074     #ifndef BOOST_NO_STD_LOCALE
0075     // note order! - if you change this, libstd++ will fail!
0076     // a) create new locale with new codecvt facet
0077     // b) save current locale
0078     // c) change locale to new one
0079     // d) use stream buffer
0080     // e) change locale back to original
0081     // f) destroy new codecvt facet
0082     boost::archive::codecvt_null<Elem> codecvt_null_facet;
0083     basic_streambuf_locale_saver<Elem, Tr> locale_saver;
0084     std::locale archive_locale;
0085     #endif
0086     // default saving of primitives.
0087     template<class T>
0088     void save(const T & t)
0089     {
0090         save_binary(& t, sizeof(T));
0091     }
0092 
0093     /////////////////////////////////////////////////////////
0094     // fundamental types that need special treatment
0095 
0096     // trap usage of invalid uninitialized boolean which would
0097     // otherwise crash on load.
0098     void save(const bool t){
0099         BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
0100         save_binary(& t, sizeof(t));
0101     }
0102     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
0103     save(const std::string &s);
0104     #ifndef BOOST_NO_STD_WSTRING
0105     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
0106     save(const std::wstring &ws);
0107     #endif
0108     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
0109     save(const char * t);
0110     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
0111     save(const wchar_t * t);
0112 
0113     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
0114     init();
0115 
0116     BOOST_ARCHIVE_OR_WARCHIVE_DECL
0117     basic_binary_oprimitive(
0118         std::basic_streambuf<Elem, Tr> & sb,
0119         bool no_codecvt
0120     );
0121     BOOST_ARCHIVE_OR_WARCHIVE_DECL
0122     ~basic_binary_oprimitive();
0123 public:
0124 
0125     // we provide an optimized save for all fundamental types
0126     // typedef serialization::is_bitwise_serializable<mpl::_1>
0127     // use_array_optimization;
0128     // workaround without using mpl lambdas
0129     struct use_array_optimization {
0130         template <class T>
0131         #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
0132             struct apply {
0133                 typedef typename boost::serialization::is_bitwise_serializable< T >::type type;
0134             };
0135         #else
0136             struct apply : public boost::serialization::is_bitwise_serializable< T > {};
0137         #endif
0138     };
0139 
0140     // the optimized save_array dispatches to save_binary
0141     template <class ValueType>
0142     void save_array(boost::serialization::array_wrapper<ValueType> const& a, unsigned int)
0143     {
0144       save_binary(a.address(),a.count()*sizeof(ValueType));
0145     }
0146 
0147     void save_binary(const void *address, std::size_t count);
0148 };
0149 
0150 template<class Archive, class Elem, class Tr>
0151 inline void
0152 basic_binary_oprimitive<Archive, Elem, Tr>::save_binary(
0153     const void *address,
0154     std::size_t count
0155 ){
0156     // BOOST_ASSERT(count <= std::size_t(boost::integer_traits<std::streamsize>::const_max));
0157     // note: if the following assertions fail
0158     // a likely cause is that the output stream is set to "text"
0159     // mode where by cr characters receive special treatment.
0160     // be sure that the output stream is opened with ios::binary
0161     //if(os.fail())
0162     //    boost::serialization::throw_exception(
0163     //        archive_exception(archive_exception::output_stream_error)
0164     //    );
0165     // figure number of elements to output - round up
0166     count = ( count + sizeof(Elem) - 1) / sizeof(Elem);
0167     std::streamsize scount = m_sb.sputn(
0168         static_cast<const Elem *>(address),
0169         static_cast<std::streamsize>(count)
0170     );
0171     if(count != static_cast<std::size_t>(scount))
0172         boost::serialization::throw_exception(
0173             archive_exception(archive_exception::output_stream_error)
0174         );
0175     //os.write(
0176     //    static_cast<const typename OStream::char_type *>(address),
0177     //    count
0178     //);
0179     //BOOST_ASSERT(os.good());
0180 }
0181 
0182 } //namespace boost
0183 } //namespace archive
0184 
0185 #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
0186 
0187 #endif // BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP