// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) // (C) Copyright 2003-2007 Jonathan Turkanis // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) // See http://www.boost.org/libs/iostreams for documentation. #ifndef BOOST_IOSTREAMS_DETAIL_RANGE_ADAPTER_HPP_INCLUDED #define BOOST_IOSTREAMS_DETAIL_RANGE_ADAPTER_HPP_INCLUDED #if defined(_MSC_VER) # pragma once #endif #include // min. #include #include // ptrdiff_t. #include // streamsize, streamoff. #include // iterator_traits. #include #include #include #include #include #include #include // Must come last. #include // MSVC. namespace boost { namespace iostreams { namespace detail { // Used for simulated tag dispatch. template struct range_adapter_impl; // // Template name: range_adapter // Description: Device based on an instance of boost::iterator_range. // Template parameters: // Mode - A mode tag. // Range - An instance of iterator_range. // template class range_adapter { private: typedef typename Range::iterator iterator; typedef std::iterator_traits iter_traits; typedef typename iter_traits::iterator_category iter_cat; public: typedef typename Range::value_type char_type; struct category : Mode, device_tag { }; typedef typename mpl::if_< is_convertible< iter_cat, std::random_access_iterator_tag >, std::random_access_iterator_tag, std::forward_iterator_tag >::type tag; typedef range_adapter_impl impl; explicit range_adapter(const Range& rng); range_adapter(iterator first, iterator last); std::streamsize read(char_type* s, std::streamsize n); std::streamsize write(const char_type* s, std::streamsize n); std::streampos seek(stream_offset off, BOOST_IOS::seekdir way); private: iterator first_, cur_, last_; }; //------------------Implementation of range_adapter---------------------------// template range_adapter::range_adapter(const Range& rng) : first_(rng.begin()), cur_(rng.begin()), last_(rng.end()) { } template range_adapter::range_adapter(iterator first, iterator last) : first_(first), cur_(first), last_(last) { } template inline std::streamsize range_adapter::read (char_type* s, std::streamsize n) { return impl::read(cur_, last_, s, n); } template inline std::streamsize range_adapter::write (const char_type* s, std::streamsize n) { return impl::write(cur_, last_, s, n); } template std::streampos range_adapter::seek (stream_offset off, BOOST_IOS::seekdir way) { impl::seek(first_, cur_, last_, off, way); return offset_to_position(cur_ - first_); } //------------------Implementation of range_adapter_impl----------------------// template<> struct range_adapter_impl { template static std::streamsize read (Iter& cur, Iter& last, Ch* s,std::streamsize n) { std::streamsize rem = n; // No. of chars remaining. while (cur != last && rem-- > 0) *s++ = *cur++; return n - rem != 0 ? n - rem : -1; } template static std::streamsize write (Iter& cur, Iter& last, const Ch* s, std::streamsize n) { while (cur != last && n-- > 0) *cur++ = *s++; if (cur == last && n > 0) boost::throw_exception(write_area_exhausted()); return n; } }; template<> struct range_adapter_impl { template static std::streamsize read (Iter& cur, Iter& last, Ch* s,std::streamsize n) { std::streamsize result = (std::min)(static_cast(last - cur), n); if (result) std::copy(cur, cur + result, s); cur += result; return result != 0 ? result : -1; } template static std::streamsize write (Iter& cur, Iter& last, const Ch* s, std::streamsize n) { std::streamsize count = (std::min)(static_cast(last - cur), n); std::copy(s, s + count, cur); cur += count; if (count < n) boost::throw_exception(write_area_exhausted()); return n; } template static void seek ( Iter& first, Iter& cur, Iter& last, stream_offset off, BOOST_IOS::seekdir way ) { using namespace std; switch (way) { case BOOST_IOS::beg: if (off > last - first || off < 0) boost::throw_exception(bad_seek()); cur = first + off; break; case BOOST_IOS::cur: { std::ptrdiff_t newoff = cur - first + off; if (newoff > last - first || newoff < 0) boost::throw_exception(bad_seek()); cur += off; break; } case BOOST_IOS::end: if (last - first + off < 0 || off > 0) boost::throw_exception(bad_seek()); cur = last + off; break; default: BOOST_ASSERT(0); } } }; } } } // End namespaces detail, iostreams, boost. #include // MSVC. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_RANGE_ADAPTER_HPP_INCLUDED //---------------//