Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame] | 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 NDNBOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED |
| 9 | #define NDNBOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED |
| 10 | |
| 11 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
| 12 | # pragma once |
| 13 | #endif |
| 14 | |
| 15 | #include <ndnboost/config.hpp> // NDNBOOST_MSVC, template friends. |
| 16 | #include <ndnboost/detail/workaround.hpp> |
| 17 | #include <ndnboost/iostreams/chain.hpp> |
| 18 | #include <ndnboost/iostreams/detail/access_control.hpp> |
| 19 | #include <ndnboost/iostreams/detail/config/wide_streams.hpp> |
| 20 | #include <ndnboost/iostreams/detail/streambuf.hpp> |
| 21 | #include <ndnboost/iostreams/detail/streambuf/linked_streambuf.hpp> |
| 22 | #include <ndnboost/iostreams/detail/translate_int_type.hpp> |
| 23 | #include <ndnboost/iostreams/traits.hpp> |
| 24 | #include <ndnboost/noncopyable.hpp> |
| 25 | |
| 26 | namespace ndnboost { 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 NDNBOOST_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 | NDNBOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(typename Chain::traits_type) |
| 51 | protected: |
| 52 | typedef linked_streambuf<char_type, traits_type> delegate_type; |
| 53 | chainbuf() { client_type::set_chain(&chain_); } |
| 54 | int_type underflow() |
| 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, NDNBOOST_IOS::seekdir way, |
| 66 | NDNBOOST_IOS::openmode which = |
| 67 | NDNBOOST_IOS::in | NDNBOOST_IOS::out ) |
| 68 | { sentry t(this); return delegate().seekoff(off, way, which); } |
| 69 | pos_type seekpos( pos_type sp, |
| 70 | NDNBOOST_IOS::openmode which = |
| 71 | NDNBOOST_IOS::in | NDNBOOST_IOS::out ) |
| 72 | { sentry t(this); return delegate().seekpos(sp, which); } |
| 73 | protected: |
| 74 | typedef NDNBOOST_IOSTREAMS_BASIC_STREAMBUF( |
| 75 | typename Chain::char_type, |
| 76 | typename Chain::traits_type |
| 77 | ) base_type; |
| 78 | //#if !NDNBOOST_WORKAROUND(__GNUC__, == 2) |
| 79 | // NDNBOOST_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 NDNBOOST_IOSTREAMS_CHAR_TRAITS(char_type) std_traits; |
| 85 | typedef typename Chain::traits_type chain_traits; |
| 86 | static typename chain_traits::int_type |
| 87 | translate(typename std_traits::int_type c) |
| 88 | { return translate_int_type<std_traits, chain_traits>(c); } |
| 89 | |
| 90 | delegate_type& delegate() |
| 91 | { return static_cast<delegate_type&>(chain_.front()); } |
| 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 | } |
| 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 { |
| 105 | sentry(chainbuf<Chain, Mode, Access>* buf) : buf_(buf) |
| 106 | { buf_->set_pointers(); } |
| 107 | ~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 NDNBOOST_IOSTREAMS_DETAIL_CHAINBUF_HPP_INCLUDED |