blob: 46d5b647d6db27673e6366b31416088deeec3831 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001// (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 NDNBOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED
9#define NDNBOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED
10
11#if defined(_MSC_VER) && (_MSC_VER >= 1020)
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 <ndnboost/config.hpp> // NDNBOOST_MSVC.
20#include <ndnboost/detail/workaround.hpp>
21#include <ndnboost/iostreams/categories.hpp>
22#include <ndnboost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
23#include <ndnboost/iostreams/traits.hpp>
24#include <ndnboost/iostreams/operations.hpp>
25#include <ndnboost/mpl/if.hpp>
26
27namespace ndnboost { namespace iostreams { namespace detail {
28
29template<typename Mode, typename T>
30class mode_adapter {
31private:
32 struct empty_base { };
33public:
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 #if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1300)
42 closable_tag, // VC6 can't see member close()!
43 #endif
44 localizable_tag
45 { };
46 explicit mode_adapter(const component_type& t) : t_(t) { }
47
48 // Device member functions.
49
50 std::streamsize read(char_type* s, std::streamsize n);
51 std::streamsize write(const char_type* s, std::streamsize n);
52 std::streampos seek( stream_offset off, NDNBOOST_IOS::seekdir way,
53 NDNBOOST_IOS::openmode which =
54 NDNBOOST_IOS::in | NDNBOOST_IOS::out );
55#if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1300)
56 void close();
57 void close(NDNBOOST_IOS::openmode which);
58#endif
59
60 // Filter member functions.
61
62 template<typename Source>
63 std::streamsize read(Source& src, char_type* s, std::streamsize n)
64 { return iostreams::read(t_, src, s, n); }
65
66 template<typename Sink>
67 std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
68 { return iostreams::write(t_, snk, s, n); }
69
70 template<typename Device>
71 std::streampos seek(Device& dev, stream_offset off, NDNBOOST_IOS::seekdir way)
72 { return iostreams::seek(t_, dev, off, way); }
73
74 template<typename Device>
75 std::streampos seek( Device& dev, stream_offset off,
76 NDNBOOST_IOS::seekdir way, NDNBOOST_IOS::openmode which )
77 { return iostreams::seek(t_, dev, off, way, which); }
78
79 template<typename Device>
80 void close(Device& dev)
81 { detail::close_all(t_, dev); }
82
83 template<typename Device>
84 void close(Device& dev, NDNBOOST_IOS::openmode which)
85 { iostreams::close(t_, dev, which); }
86
87 template<typename Locale>
88 void imbue(const Locale& loc)
89 { iostreams::imbue(t_, loc); }
90private:
91 component_type t_;
92};
93
94//------------------Implementation of mode_adapter----------------------------//
95
96template<typename Mode, typename T>
97std::streamsize mode_adapter<Mode, T>::read
98 (char_type* s, std::streamsize n)
99{ return ndnboost::iostreams::read(t_, s, n); }
100
101template<typename Mode, typename T>
102std::streamsize mode_adapter<Mode, T>::write
103 (const char_type* s, std::streamsize n)
104{ return ndnboost::iostreams::write(t_, s, n); }
105
106template<typename Mode, typename T>
107std::streampos mode_adapter<Mode, T>::seek
108 (stream_offset off, NDNBOOST_IOS::seekdir way, NDNBOOST_IOS::openmode which)
109{ return ndnboost::iostreams::seek(t_, off, way, which); }
110
111#if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1300)
112 template<typename Mode, typename T>
113 void mode_adapter<Mode, T>::close()
114 { detail::close_all(t_); }
115
116 template<typename Mode, typename T>
117 void mode_adapter<Mode, T>::close(NDNBOOST_IOS::openmode which)
118 { iostreams::close(t_, which); }
119#endif
120
121} } } // End namespaces detail, iostreams, boost.
122
123#endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED //-----//