| 1 | #ifndef PORTABLE_BINARY_OARCHIVE_HPP |
| 2 | #define PORTABLE_BINARY_OARCHIVE_HPP |
| 3 | |
| 4 | // MS compatible compilers support #pragma once |
| 5 | #if defined(_MSC_VER) |
| 6 | # pragma once |
| 7 | #endif |
| 8 | |
| 9 | #if defined(_MSC_VER) |
| 10 | #pragma warning( push ) |
| 11 | #pragma warning( disable : 4244 ) |
| 12 | #endif |
| 13 | |
| 14 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 15 | // portable_binary_oarchive.hpp |
| 16 | |
| 17 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
| 18 | // Use, modification and distribution is subject to the Boost Software |
| 19 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 20 | // http://www.boost.org/LICENSE_1_0.txt) |
| 21 | |
| 22 | // See http://www.boost.org for updates, documentation, and revision history. |
| 23 | |
| 24 | #include <ostream> |
| 25 | #include <boost/version.hpp> |
| 26 | #include <boost/serialization/string.hpp> |
| 27 | #include <boost/archive/archive_exception.hpp> |
| 28 | #include <boost/archive/basic_binary_oprimitive.hpp> |
| 29 | #include <boost/archive/detail/common_oarchive.hpp> |
| 30 | #include <boost/archive/detail/register_archive.hpp> |
| 31 | |
| 32 | #include <boost/archive/portable_binary_archive.hpp> |
| 33 | #include <boost/archive/impl/basic_binary_oprimitive.ipp> |
| 34 | |
| 35 | namespace boost { namespace archive { |
| 36 | |
| 37 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 38 | // exception to be thrown if integer read from archive doesn't fit |
| 39 | // variable being loaded |
| 40 | class portable_binary_oarchive_exception : |
| 41 | public boost::archive::archive_exception |
| 42 | { |
| 43 | public: |
| 44 | enum exception_code { |
| 45 | invalid_flags |
| 46 | } m_exception_code ; |
| 47 | portable_binary_oarchive_exception(exception_code c = invalid_flags ) : |
| 48 | boost::archive::archive_exception(boost::archive::archive_exception::other_exception), |
| 49 | m_exception_code(c) |
| 50 | {} |
| 51 | virtual const char *what( ) const throw( ) |
| 52 | { |
| 53 | const char *msg = "programmer error"; |
| 54 | switch(m_exception_code){ |
| 55 | case invalid_flags: |
| 56 | msg = "cannot be both big and little endian"; |
| 57 | break; |
| 58 | default: |
| 59 | msg = boost::archive::archive_exception::what(); |
| 60 | assert(false); |
| 61 | break; |
| 62 | } |
| 63 | return msg; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 68 | // "Portable" output binary archive. This is a variation of the native binary |
| 69 | // archive. it addresses integer size and endienness so that binary archives can |
| 70 | // be passed across systems. Note:floating point types not addressed here |
| 71 | |
| 72 | class portable_binary_oarchive : |
| 73 | public boost::archive::basic_binary_oprimitive< |
| 74 | portable_binary_oarchive, |
| 75 | std::ostream::char_type, |
| 76 | std::ostream::traits_type |
| 77 | >, |
| 78 | public boost::archive::detail::common_oarchive< |
| 79 | portable_binary_oarchive |
| 80 | > |
| 81 | { |
| 82 | typedef boost::archive::basic_binary_oprimitive< |
| 83 | portable_binary_oarchive, |
| 84 | std::ostream::char_type, |
| 85 | std::ostream::traits_type |
| 86 | > primitive_base_t; |
| 87 | typedef boost::archive::detail::common_oarchive< |
| 88 | portable_binary_oarchive |
| 89 | > archive_base_t; |
| 90 | #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
| 91 | public: |
| 92 | #else |
| 93 | friend archive_base_t; |
| 94 | friend primitive_base_t; // since with override save below |
| 95 | friend class boost::archive::detail::interface_oarchive< |
| 96 | portable_binary_oarchive |
| 97 | >; |
| 98 | friend class boost::archive::save_access; |
| 99 | protected: |
| 100 | #endif |
| 101 | unsigned int m_flags; |
| 102 | void save_impl(const boost::intmax_t l, const char maxsize); |
| 103 | // add base class to the places considered when matching |
| 104 | // save function to a specific set of arguments. Note, this didn't |
| 105 | // work on my MSVC 7.0 system so we use the sure-fire method below |
| 106 | // using archive_base_t::save; |
| 107 | |
| 108 | // default fall through for any types not specified here |
| 109 | template<class T> |
| 110 | void save(const T & t){ |
| 111 | save_impl(t, sizeof(T)); |
| 112 | } |
| 113 | void save(const std::string & t){ |
| 114 | this->primitive_base_t::save(t); |
| 115 | } |
| 116 | #ifndef BOOST_NO_STD_WSTRING |
| 117 | void save(const std::wstring & t){ |
| 118 | this->primitive_base_t::save(t); |
| 119 | } |
| 120 | #endif |
| 121 | void save(const float & t){ |
| 122 | this->primitive_base_t::save(t); |
| 123 | // floats not supported |
| 124 | //BOOST_STATIC_ASSERT(false); |
| 125 | } |
| 126 | void save(const double & t){ |
| 127 | this->primitive_base_t::save(t); |
| 128 | // doubles not supported |
| 129 | //BOOST_STATIC_ASSERT(false); |
| 130 | } |
| 131 | void save(const char & t){ |
| 132 | this->primitive_base_t::save(t); |
| 133 | } |
| 134 | void save(const unsigned char & t){ |
| 135 | this->primitive_base_t::save(t); |
| 136 | } |
| 137 | |
| 138 | // default processing - kick back to base class. Note the |
| 139 | // extra stuff to get it passed borland compilers |
| 140 | typedef boost::archive::detail::common_oarchive<portable_binary_oarchive> |
| 141 | detail_common_oarchive; |
| 142 | #if BOOST_VERSION > 105800 |
| 143 | template<class T> |
| 144 | void save_override(T & t){ |
| 145 | this->detail_common_oarchive::save_override(t); |
| 146 | } |
| 147 | // explicitly convert to char * to avoid compile ambiguities |
| 148 | void save_override(const boost::archive::class_name_type & t){ |
| 149 | const std::string s(t); |
| 150 | * this << s; |
| 151 | } |
| 152 | // binary files don't include the optional information |
| 153 | void save_override( |
| 154 | const boost::archive::class_id_optional_type & /* t */ |
| 155 | ){} |
| 156 | #else |
| 157 | template<class T> |
| 158 | void save_override(T & t, int){ |
| 159 | this->detail_common_oarchive::save_override(t, 0); |
| 160 | } |
| 161 | // explicitly convert to char * to avoid compile ambiguities |
| 162 | void save_override(const boost::archive::class_name_type & t, int){ |
| 163 | const std::string s(t); |
| 164 | * this << s; |
| 165 | } |
| 166 | // binary files don't include the optional information |
| 167 | void save_override( |
| 168 | const boost::archive::class_id_optional_type & /* t */, int |
| 169 | ){} |
| 170 | #endif |
| 171 | |
| 172 | void init(unsigned int flags); |
| 173 | public: |
| 174 | portable_binary_oarchive(std::ostream & os, unsigned flags = endian_little) : |
| 175 | primitive_base_t( |
| 176 | * os.rdbuf(), |
| 177 | 0 != (flags & boost::archive::no_codecvt) |
| 178 | ), |
| 179 | archive_base_t(flags), |
| 180 | m_flags(flags & (endian_big | endian_little)) |
| 181 | { |
| 182 | init(flags); |
| 183 | } |
| 184 | |
| 185 | portable_binary_oarchive( |
| 186 | std::basic_streambuf< |
| 187 | std::ostream::char_type, |
| 188 | std::ostream::traits_type |
| 189 | > & bsb, |
| 190 | unsigned int flags |
| 191 | ) : |
| 192 | primitive_base_t( |
| 193 | bsb, |
| 194 | 0 != (flags & boost::archive::no_codecvt) |
| 195 | ), |
| 196 | archive_base_t(flags), |
| 197 | m_flags(0) |
| 198 | { |
| 199 | init(flags); |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | } } |
| 204 | |
| 205 | // required by export in boost version > 1.34 |
| 206 | #ifdef BOOST_SERIALIZATION_REGISTER_ARCHIVE |
| 207 | BOOST_SERIALIZATION_REGISTER_ARCHIVE(portable_binary_oarchive) |
| 208 | #endif |
| 209 | |
| 210 | // required by export in boost <= 1.34 |
| 211 | #define BOOST_ARCHIVE_CUSTOM_OARCHIVE_TYPES portable_binary_oarchive |
| 212 | |
| 213 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 214 | // portable_binary_oarchive.cpp |
| 215 | |
| 216 | // (C) Copyright 2002-7 Robert Ramey - http://www.rrsd.com . |
| 217 | // Use, modification and distribution is subject to the Boost Software |
| 218 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 219 | // http://www.boost.org/LICENSE_1_0.txt) |
| 220 | |
| 221 | // See http://www.boost.org for updates, documentation, and revision history. |
| 222 | |
| 223 | #include <ostream> |
| 224 | #include <boost/predef/other/endian.h> |
| 225 | |
| 226 | namespace boost { namespace archive { |
| 227 | |
| 228 | inline void |
| 229 | portable_binary_oarchive::save_impl( |
| 230 | const boost::intmax_t l, |
| 231 | const char maxsize |
| 232 | ){ |
| 233 | signed char size = 0; |
| 234 | |
| 235 | if(l == 0){ |
| 236 | this->primitive_base_t::save(size); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | boost::intmax_t ll; |
| 241 | bool negative = (l < 0); |
| 242 | if(negative) |
| 243 | ll = -l; |
| 244 | else |
| 245 | ll = l; |
| 246 | |
| 247 | do{ |
| 248 | ll >>= CHAR_BIT; |
| 249 | ++size; |
| 250 | }while(ll != 0); |
| 251 | |
| 252 | this->primitive_base_t::save( |
| 253 | static_cast<signed char>(negative ? -size : size) |
| 254 | ); |
| 255 | |
| 256 | if(negative) |
| 257 | ll = -l; |
| 258 | else |
| 259 | ll = l; |
| 260 | char * cptr = reinterpret_cast<char *>(& ll); |
| 261 | #if BOOST_ENDIAN_BIG_BYTE |
| 262 | cptr += (sizeof(boost::intmax_t) - size); |
| 263 | if(m_flags & endian_little) |
| 264 | reverse_bytes(size, cptr); |
| 265 | #else |
| 266 | if(m_flags & endian_big) |
| 267 | reverse_bytes(size, cptr); |
| 268 | #endif |
| 269 | this->primitive_base_t::save_binary(cptr, size); |
| 270 | } |
| 271 | |
| 272 | inline void |
| 273 | portable_binary_oarchive::init(unsigned int flags) { |
| 274 | if(m_flags == (endian_big | endian_little)){ |
| 275 | boost::serialization::throw_exception( |
| 276 | portable_binary_oarchive_exception() |
| 277 | ); |
| 278 | } |
| 279 | if(0 == (flags & boost::archive::no_header)){ |
| 280 | // write signature in an archive version independent manner |
| 281 | const std::string file_signature( |
| 282 | boost::archive::BOOST_ARCHIVE_SIGNATURE() |
| 283 | ); |
| 284 | * this << file_signature; |
| 285 | // ignore archive version checking |
| 286 | const boost::archive::library_version_type v{}; |
| 287 | /* |
| 288 | // write library version |
| 289 | const boost::archive::library_version_type v( |
| 290 | boost::archive::BOOST_ARCHIVE_VERSION() |
| 291 | ); |
| 292 | */ |
| 293 | * this << v; |
| 294 | } |
| 295 | save(static_cast<unsigned char>(m_flags >> CHAR_BIT)); |
| 296 | } |
| 297 | |
| 298 | } } |
| 299 | |
| 300 | namespace boost { |
| 301 | namespace archive { |
| 302 | |
| 303 | namespace detail { |
| 304 | template class archive_serializer_map<portable_binary_oarchive>; |
| 305 | } |
| 306 | |
| 307 | // template class basic_binary_oprimitive< |
| 308 | // portable_binary_oarchive, |
| 309 | // std::ostream::char_type, |
| 310 | // std::ostream::traits_type |
| 311 | // > ; |
| 312 | |
| 313 | } // namespace archive |
| 314 | } // namespace boost |
| 315 | |
| 316 | #if defined(_MSC_VER) |
| 317 | #pragma warning( pop ) |
| 318 | #endif |
| 319 | |
| 320 | #endif // PORTABLE_BINARY_OARCHIVE_HPP |