File indexing completed on 2025-01-18 09:38:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED
0012 #define BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED
0013
0014 #include <boost/iostreams/categories.hpp>
0015 #include <boost/iostreams/detail/dispatch.hpp>
0016 #include <boost/iostreams/detail/error.hpp>
0017 #include <boost/iostreams/detail/config/unreachable_return.hpp>
0018 #include <boost/iostreams/get.hpp>
0019 #include <boost/iostreams/put.hpp>
0020 #include <boost/iostreams/read.hpp>
0021 #include <boost/iostreams/seek.hpp>
0022 #include <boost/iostreams/traits.hpp>
0023 #include <boost/iostreams/write.hpp>
0024 #include <boost/throw_exception.hpp>
0025
0026
0027 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
0028
0029 namespace boost { namespace iostreams {
0030
0031 namespace detail {
0032
0033 template<typename T>
0034 struct read_write_if_impl;
0035
0036 template<typename T>
0037 struct seek_if_impl;
0038
0039 }
0040
0041 template<typename T>
0042 typename int_type_of<T>::type get_if(T& t)
0043 {
0044 typedef typename detail::dispatch<T, input, output>::type tag;
0045 return detail::read_write_if_impl<tag>::get(t);
0046 }
0047
0048 template<typename T>
0049 inline std::streamsize
0050 read_if(T& t, typename char_type_of<T>::type* s, std::streamsize n)
0051 {
0052 typedef typename detail::dispatch<T, input, output>::type tag;
0053 return detail::read_write_if_impl<tag>::read(t, s, n);
0054 }
0055
0056 template<typename T>
0057 bool put_if(T& t, typename char_type_of<T>::type c)
0058 {
0059 typedef typename detail::dispatch<T, output, input>::type tag;
0060 return detail::read_write_if_impl<tag>::put(t, c);
0061 }
0062
0063 template<typename T>
0064 inline std::streamsize write_if
0065 (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
0066 {
0067 typedef typename detail::dispatch<T, output, input>::type tag;
0068 return detail::read_write_if_impl<tag>::write(t, s, n);
0069 }
0070
0071 template<typename T>
0072 inline std::streampos
0073 seek_if( T& t, stream_offset off, BOOST_IOS::seekdir way,
0074 BOOST_IOS::openmode which = BOOST_IOS::in | BOOST_IOS::out )
0075 {
0076 using namespace detail;
0077 typedef typename dispatch<T, random_access, any_tag>::type tag;
0078 return seek_if_impl<tag>::seek(t, off, way, which);
0079 }
0080
0081 namespace detail {
0082
0083
0084
0085 template<>
0086 struct read_write_if_impl<input> {
0087 template<typename T>
0088 static typename int_type_of<T>::type get(T& t)
0089 { return iostreams::get(t); }
0090
0091 template<typename T>
0092 static std::streamsize
0093 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
0094 { return iostreams::read(t, s, n); }
0095
0096 template<typename T>
0097 static bool put(T&, typename char_type_of<T>::type)
0098 { boost::throw_exception(cant_write());
0099 BOOST_IOSTREAMS_UNREACHABLE_RETURN(false) }
0100
0101 template<typename T>
0102 static std::streamsize
0103 write(T&, const typename char_type_of<T>::type*, std::streamsize)
0104 { boost::throw_exception(cant_write());
0105 BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) }
0106 };
0107
0108 template<>
0109 struct read_write_if_impl<output> {
0110 template<typename T>
0111 static typename int_type_of<T>::type get(T&)
0112 { boost::throw_exception(cant_read());
0113 BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) }
0114
0115 template<typename T>
0116 static std::streamsize
0117 read(T&, typename char_type_of<T>::type*, std::streamsize)
0118 { boost::throw_exception(cant_read());
0119 BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) }
0120
0121 template<typename T>
0122 static bool put(T& t, typename char_type_of<T>::type c)
0123 { return iostreams::put(t, c); }
0124
0125 template<typename T>
0126 static std::streamsize
0127 write( T& t, const typename char_type_of<T>::type* s,
0128 std::streamsize n )
0129 { return iostreams::write(t, s, n); }
0130 };
0131
0132
0133
0134 template<>
0135 struct seek_if_impl<random_access> {
0136 template<typename T>
0137 static std::streampos
0138 seek( T& t, stream_offset off, BOOST_IOS::seekdir way,
0139 BOOST_IOS::openmode which )
0140 { return iostreams::seek(t, off, way, which); }
0141 };
0142
0143 template<>
0144 struct seek_if_impl<any_tag> {
0145 template<typename T>
0146 static std::streampos
0147 seek(T&, stream_offset, BOOST_IOS::seekdir, BOOST_IOS::openmode)
0148 { boost::throw_exception(cant_seek());
0149 BOOST_IOSTREAMS_UNREACHABLE_RETURN(std::streampos()) }
0150 };
0151
0152 }
0153
0154 } }
0155
0156 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
0157
0158 #endif