filtering_stream.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2004-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_FILTER_STREAM_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <memory> // allocator.
  12. #include <boost/iostreams/detail/access_control.hpp>
  13. #include <boost/iostreams/detail/char_traits.hpp>
  14. #include <boost/iostreams/detail/iostream.hpp> // standard streams.
  15. #include <boost/iostreams/detail/push.hpp>
  16. #include <boost/iostreams/detail/select.hpp>
  17. #include <boost/iostreams/detail/streambuf.hpp> // pubsync.
  18. #include <boost/iostreams/filtering_streambuf.hpp>
  19. #include <boost/mpl/and.hpp>
  20. #include <boost/mpl/bool.hpp>
  21. #include <boost/static_assert.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 {
  26. //--------------Definition of filtered_istream--------------------------------//
  27. namespace detail {
  28. template<typename Mode, typename Ch, typename Tr>
  29. struct filtering_stream_traits {
  30. typedef typename
  31. iostreams::select< // Disambiguation for Tru64
  32. mpl::and_<
  33. is_convertible<Mode, input>,
  34. is_convertible<Mode, output>
  35. >,
  36. BOOST_IOSTREAMS_BASIC_IOSTREAM(Ch, Tr),
  37. is_convertible<Mode, input>,
  38. BOOST_IOSTREAMS_BASIC_ISTREAM(Ch, Tr),
  39. else_,
  40. BOOST_IOSTREAMS_BASIC_OSTREAM(Ch, Tr)
  41. >::type stream_type;
  42. typedef typename
  43. iostreams::select< // Dismbiguation required for Tru64.
  44. mpl::and_<
  45. is_convertible<Mode, input>,
  46. is_convertible<Mode, output>
  47. >,
  48. iostream_tag,
  49. is_convertible<Mode, input>,
  50. istream_tag,
  51. else_,
  52. ostream_tag
  53. >::type stream_tag;
  54. };
  55. #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
  56. # pragma warning(push)
  57. // https://connect.microsoft.com/VisualStudio/feedback/details/733720/
  58. # pragma warning(disable: 4250)
  59. #endif
  60. template<typename Chain, typename Access>
  61. class filtering_stream_base
  62. : public access_control<
  63. boost::iostreams::detail::chain_client<Chain>,
  64. Access
  65. >,
  66. public filtering_stream_traits<
  67. typename Chain::mode,
  68. typename Chain::char_type,
  69. typename Chain::traits_type
  70. >::stream_type
  71. {
  72. public:
  73. typedef Chain chain_type;
  74. typedef access_control<
  75. boost::iostreams::detail::chain_client<Chain>,
  76. Access
  77. > client_type;
  78. protected:
  79. typedef typename
  80. filtering_stream_traits<
  81. typename Chain::mode,
  82. typename Chain::char_type,
  83. typename Chain::traits_type
  84. >::stream_type stream_type;
  85. filtering_stream_base() : stream_type(0) { this->set_chain(&chain_); }
  86. private:
  87. void notify() { this->rdbuf(chain_.empty() ? 0 : &chain_.front()); }
  88. Chain chain_;
  89. };
  90. #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
  91. # pragma warning(pop)
  92. #endif
  93. } // End namespace detail.
  94. //
  95. // Macro: BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(name_, chain_type_, default_char_)
  96. // Description: Defines a template derived from std::basic_streambuf which uses
  97. // a chain to perform i/o. The template has the following parameters:
  98. // Mode - the i/o mode.
  99. // Ch - The character type.
  100. // Tr - The character traits type.
  101. // Alloc - The allocator type.
  102. // Access - Indicates accessibility of the chain interface; must be either
  103. // public_ or protected_; defaults to public_.
  104. // Macro parameters:
  105. // name_ - The name of the template to be defined.
  106. // chain_type_ - The name of the chain template.
  107. // default_char_ - The default value for the char template parameter.
  108. //
  109. #define BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(name_, chain_type_, default_char_) \
  110. template< typename Mode, \
  111. typename Ch = default_char_, \
  112. typename Tr = BOOST_IOSTREAMS_CHAR_TRAITS(Ch), \
  113. typename Alloc = std::allocator<Ch>, \
  114. typename Access = public_ > \
  115. class name_ \
  116. : public boost::iostreams::detail::filtering_stream_base< \
  117. chain_type_<Mode, Ch, Tr, Alloc>, Access \
  118. > \
  119. { \
  120. public: \
  121. typedef Ch char_type; \
  122. struct category \
  123. : Mode, \
  124. closable_tag, \
  125. detail::filtering_stream_traits<Mode, Ch, Tr>::stream_tag \
  126. { }; \
  127. BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr) \
  128. typedef Mode mode; \
  129. typedef chain_type_<Mode, Ch, Tr, Alloc> chain_type; \
  130. name_() { } \
  131. BOOST_IOSTREAMS_DEFINE_PUSH_CONSTRUCTOR(name_, mode, Ch, push_impl) \
  132. ~name_() { \
  133. if (this->is_complete()) \
  134. this->rdbuf()->BOOST_IOSTREAMS_PUBSYNC(); \
  135. } \
  136. private: \
  137. typedef access_control< \
  138. boost::iostreams::detail::chain_client< \
  139. chain_type_<Mode, Ch, Tr, Alloc> \
  140. >, \
  141. Access \
  142. > client_type; \
  143. template<typename T> \
  144. void push_impl(const T& t BOOST_IOSTREAMS_PUSH_PARAMS()) \
  145. { client_type::push(t BOOST_IOSTREAMS_PUSH_ARGS()); } \
  146. }; \
  147. /**/
  148. #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
  149. # pragma warning(push)
  150. // https://connect.microsoft.com/VisualStudio/feedback/details/733720/
  151. # pragma warning(disable: 4250)
  152. #endif
  153. BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(filtering_stream, boost::iostreams::chain, char)
  154. BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(wfiltering_stream, boost::iostreams::chain, wchar_t)
  155. #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
  156. # pragma warning(pop)
  157. #endif
  158. typedef filtering_stream<input> filtering_istream;
  159. typedef filtering_stream<output> filtering_ostream;
  160. typedef wfiltering_stream<input> filtering_wistream;
  161. typedef wfiltering_stream<output> filtering_wostream;
  162. //----------------------------------------------------------------------------//
  163. } } // End namespace iostreams, boost
  164. #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
  165. #endif // #ifndef BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED