stream_buffer.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_BROKEN_OVERLOAD_RESOLUTION_STREAM_BUFFER_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_BROKEN_OVERLOAD_RESOLUTION_STREAM_BUFFER_HPP_INCLUDED
  8. #include <boost/iostreams/detail/broken_overload_resolution/forward.hpp>
  9. #include <boost/throw_exception.hpp>
  10. namespace boost { namespace iostreams {
  11. template< typename T,
  12. typename Tr =
  13. BOOST_IOSTREAMS_CHAR_TRAITS(
  14. BOOST_DEDUCED_TYPENAME char_type_of<T>::type
  15. ),
  16. typename Alloc =
  17. std::allocator<
  18. BOOST_DEDUCED_TYPENAME char_type_of<T>::type
  19. >,
  20. typename Mode = BOOST_DEDUCED_TYPENAME mode_of<T>::type >
  21. class stream_buffer
  22. : public detail::stream_buffer_traits<T, Tr, Alloc, Mode>::type
  23. {
  24. private:
  25. BOOST_STATIC_ASSERT((
  26. is_convertible<
  27. BOOST_DEDUCED_TYPENAME iostreams::category_of<T>::type, Mode
  28. >::value
  29. ));
  30. typedef typename
  31. detail::stream_buffer_traits<
  32. T, Tr, Alloc, Mode
  33. >::type base_type;
  34. public:
  35. typedef typename char_type_of<T>::type char_type;
  36. struct category
  37. : Mode,
  38. closable_tag,
  39. streambuf_tag
  40. { };
  41. BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
  42. stream_buffer() { }
  43. ~stream_buffer()
  44. {
  45. try {
  46. if (this->is_open() && this->auto_close())
  47. this->close();
  48. } catch (...) { }
  49. }
  50. template<typename U0>
  51. stream_buffer(const U0& u0)
  52. {
  53. open_impl(detail::forward<T, U0>(), u0);
  54. }
  55. template<typename U0, typename U1>
  56. stream_buffer(const U0& u0, const U1& u1)
  57. {
  58. open_impl(detail::forward<T, U0>(), u0, u1);
  59. }
  60. template<typename U0, typename U1, typename U2>
  61. stream_buffer(const U0& u0, const U1& u1, const U2& u2)
  62. {
  63. open_impl(detail::forward<T, U0>(), u0, u1, u2);
  64. }
  65. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------------//
  66. template<typename U0>
  67. stream_buffer(U0& u0)
  68. {
  69. open_impl(detail::forward<T, U0>(), u0);
  70. }
  71. template<typename U0, typename U1>
  72. stream_buffer(U0& u0, const U1& u1)
  73. {
  74. open_impl(detail::forward<T, U0>(), u0, u1);
  75. }
  76. template<typename U0, typename U1, typename U2>
  77. stream_buffer(U0& u0, const U1& u1, const U2& u2)
  78. {
  79. open_impl(detail::forward<T, U0>(), u0, u1, u2);
  80. }
  81. #endif // !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------//
  82. template<typename U0>
  83. void open(const U0& u0)
  84. {
  85. open_impl(detail::forward<T, U0>(), u0);
  86. }
  87. template<typename U0, typename U1>
  88. void open(const U0& u0, const U1& u1)
  89. {
  90. open_impl(detail::forward<T, U0>(), u0, u1);
  91. }
  92. template<typename U0, typename U1, typename U2>
  93. void open(const U0& u0, const U1& u1, const U2& u2)
  94. {
  95. open_impl(detail::forward<T, U0>(), u0, u1, u2);
  96. }
  97. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------------//
  98. template<typename U0>
  99. void open(U0& u0)
  100. {
  101. open_impl(detail::forward<T, U0>(), u0);
  102. }
  103. template<typename U0, typename U1>
  104. void open(U0& u0, const U1& u1)
  105. {
  106. open_impl(detail::forward<T, U0>(), u0, u1);
  107. }
  108. template<typename U0, typename U1, typename U2>
  109. void open(U0& u0, const U1& u1, const U2& u2)
  110. {
  111. open_impl(detail::forward<T, U0>(), u0, u1, u2);
  112. }
  113. #endif // !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------//
  114. T& operator*() { return *this->component(); }
  115. T* operator->() { return this->component(); }
  116. private:
  117. template<typename U0>
  118. void open_impl(mpl::false_, const U0& u0)
  119. {
  120. base_type::open(const_cast<U0&>(u0), -1, -1);
  121. }
  122. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------------//
  123. template<typename U0>
  124. void open_impl(mpl::false_, U0& u0)
  125. {
  126. base_type::open(detail::wrap(u0), -1, -1);
  127. }
  128. #endif // !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------//
  129. template<typename U0>
  130. void open_impl(mpl::true_, const U0& u0)
  131. {
  132. base_type::open(T(const_cast<U0&>(u0)), -1, -1);
  133. }
  134. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------------//
  135. template<typename U0>
  136. void open_impl(mpl::true_, U0& u0)
  137. {
  138. base_type::open(T(u0), -1, -1);
  139. }
  140. #endif // !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------//
  141. template<typename U0, typename U1>
  142. void open_impl(mpl::false_, const U0& u0, const U1& u1)
  143. {
  144. base_type::open(u0, u1, -1);
  145. }
  146. template<typename U0, typename U1>
  147. void open_impl(mpl::true_, const U0& u0, const U1& u1)
  148. {
  149. base_type::open(T(const_cast<U0&>(u0), u1), -1, -1);
  150. }
  151. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------------//
  152. template<typename U0, typename U1>
  153. void open_impl(mpl::true_, U0& u0, const U1& u1)
  154. {
  155. base_type::open(T(u0, u1), -1, -1);
  156. }
  157. #endif // !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------//
  158. template<typename U0, typename U1, typename U2>
  159. void open_impl(mpl::false_, const U0& u0, const U1& u1, const U2& u2)
  160. {
  161. base_type::open(u0, u1, u2);
  162. }
  163. template<typename U0, typename U1, typename U2>
  164. void open_impl(mpl::true_, const U0& u0, const U1& u1, const U2& u2)
  165. {
  166. base_type::open(T(const_cast<U0&>(u0), u1, u2), -1, -1);
  167. }
  168. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------------//
  169. template<typename U0, typename U1, typename U2>
  170. void open_impl(mpl::true_, U0& u0, const U1& u1, const U2& u2)
  171. {
  172. base_type::open(T(u0, u1, u2), -1, -1);
  173. }
  174. #endif // !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) //---------------------------//
  175. void check_open()
  176. {
  177. if (this->is_open())
  178. boost::throw_exception(BOOST_IOSTREAMS_FAILURE("already open"));
  179. }
  180. };
  181. } } // End namespaces iostreams, boost.
  182. #endif // BOOST_IOSTREAMS_DETAIL_BROKEN_OVERLOAD_RESOLUTION_STREAM_BUFFER_HPP_INCLUDED