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_WRITE_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_WRITE_HPP_INCLUDED
15 #include <boost/config.hpp> // DEDUCED_TYPENAME, MSVC.
16 #include <boost/detail/workaround.hpp>
17 #include <boost/iostreams/categories.hpp>
18 #include <boost/iostreams/detail/char_traits.hpp>
19 #include <boost/iostreams/detail/dispatch.hpp>
20 #include <boost/iostreams/detail/ios.hpp> // streamsize.
21 #include <boost/iostreams/detail/streambuf.hpp>
22 #include <boost/iostreams/detail/wrap_unwrap.hpp>
23 #include <boost/iostreams/operations_fwd.hpp>
24 #include <boost/iostreams/traits.hpp>
25 #include <boost/mpl/if.hpp>
28 #include <boost/iostreams/detail/config/disable_warnings.hpp>
30 namespace boost { namespace iostreams {
35 struct write_device_impl;
38 struct write_filter_impl;
40 } // End namespace detail.
43 bool put(T& t, typename char_type_of<T>::type c)
44 { return detail::write_device_impl<T>::put(detail::unwrap(t), c); }
47 inline std::streamsize write
48 (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
49 { return detail::write_device_impl<T>::write(detail::unwrap(t), s, n); }
51 template<typename T, typename Sink>
52 inline std::streamsize
53 write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
55 { return detail::write_filter_impl<T>::write(detail::unwrap(t), snk, s, n); }
59 //------------------Definition of write_device_impl---------------------------//
62 struct write_device_impl
67 BOOST_DEDUCED_TYPENAME
69 T, ostream_tag, streambuf_tag, output
76 struct write_device_impl<ostream_tag> {
78 static bool put(T& t, typename char_type_of<T>::type c)
80 typedef typename char_type_of<T>::type char_type;
81 typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type;
82 return !traits_type::eq_int_type( t.rdbuf()->sputc(c),
87 static std::streamsize write
88 (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
89 { return t.rdbuf()->sputn(s, n); }
93 struct write_device_impl<streambuf_tag> {
95 static bool put(T& t, typename char_type_of<T>::type c)
97 typedef typename char_type_of<T>::type char_type;
98 typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type;
99 return !traits_type::eq_int_type(t.sputc(c), traits_type::eof());
103 static std::streamsize write
104 (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
105 { return t.sputn(s, n); }
109 struct write_device_impl<output> {
111 static bool put(T& t, typename char_type_of<T>::type c)
112 { return t.write(&c, 1) == 1; }
115 static std::streamsize
116 write(T& t, const typename char_type_of<T>::type* s, std::streamsize n)
117 { return t.write(s, n); }
120 //------------------Definition of write_filter_impl---------------------------//
123 struct write_filter_impl
128 BOOST_DEDUCED_TYPENAME
130 T, multichar_tag, any_tag
137 struct write_filter_impl<multichar_tag> {
138 template<typename T, typename Sink>
139 static std::streamsize
140 write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
142 { return t.write(snk, s, n); }
146 struct write_filter_impl<any_tag> {
147 template<typename T, typename Sink>
148 static std::streamsize
149 write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
152 for (std::streamsize off = 0; off < n; ++off)
153 if (!t.put(snk, s[off]))
159 } // End namespace detail.
161 } } // End namespaces iostreams, boost.
163 #include <boost/iostreams/detail/config/enable_warnings.hpp>
165 #endif // #ifndef BOOST_IOSTREAMS_WRITE_HPP_INCLUDED