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.) 5 6 // See http://www.boost.org/libs/iostreams for documentation. 7 8 #ifndef BOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED 9 #define BOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED 10 11 #if defined(_MSC_VER) 12 # pragma once 13 #endif 14 15 #include <boost/config.hpp> // BOOST_MSVC, template friends. 16 #include <boost/detail/workaround.hpp> 17 #include <boost/iostreams/chain.hpp> 18 #include <boost/iostreams/detail/access_control.hpp> 19 #include <boost/iostreams/detail/config/wide_streams.hpp> 20 #include <boost/iostreams/detail/streambuf.hpp> 21 #include <boost/iostreams/detail/streambuf/linked_streambuf.hpp> 22 #include <boost/iostreams/detail/translate_int_type.hpp> 23 #include <boost/iostreams/traits.hpp> 24 #include <boost/noncopyable.hpp> 25 26 namespace boost { namespace iostreams { namespace detail { 27 28 //--------------Definition of chainbuf----------------------------------------// 29 30 // 31 // Template name: chainbuf. 32 // Description: Stream buffer which operates by delegating to the first 33 // linked_streambuf in a chain. 34 // Template parameters: 35 // Chain - The chain type. 36 // 37 template<typename Chain, typename Mode, typename Access> 38 class chainbuf 39 : public BOOST_IOSTREAMS_BASIC_STREAMBUF( 40 typename Chain::char_type, 41 typename Chain::traits_type 42 ), 43 public access_control<typename Chain::client_type, Access>, 44 private noncopyable 45 { 46 private: 47 typedef access_control<chain_client<Chain>, Access> client_type; 48 public: 49 typedef typename Chain::char_type char_type; 50 BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(typename Chain::traits_type) 51 protected: 52 typedef linked_streambuf<char_type, traits_type> delegate_type; chainbuf()53 chainbuf() { client_type::set_chain(&chain_); } underflow()54 int_type underflow() 55 { sentry t(this); return translate(delegate().underflow()); } pbackfail(int_type c)56 int_type pbackfail(int_type c) 57 { sentry t(this); return translate(delegate().pbackfail(c)); } xsgetn(char_type * s,std::streamsize n)58 std::streamsize xsgetn(char_type* s, std::streamsize n) 59 { sentry t(this); return delegate().xsgetn(s, n); } overflow(int_type c)60 int_type overflow(int_type c) 61 { sentry t(this); return translate(delegate().overflow(c)); } xsputn(const char_type * s,std::streamsize n)62 std::streamsize xsputn(const char_type* s, std::streamsize n) 63 { sentry t(this); return delegate().xsputn(s, n); } sync()64 int sync() { sentry t(this); return delegate().sync(); } seekoff(off_type off,BOOST_IOS::seekdir way,BOOST_IOS::openmode which=BOOST_IOS::in|BOOST_IOS::out)65 pos_type seekoff( off_type off, BOOST_IOS::seekdir way, 66 BOOST_IOS::openmode which = 67 BOOST_IOS::in | BOOST_IOS::out ) 68 { sentry t(this); return delegate().seekoff(off, way, which); } seekpos(pos_type sp,BOOST_IOS::openmode which=BOOST_IOS::in|BOOST_IOS::out)69 pos_type seekpos( pos_type sp, 70 BOOST_IOS::openmode which = 71 BOOST_IOS::in | BOOST_IOS::out ) 72 { sentry t(this); return delegate().seekpos(sp, which); } 73 protected: 74 typedef BOOST_IOSTREAMS_BASIC_STREAMBUF( 75 typename Chain::char_type, 76 typename Chain::traits_type 77 ) base_type; 78 private: 79 80 // Translate from std int_type to chain's int_type. 81 typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) std_traits; 82 typedef typename Chain::traits_type chain_traits; 83 static typename chain_traits::int_type translate(typename std_traits::int_type c)84 translate(typename std_traits::int_type c) 85 { return translate_int_type<std_traits, chain_traits>(c); } 86 delegate()87 delegate_type& delegate() 88 { return static_cast<delegate_type&>(chain_.front()); } get_pointers()89 void get_pointers() 90 { 91 this->setg(delegate().eback(), delegate().gptr(), delegate().egptr()); 92 this->setp(delegate().pbase(), delegate().epptr()); 93 this->pbump((int) (delegate().pptr() - delegate().pbase())); 94 } set_pointers()95 void set_pointers() 96 { 97 delegate().setg(this->eback(), this->gptr(), this->egptr()); 98 delegate().setp(this->pbase(), this->epptr()); 99 delegate().pbump((int) (this->pptr() - this->pbase())); 100 } 101 struct sentry { sentryboost::iostreams::detail::chainbuf::sentry102 sentry(chainbuf<Chain, Mode, Access>* buf) : buf_(buf) 103 { buf_->set_pointers(); } ~sentryboost::iostreams::detail::chainbuf::sentry104 ~sentry() { buf_->get_pointers(); } 105 chainbuf<Chain, Mode, Access>* buf_; 106 }; 107 friend struct sentry; 108 Chain chain_; 109 }; 110 111 } } } // End namespaces detail, iostreams, boost. 112 113 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED 114