Update contrib.
1 // (C) Copyright Jonathan Turkanis 2005.
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.)
5 // See http://www.boost.org/libs/iostreams for documentation.
7 #ifndef BOOST_IOSTREAMS_RESTRICT_HPP_INCLUDED
8 #define BOOST_IOSTREAMS_RESTRICT_HPP_INCLUDED
10 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
14 #include <algorithm> // min.
15 #include <utility> // pair.
16 #include <boost/cstdint.hpp> // intmax_t.
17 #include <boost/config.hpp> // DEDUCED_TYPENAME.
18 #include <boost/iostreams/categories.hpp>
19 #include <boost/iostreams/char_traits.hpp>
20 #include <boost/iostreams/detail/adapter/basic_adapter.hpp>
21 #include <boost/iostreams/detail/call_traits.hpp>
22 #include <boost/iostreams/detail/enable_if_stream.hpp>
23 #include <boost/iostreams/detail/error.hpp>
24 #include <boost/iostreams/detail/ios.hpp> // failure.
25 #include <boost/iostreams/detail/select.hpp>
26 #include <boost/iostreams/operations.hpp>
27 #include <boost/iostreams/skip.hpp>
28 #include <boost/iostreams/traits.hpp> // mode_of, is_direct.
29 #include <boost/mpl/bool.hpp>
30 #include <boost/static_assert.hpp>
31 #include <boost/type_traits/is_convertible.hpp>
33 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4244.
35 namespace boost { namespace iostreams {
40 // Template name: restricted_indirect_device.
41 // Description: Provides an restricted view of an indirect Device.
42 // Template paramters:
43 // Device - An indirect model of Device that models either Source or
46 template<typename Device>
47 class restricted_indirect_device : public basic_adapter<Device> {
49 typedef typename detail::param_type<Device>::type param_type;
51 typedef typename char_type_of<Device>::type char_type;
53 : mode_of<Device>::type,
58 optimally_buffered_tag
60 restricted_indirect_device( param_type dev, stream_offset off,
61 stream_offset len = -1 );
62 std::streamsize read(char_type* s, std::streamsize n);
63 std::streamsize write(const char_type* s, std::streamsize n);
64 stream_offset seek(stream_offset off, BOOST_IOS::seekdir way);
66 stream_offset beg_, pos_, end_;
70 // Template name: restricted_direct_device.
71 // Description: Provides an restricted view of a Direct Device.
72 // Template paramters:
73 // Device - A model of Direct and Device.
75 template<typename Device>
76 class restricted_direct_device : public basic_adapter<Device> {
78 typedef typename char_type_of<Device>::type char_type;
79 typedef std::pair<char_type*, char_type*> pair_type;
81 : mode_of<Device>::type,
87 restricted_direct_device( const Device& dev, stream_offset off,
88 stream_offset len = -1 );
89 pair_type input_sequence();
90 pair_type output_sequence();
92 pair_type sequence(mpl::true_);
93 pair_type sequence(mpl::false_);
94 char_type *beg_, *end_;
98 // Template name: restricted_filter.
99 // Description: Provides an restricted view of a Filter.
100 // Template paramters:
101 // Filter - An indirect model of Filter.
103 template<typename Filter>
104 class restricted_filter : public basic_adapter<Filter> {
106 typedef typename char_type_of<Filter>::type char_type;
108 : mode_of<Filter>::type,
113 optimally_buffered_tag
115 restricted_filter( const Filter& flt, stream_offset off,
116 stream_offset len = -1 );
118 template<typename Source>
119 std::streamsize read(Source& src, char_type* s, std::streamsize n)
126 (std::min) (n, static_cast<streamsize>(end_ - pos_)) :
128 streamsize result = iostreams::read(this->component(), src, s, amt);
134 template<typename Sink>
135 std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
139 if (end_ != -1 && pos_ + n >= end_)
141 std::streamsize result =
142 iostreams::write(this->component(), snk, s, n);
147 template<typename Device>
148 stream_offset seek(Device& dev, stream_offset off, BOOST_IOS::seekdir way)
151 if (way == BOOST_IOS::beg) {
153 } else if (way == BOOST_IOS::cur) {
155 } else if (end_ != -1) {
158 // Restriction is half-open; seek relative to the actual end.
159 pos_ = this->component().seek(dev, off, BOOST_IOS::end);
164 if (next < beg_ || end_ != -1 && next >= end_)
166 pos_ = this->component().seek(dev, next, BOOST_IOS::cur);
170 template<typename Device>
171 void open(Device& dev)
174 iostreams::skip(this->component(), dev, beg_);
176 stream_offset beg_, pos_, end_;
181 struct restriction_traits
182 : iostreams::select< // Disambiguation for Tru64.
183 is_filter<T>, restricted_filter<T>,
184 is_direct<T>, restricted_direct_device<T>,
185 else_, restricted_indirect_device<T>
189 } // End namespace detail.
192 struct restriction : public detail::restriction_traits<T>::type {
193 typedef typename detail::param_type<T>::type param_type;
194 typedef typename detail::restriction_traits<T>::type base_type;
195 restriction(param_type t, stream_offset off, stream_offset len = -1)
196 : base_type(t, off, len)
200 //--------------Implementation of restrict------------------------------------//
202 // Note: The following workarounds are patterned after resolve.hpp. It has not
203 // yet been confirmed that they are necessary.
205 #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //-------------------------//
206 # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------------------//
209 restriction<T> restrict( const T& t, stream_offset off, stream_offset len = -1
210 BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
211 { return restriction<T>(t, off, len); }
213 template<typename Ch, typename Tr>
214 restriction< std::basic_streambuf<Ch, Tr> >
215 restrict(std::basic_streambuf<Ch, Tr>& sb, stream_offset off, stream_offset len = -1)
216 { return restriction< std::basic_streambuf<Ch, Tr> >(sb, off, len); }
218 template<typename Ch, typename Tr>
219 restriction< std::basic_istream<Ch, Tr> >
220 restrict(std::basic_istream<Ch, Tr>& is, stream_offset off, stream_offset len = -1)
221 { return restriction< std::basic_istream<Ch, Tr> >(is, off, len); }
223 template<typename Ch, typename Tr>
224 restriction< std::basic_ostream<Ch, Tr> >
225 restrict(std::basic_ostream<Ch, Tr>& os, stream_offset off, stream_offset len = -1)
226 { return restriction< std::basic_ostream<Ch, Tr> >(os, off, len); }
228 template<typename Ch, typename Tr>
229 restriction< std::basic_iostream<Ch, Tr> >
230 restrict(std::basic_iostream<Ch, Tr>& io, stream_offset off, stream_offset len = -1)
231 { return restriction< std::basic_iostream<Ch, Tr> >(io, off, len); }
233 # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //---------------------//
236 restriction<T> restrict( const T& t, stream_offset off, stream_offset len = -1
237 BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
238 { return restriction<T>(t, off, len); }
240 restriction<std::streambuf>
241 restrict(std::streambuf& sb, stream_offset off, stream_offset len = -1)
242 { return restriction<std::streambuf>(sb, off, len); }
244 restriction<std::istream>
245 restrict(std::istream<Ch, Tr>& is, stream_offset off, stream_offset len = -1)
246 { return restriction<std::istream>(is, off, len); }
248 restriction<std::ostream>
249 restrict(std::ostream<Ch, Tr>& os, stream_offset off, stream_offset len = -1)
250 { return restriction<std::ostream>(os, off, len); }
252 restriction<std::iostream>
253 restrict(std::iostream<Ch, Tr>& io, stream_offset off, stream_offset len = -1)
254 { return restriction<std::iostream>(io, off, len); }
256 # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
257 #else // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //----------------//
261 restrict(const T& t, stream_offset off, stream_offset len, mpl::true_)
262 { // Bad overload resolution.
263 return restriction<T>(const_cast<T&>(t, off, len));
268 restrict(const T& t, stream_offset off, stream_offset len, mpl::false_)
269 { return restriction<T>(t, off, len); }
273 restrict( const T& t, stream_offset off, stream_offset len = -1
274 BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
275 { return restrict(t, off, len, is_std_io<T>()); }
277 # if !BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \
278 !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) && \
279 !defined(__GNUC__) // ---------------------------------------------------//
283 restrict(T& t, stream_offset off, stream_offset len = -1)
284 { return restriction<T>(t, off, len); }
286 # endif // Borland 5.x, VC6-7.0 or GCC 2.9x //--------------------------------//
287 #endif // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------//
288 //----------------------------------------------------------------------------//
292 //--------------Implementation of restricted_indirect_device------------------//
294 template<typename Device>
295 restricted_indirect_device<Device>::restricted_indirect_device
296 (param_type dev, stream_offset off, stream_offset len)
297 : basic_adapter<Device>(dev), beg_(off), pos_(off),
298 end_(len != -1 ? off + len : -1)
300 if (len < -1 || off < 0)
301 throw BOOST_IOSTREAMS_FAILURE("bad offset");
302 iostreams::skip(this->component(), off);
305 template<typename Device>
306 inline std::streamsize restricted_indirect_device<Device>::read
307 (char_type* s, std::streamsize n)
312 (std::min) (n, static_cast<streamsize>(end_ - pos_)) :
314 streamsize result = iostreams::read(this->component(), s, amt);
320 template<typename Device>
321 inline std::streamsize restricted_indirect_device<Device>::write
322 (const char_type* s, std::streamsize n)
324 if (end_ != -1 && pos_ + n >= end_)
326 std::streamsize result = iostreams::write(this->component(), s, n);
331 template<typename Device>
332 stream_offset restricted_indirect_device<Device>::seek
333 (stream_offset off, BOOST_IOS::seekdir way)
336 if (way == BOOST_IOS::beg) {
338 } else if (way == BOOST_IOS::cur) {
340 } else if (end_ != -1) {
343 // Restriction is half-open; seek relative to the actual end.
344 pos_ = iostreams::seek(this->component(), off, BOOST_IOS::end);
349 if (next < beg_ || end_ != -1 && next >= end_)
351 pos_ = iostreams::seek(this->component(), next - pos_, BOOST_IOS::cur);
355 //--------------Implementation of restricted_direct_device--------------------//
357 template<typename Device>
358 restricted_direct_device<Device>::restricted_direct_device
359 (const Device& dev, stream_offset off, stream_offset len)
360 : basic_adapter<Device>(dev), beg_(0), end_(0)
362 std::pair<char_type*, char_type*> seq =
363 sequence(is_convertible<category, input>());
364 if ( off < 0 || len < -1 ||
365 len != -1 && off + len > seq.second - seq.first )
367 throw BOOST_IOSTREAMS_FAILURE("bad offset");
369 beg_ = seq.first + off;
371 seq.first + off + len :
375 template<typename Device>
376 typename restricted_direct_device<Device>::pair_type
377 restricted_direct_device<Device>::input_sequence()
379 BOOST_STATIC_ASSERT((is_convertible<category, input>::value));
380 return std::make_pair(beg_, end_);
383 template<typename Device>
384 typename restricted_direct_device<Device>::pair_type
385 restricted_direct_device<Device>::output_sequence()
387 BOOST_STATIC_ASSERT((is_convertible<category, output>::value));
388 return std::make_pair(beg_, end_);
391 template<typename Device>
392 typename restricted_direct_device<Device>::pair_type
393 restricted_direct_device<Device>::sequence(mpl::true_)
394 { return iostreams::input_sequence(this->component()); }
396 template<typename Device>
397 typename restricted_direct_device<Device>::pair_type
398 restricted_direct_device<Device>::sequence(mpl::false_)
399 { return iostreams::output_sequence(this->component()); }
401 //--------------Implementation of restricted_filter---------------------------//
403 template<typename Filter>
404 restricted_filter<Filter>::restricted_filter
405 (const Filter& flt, stream_offset off, stream_offset len)
406 : basic_adapter<Filter>(flt), beg_(off),
407 pos_(off), end_(len != -1 ? off + len : -1), open_(false)
409 if (len < -1 || off < 0)
410 throw BOOST_IOSTREAMS_FAILURE("bad offset");
413 } // End namespace detail.
415 } } // End namespaces iostreams, boost.
418 #endif // #ifndef BOOST_IOSTREAMS_RESTRICT_HPP_INCLUDED