1 // boost/integer/endian_binary_stream.hpp --------------------------------------------//
3 // Copyright Beman Dawes 2009
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
8 // See library home page at http://www.boost.org/libs/endian
10 #ifndef BOOST_ENDIAN_BINARY_STREAM_HPP
11 #define BOOST_ENDIAN_BINARY_STREAM_HPP
13 #include <boost/integer/endian.hpp>
14 #include <boost/utility/enable_if.hpp>
18 // unformatted binary (as opposed to formatted character) stream insertion
21 // Warning: Use only on streams opened with filemode std::ios_base::binary. Thus
22 // unformatted binary I/O should not be with the standard streams (cout, cin, etc.)
23 // since they are opened in text mode. Use on text streams may produce incorrect
24 // results, such as insertion of unwanted characters or premature end-of-file.
25 // For example, on Windows 0x0D would become 0x0D, 0x0A.
27 // Caution: When mixing formatted (i.e. operator << or >>) and unformatted (i.e.
28 // operator <= or >=) be aware that << and >> take precedence over <= and >=. Use
29 // parentheses to force correct order of evaluation. For example:
31 // my_stream << foo <= bar; // no parentheses needed
32 // (my_stream <= foo) << bar; // parentheses required
34 // This implementation uses reinterpret_cast<>() when needed to convert one pointer
35 // type to another. See 5.2.10 [expr.reinterpret.cast], Reinterpret cast, para 7.
41 template< class T
> struct is_endian
{ static const bool value
= false; };
43 template<> struct is_endian
<big8_t
> { static const bool value
= true; };
44 template<> struct is_endian
<big16_t
> { static const bool value
= true; };
45 template<> struct is_endian
<big24_t
> { static const bool value
= true; };
46 template<> struct is_endian
<big32_t
> { static const bool value
= true; };
47 template<> struct is_endian
<big40_t
> { static const bool value
= true; };
48 template<> struct is_endian
<big48_t
> { static const bool value
= true; };
49 template<> struct is_endian
<big56_t
> { static const bool value
= true; };
50 template<> struct is_endian
<big64_t
> { static const bool value
= true; };
52 template<> struct is_endian
<ubig8_t
> { static const bool value
= true; };
53 template<> struct is_endian
<ubig16_t
> { static const bool value
= true; };
54 template<> struct is_endian
<ubig24_t
> { static const bool value
= true; };
55 template<> struct is_endian
<ubig32_t
> { static const bool value
= true; };
56 template<> struct is_endian
<ubig40_t
> { static const bool value
= true; };
57 template<> struct is_endian
<ubig48_t
> { static const bool value
= true; };
58 template<> struct is_endian
<ubig56_t
> { static const bool value
= true; };
59 template<> struct is_endian
<ubig64_t
> { static const bool value
= true; };
61 template<> struct is_endian
<little8_t
> { static const bool value
= true; };
62 template<> struct is_endian
<little16_t
> { static const bool value
= true; };
63 template<> struct is_endian
<little24_t
> { static const bool value
= true; };
64 template<> struct is_endian
<little32_t
> { static const bool value
= true; };
65 template<> struct is_endian
<little40_t
> { static const bool value
= true; };
66 template<> struct is_endian
<little48_t
> { static const bool value
= true; };
67 template<> struct is_endian
<little56_t
> { static const bool value
= true; };
68 template<> struct is_endian
<little64_t
> { static const bool value
= true; };
70 template<> struct is_endian
<ulittle8_t
> { static const bool value
= true; };
71 template<> struct is_endian
<ulittle16_t
> { static const bool value
= true; };
72 template<> struct is_endian
<ulittle24_t
> { static const bool value
= true; };
73 template<> struct is_endian
<ulittle32_t
> { static const bool value
= true; };
74 template<> struct is_endian
<ulittle40_t
> { static const bool value
= true; };
75 template<> struct is_endian
<ulittle48_t
> { static const bool value
= true; };
76 template<> struct is_endian
<ulittle56_t
> { static const bool value
= true; };
77 template<> struct is_endian
<ulittle64_t
> { static const bool value
= true; };
79 template<> struct is_endian
<native8_t
> { static const bool value
= true; };
80 template<> struct is_endian
<native16_t
> { static const bool value
= true; };
81 template<> struct is_endian
<native24_t
> { static const bool value
= true; };
82 template<> struct is_endian
<native32_t
> { static const bool value
= true; };
83 template<> struct is_endian
<native40_t
> { static const bool value
= true; };
84 template<> struct is_endian
<native48_t
> { static const bool value
= true; };
85 template<> struct is_endian
<native56_t
> { static const bool value
= true; };
86 template<> struct is_endian
<native64_t
> { static const bool value
= true; };
88 template<> struct is_endian
<unative8_t
> { static const bool value
= true; };
89 template<> struct is_endian
<unative16_t
> { static const bool value
= true; };
90 template<> struct is_endian
<unative24_t
> { static const bool value
= true; };
91 template<> struct is_endian
<unative32_t
> { static const bool value
= true; };
92 template<> struct is_endian
<unative40_t
> { static const bool value
= true; };
93 template<> struct is_endian
<unative48_t
> { static const bool value
= true; };
94 template<> struct is_endian
<unative56_t
> { static const bool value
= true; };
95 template<> struct is_endian
<unative64_t
> { static const bool value
= true; };
97 # if defined(BOOST_HAS_INT16_T)
98 template<> struct is_endian
<aligned_big16_t
> { static const bool value
= true; };
99 template<> struct is_endian
<aligned_ubig16_t
> { static const bool value
= true; };
100 template<> struct is_endian
<aligned_little16_t
> { static const bool value
= true; };
101 template<> struct is_endian
<aligned_ulittle16_t
> { static const bool value
= true; };
104 # if defined(BOOST_HAS_INT32_T)
105 template<> struct is_endian
<aligned_big32_t
> { static const bool value
= true; };
106 template<> struct is_endian
<aligned_ubig32_t
> { static const bool value
= true; };
107 template<> struct is_endian
<aligned_little32_t
> { static const bool value
= true; };
108 template<> struct is_endian
<aligned_ulittle32_t
> { static const bool value
= true; };
111 # if defined(BOOST_HAS_INT64_T)
112 template<> struct is_endian
<aligned_big64_t
> { static const bool value
= true; };
113 template<> struct is_endian
<aligned_ubig64_t
> { static const bool value
= true; };
114 template<> struct is_endian
<aligned_little64_t
> { static const bool value
= true; };
115 template<> struct is_endian
<aligned_ulittle64_t
> { static const bool value
= true; };
118 template < class Endian
>
119 inline typename
boost::enable_if
< is_endian
<Endian
>, std::ostream
& >::type
120 operator<=( std::ostream
& os
, const Endian
& e
)
122 return os
.write( reinterpret_cast<const char*>(&e
), sizeof(e
) );
125 template < class Endian
>
126 inline typename
boost::enable_if
< is_endian
<Endian
>, std::istream
& >::type
127 operator>=( std::istream
& is
, Endian
& e
)
129 return is
.read( reinterpret_cast<char*>(&e
), sizeof(e
) );
132 } // namespace integer
135 #endif // BOOST_ENDIAN_BINARY_STREAM_HPP