1 | // (C) Copyright Jonathan Turkanis 2003. |
---|
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 | #ifndef BOOST_IOSTREAMS_WRITE_HPP_INCLUDED |
---|
8 | #define BOOST_IOSTREAMS_WRITE_HPP_INCLUDED |
---|
9 | |
---|
10 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
---|
11 | # pragma once |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <boost/config.hpp> // DEDUCED_TYPENAME, MSVC. |
---|
15 | #include <boost/detail/workaround.hpp> |
---|
16 | #include <boost/iostreams/categories.hpp> |
---|
17 | #include <boost/iostreams/detail/char_traits.hpp> |
---|
18 | #include <boost/iostreams/detail/dispatch.hpp> |
---|
19 | #include <boost/iostreams/detail/ios.hpp> // streamsize. |
---|
20 | #include <boost/iostreams/detail/streambuf.hpp> |
---|
21 | #include <boost/iostreams/detail/wrap_unwrap.hpp> |
---|
22 | #include <boost/iostreams/operations_fwd.hpp> |
---|
23 | #include <boost/iostreams/traits.hpp> |
---|
24 | #include <boost/mpl/if.hpp> |
---|
25 | |
---|
26 | // Must come last. |
---|
27 | #include <boost/iostreams/detail/config/disable_warnings.hpp> |
---|
28 | |
---|
29 | #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //-----------------------------------// |
---|
30 | # include <boost/iostreams/detail/vc6/write.hpp> |
---|
31 | #else // #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //--------------------------// |
---|
32 | |
---|
33 | namespace boost { namespace iostreams { |
---|
34 | |
---|
35 | namespace detail { |
---|
36 | |
---|
37 | template<typename T> |
---|
38 | struct write_device_impl; |
---|
39 | |
---|
40 | template<typename T> |
---|
41 | struct write_filter_impl; |
---|
42 | |
---|
43 | } // End namespace detail. |
---|
44 | |
---|
45 | template<typename T> |
---|
46 | bool put(T& t, typename char_type_of<T>::type c) |
---|
47 | { return detail::write_device_impl<T>::put(detail::unwrap(t), c); } |
---|
48 | |
---|
49 | template<typename T> |
---|
50 | inline std::streamsize write |
---|
51 | (T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
---|
52 | { return detail::write_device_impl<T>::write(detail::unwrap(t), s, n); } |
---|
53 | |
---|
54 | template<typename T, typename Sink> |
---|
55 | inline std::streamsize |
---|
56 | write( T& t, Sink& snk, const typename char_type_of<T>::type* s, |
---|
57 | std::streamsize n ) |
---|
58 | { return detail::write_filter_impl<T>::write(detail::unwrap(t), snk, s, n); } |
---|
59 | |
---|
60 | namespace detail { |
---|
61 | |
---|
62 | //------------------Definition of write_device_impl---------------------------// |
---|
63 | |
---|
64 | template<typename T> |
---|
65 | struct write_device_impl |
---|
66 | : mpl::if_< |
---|
67 | is_custom<T>, |
---|
68 | operations<T>, |
---|
69 | write_device_impl< |
---|
70 | BOOST_DEDUCED_TYPENAME |
---|
71 | dispatch< |
---|
72 | T, ostream_tag, streambuf_tag, output |
---|
73 | >::type |
---|
74 | > |
---|
75 | >::type |
---|
76 | { }; |
---|
77 | |
---|
78 | template<> |
---|
79 | struct write_device_impl<ostream_tag> { |
---|
80 | template<typename T> |
---|
81 | static bool put(T& t, typename char_type_of<T>::type c) |
---|
82 | { |
---|
83 | typedef typename char_type_of<T>::type char_type; |
---|
84 | typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type; |
---|
85 | return !traits_type::eq_int_type( t.rdbuf()->s.sputc(), |
---|
86 | traits_type::eof() ); |
---|
87 | } |
---|
88 | |
---|
89 | template<typename T> |
---|
90 | static std::streamsize write |
---|
91 | (T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
---|
92 | { return t.rdbuf()->sputn(s, n); } |
---|
93 | }; |
---|
94 | |
---|
95 | template<> |
---|
96 | struct write_device_impl<streambuf_tag> { |
---|
97 | template<typename T> |
---|
98 | static bool put(T& t, typename char_type_of<T>::type c) |
---|
99 | { |
---|
100 | typedef typename char_type_of<T>::type char_type; |
---|
101 | typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type; |
---|
102 | return !traits_type::eq_int_type(t.sputc(c), traits_type::eof()); |
---|
103 | } |
---|
104 | |
---|
105 | template<typename T> |
---|
106 | static std::streamsize write |
---|
107 | (T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
---|
108 | { return t.sputn(s, n); } |
---|
109 | }; |
---|
110 | |
---|
111 | template<> |
---|
112 | struct write_device_impl<output> { |
---|
113 | template<typename T> |
---|
114 | static bool put(T& t, typename char_type_of<T>::type c) |
---|
115 | { return t.write(&c, 1) == 1; } |
---|
116 | |
---|
117 | template<typename T> |
---|
118 | static std::streamsize |
---|
119 | write(T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
---|
120 | { return t.write(s, n); } |
---|
121 | }; |
---|
122 | |
---|
123 | //------------------Definition of write_filter_impl---------------------------// |
---|
124 | |
---|
125 | template<typename T> |
---|
126 | struct write_filter_impl |
---|
127 | : mpl::if_< |
---|
128 | is_custom<T>, |
---|
129 | operations<T>, |
---|
130 | write_filter_impl< |
---|
131 | BOOST_DEDUCED_TYPENAME |
---|
132 | dispatch< |
---|
133 | T, multichar_tag, any_tag |
---|
134 | >::type |
---|
135 | > |
---|
136 | >::type |
---|
137 | { }; |
---|
138 | |
---|
139 | template<> |
---|
140 | struct write_filter_impl<multichar_tag> { |
---|
141 | template<typename T, typename Sink> |
---|
142 | static std::streamsize |
---|
143 | write( T& t, Sink& snk, const typename char_type_of<T>::type* s, |
---|
144 | std::streamsize n ) |
---|
145 | { return t.write(snk, s, n); } |
---|
146 | }; |
---|
147 | |
---|
148 | template<> |
---|
149 | struct write_filter_impl<any_tag> { |
---|
150 | template<typename T, typename Sink> |
---|
151 | static std::streamsize |
---|
152 | write( T& t, Sink& snk, const typename char_type_of<T>::type* s, |
---|
153 | std::streamsize n ) |
---|
154 | { |
---|
155 | for (std::streamsize off = 0; off < n; ++off) |
---|
156 | if (!t.put(snk, s[off])) |
---|
157 | return off; |
---|
158 | return n; |
---|
159 | } |
---|
160 | }; |
---|
161 | |
---|
162 | } // End namespace detail. |
---|
163 | |
---|
164 | } } // End namespaces iostreams, boost. |
---|
165 | |
---|
166 | #endif // #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //-------------------------// |
---|
167 | |
---|
168 | #include <boost/iostreams/detail/config/enable_warnings.hpp> |
---|
169 | |
---|
170 | #endif // #ifndef BOOST_IOSTREAMS_WRITE_HPP_INCLUDED |
---|