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.)
5 
6 // See http://www.boost.org/libs/iostreams for documentation.
7 
8 #ifndef BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED
10 
11 #if defined(_MSC_VER)
12 # pragma once
13 #endif
14 
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.
18 
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>
26 
27 namespace boost { namespace iostreams { namespace detail {
28 
29 template<typename Mode, typename T>
30 class mode_adapter {
31 private:
32     struct empty_base { };
33 public:
34     typedef typename wrapped_type<T>::type  component_type;
35     typedef typename char_type_of<T>::type  char_type;
36     struct category
37         : Mode,
38           device_tag,
39           mpl::if_<is_filter<T>, filter_tag, device_tag>,
40           mpl::if_<is_filter<T>, multichar_tag, empty_base>,
41           closable_tag,
42           localizable_tag
43         { };
mode_adapter(const component_type & t)44     explicit mode_adapter(const component_type& t) : t_(t) { }
45 
46         // Device member functions.
47 
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 );
53     void close();
54     void close(BOOST_IOS::openmode which);
55 
56         // Filter member functions.
57 
58     template<typename Source>
read(Source & src,char_type * s,std::streamsize n)59     std::streamsize read(Source& src, char_type* s, std::streamsize n)
60     { return iostreams::read(t_, src, s, n); }
61 
62     template<typename Sink>
write(Sink & snk,const char_type * s,std::streamsize n)63     std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
64     { return iostreams::write(t_, snk, s, n); }
65 
66     template<typename Device>
seek(Device & dev,stream_offset off,BOOST_IOS::seekdir way)67     std::streampos seek(Device& dev, stream_offset off, BOOST_IOS::seekdir way)
68     { return iostreams::seek(t_, dev, off, way); }
69 
70     template<typename Device>
seek(Device & dev,stream_offset off,BOOST_IOS::seekdir way,BOOST_IOS::openmode which)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); }
74 
75     template<typename Device>
close(Device & dev)76     void close(Device& dev)
77     { detail::close_all(t_, dev); }
78 
79     template<typename Device>
close(Device & dev,BOOST_IOS::openmode which)80     void close(Device& dev, BOOST_IOS::openmode which)
81     { iostreams::close(t_, dev, which); }
82 
83     template<typename Locale>
imbue(const Locale & loc)84     void imbue(const Locale& loc)
85     { iostreams::imbue(t_, loc); }
86 private:
87     component_type t_;
88 };
89 
90 //------------------Implementation of mode_adapter----------------------------//
91 
92 template<typename Mode, typename T>
read(char_type * s,std::streamsize n)93 std::streamsize mode_adapter<Mode, T>::read
94     (char_type* s, std::streamsize n)
95 { return boost::iostreams::read(t_, s, n); }
96 
97 template<typename Mode, typename T>
write(const char_type * s,std::streamsize n)98 std::streamsize mode_adapter<Mode, T>::write
99     (const char_type* s, std::streamsize n)
100 { return boost::iostreams::write(t_, s, n); }
101 
102 template<typename Mode, typename T>
seek(stream_offset off,BOOST_IOS::seekdir way,BOOST_IOS::openmode which)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); }
106 
107 template<typename Mode, typename T>
close()108 void mode_adapter<Mode, T>::close()
109 { detail::close_all(t_); }
110 
111 template<typename Mode, typename T>
close(BOOST_IOS::openmode which)112 void mode_adapter<Mode, T>::close(BOOST_IOS::openmode which)
113 { iostreams::close(t_, which); }
114 
115 } } } // End namespaces detail, iostreams, boost.
116 
117 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED //-----//
118