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 // Contains: The function template copy, which reads data from a Source
9 // and writes it to a Sink until the end of the sequence is reached, returning
10 // the number of characters transfered.
12 // The implementation is complicated by the need to handle smart adapters
13 // and direct devices.
15 #ifndef BOOST_IOSTREAMS_COPY_HPP_INCLUDED
16 #define BOOST_IOSTREAMS_COPY_HPP_INCLUDED
22 #include <boost/config.hpp> // Make sure ptrdiff_t is in std.
23 #include <algorithm> // copy, min.
24 #include <cstddef> // ptrdiff_t.
25 #include <utility> // pair.
26 #include <boost/bind.hpp>
27 #include <boost/detail/workaround.hpp>
28 #include <boost/iostreams/chain.hpp>
29 #include <boost/iostreams/constants.hpp>
30 #include <boost/iostreams/detail/adapter/non_blocking_adapter.hpp>
31 #include <boost/iostreams/detail/buffer.hpp>
32 #include <boost/iostreams/detail/enable_if_stream.hpp>
33 #include <boost/iostreams/detail/execute.hpp>
34 #include <boost/iostreams/detail/functional.hpp>
35 #include <boost/iostreams/detail/ios.hpp> // failure, streamsize.
36 #include <boost/iostreams/detail/resolve.hpp>
37 #include <boost/iostreams/detail/wrap_unwrap.hpp>
38 #include <boost/iostreams/operations.hpp> // read, write, close.
39 #include <boost/iostreams/pipeline.hpp>
40 #include <boost/static_assert.hpp>
41 #include <boost/type_traits/is_same.hpp>
43 namespace boost { namespace iostreams {
47 // The following four overloads of copy_impl() optimize
48 // copying in the case that one or both of the two devices
50 // http://www.boost.org/libs/iostreams/doc/index.html?path=4.1.1.4)
52 // Copy from a direct source to a direct sink
53 template<typename Source, typename Sink>
54 std::streamsize copy_impl( Source& src, Sink& snk,
55 std::streamsize /* buffer_size */,
56 mpl::true_, mpl::true_ )
59 typedef typename char_type_of<Source>::type char_type;
60 typedef std::pair<char_type*, char_type*> pair_type;
61 pair_type p1 = iostreams::input_sequence(src);
62 pair_type p2 = iostreams::output_sequence(snk);
63 std::streamsize total =
64 static_cast<std::streamsize>(
65 (std::min)(p1.second - p1.first, p2.second - p2.first)
67 std::copy(p1.first, p1.first + total, p2.first);
71 // Copy from a direct source to an indirect sink
72 template<typename Source, typename Sink>
73 std::streamsize copy_impl( Source& src, Sink& snk,
74 std::streamsize /* buffer_size */,
75 mpl::true_, mpl::false_ )
78 typedef typename char_type_of<Source>::type char_type;
79 typedef std::pair<char_type*, char_type*> pair_type;
80 pair_type p = iostreams::input_sequence(src);
81 std::streamsize size, total;
82 for ( total = 0, size = static_cast<std::streamsize>(p.second - p.first);
86 iostreams::write(snk, p.first + total, size - total);
92 // Copy from an indirect source to a direct sink
93 template<typename Source, typename Sink>
94 std::streamsize copy_impl( Source& src, Sink& snk,
95 std::streamsize buffer_size,
96 mpl::false_, mpl::true_ )
98 typedef typename char_type_of<Source>::type char_type;
99 typedef std::pair<char_type*, char_type*> pair_type;
100 detail::basic_buffer<char_type> buf(buffer_size);
101 pair_type p = snk.output_sequence();
102 std::streamsize total = 0;
103 std::ptrdiff_t capacity = p.second - p.first;
105 std::streamsize amt =
109 buffer_size < capacity - total ?
111 static_cast<std::streamsize>(capacity - total)
115 std::copy(buf.data(), buf.data() + amt, p.first + total);
121 // Copy from an indirect source to an indirect sink
122 template<typename Source, typename Sink>
123 std::streamsize copy_impl( Source& src, Sink& snk,
124 std::streamsize buffer_size,
125 mpl::false_, mpl::false_ )
127 typedef typename char_type_of<Source>::type char_type;
128 detail::basic_buffer<char_type> buf(buffer_size);
129 non_blocking_adapter<Sink> nb(snk);
130 std::streamsize total = 0;
134 done = (amt = iostreams::read(src, buf.data(), buffer_size)) == -1;
136 iostreams::write(nb, buf.data(), amt);
143 // The following function object is used with
144 // boost::iostreams::detail::execute() in the primary
145 // overload of copy_impl(), below
147 // Function object that delegates to one of the above four
148 // overloads of compl_impl()
149 template<typename Source, typename Sink>
150 class copy_operation {
152 typedef std::streamsize result_type;
153 copy_operation(Source& src, Sink& snk, std::streamsize buffer_size)
154 : src_(src), snk_(snk), buffer_size_(buffer_size)
156 std::streamsize operator()()
158 return copy_impl( src_, snk_, buffer_size_,
159 is_direct<Source>(), is_direct<Sink>() );
162 copy_operation& operator=(const copy_operation&);
165 std::streamsize buffer_size_;
168 // Primary overload of copy_impl. Delegates to one of the above four
169 // overloads of compl_impl(), depending on which of the two given
170 // devices, if any, models Direct (see
171 // http://www.boost.org/libs/iostreams/doc/index.html?path=4.1.1.4)
172 template<typename Source, typename Sink>
173 std::streamsize copy_impl(Source src, Sink snk, std::streamsize buffer_size)
176 typedef typename char_type_of<Source>::type src_char;
177 typedef typename char_type_of<Sink>::type snk_char;
178 BOOST_STATIC_ASSERT((is_same<src_char, snk_char>::value));
179 return detail::execute_all(
180 copy_operation<Source, Sink>(src, snk, buffer_size),
181 detail::call_close_all(src),
182 detail::call_close_all(snk)
186 } // End namespace detail.
188 //------------------Definition of copy----------------------------------------//
190 // Overload of copy() for the case where neither the source nor the sink is
191 // a standard stream or stream buffer
192 template<typename Source, typename Sink>
194 copy( const Source& src, const Sink& snk,
195 std::streamsize buffer_size = default_device_buffer_size
196 BOOST_IOSTREAMS_DISABLE_IF_STREAM(Source)
197 BOOST_IOSTREAMS_DISABLE_IF_STREAM(Sink) )
199 typedef typename char_type_of<Source>::type char_type;
200 return detail::copy_impl( detail::resolve<input, char_type>(src),
201 detail::resolve<output, char_type>(snk),
205 // Overload of copy() for the case where the source, but not the sink, is
206 // a standard stream or stream buffer
207 template<typename Source, typename Sink>
209 copy( Source& src, const Sink& snk,
210 std::streamsize buffer_size = default_device_buffer_size
211 BOOST_IOSTREAMS_ENABLE_IF_STREAM(Source)
212 BOOST_IOSTREAMS_DISABLE_IF_STREAM(Sink) )
214 typedef typename char_type_of<Source>::type char_type;
215 return detail::copy_impl( detail::wrap(src),
216 detail::resolve<output, char_type>(snk),
220 // Overload of copy() for the case where the sink, but not the source, is
221 // a standard stream or stream buffer
222 template<typename Source, typename Sink>
224 copy( const Source& src, Sink& snk,
225 std::streamsize buffer_size = default_device_buffer_size
226 BOOST_IOSTREAMS_DISABLE_IF_STREAM(Source)
227 BOOST_IOSTREAMS_ENABLE_IF_STREAM(Sink) )
229 typedef typename char_type_of<Source>::type char_type;
230 return detail::copy_impl( detail::resolve<input, char_type>(src),
231 detail::wrap(snk), buffer_size );
234 // Overload of copy() for the case where neither the source nor the sink is
235 // a standard stream or stream buffer
236 template<typename Source, typename Sink>
238 copy( Source& src, Sink& snk,
239 std::streamsize buffer_size = default_device_buffer_size
240 BOOST_IOSTREAMS_ENABLE_IF_STREAM(Source)
241 BOOST_IOSTREAMS_ENABLE_IF_STREAM(Sink) )
243 return detail::copy_impl(detail::wrap(src), detail::wrap(snk), buffer_size);
246 } } // End namespaces iostreams, boost.
248 #endif // #ifndef BOOST_IOSTREAMS_COPY_HPP_INCLUDED