1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2003-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 #ifndef BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED
15 // Contains the definition of the class template mode_adapter, which allows
16 // a filter or device to function as if it has a different i/o mode than that
17 // deduced by the metafunction mode_of.
19 #include <boost/config.hpp> // BOOST_MSVC.
20 #include <boost/detail/workaround.hpp>
21 #include <boost/iostreams/categories.hpp>
22 #include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
23 #include <boost/iostreams/traits.hpp>
24 #include <boost/iostreams/operations.hpp>
25 #include <boost/mpl/if.hpp>
27 namespace boost { namespace iostreams { namespace detail {
29 template<typename Mode, typename T>
32 struct empty_base { };
34 typedef typename wrapped_type<T>::type component_type;
35 typedef typename char_type_of<T>::type char_type;
39 mpl::if_<is_filter<T>, filter_tag, device_tag>,
40 mpl::if_<is_filter<T>, multichar_tag, empty_base>,
44 explicit mode_adapter(const component_type& t) : t_(t) { }
46 // Device member functions.
48 std::streamsize read(char_type* s, std::streamsize n);
49 std::streamsize write(const char_type* s, std::streamsize n);
50 std::streampos seek( stream_offset off, BOOST_IOS::seekdir way,
51 BOOST_IOS::openmode which =
52 BOOST_IOS::in | BOOST_IOS::out );
54 void close(BOOST_IOS::openmode which);
56 // Filter member functions.
58 template<typename Source>
59 std::streamsize read(Source& src, char_type* s, std::streamsize n)
60 { return iostreams::read(t_, src, s, n); }
62 template<typename Sink>
63 std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
64 { return iostreams::write(t_, snk, s, n); }
66 template<typename Device>
67 std::streampos seek(Device& dev, stream_offset off, BOOST_IOS::seekdir way)
68 { return iostreams::seek(t_, dev, off, way); }
70 template<typename Device>
71 std::streampos seek( Device& dev, stream_offset off,
72 BOOST_IOS::seekdir way, BOOST_IOS::openmode which )
73 { return iostreams::seek(t_, dev, off, way, which); }
75 template<typename Device>
76 void close(Device& dev)
77 { detail::close_all(t_, dev); }
79 template<typename Device>
80 void close(Device& dev, BOOST_IOS::openmode which)
81 { iostreams::close(t_, dev, which); }
83 template<typename Locale>
84 void imbue(const Locale& loc)
85 { iostreams::imbue(t_, loc); }
90 //------------------Implementation of mode_adapter----------------------------//
92 template<typename Mode, typename T>
93 std::streamsize mode_adapter<Mode, T>::read
94 (char_type* s, std::streamsize n)
95 { return boost::iostreams::read(t_, s, n); }
97 template<typename Mode, typename T>
98 std::streamsize mode_adapter<Mode, T>::write
99 (const char_type* s, std::streamsize n)
100 { return boost::iostreams::write(t_, s, n); }
102 template<typename Mode, typename T>
103 std::streampos mode_adapter<Mode, T>::seek
104 (stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
105 { return boost::iostreams::seek(t_, off, way, which); }
107 template<typename Mode, typename T>
108 void mode_adapter<Mode, T>::close()
109 { detail::close_all(t_); }
111 template<typename Mode, typename T>
112 void mode_adapter<Mode, T>::close(BOOST_IOS::openmode which)
113 { iostreams::close(t_, which); }
115 } } } // End namespaces detail, iostreams, boost.
117 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED //-----//