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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 #ifndef BOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED
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>
26 namespace boost { namespace iostreams { namespace detail {
28 //--------------Definition of chainbuf----------------------------------------//
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.
37 template<typename Chain, typename Mode, typename Access>
39 : public BOOST_IOSTREAMS_BASIC_STREAMBUF(
40 typename Chain::char_type,
41 typename Chain::traits_type
43 public access_control<typename Chain::client_type, Access>,
47 typedef access_control<chain_client<Chain>, Access> client_type;
49 typedef typename Chain::char_type char_type;
50 BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(typename Chain::traits_type)
52 typedef linked_streambuf<char_type, traits_type> delegate_type;
53 chainbuf() { client_type::set_chain(&chain_); }
55 { sentry t(this); return translate(delegate().underflow()); }
56 int_type pbackfail(int_type c)
57 { sentry t(this); return translate(delegate().pbackfail(c)); }
58 std::streamsize xsgetn(char_type* s, std::streamsize n)
59 { sentry t(this); return delegate().xsgetn(s, n); }
60 int_type overflow(int_type c)
61 { sentry t(this); return translate(delegate().overflow(c)); }
62 std::streamsize xsputn(const char_type* s, std::streamsize n)
63 { sentry t(this); return delegate().xsputn(s, n); }
64 int sync() { sentry t(this); return delegate().sync(); }
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); }
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); }
74 typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(
75 typename Chain::char_type,
76 typename Chain::traits_type
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
84 translate(typename std_traits::int_type c)
85 { return translate_int_type<std_traits, chain_traits>(c); }
87 delegate_type& delegate()
88 { return static_cast<delegate_type&>(chain_.front()); }
91 this->setg(delegate().eback(), delegate().gptr(), delegate().egptr());
92 this->setp(delegate().pbase(), delegate().epptr());
93 this->pbump((int) (delegate().pptr() - delegate().pbase()));
97 delegate().setg(this->eback(), this->gptr(), this->egptr());
98 delegate().setp(this->pbase(), this->epptr());
99 delegate().pbump((int) (this->pptr() - this->pbase()));
102 sentry(chainbuf<Chain, Mode, Access>* buf) : buf_(buf)
103 { buf_->set_pointers(); }
104 ~sentry() { buf_->get_pointers(); }
105 chainbuf<Chain, Mode, Access>* buf_;
107 friend struct sentry;
111 } } } // End namespaces detail, iostreams, boost.
113 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED