stream_buffer.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_STREAM_BUFFER_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_STREAM_BUFFER_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <memory> // allocator.
  12. #include <boost/config.hpp> // BOOST_DEDUCED_TYPENAME.
  13. #include <boost/iostreams/detail/char_traits.hpp>
  14. #include <boost/iostreams/detail/config/overload_resolution.hpp>
  15. #include <boost/iostreams/detail/forward.hpp>
  16. #include <boost/iostreams/detail/ios.hpp> // failure, streamsize.
  17. #include <boost/iostreams/detail/streambuf/direct_streambuf.hpp>
  18. #include <boost/iostreams/detail/streambuf/indirect_streambuf.hpp>
  19. #include <boost/iostreams/traits.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include <boost/throw_exception.hpp>
  22. #include <boost/type_traits/is_convertible.hpp>
  23. // Must come last.
  24. #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
  25. namespace boost { namespace iostreams { namespace detail {
  26. template<typename T, typename Tr, typename Alloc, typename Mode>
  27. struct stream_buffer_traits {
  28. typedef typename
  29. mpl::if_<
  30. is_convertible<
  31. BOOST_DEDUCED_TYPENAME category_of<T>::type,
  32. direct_tag
  33. >,
  34. direct_streambuf<T, Tr>,
  35. indirect_streambuf<T, Tr, Alloc, Mode>
  36. >::type type;
  37. };
  38. } } } // End namespaces detail, iostreams, boost
  39. #ifdef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION
  40. # include <boost/iostreams/detail/broken_overload_resolution/stream_buffer.hpp>
  41. #else
  42. namespace boost { namespace iostreams {
  43. template< typename T,
  44. typename Tr =
  45. BOOST_IOSTREAMS_CHAR_TRAITS(
  46. BOOST_DEDUCED_TYPENAME char_type_of<T>::type
  47. ),
  48. typename Alloc =
  49. std::allocator<
  50. BOOST_DEDUCED_TYPENAME char_type_of<T>::type
  51. >,
  52. typename Mode = BOOST_DEDUCED_TYPENAME mode_of<T>::type >
  53. class stream_buffer
  54. : public detail::stream_buffer_traits<T, Tr, Alloc, Mode>::type
  55. {
  56. private:
  57. BOOST_STATIC_ASSERT((
  58. is_convertible<
  59. BOOST_DEDUCED_TYPENAME iostreams::category_of<T>::type, Mode
  60. >::value
  61. ));
  62. typedef typename
  63. detail::stream_buffer_traits<
  64. T, Tr, Alloc, Mode
  65. >::type base_type;
  66. public:
  67. typedef typename char_type_of<T>::type char_type;
  68. struct category
  69. : Mode,
  70. closable_tag,
  71. streambuf_tag
  72. { };
  73. BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
  74. public:
  75. stream_buffer() { }
  76. ~stream_buffer()
  77. {
  78. try {
  79. if (this->is_open() && this->auto_close())
  80. this->close();
  81. } catch (...) { }
  82. }
  83. BOOST_IOSTREAMS_FORWARD( stream_buffer, open_impl, T,
  84. BOOST_IOSTREAMS_PUSH_PARAMS,
  85. BOOST_IOSTREAMS_PUSH_ARGS )
  86. T& operator*() { return *this->component(); }
  87. T* operator->() { return this->component(); }
  88. private:
  89. void open_impl(const T& t BOOST_IOSTREAMS_PUSH_PARAMS())
  90. { // Used for forwarding.
  91. if (this->is_open())
  92. boost::throw_exception(
  93. BOOST_IOSTREAMS_FAILURE("already open")
  94. );
  95. base_type::open(t BOOST_IOSTREAMS_PUSH_ARGS());
  96. }
  97. };
  98. } } // End namespaces iostreams, boost.
  99. #endif // #ifdef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION
  100. #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
  101. #endif // #ifndef BOOST_IOSTREAMS_STREAM_BUFFER_HPP_INCLUDED