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_WRITE_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_WRITE_HPP_INCLUDED
10 
11 #if defined(_MSC_VER)
12 # pragma once
13 #endif
14 
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>
26 
27 // Must come last.
28 #include <boost/iostreams/detail/config/disable_warnings.hpp>
29 
30 namespace boost { namespace iostreams {
31 
32 namespace detail {
33 
34 template<typename T>
35 struct write_device_impl;
36 
37 template<typename T>
38 struct write_filter_impl;
39 
40 } // End namespace detail.
41 
42 template<typename T>
put(T & t,typename char_type_of<T>::type c)43 bool put(T& t, typename char_type_of<T>::type c)
44 { return detail::write_device_impl<T>::put(detail::unwrap(t), c); }
45 
46 template<typename T>
write(T & t,const typename char_type_of<T>::type * s,std::streamsize n)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); }
50 
51 template<typename T, typename Sink>
52 inline std::streamsize
write(T & t,Sink & snk,const typename char_type_of<T>::type * s,std::streamsize n)53 write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
54        std::streamsize n )
55 { return detail::write_filter_impl<T>::write(detail::unwrap(t), snk, s, n); }
56 
57 namespace detail {
58 
59 //------------------Definition of write_device_impl---------------------------//
60 
61 template<typename T>
62 struct write_device_impl
63     : mpl::if_<
64           is_custom<T>,
65           operations<T>,
66           write_device_impl<
67               BOOST_DEDUCED_TYPENAME
68               dispatch<
69                   T, ostream_tag, streambuf_tag, output
70               >::type
71           >
72       >::type
73     { };
74 
75 template<>
76 struct write_device_impl<ostream_tag> {
77     template<typename T>
putboost::iostreams::detail::write_device_impl78     static bool put(T& t, typename char_type_of<T>::type c)
79     {
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),
83                                           traits_type::eof() );
84     }
85 
86     template<typename T>
writeboost::iostreams::detail::write_device_impl87     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); }
90 };
91 
92 template<>
93 struct write_device_impl<streambuf_tag> {
94     template<typename T>
putboost::iostreams::detail::write_device_impl95     static bool put(T& t, typename char_type_of<T>::type c)
96     {
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());
100     }
101 
102     template<typename T>
writeboost::iostreams::detail::write_device_impl103     static std::streamsize write
104         (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
105     { return t.sputn(s, n); }
106 };
107 
108 template<>
109 struct write_device_impl<output> {
110     template<typename T>
putboost::iostreams::detail::write_device_impl111     static bool put(T& t, typename char_type_of<T>::type c)
112     { return t.write(&c, 1) == 1; }
113 
114     template<typename T>
115     static std::streamsize
writeboost::iostreams::detail::write_device_impl116     write(T& t, const typename char_type_of<T>::type* s, std::streamsize n)
117     { return t.write(s, n); }
118 };
119 
120 //------------------Definition of write_filter_impl---------------------------//
121 
122 template<typename T>
123 struct write_filter_impl
124     : mpl::if_<
125           is_custom<T>,
126           operations<T>,
127           write_filter_impl<
128               BOOST_DEDUCED_TYPENAME
129               dispatch<
130                   T, multichar_tag, any_tag
131               >::type
132           >
133       >::type
134     { };
135 
136 template<>
137 struct write_filter_impl<multichar_tag> {
138     template<typename T, typename Sink>
139     static std::streamsize
writeboost::iostreams::detail::write_filter_impl140     write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
141            std::streamsize n )
142     { return t.write(snk, s, n); }
143 };
144 
145 template<>
146 struct write_filter_impl<any_tag> {
147     template<typename T, typename Sink>
148     static std::streamsize
writeboost::iostreams::detail::write_filter_impl149     write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
150            std::streamsize n )
151     {
152         for (std::streamsize off = 0; off < n; ++off)
153             if (!t.put(snk, s[off]))
154                 return off;
155         return n;
156     }
157 };
158 
159 } // End namespace detail.
160 
161 } } // End namespaces iostreams, boost.
162 
163 #include <boost/iostreams/detail/config/enable_warnings.hpp>
164 
165 #endif // #ifndef BOOST_IOSTREAMS_WRITE_HPP_INCLUDED
166