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>
25 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
27 namespace boost
{ namespace iostreams
{
32 struct read_write_if_impl
;
37 } // End namespace detail.
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
);
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
);
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
);
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
);
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
);
81 //------------------Specializations of read_write_if_impl---------------------//
84 struct read_write_if_impl
<input
> {
86 static typename int_type_of
<T
>::type
get(T
& t
)
87 { return iostreams::get(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
); }
95 static bool put(T
&, typename char_type_of
<T
>::type
)
96 { throw cant_write(); }
99 static std::streamsize
100 write(T
&, const typename char_type_of
<T
>::type
*, std::streamsize
)
101 { throw cant_write(); }
105 struct read_write_if_impl
<output
> {
107 static typename int_type_of
<T
>::type
get(T
&)
108 { throw cant_read(); }
111 static std::streamsize
112 read(T
&, typename char_type_of
<T
>::type
*, std::streamsize
)
113 { throw cant_read(); }
116 static bool put(T
& t
, typename char_type_of
<T
>::type c
)
117 { return iostreams::put(t
, c
); }
120 static std::streamsize
121 write( T
& t
, const typename char_type_of
<T
>::type
* s
,
123 { return iostreams::write(t
, s
, n
); }
126 //------------------Specializations of seek_if_impl---------------------------//
129 struct seek_if_impl
<random_access
> {
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
); }
138 struct seek_if_impl
<any_tag
> {
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