fix doc example typo
[boost.git] / boost / iostreams / checked_operations.hpp
blob2c999c370a11cf190e242fd272918005baa5e093
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2005-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 // Contains implementations of get, read, put, write and seek which
9 // check a device's mode at runtime instead of compile time.
11 #ifndef BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED
14 #include <boost/iostreams/categories.hpp>
15 #include <boost/iostreams/detail/dispatch.hpp>
16 #include <boost/iostreams/detail/error.hpp>
17 #include <boost/iostreams/get.hpp>
18 #include <boost/iostreams/put.hpp>
19 #include <boost/iostreams/read.hpp>
20 #include <boost/iostreams/seek.hpp>
21 #include <boost/iostreams/traits.hpp>
22 #include <boost/iostreams/write.hpp>
24 // Must come last.
25 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
27 namespace boost { namespace iostreams {
29 namespace detail {
31 template<typename T>
32 struct read_write_if_impl;
34 template<typename T>
35 struct seek_if_impl;
37 } // End namespace detail.
39 template<typename T>
40 typename int_type_of<T>::type get_if(T& t)
42 typedef typename detail::dispatch<T, input, output>::type tag;
43 return detail::read_write_if_impl<tag>::get(t);
46 template<typename T>
47 inline std::streamsize
48 read_if(T& t, typename char_type_of<T>::type* s, std::streamsize n)
50 typedef typename detail::dispatch<T, input, output>::type tag;
51 return detail::read_write_if_impl<tag>::read(t, s, n);
54 template<typename T>
55 bool put_if(T& t, typename char_type_of<T>::type c)
57 typedef typename detail::dispatch<T, output, input>::type tag;
58 return detail::read_write_if_impl<tag>::put(t, c);
61 template<typename T>
62 inline std::streamsize write_if
63 (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
65 typedef typename detail::dispatch<T, output, input>::type tag;
66 return detail::read_write_if_impl<tag>::write(t, s, n);
69 template<typename T>
70 inline std::streampos
71 seek_if( T& t, stream_offset off, BOOST_IOS::seekdir way,
72 BOOST_IOS::openmode which = BOOST_IOS::in | BOOST_IOS::out )
74 using namespace detail;
75 typedef typename dispatch<T, random_access, any_tag>::type tag;
76 return seek_if_impl<tag>::seek(t, off, way, which);
79 namespace detail {
81 //------------------Specializations of read_write_if_impl---------------------//
83 template<>
84 struct read_write_if_impl<input> {
85 template<typename T>
86 static typename int_type_of<T>::type get(T& t)
87 { return iostreams::get(t); }
89 template<typename T>
90 static std::streamsize
91 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
92 { return iostreams::read(t, s, n); }
94 template<typename T>
95 static bool put(T&, typename char_type_of<T>::type)
96 { throw cant_write(); }
98 template<typename T>
99 static std::streamsize
100 write(T&, const typename char_type_of<T>::type*, std::streamsize)
101 { throw cant_write(); }
104 template<>
105 struct read_write_if_impl<output> {
106 template<typename T>
107 static typename int_type_of<T>::type get(T&)
108 { throw cant_read(); }
110 template<typename T>
111 static std::streamsize
112 read(T&, typename char_type_of<T>::type*, std::streamsize)
113 { throw cant_read(); }
115 template<typename T>
116 static bool put(T& t, typename char_type_of<T>::type c)
117 { return iostreams::put(t, c); }
119 template<typename T>
120 static std::streamsize
121 write( T& t, const typename char_type_of<T>::type* s,
122 std::streamsize n )
123 { return iostreams::write(t, s, n); }
126 //------------------Specializations of seek_if_impl---------------------------//
128 template<>
129 struct seek_if_impl<random_access> {
130 template<typename T>
131 static std::streampos
132 seek( T& t, stream_offset off, BOOST_IOS::seekdir way,
133 BOOST_IOS::openmode which )
134 { return iostreams::seek(t, off, way, which); }
137 template<>
138 struct seek_if_impl<any_tag> {
139 template<typename T>
140 static std::streampos
141 seek(T&, stream_offset, BOOST_IOS::seekdir, BOOST_IOS::openmode)
142 { throw cant_seek(); }
145 } // End namespace detail.
147 } } // End namespaces iostreams, boost.
149 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
151 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED