2009-03-14 01:36:19 +00:00
|
|
|
// boost/binary_stream.hpp ----------------------------------------------------------//
|
|
|
|
|
|
2011-04-22 20:34:30 +00:00
|
|
|
// Copyright Beman Dawes 2009, 2011
|
2009-03-14 01:36:19 +00:00
|
|
|
|
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
|
// See http://www.boost.org/LICENSE_1_0.txt
|
|
|
|
|
|
|
|
|
|
// See documentation at http://www.boost.org/libs/utility
|
|
|
|
|
|
|
|
|
|
#ifndef BOOST_BINARY_STREAM_HPP
|
|
|
|
|
#define BOOST_BINARY_STREAM_HPP
|
|
|
|
|
|
2009-03-20 13:40:24 +00:00
|
|
|
#include <boost/config.hpp>
|
2009-03-14 01:36:19 +00:00
|
|
|
#include <ostream>
|
|
|
|
|
#include <istream>
|
2009-03-14 13:54:35 +00:00
|
|
|
#include <string>
|
|
|
|
|
#include <cstring> // for strlen
|
2009-03-20 13:40:24 +00:00
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_CWCHAR
|
|
|
|
|
# include <cwchar> // for wcslen
|
|
|
|
|
#endif
|
2009-03-14 01:36:19 +00:00
|
|
|
|
2011-04-22 20:34:30 +00:00
|
|
|
// unformatted binary (as opposed to formatted character) input and output
|
2009-03-15 13:28:43 +00:00
|
|
|
|
|
|
|
|
// Caution: 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.
|
|
|
|
|
|
2009-03-14 01:36:19 +00:00
|
|
|
namespace boost
|
|
|
|
|
{
|
|
|
|
|
|
2011-04-22 20:34:30 +00:00
|
|
|
namespace detail
|
|
|
|
|
{
|
|
|
|
|
template <class T>
|
|
|
|
|
struct const_binary_data
|
|
|
|
|
{
|
|
|
|
|
const char* ptr;
|
|
|
|
|
explicit const_binary_data(const T& x) : ptr(reinterpret_cast<const char*>(&x)) {}
|
|
|
|
|
};
|
|
|
|
|
template <class T>
|
|
|
|
|
struct binary_data
|
|
|
|
|
{
|
|
|
|
|
char* ptr;
|
|
|
|
|
explicit binary_data(T& x) : ptr(reinterpret_cast<char*>(&x)) {}
|
|
|
|
|
};
|
|
|
|
|
}
|
2009-03-14 13:54:35 +00:00
|
|
|
|
2011-04-22 20:34:30 +00:00
|
|
|
template <class T>
|
|
|
|
|
inline detail::const_binary_data<T> bin(const T& x)
|
|
|
|
|
{
|
|
|
|
|
return detail::const_binary_data<T>(x);
|
|
|
|
|
}
|
2009-03-14 01:36:19 +00:00
|
|
|
|
2011-04-22 20:34:30 +00:00
|
|
|
template <class T>
|
|
|
|
|
inline detail::binary_data<T> bin(T& x)
|
|
|
|
|
{
|
|
|
|
|
return detail::binary_data<T>(x);
|
|
|
|
|
}
|
2009-03-14 13:54:35 +00:00
|
|
|
|
2011-04-22 20:34:30 +00:00
|
|
|
template <class T>
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& os, detail::const_binary_data<T> x)
|
|
|
|
|
{
|
|
|
|
|
return os.write(x.ptr, sizeof(T));
|
|
|
|
|
}
|
2009-03-14 13:54:35 +00:00
|
|
|
|
2011-04-22 20:34:30 +00:00
|
|
|
template <class T>
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& os, detail::binary_data<T> x)
|
|
|
|
|
{
|
|
|
|
|
return os.write(x.ptr, sizeof(T));
|
|
|
|
|
}
|
2009-03-14 13:54:35 +00:00
|
|
|
|
2011-04-22 20:34:30 +00:00
|
|
|
template <class T>
|
|
|
|
|
inline std::istream& operator>>(std::istream& is, detail::binary_data<T> x)
|
|
|
|
|
{
|
|
|
|
|
return is.read(x.ptr, sizeof(T));
|
|
|
|
|
}
|
2009-03-14 01:36:19 +00:00
|
|
|
|
|
|
|
|
} // namespace boost
|
|
|
|
|
|
|
|
|
|
#endif // BOOST_BINARY_STREAM_HPP
|