Chris@16: /* Chris@16: * Distributed under the Boost Software License, Version 1.0.(See accompanying Chris@16: * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) Chris@16: * Chris@16: * See http://www.boost.org/libs/iostreams for documentation. Chris@16: Chris@16: * File: boost/iostreams/detail/restrict_impl.hpp Chris@16: * Date: Sun Jan 06 12:57:30 MST 2008 Chris@16: * Copyright: 2007-2008 CodeRage, LLC Chris@16: * Author: Jonathan Turkanis Chris@16: * Contact: turkanis at coderage dot com Chris@16: * Chris@16: * If included with the macro BOOST_IOSTREAMS_RESTRICT undefined, defines the Chris@16: * class template boost::iostreams::restriction. If included with the macro Chris@16: * BOOST_IOSTREAMS_RESTRICT defined as an identifier, defines the overloaded Chris@16: * function template boost::iostreams::BOOST_IOSTREAMS_RESTRICT, and object Chris@16: * generator for boost::iostreams::restriction. Chris@16: * Chris@16: * This design allows and Chris@16: * to share an implementation. Chris@16: */ Chris@16: Chris@16: #if !defined(BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) && \ Chris@16: !defined(BOOST_IOSTREAMS_RESTRICT) Chris@16: # define BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED Chris@16: Chris@16: //------------------Implementation of restriction-----------------------------// Chris@16: Chris@16: # include // min. Chris@16: # include // pair. Chris@16: # include // intmax_t. Chris@16: # include // DEDUCED_TYPENAME. Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include // failure. Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include // mode_of, is_direct. Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: Chris@16: namespace boost { namespace iostreams { Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: // Chris@16: // Template name: restricted_indirect_device. Chris@16: // Description: Provides an restricted view of an indirect Device. Chris@16: // Template parameters: Chris@16: // Device - An indirect model of Device that models either Source or Chris@16: // SeekableDevice. Chris@16: // Chris@16: template Chris@16: class restricted_indirect_device : public device_adapter { Chris@16: private: Chris@16: typedef typename detail::param_type::type param_type; Chris@16: public: Chris@16: typedef typename char_type_of::type char_type; Chris@16: typedef typename mode_of::type mode; Chris@16: BOOST_STATIC_ASSERT(!(is_convertible::value)); Chris@16: struct category Chris@16: : mode, Chris@16: device_tag, Chris@16: closable_tag, Chris@16: flushable_tag, Chris@16: localizable_tag, Chris@16: optimally_buffered_tag Chris@16: { }; Chris@16: restricted_indirect_device( param_type dev, stream_offset off, Chris@16: stream_offset len = -1 ); Chris@16: std::streamsize read(char_type* s, std::streamsize n); Chris@16: std::streamsize write(const char_type* s, std::streamsize n); Chris@16: std::streampos seek(stream_offset off, BOOST_IOS::seekdir way); Chris@16: private: Chris@16: stream_offset beg_, pos_, end_; Chris@16: }; Chris@16: Chris@16: // Chris@16: // Template name: restricted_direct_device. Chris@16: // Description: Provides an restricted view of a Direct Device. Chris@16: // Template parameters: Chris@16: // Device - A model of Direct and Device. Chris@16: // Chris@16: template Chris@16: class restricted_direct_device : public device_adapter { Chris@16: public: Chris@16: typedef typename char_type_of::type char_type; Chris@16: typedef std::pair pair_type; Chris@16: typedef typename mode_of::type mode; Chris@16: BOOST_STATIC_ASSERT(!(is_convertible::value)); Chris@16: struct category Chris@16: : mode_of::type, Chris@16: device_tag, Chris@16: direct_tag, Chris@16: closable_tag, Chris@16: localizable_tag Chris@16: { }; Chris@16: restricted_direct_device( const Device& dev, stream_offset off, Chris@16: stream_offset len = -1 ); Chris@16: pair_type input_sequence(); Chris@16: pair_type output_sequence(); Chris@16: private: Chris@16: pair_type sequence(mpl::true_); Chris@16: pair_type sequence(mpl::false_); Chris@16: char_type *beg_, *end_; Chris@16: }; Chris@16: Chris@16: // Chris@16: // Template name: restricted_filter. Chris@16: // Description: Provides an restricted view of a Filter. Chris@16: // Template parameters: Chris@16: // Filter - An indirect model of Filter. Chris@16: // Chris@16: template Chris@16: class restricted_filter : public filter_adapter { Chris@16: public: Chris@16: typedef typename char_type_of::type char_type; Chris@16: typedef typename mode_of::type mode; Chris@16: BOOST_STATIC_ASSERT(!(is_convertible::value)); Chris@16: struct category Chris@16: : mode, Chris@16: filter_tag, Chris@16: multichar_tag, Chris@16: closable_tag, Chris@16: localizable_tag, Chris@16: optimally_buffered_tag Chris@16: { }; Chris@16: restricted_filter( const Filter& flt, stream_offset off, Chris@16: stream_offset len = -1 ); Chris@16: Chris@16: template Chris@16: std::streamsize read(Source& src, char_type* s, std::streamsize n) Chris@16: { Chris@16: using namespace std; Chris@16: if (!open_) Chris@16: open(src, BOOST_IOS::in); Chris@16: std::streamsize amt = Chris@16: end_ != -1 ? Chris@16: (std::min) (n, static_cast(end_ - pos_)) : Chris@16: n; Chris@16: std::streamsize result = Chris@16: iostreams::read(this->component(), src, s, amt); Chris@16: if (result != -1) Chris@16: pos_ += result; Chris@16: return result; Chris@16: } Chris@16: Chris@16: template Chris@16: std::streamsize write(Sink& snk, const char_type* s, std::streamsize n) Chris@16: { Chris@16: if (!open_) Chris@16: open(snk, BOOST_IOS::out); Chris@16: if (end_ != -1 && pos_ + n >= end_) { Chris@16: if(pos_ < end_) Chris@16: pos_ += iostreams::write(this->component(), Chris@16: snk, s, end_ - pos_); Chris@16: boost::throw_exception(bad_write()); Chris@16: } Chris@16: std::streamsize result = Chris@16: iostreams::write(this->component(), snk, s, n); Chris@16: pos_ += result; Chris@16: return result; Chris@16: } Chris@16: Chris@16: template Chris@16: std::streampos seek(Device& dev, stream_offset off, BOOST_IOS::seekdir way) Chris@16: { Chris@16: stream_offset next; Chris@16: if (way == BOOST_IOS::beg) { Chris@16: next = beg_ + off; Chris@16: } else if (way == BOOST_IOS::cur) { Chris@16: next = pos_ + off; Chris@16: } else if (end_ != -1) { Chris@16: next = end_ + off; Chris@16: } else { Chris@16: // Restriction is half-open; seek relative to the actual end. Chris@16: pos_ = this->component().seek(dev, off, BOOST_IOS::end); Chris@16: if (pos_ < beg_) Chris@16: boost::throw_exception(bad_seek()); Chris@16: return offset_to_position(pos_ - beg_); Chris@16: } Chris@16: if (next < beg_ || (end_ != -1 && next >= end_)) Chris@16: boost::throw_exception(bad_seek()); Chris@16: pos_ = this->component().seek(dev, next, BOOST_IOS::cur); Chris@16: return offset_to_position(pos_ - beg_); Chris@16: } Chris@16: Chris@16: template Chris@16: void close(Device& dev) Chris@16: { Chris@16: open_ = false; Chris@16: detail::close_all(this->component(), dev); Chris@16: } Chris@16: Chris@16: template Chris@16: void close(Device& dev, BOOST_IOS::openmode which) Chris@16: { Chris@16: open_ = false; Chris@16: iostreams::close(this->component(), dev, which); Chris@16: } Chris@16: private: Chris@16: template Chris@16: void open(Device& dev, BOOST_IOS::openmode which) Chris@16: { Chris@16: typedef typename is_convertible::type is_dual_use; Chris@16: open_ = true; Chris@16: which = is_dual_use() ? which : (BOOST_IOS::in | BOOST_IOS::out); Chris@16: iostreams::skip(this->component(), dev, beg_, which); Chris@16: } Chris@16: Chris@16: stream_offset beg_, pos_, end_; Chris@16: bool open_; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct restriction_traits Chris@16: : iostreams::select< // Disambiguation for Tru64. Chris@16: is_filter, restricted_filter, Chris@16: is_direct, restricted_direct_device, Chris@16: else_, restricted_indirect_device Chris@16: > Chris@16: { }; Chris@16: Chris@16: } // End namespace detail. Chris@16: Chris@16: template Chris@16: struct restriction : public detail::restriction_traits::type { Chris@16: typedef typename detail::param_type::type param_type; Chris@16: typedef typename detail::restriction_traits::type base_type; Chris@16: restriction(param_type t, stream_offset off, stream_offset len = -1) Chris@16: : base_type(t, off, len) Chris@16: { } Chris@16: }; Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: //--------------Implementation of restricted_indirect_device------------------// Chris@16: Chris@16: template Chris@16: restricted_indirect_device::restricted_indirect_device Chris@16: (param_type dev, stream_offset off, stream_offset len) Chris@16: : device_adapter(dev), beg_(off), pos_(off), Chris@16: end_(len != -1 ? off + len : -1) Chris@16: { Chris@16: if (len < -1 || off < 0) Chris@16: boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset")); Chris@16: iostreams::skip(this->component(), off); Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::streamsize restricted_indirect_device::read Chris@16: (char_type* s, std::streamsize n) Chris@16: { Chris@16: using namespace std; Chris@16: std::streamsize amt = Chris@16: end_ != -1 ? Chris@16: (std::min) (n, static_cast(end_ - pos_)) : Chris@16: n; Chris@16: std::streamsize result = iostreams::read(this->component(), s, amt); Chris@16: if (result != -1) Chris@16: pos_ += result; Chris@16: return result; Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::streamsize restricted_indirect_device::write Chris@16: (const char_type* s, std::streamsize n) Chris@16: { Chris@16: if (end_ != -1 && pos_ + n >= end_) { Chris@16: if(pos_ < end_) Chris@16: pos_ += iostreams::write(this->component(), s, end_ - pos_); Chris@16: boost::throw_exception(bad_write()); Chris@16: } Chris@16: std::streamsize result = iostreams::write(this->component(), s, n); Chris@16: pos_ += result; Chris@16: return result; Chris@16: } Chris@16: Chris@16: template Chris@16: std::streampos restricted_indirect_device::seek Chris@16: (stream_offset off, BOOST_IOS::seekdir way) Chris@16: { Chris@16: stream_offset next; Chris@16: if (way == BOOST_IOS::beg) { Chris@16: next = beg_ + off; Chris@16: } else if (way == BOOST_IOS::cur) { Chris@16: next = pos_ + off; Chris@16: } else if (end_ != -1) { Chris@16: next = end_ + off; Chris@16: } else { Chris@16: // Restriction is half-open; seek relative to the actual end. Chris@16: pos_ = iostreams::seek(this->component(), off, BOOST_IOS::end); Chris@16: if (pos_ < beg_) Chris@16: boost::throw_exception(bad_seek()); Chris@16: return offset_to_position(pos_ - beg_); Chris@16: } Chris@16: if (next < beg_ || (end_ != -1 && next > end_)) Chris@16: boost::throw_exception(bad_seek()); Chris@16: pos_ = iostreams::seek(this->component(), next - pos_, BOOST_IOS::cur); Chris@16: return offset_to_position(pos_ - beg_); Chris@16: } Chris@16: Chris@16: //--------------Implementation of restricted_direct_device--------------------// Chris@16: Chris@16: template Chris@16: restricted_direct_device::restricted_direct_device Chris@16: (const Device& dev, stream_offset off, stream_offset len) Chris@16: : device_adapter(dev), beg_(0), end_(0) Chris@16: { Chris@16: std::pair seq = Chris@16: sequence(is_convertible()); Chris@16: if ( off < 0 || len < -1 || Chris@16: (len != -1 && off + len > seq.second - seq.first) ) Chris@16: { Chris@16: boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset")); Chris@16: } Chris@16: beg_ = seq.first + off; Chris@16: end_ = len != -1 ? Chris@16: seq.first + off + len : Chris@16: seq.second; Chris@16: } Chris@16: Chris@16: template Chris@16: typename restricted_direct_device::pair_type Chris@16: restricted_direct_device::input_sequence() Chris@16: { Chris@16: BOOST_STATIC_ASSERT((is_convertible::value)); Chris@16: return std::make_pair(beg_, end_); Chris@16: } Chris@16: Chris@16: template Chris@16: typename restricted_direct_device::pair_type Chris@16: restricted_direct_device::output_sequence() Chris@16: { Chris@16: BOOST_STATIC_ASSERT((is_convertible::value)); Chris@16: return std::make_pair(beg_, end_); Chris@16: } Chris@16: Chris@16: template Chris@16: typename restricted_direct_device::pair_type Chris@16: restricted_direct_device::sequence(mpl::true_) Chris@16: { return iostreams::input_sequence(this->component()); } Chris@16: Chris@16: template Chris@16: typename restricted_direct_device::pair_type Chris@16: restricted_direct_device::sequence(mpl::false_) Chris@16: { return iostreams::output_sequence(this->component()); } Chris@16: Chris@16: //--------------Implementation of restricted_filter---------------------------// Chris@16: Chris@16: template Chris@16: restricted_filter::restricted_filter Chris@16: (const Filter& flt, stream_offset off, stream_offset len) Chris@16: : filter_adapter(flt), beg_(off), Chris@16: pos_(off), end_(len != -1 ? off + len : -1), open_(false) Chris@16: { Chris@16: if (len < -1 || off < 0) Chris@16: boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset")); Chris@16: } Chris@16: Chris@16: } // End namespace detail. Chris@16: Chris@16: } } // End namespaces iostreams, boost. Chris@16: Chris@16: #elif defined(BOOST_IOSTREAMS_RESTRICT) Chris@16: Chris@16: namespace boost { namespace iostreams { Chris@16: Chris@16: //--------------Implementation of restrict/slice------------------------------// Chris@16: Chris@16: // Note: The following workarounds are patterned after resolve.hpp. It has not Chris@16: // yet been confirmed that they are necessary. Chris@16: Chris@16: # ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //------------------------// Chris@16: # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //------------------------------// Chris@16: Chris@16: template Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1 Chris@16: BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) ) Chris@16: { return restriction(t, off, len); } Chris@16: Chris@16: template Chris@16: restriction< std::basic_streambuf > Chris@16: BOOST_IOSTREAMS_RESTRICT( std::basic_streambuf& sb, stream_offset off, Chris@16: stream_offset len = -1 ) Chris@16: { return restriction< std::basic_streambuf >(sb, off, len); } Chris@16: Chris@16: template Chris@16: restriction< std::basic_istream > Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (std::basic_istream& is, stream_offset off, stream_offset len = -1) Chris@16: { return restriction< std::basic_istream >(is, off, len); } Chris@16: Chris@16: template Chris@16: restriction< std::basic_ostream > Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (std::basic_ostream& os, stream_offset off, stream_offset len = -1) Chris@16: { return restriction< std::basic_ostream >(os, off, len); } Chris@16: Chris@16: template Chris@16: restriction< std::basic_iostream > Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (std::basic_iostream& io, stream_offset off, stream_offset len = -1) Chris@16: { return restriction< std::basic_iostream >(io, off, len); } Chris@16: Chris@16: # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------// Chris@16: Chris@16: template Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1 Chris@16: BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) ) Chris@16: { return restriction(t, off, len); } Chris@16: Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (std::streambuf& sb, stream_offset off, stream_offset len = -1) Chris@16: { return restriction(sb, off, len); } Chris@16: Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (std::istream& is, stream_offset off, stream_offset len = -1) Chris@16: { return restriction(is, off, len); } Chris@16: Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (std::ostream& os, stream_offset off, stream_offset len = -1) Chris@16: { return restriction(os, off, len); } Chris@16: Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (std::iostream& io, stream_offset off, stream_offset len = -1) Chris@16: { return restriction(io, off, len); } Chris@16: Chris@16: # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------// Chris@16: # else // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------// Chris@16: Chris@16: template Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (const T& t, stream_offset off, stream_offset len, mpl::true_) Chris@16: { // Bad overload resolution. Chris@16: return restriction(const_cast(t, off, len)); Chris@16: } Chris@16: Chris@16: template Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT Chris@16: (const T& t, stream_offset off, stream_offset len, mpl::false_) Chris@16: { return restriction(t, off, len); } Chris@16: Chris@16: template Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1 Chris@16: BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) ) Chris@16: { return BOOST_IOSTREAMS_RESTRICT(t, off, len, is_std_io()); } Chris@16: Chris@16: # if !BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \ Chris@16: !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) && \ Chris@16: !defined(__GNUC__) // ---------------------------------------------------// Chris@16: Chris@16: template Chris@16: restriction Chris@16: BOOST_IOSTREAMS_RESTRICT(T& t, stream_offset off, stream_offset len = -1) Chris@16: { return restriction(t, off, len); } Chris@16: Chris@16: # endif // Borland 5.x, VC6-7.0 or GCC 2.9x //-------------------------------// Chris@16: # endif // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //--------------// Chris@16: Chris@16: } } // End namespaces iostreams, boost. Chris@16: Chris@16: #endif // #if !defined(BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) ...