| 1 | // (C) Copyright Jonathan Turkanis 2005. |
|---|
| 2 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
|---|
| 3 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) |
|---|
| 4 | |
|---|
| 5 | // See http://www.boost.org/libs/iostreams for documentation. |
|---|
| 6 | |
|---|
| 7 | // Contains implementations of get, read, put, write and seek which |
|---|
| 8 | // check a device's mode at runtime instead of compile time. |
|---|
| 9 | |
|---|
| 10 | #ifndef BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED |
|---|
| 11 | #define BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED |
|---|
| 12 | |
|---|
| 13 | #include <boost/iostreams/categories.hpp> |
|---|
| 14 | #include <boost/iostreams/detail/dispatch.hpp> |
|---|
| 15 | #include <boost/iostreams/detail/error.hpp> |
|---|
| 16 | #include <boost/iostreams/get.hpp> |
|---|
| 17 | #include <boost/iostreams/put.hpp> |
|---|
| 18 | #include <boost/iostreams/read.hpp> |
|---|
| 19 | #include <boost/iostreams/seek.hpp> |
|---|
| 20 | #include <boost/iostreams/traits.hpp> |
|---|
| 21 | #include <boost/iostreams/write.hpp> |
|---|
| 22 | |
|---|
| 23 | // Must come last. |
|---|
| 24 | #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC. |
|---|
| 25 | |
|---|
| 26 | namespace boost { namespace iostreams { |
|---|
| 27 | |
|---|
| 28 | namespace detail { |
|---|
| 29 | |
|---|
| 30 | template<typename T> |
|---|
| 31 | struct read_write_if_impl; |
|---|
| 32 | |
|---|
| 33 | template<typename T> |
|---|
| 34 | struct seek_if_impl; |
|---|
| 35 | |
|---|
| 36 | } // End namespace detail. |
|---|
| 37 | |
|---|
| 38 | template<typename T> |
|---|
| 39 | typename int_type_of<T>::type get_if(T& t) |
|---|
| 40 | { |
|---|
| 41 | typedef typename detail::dispatch<T, input, output>::type tag; |
|---|
| 42 | return detail::read_write_if_impl<tag>::get(t); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | template<typename T> |
|---|
| 46 | inline std::streamsize |
|---|
| 47 | read_if(T& t, typename char_type_of<T>::type* s, std::streamsize n) |
|---|
| 48 | { |
|---|
| 49 | typedef typename detail::dispatch<T, input, output>::type tag; |
|---|
| 50 | return detail::read_write_if_impl<tag>::read(t, s, n); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | template<typename T> |
|---|
| 54 | bool put_if(T& t, typename char_type_of<T>::type c) |
|---|
| 55 | { |
|---|
| 56 | typedef typename detail::dispatch<T, output, input>::type tag; |
|---|
| 57 | return detail::read_write_if_impl<tag>::put(t, c); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | template<typename T> |
|---|
| 61 | inline std::streamsize write_if |
|---|
| 62 | (T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
|---|
| 63 | { |
|---|
| 64 | typedef typename detail::dispatch<T, output, input>::type tag; |
|---|
| 65 | return detail::read_write_if_impl<tag>::write(t, s, n); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | template<typename T> |
|---|
| 69 | inline std::streampos |
|---|
| 70 | seek_if( T& t, stream_offset off, BOOST_IOS::seekdir way, |
|---|
| 71 | BOOST_IOS::openmode which = BOOST_IOS::in | BOOST_IOS::out ) |
|---|
| 72 | { |
|---|
| 73 | using namespace detail; |
|---|
| 74 | typedef typename dispatch<T, random_access, any_tag>::type tag; |
|---|
| 75 | return seek_if_impl<tag>::seek(t, off, way, which); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | namespace detail { |
|---|
| 79 | |
|---|
| 80 | //------------------Specializations of read_write_if_impl---------------------// |
|---|
| 81 | |
|---|
| 82 | template<> |
|---|
| 83 | struct read_write_if_impl<input> { |
|---|
| 84 | template<typename T> |
|---|
| 85 | static typename int_type_of<T>::type get(T& t) |
|---|
| 86 | { return iostreams::get(t); } |
|---|
| 87 | |
|---|
| 88 | template<typename T> |
|---|
| 89 | static std::streamsize |
|---|
| 90 | read(T& t, typename char_type_of<T>::type* s, std::streamsize n) |
|---|
| 91 | { return iostreams::read(t, s, n); } |
|---|
| 92 | |
|---|
| 93 | template<typename T> |
|---|
| 94 | static bool put(T&, typename char_type_of<T>::type) |
|---|
| 95 | { throw cant_write(); } |
|---|
| 96 | |
|---|
| 97 | template<typename T> |
|---|
| 98 | static std::streamsize |
|---|
| 99 | write(T&, const typename char_type_of<T>::type*, std::streamsize) |
|---|
| 100 | { throw cant_write(); } |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | template<> |
|---|
| 104 | struct read_write_if_impl<output> { |
|---|
| 105 | template<typename T> |
|---|
| 106 | static typename int_type_of<T>::type get(T&) |
|---|
| 107 | { throw cant_read(); } |
|---|
| 108 | |
|---|
| 109 | template<typename T> |
|---|
| 110 | static std::streamsize |
|---|
| 111 | read(T&, typename char_type_of<T>::type*, std::streamsize) |
|---|
| 112 | { throw cant_read(); } |
|---|
| 113 | |
|---|
| 114 | template<typename T> |
|---|
| 115 | static bool put(T& t, typename char_type_of<T>::type c) |
|---|
| 116 | { return iostreams::put(t, c); } |
|---|
| 117 | |
|---|
| 118 | template<typename T> |
|---|
| 119 | static std::streamsize |
|---|
| 120 | write( T& t, const typename char_type_of<T>::type* s, |
|---|
| 121 | std::streamsize n ) |
|---|
| 122 | { return iostreams::write(t, s, n); } |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | //------------------Specializations of seek_if_impl---------------------------// |
|---|
| 126 | |
|---|
| 127 | template<> |
|---|
| 128 | struct seek_if_impl<random_access> { |
|---|
| 129 | template<typename T> |
|---|
| 130 | static std::streampos |
|---|
| 131 | seek( T& t, stream_offset off, BOOST_IOS::seekdir way, |
|---|
| 132 | BOOST_IOS::openmode which ) |
|---|
| 133 | { return iostreams::seek(t, off, way, which); } |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | template<> |
|---|
| 137 | struct seek_if_impl<any_tag> { |
|---|
| 138 | template<typename T> |
|---|
| 139 | static std::streampos |
|---|
| 140 | seek(T&, stream_offset, BOOST_IOS::seekdir, BOOST_IOS::openmode) |
|---|
| 141 | { throw cant_seek(); } |
|---|
| 142 | }; |
|---|
| 143 | |
|---|
| 144 | } // End namespace detail. |
|---|
| 145 | |
|---|
| 146 | } } // End namespaces iostreams, boost. |
|---|
| 147 | |
|---|
| 148 | #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC. |
|---|
| 149 | |
|---|
| 150 | #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED |
|---|