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