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 * File: boost/iostreams/detail/restrict_impl.hpp
8 * Date: Sun Jan 06 12:57:30 MST 2008
9 * Copyright: 2007-2008 CodeRage, LLC
10 * Author: Jonathan Turkanis
11 * Contact: turkanis at coderage dot com
13 * If included with the macro BOOST_IOSTREAMS_RESTRICT undefined, defines the
14 * class template boost::iostreams::restriction. If included with the macro
15 * BOOST_IOSTREAMS_RESTRICT defined as an identifier, defines the overloaded
16 * function template boost::iostreams::BOOST_IOSTREAMS_RESTRICT, and object
17 * generator for boost::iostreams::restriction.
19 * This design allows <boost/iostreams/restrict.hpp> and
20 * <boost/iostreams/slice.hpp> to share an implementation.
23 #if !defined(BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) && \
24 !defined(BOOST_IOSTREAMS_RESTRICT)
25 # define BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED
27 //------------------Implementation of restriction-----------------------------//
29 # include <algorithm> // min.
30 # include <utility> // pair.
31 # include <boost/cstdint.hpp> // intmax_t.
32 # include <boost/config.hpp> // DEDUCED_TYPENAME.
33 # include <boost/iostreams/categories.hpp>
34 # include <boost/iostreams/char_traits.hpp>
35 # include <boost/iostreams/detail/adapter/device_adapter.hpp>
36 # include <boost/iostreams/detail/adapter/filter_adapter.hpp>
37 # include <boost/iostreams/detail/call_traits.hpp>
38 # include <boost/iostreams/detail/enable_if_stream.hpp>
39 # include <boost/iostreams/detail/error.hpp>
40 # include <boost/iostreams/detail/ios.hpp> // failure.
41 # include <boost/iostreams/detail/select.hpp>
42 # include <boost/iostreams/operations.hpp>
43 # include <boost/iostreams/skip.hpp>
44 # include <boost/iostreams/traits.hpp> // mode_of, is_direct.
45 # include <boost/mpl/bool.hpp>
46 # include <boost/static_assert.hpp>
47 # include <boost/throw_exception.hpp>
48 # include <boost/type_traits/is_convertible.hpp>
50 # include <boost/iostreams/detail/config/disable_warnings.hpp>
52 namespace boost { namespace iostreams {
57 // Template name: restricted_indirect_device.
58 // Description: Provides an restricted view of an indirect Device.
59 // Template parameters:
60 // Device - An indirect model of Device that models either Source or
63 template<typename Device>
64 class restricted_indirect_device : public device_adapter<Device> {
66 typedef typename detail::param_type<Device>::type param_type;
68 typedef typename char_type_of<Device>::type char_type;
69 typedef typename mode_of<Device>::type mode;
70 BOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
77 optimally_buffered_tag
79 restricted_indirect_device( param_type dev, stream_offset off,
80 stream_offset len = -1 );
81 std::streamsize read(char_type* s, std::streamsize n);
82 std::streamsize write(const char_type* s, std::streamsize n);
83 std::streampos seek(stream_offset off, BOOST_IOS::seekdir way);
85 stream_offset beg_, pos_, end_;
89 // Template name: restricted_direct_device.
90 // Description: Provides an restricted view of a Direct Device.
91 // Template parameters:
92 // Device - A model of Direct and Device.
94 template<typename Device>
95 class restricted_direct_device : public device_adapter<Device> {
97 typedef typename char_type_of<Device>::type char_type;
98 typedef std::pair<char_type*, char_type*> pair_type;
99 typedef typename mode_of<Device>::type mode;
100 BOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
102 : mode_of<Device>::type,
108 restricted_direct_device( const Device& dev, stream_offset off,
109 stream_offset len = -1 );
110 pair_type input_sequence();
111 pair_type output_sequence();
113 pair_type sequence(mpl::true_);
114 pair_type sequence(mpl::false_);
115 char_type *beg_, *end_;
119 // Template name: restricted_filter.
120 // Description: Provides an restricted view of a Filter.
121 // Template parameters:
122 // Filter - An indirect model of Filter.
124 template<typename Filter>
125 class restricted_filter : public filter_adapter<Filter> {
127 typedef typename char_type_of<Filter>::type char_type;
128 typedef typename mode_of<Filter>::type mode;
129 BOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
136 optimally_buffered_tag
138 restricted_filter( const Filter& flt, stream_offset off,
139 stream_offset len = -1 );
141 template<typename Source>
142 std::streamsize read(Source& src, char_type* s, std::streamsize n)
146 open(src, BOOST_IOS::in);
147 std::streamsize amt =
149 (std::min) (n, static_cast<std::streamsize>(end_ - pos_)) :
151 std::streamsize result =
152 iostreams::read(this->component(), src, s, amt);
158 template<typename Sink>
159 std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
162 open(snk, BOOST_IOS::out);
163 if (end_ != -1 && pos_ + n >= end_) {
165 pos_ += iostreams::write(this->component(),
166 snk, s, end_ - pos_);
167 boost::throw_exception(bad_write());
169 std::streamsize result =
170 iostreams::write(this->component(), snk, s, n);
175 template<typename Device>
176 std::streampos seek(Device& dev, stream_offset off, BOOST_IOS::seekdir way)
179 if (way == BOOST_IOS::beg) {
181 } else if (way == BOOST_IOS::cur) {
183 } else if (end_ != -1) {
186 // Restriction is half-open; seek relative to the actual end.
187 pos_ = this->component().seek(dev, off, BOOST_IOS::end);
189 boost::throw_exception(bad_seek());
190 return offset_to_position(pos_ - beg_);
192 if (next < beg_ || (end_ != -1 && next >= end_))
193 boost::throw_exception(bad_seek());
194 pos_ = this->component().seek(dev, next, BOOST_IOS::cur);
195 return offset_to_position(pos_ - beg_);
198 template<typename Device>
199 void close(Device& dev)
202 detail::close_all(this->component(), dev);
205 template<typename Device>
206 void close(Device& dev, BOOST_IOS::openmode which)
209 iostreams::close(this->component(), dev, which);
212 template<typename Device>
213 void open(Device& dev, BOOST_IOS::openmode which)
215 typedef typename is_convertible<mode, dual_use>::type is_dual_use;
217 which = is_dual_use() ? which : (BOOST_IOS::in | BOOST_IOS::out);
218 iostreams::skip(this->component(), dev, beg_, which);
221 stream_offset beg_, pos_, end_;
226 struct restriction_traits
227 : iostreams::select< // Disambiguation for Tru64.
228 is_filter<T>, restricted_filter<T>,
229 is_direct<T>, restricted_direct_device<T>,
230 else_, restricted_indirect_device<T>
234 } // End namespace detail.
237 struct restriction : public detail::restriction_traits<T>::type {
238 typedef typename detail::param_type<T>::type param_type;
239 typedef typename detail::restriction_traits<T>::type base_type;
240 restriction(param_type t, stream_offset off, stream_offset len = -1)
241 : base_type(t, off, len)
247 //--------------Implementation of restricted_indirect_device------------------//
249 template<typename Device>
250 restricted_indirect_device<Device>::restricted_indirect_device
251 (param_type dev, stream_offset off, stream_offset len)
252 : device_adapter<Device>(dev), beg_(off), pos_(off),
253 end_(len != -1 ? off + len : -1)
255 if (len < -1 || off < 0)
256 boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset"));
257 iostreams::skip(this->component(), off);
260 template<typename Device>
261 inline std::streamsize restricted_indirect_device<Device>::read
262 (char_type* s, std::streamsize n)
265 std::streamsize amt =
267 (std::min) (n, static_cast<std::streamsize>(end_ - pos_)) :
269 std::streamsize result = iostreams::read(this->component(), s, amt);
275 template<typename Device>
276 inline std::streamsize restricted_indirect_device<Device>::write
277 (const char_type* s, std::streamsize n)
279 if (end_ != -1 && pos_ + n >= end_) {
281 pos_ += iostreams::write(this->component(), s, end_ - pos_);
282 boost::throw_exception(bad_write());
284 std::streamsize result = iostreams::write(this->component(), s, n);
289 template<typename Device>
290 std::streampos restricted_indirect_device<Device>::seek
291 (stream_offset off, BOOST_IOS::seekdir way)
294 if (way == BOOST_IOS::beg) {
296 } else if (way == BOOST_IOS::cur) {
298 } else if (end_ != -1) {
301 // Restriction is half-open; seek relative to the actual end.
302 pos_ = iostreams::seek(this->component(), off, BOOST_IOS::end);
304 boost::throw_exception(bad_seek());
305 return offset_to_position(pos_ - beg_);
307 if (next < beg_ || (end_ != -1 && next > end_))
308 boost::throw_exception(bad_seek());
309 pos_ = iostreams::seek(this->component(), next - pos_, BOOST_IOS::cur);
310 return offset_to_position(pos_ - beg_);
313 //--------------Implementation of restricted_direct_device--------------------//
315 template<typename Device>
316 restricted_direct_device<Device>::restricted_direct_device
317 (const Device& dev, stream_offset off, stream_offset len)
318 : device_adapter<Device>(dev), beg_(0), end_(0)
320 std::pair<char_type*, char_type*> seq =
321 sequence(is_convertible<category, input>());
322 if ( off < 0 || len < -1 ||
323 (len != -1 && off + len > seq.second - seq.first) )
325 boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset"));
327 beg_ = seq.first + off;
329 seq.first + off + len :
333 template<typename Device>
334 typename restricted_direct_device<Device>::pair_type
335 restricted_direct_device<Device>::input_sequence()
337 BOOST_STATIC_ASSERT((is_convertible<category, input>::value));
338 return std::make_pair(beg_, end_);
341 template<typename Device>
342 typename restricted_direct_device<Device>::pair_type
343 restricted_direct_device<Device>::output_sequence()
345 BOOST_STATIC_ASSERT((is_convertible<category, output>::value));
346 return std::make_pair(beg_, end_);
349 template<typename Device>
350 typename restricted_direct_device<Device>::pair_type
351 restricted_direct_device<Device>::sequence(mpl::true_)
352 { return iostreams::input_sequence(this->component()); }
354 template<typename Device>
355 typename restricted_direct_device<Device>::pair_type
356 restricted_direct_device<Device>::sequence(mpl::false_)
357 { return iostreams::output_sequence(this->component()); }
359 //--------------Implementation of restricted_filter---------------------------//
361 template<typename Filter>
362 restricted_filter<Filter>::restricted_filter
363 (const Filter& flt, stream_offset off, stream_offset len)
364 : filter_adapter<Filter>(flt), beg_(off),
365 pos_(off), end_(len != -1 ? off + len : -1), open_(false)
367 if (len < -1 || off < 0)
368 boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset"));
371 } // End namespace detail.
373 } } // End namespaces iostreams, boost.
375 #elif defined(BOOST_IOSTREAMS_RESTRICT)
377 namespace boost { namespace iostreams {
379 //--------------Implementation of restrict/slice------------------------------//
381 // Note: The following workarounds are patterned after resolve.hpp. It has not
382 // yet been confirmed that they are necessary.
384 # ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //------------------------//
385 # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //------------------------------//
389 BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
390 BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
391 { return restriction<T>(t, off, len); }
393 template<typename Ch, typename Tr>
394 restriction< std::basic_streambuf<Ch, Tr> >
395 BOOST_IOSTREAMS_RESTRICT( std::basic_streambuf<Ch, Tr>& sb, stream_offset off,
396 stream_offset len = -1 )
397 { return restriction< std::basic_streambuf<Ch, Tr> >(sb, off, len); }
399 template<typename Ch, typename Tr>
400 restriction< std::basic_istream<Ch, Tr> >
401 BOOST_IOSTREAMS_RESTRICT
402 (std::basic_istream<Ch, Tr>& is, stream_offset off, stream_offset len = -1)
403 { return restriction< std::basic_istream<Ch, Tr> >(is, off, len); }
405 template<typename Ch, typename Tr>
406 restriction< std::basic_ostream<Ch, Tr> >
407 BOOST_IOSTREAMS_RESTRICT
408 (std::basic_ostream<Ch, Tr>& os, stream_offset off, stream_offset len = -1)
409 { return restriction< std::basic_ostream<Ch, Tr> >(os, off, len); }
411 template<typename Ch, typename Tr>
412 restriction< std::basic_iostream<Ch, Tr> >
413 BOOST_IOSTREAMS_RESTRICT
414 (std::basic_iostream<Ch, Tr>& io, stream_offset off, stream_offset len = -1)
415 { return restriction< std::basic_iostream<Ch, Tr> >(io, off, len); }
417 # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
421 BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
422 BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
423 { return restriction<T>(t, off, len); }
425 restriction<std::streambuf>
426 BOOST_IOSTREAMS_RESTRICT
427 (std::streambuf& sb, stream_offset off, stream_offset len = -1)
428 { return restriction<std::streambuf>(sb, off, len); }
430 restriction<std::istream>
431 BOOST_IOSTREAMS_RESTRICT
432 (std::istream<Ch, Tr>& is, stream_offset off, stream_offset len = -1)
433 { return restriction<std::istream>(is, off, len); }
435 restriction<std::ostream>
436 BOOST_IOSTREAMS_RESTRICT
437 (std::ostream<Ch, Tr>& os, stream_offset off, stream_offset len = -1)
438 { return restriction<std::ostream>(os, off, len); }
440 restriction<std::iostream>
441 BOOST_IOSTREAMS_RESTRICT
442 (std::iostream<Ch, Tr>& io, stream_offset off, stream_offset len = -1)
443 { return restriction<std::iostream>(io, off, len); }
445 # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------//
446 # else // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------//
450 BOOST_IOSTREAMS_RESTRICT
451 (const T& t, stream_offset off, stream_offset len, mpl::true_)
452 { // Bad overload resolution.
453 return restriction<T>(const_cast<T&>(t, off, len));
458 BOOST_IOSTREAMS_RESTRICT
459 (const T& t, stream_offset off, stream_offset len, mpl::false_)
460 { return restriction<T>(t, off, len); }
464 BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
465 BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
466 { return BOOST_IOSTREAMS_RESTRICT(t, off, len, is_std_io<T>()); }
468 # if !BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \
469 !defined(__GNUC__) // ---------------------------------------------------//
473 BOOST_IOSTREAMS_RESTRICT(T& t, stream_offset off, stream_offset len = -1)
474 { return restriction<T>(t, off, len); }
476 # endif // Borland 5.x or GCC //-------------------------------//
477 # endif // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //--------------//
479 } } // End namespaces iostreams, boost.
481 #endif // #if !defined(BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) ...