linked_streambuf.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_LINKED_STREAMBUF_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_LINKED_STREAMBUF_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/config.hpp> // member template friends.
  12. #include <boost/core/typeinfo.hpp>
  13. #include <boost/iostreams/detail/char_traits.hpp>
  14. #include <boost/iostreams/detail/ios.hpp> // openmode.
  15. #include <boost/iostreams/detail/streambuf.hpp>
  16. // Must come last.
  17. #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
  18. namespace boost { namespace iostreams { namespace detail {
  19. template<typename Self, typename Ch, typename Tr, typename Alloc, typename Mode>
  20. class chain_base;
  21. template<typename Chain, typename Access, typename Mode> class chainbuf;
  22. #define BOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base) \
  23. using base::eback; using base::gptr; using base::egptr; \
  24. using base::setg; using base::gbump; using base::pbase; \
  25. using base::pptr; using base::epptr; using base::setp; \
  26. using base::pbump; using base::underflow; using base::pbackfail; \
  27. using base::xsgetn; using base::overflow; using base::xsputn; \
  28. using base::sync; using base::seekoff; using base::seekpos; \
  29. /**/
  30. template<typename Ch, typename Tr = BOOST_IOSTREAMS_CHAR_TRAITS(Ch) >
  31. class linked_streambuf : public BOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, Tr) {
  32. protected:
  33. linked_streambuf() : flags_(0) { }
  34. void set_true_eof(bool eof)
  35. {
  36. flags_ = (flags_ & ~f_true_eof) | (eof ? f_true_eof : 0);
  37. }
  38. public:
  39. // Should be called only after receiving an ordinary EOF indication,
  40. // to confirm that it represents EOF rather than WOULD_BLOCK.
  41. bool true_eof() const { return (flags_ & f_true_eof) != 0; }
  42. protected:
  43. //----------grant friendship to chain_base and chainbuf-------------------//
  44. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  45. template< typename Self, typename ChT, typename TrT,
  46. typename Alloc, typename Mode >
  47. friend class chain_base;
  48. template<typename Chain, typename Mode, typename Access>
  49. friend class chainbuf;
  50. template<typename U>
  51. friend class member_close_operation;
  52. #else
  53. public:
  54. typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(Ch, Tr) base;
  55. BOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base)
  56. #endif
  57. void close(BOOST_IOS::openmode which)
  58. {
  59. if ( which == BOOST_IOS::in &&
  60. (flags_ & f_input_closed) == 0 )
  61. {
  62. flags_ |= f_input_closed;
  63. close_impl(which);
  64. }
  65. if ( which == BOOST_IOS::out &&
  66. (flags_ & f_output_closed) == 0 )
  67. {
  68. flags_ |= f_output_closed;
  69. close_impl(which);
  70. }
  71. }
  72. void set_needs_close()
  73. {
  74. flags_ &= ~(f_input_closed | f_output_closed);
  75. }
  76. virtual void set_next(linked_streambuf<Ch, Tr>* /* next */) { }
  77. virtual void close_impl(BOOST_IOS::openmode) = 0;
  78. virtual bool auto_close() const = 0;
  79. virtual void set_auto_close(bool) = 0;
  80. virtual bool strict_sync() = 0;
  81. virtual const boost::core::typeinfo& component_type() const = 0;
  82. virtual void* component_impl() = 0;
  83. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  84. private:
  85. #else
  86. public:
  87. #endif
  88. private:
  89. enum flag_type {
  90. f_true_eof = 1,
  91. f_input_closed = f_true_eof << 1,
  92. f_output_closed = f_input_closed << 1
  93. };
  94. int flags_;
  95. };
  96. } } } // End namespaces detail, iostreams, boost.
  97. #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
  98. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_LINKED_STREAMBUF_HPP_INCLUDED