// boost/integer/endian_binary_stream.hpp --------------------------------------------// // Copyright Beman Dawes 2009 // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt // See library home page at http://www.boost.org/libs/endian #ifndef BOOST_ENDIAN_BINARY_STREAM_HPP #define BOOST_ENDIAN_BINARY_STREAM_HPP #include #include #include #include // unformatted binary (as opposed to formatted character) stream insertion // and extraction. // Warning: Use only on streams opened with filemode std::ios_base::binary. Thus // unformatted binary I/O should not be with the standard streams (cout, cin, etc.) // since they are opened in text mode. Use on text streams may produce incorrect // results, such as insertion of unwanted characters or premature end-of-file. // For example, on Windows 0x0D would become 0x0D, 0x0A. // Caution: When mixing formatted (i.e. operator << or >>) and unformatted (i.e. // operator <= or >=) be aware that << and >> take precedence over <= and >=. Use // parentheses to force correct order of evaluation. For example: // // my_stream << foo <= bar; // no parentheses needed // (my_stream <= foo) << bar; // parentheses required // This implementation uses reinterpret_cast<>() when needed to convert one pointer // type to another. See 5.2.10 [expr.reinterpret.cast], Reinterpret cast, para 7. namespace boost { namespace integer { template< class T > struct is_endian { static const bool value = false; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; # if defined(BOOST_HAS_INT16_T) template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; # endif # if defined(BOOST_HAS_INT32_T) template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; # endif # if defined(BOOST_HAS_INT64_T) template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; template<> struct is_endian { static const bool value = true; }; # endif template < class Endian > inline typename boost::enable_if< is_endian, std::ostream & >::type operator<=( std::ostream & os, const Endian & e ) { return os.write( e.data(), sizeof(e) ); } template < class Endian > inline typename boost::enable_if< is_endian, std::istream & >::type operator>=( std::istream & is, Endian & e ) { return is.read( reinterpret_cast(&e), sizeof(e) ); } } // namespace integer } // namespace boost #endif // BOOST_ENDIAN_BINARY_STREAM_HPP