1 // boost/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 documentation at http://www.boost.org/libs/utility
10 #ifndef BOOST_BINARY_STREAM_HPP
11 #define BOOST_BINARY_STREAM_HPP
13 #include <boost/config.hpp>
17 #include <cstring> // for strlen
19 #ifndef BOOST_NO_CWCHAR
20 # include <cwchar> // for wcslen
23 // unformatted binary (as opposed to formatted character-set) input and output
25 // Caution: Use only on streams opened with filemode std::ios_base::binary. Thus
26 // unformatted binary I/O should not be with the standard streams (cout, cin, etc.)
27 // since they are opened in text mode. Use on text streams may produce incorrect
28 // results, such as insertion of unwanted characters or premature end-of-file.
29 // For example, on Windows 0x0D would become 0x0D, 0x0A.
31 // Caution: When mixing formatted (i.e. operator << or >>) and unformatted (i.e.
32 // operator <= or >=) be aware that << and >> take precedence over <= and >=. Use
33 // parentheses to force correct order of evaluation. For example:
35 // my_stream << foo <= bar; // no parentheses needed
36 // (my_stream <= foo) << bar; // parentheses required
38 // This implementation uses reinterpret_cast<>() when needed to convert one pointer
39 // type to another. See 5.2.10 [expr.reinterpret.cast], Reinterpret cast, para 7.
44 // built-in types ------------------------------------------------------------------//
46 // omission of bool and void* is deliberate; any semantics would be questionable
48 inline std::ostream
& operator<=(std::ostream
& os
, short v
)
49 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
50 inline std::istream
& operator>=(std::istream
& is
, short& v
)
51 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
53 inline std::ostream
& operator<=(std::ostream
& os
, unsigned short v
)
54 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
55 inline std::istream
& operator>=(std::istream
& is
, unsigned short& v
)
56 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
58 inline std::ostream
& operator<=(std::ostream
& os
, int v
)
59 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
60 inline std::istream
& operator>=(std::istream
& is
, int& v
)
61 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
63 inline std::ostream
& operator<=(std::ostream
& os
, unsigned int v
)
64 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
65 inline std::istream
& operator>=(std::istream
& is
, unsigned int& v
)
66 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
68 inline std::ostream
& operator<=(std::ostream
& os
, long v
)
69 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
70 inline std::istream
& operator>=(std::istream
& is
, long& v
)
71 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
73 inline std::ostream
& operator<=(std::ostream
& os
, unsigned long v
)
74 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
75 inline std::istream
& operator>=(std::istream
& is
, unsigned long& v
)
76 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
78 inline std::ostream
& operator<=(std::ostream
& os
, long long v
)
79 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
80 inline std::istream
& operator>=(std::istream
& is
, long long& v
)
81 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
83 inline std::ostream
& operator<=(std::ostream
& os
, unsigned long long v
)
84 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
85 inline std::istream
& operator>=(std::istream
& is
, unsigned long long& v
)
86 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
88 inline std::ostream
& operator<=(std::ostream
& os
, float v
)
89 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
90 inline std::istream
& operator>=(std::istream
& is
, float& v
)
91 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
93 inline std::ostream
& operator<=(std::ostream
& os
, double v
)
94 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
95 inline std::istream
& operator>=(std::istream
& is
, double& v
)
96 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
98 inline std::ostream
& operator<=(std::ostream
& os
, long double v
)
99 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
100 inline std::istream
& operator>=(std::istream
& is
, long double& v
)
101 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
103 inline std::ostream
& operator<=(std::ostream
& os
, char c
)
104 { return os
.put( c
); }
105 inline std::istream
& operator>=(std::istream
& is
, char& c
)
106 { return is
.get( c
); }
108 inline std::ostream
& operator<=(std::ostream
& os
, signed char c
)
109 { return os
.put( c
); }
110 inline std::istream
& operator>=(std::istream
& is
, signed char& c
)
111 { return is
.get( reinterpret_cast<char&>(c
) ); }
113 inline std::ostream
& operator<=(std::ostream
& os
, unsigned char c
)
114 { return os
.put( c
); }
115 inline std::istream
& operator>=(std::istream
& is
, unsigned char& c
)
116 { return is
.get( reinterpret_cast<char&>(c
) ); }
118 inline std::ostream
& operator<=(std::ostream
& os
, wchar_t v
)
119 { return os
.write( reinterpret_cast<const char*>(&v
), sizeof(v
) ); }
120 inline std::istream
& operator>=(std::istream
& is
, wchar_t& v
)
121 { return is
.read( reinterpret_cast<char*>(&v
), sizeof(v
) ); }
123 // strings -------------------------------------------------------------------------//
125 inline std::ostream
& operator<=(std::ostream
& os
, const char* p
)
126 { return os
.write( p
, std::strlen(p
)+1 ); }
128 inline std::ostream
& operator<=(std::ostream
& os
, const signed char* p
)
129 { return os
.write( reinterpret_cast<const char*>(p
), std::strlen(reinterpret_cast<const char*>(p
))+1 ); }
131 inline std::ostream
& operator<=(std::ostream
& os
, const unsigned char* p
)
132 { return os
.write( reinterpret_cast<const char*>(p
), std::strlen(reinterpret_cast<const char*>(p
))+1 ); }
134 #ifndef BOOST_NO_CWCHAR
135 inline std::ostream
& operator<=(std::ostream
& os
, const wchar_t* p
)
136 { return os
.write( reinterpret_cast<const char*>(p
), (std::wcslen(p
)+1)*sizeof(wchar_t) ); }
139 // Caution: note the asymmetry between output and input; a string with embedded
140 // nulls will be output with the embedded nulls, but input will stop at the first null.
141 // So it probably isn't a good idea to use these functions for strings with nulls.
142 inline std::ostream
& operator<=(std::ostream
& os
, const std::string
& s
)
143 { return os
.write( s
.c_str(), s
.size()+1 ); }
144 inline std::istream
& operator>=(std::istream
& is
, std::string
& s
)
145 { return getline(is
, s
, '\0'); }
147 #ifndef BOOST_NO_STD_WSTRING
148 inline std::ostream
& operator<=(std::ostream
& os
, const std::wstring
& s
)
149 { return os
.write( reinterpret_cast<const char*>(s
.c_str()), (s
.size()+1)*sizeof(wchar_t) ); }
150 // TODO: provide input function
155 #endif // BOOST_BINARY_STREAM_HPP