#ifndef PORTABLE_OARCHIVE_H_ #define PORTABLE_OARCHIVE_H_ /* Copyright (c) 2007 Andrew Molloy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include "../Endianness.h" namespace BeeLib { namespace archive { template class portable_oarchive : public boost::archive::binary_oarchive_impl > { typedef portable_oarchive derived_t; #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS public: #else friend class boost::archive::basic_binary_oarchive; friend class boost::archive::basic_binary_oprimitive; friend class boost::archive::save_access; #endif // add base class to the places considered when matching // save function to a specific set of arguments. Note, this didn't // work on my MSVC 7.0 system so we use the sure-fire method below // using binary_oarchive_impl::save; // default fall through for any types not specified here template void save(const T & t) { boost::archive::binary_oarchive_impl::save(t); } // handle integer types void save(int16 t) { t = swapBytes(t); this->save_binary(&t, sizeof(int16)); } void save(uint16 t) { t = swapBytes(t); this->save_binary(&t, sizeof(uint16)); } void save(int32 t) { t = swapBytes(t); this->save_binary(&t, sizeof(int32)); } void save(uint32 t) { t = swapBytes(t); this->save_binary(&t, sizeof(uint32)); } void save(int64 t) { t = swapBytes(t); this->save_binary(&t, sizeof(int64)); } void save(uint64 t) { t = swapBytes(t); this->save_binary(&t, sizeof(uint64)); } // floating point numbers - we just do byte swapping, we don't even try to perform any // conversion between different floating point representations. This is okay, because // every platform we're currently interested in uses IEEE 754 floating point representation. void save(float t) { BOOST_STATIC_ASSERT(sizeof(float) == 4); t = swapBytes(t); this->save_binary(&t, sizeof(float)); } void save(double t) { BOOST_STATIC_ASSERT(sizeof(double) == 8); t = swapBytes(t); this->save_binary(&t, sizeof(double)); } public: portable_oarchive(std::ostream & os, unsigned flags = 0) : boost::archive::binary_oarchive_impl( os, flags | boost::archive::no_header // skip default header checking ) { // use our own header checking if(0 != (flags & boost::archive::no_header)) { this->boost::archive::basic_binary_oarchive::init(); // skip the following for "portable" binary archives // boost::archive::basic_binary_iprimitive::init(); } } }; } } // namespace BeeLib #include #include #include namespace boost { namespace archive { // explicitly instantiate for this type of binary stream // Big Endian Data template class basic_binary_oarchive > ; template class basic_binary_oprimitive, std::ostream> ; template class binary_oarchive_impl > ; template class detail::archive_pointer_oserializer > ; // Little Endian data template class basic_binary_oarchive > ; template class basic_binary_oprimitive, std::ostream> ; template class binary_oarchive_impl > ; template class detail::archive_pointer_oserializer > ; } // namespace archive } // namespace boost #define BOOST_ARCHIVE_CUSTOM_OARCHIVE_TYPES portable_oarchive #endif /*PORTABLE_OARCHIVE_H_*/