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) && (_MSC_VER >= 1020)
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 //#if !BOOST_WORKAROUND(__GNUC__, == 2)
79 //    BOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
80 //#endif
81 private:
82 
83     // Translate from std int_type to chain's int_type.
84     typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type)           std_traits;
85     typedef typename Chain::traits_type                      chain_traits;
86     static typename chain_traits::int_type
translate(typename std_traits::int_type c)87     translate(typename std_traits::int_type c)
88         { return translate_int_type<std_traits, chain_traits>(c); }
89 
delegate()90     delegate_type& delegate()
91         { return static_cast<delegate_type&>(chain_.front()); }
get_pointers()92     void get_pointers()
93         {
94             this->setg(delegate().eback(), delegate().gptr(), delegate().egptr());
95             this->setp(delegate().pbase(), delegate().epptr());
96             this->pbump((int) (delegate().pptr() - delegate().pbase()));
97         }
set_pointers()98     void set_pointers()
99         {
100             delegate().setg(this->eback(), this->gptr(), this->egptr());
101             delegate().setp(this->pbase(), this->epptr());
102             delegate().pbump((int) (this->pptr() - this->pbase()));
103         }
104     struct sentry {
sentryboost::iostreams::detail::chainbuf::sentry105         sentry(chainbuf<Chain, Mode, Access>* buf) : buf_(buf)
106             { buf_->set_pointers(); }
~sentryboost::iostreams::detail::chainbuf::sentry107         ~sentry() { buf_->get_pointers(); }
108         chainbuf<Chain, Mode, Access>* buf_;
109     };
110     friend struct sentry;
111     Chain chain_;
112 };
113 
114 } } } // End namespaces detail, iostreams, boost.
115 
116 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED
117