source: NonGTP/Boost/boost/iostreams/checked_operations.hpp @ 857

Revision 857, 4.4 KB checked in by igarcia, 18 years ago (diff)
Line 
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
26namespace boost { namespace iostreams {
27
28namespace detail {
29
30template<typename T>
31struct read_write_if_impl;
32
33template<typename T>
34struct seek_if_impl;
35
36} // End namespace detail.
37
38template<typename T>
39typename 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
45template<typename T>
46inline std::streamsize
47read_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
53template<typename T>
54bool 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
60template<typename T>
61inline 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
68template<typename T>
69inline std::streampos
70seek_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
78namespace detail {
79
80//------------------Specializations of read_write_if_impl---------------------//
81
82template<>
83struct 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
103template<>
104struct 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
127template<>
128struct 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
136template<>
137struct 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
Note: See TracBrowser for help on using the repository browser.