chainbuf.hpp 4.6 KB

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