source: NonGTP/Boost/boost/iostreams/detail/streambuf/chainbuf.hpp @ 857

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