aggregate.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_AGGREGATE_FILTER_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_AGGREGATE_FILTER_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <algorithm> // copy, min.
  12. #include <boost/assert.hpp>
  13. #include <iterator> // back_inserter
  14. #include <vector>
  15. #include <boost/iostreams/constants.hpp> // default_device_buffer_size
  16. #include <boost/iostreams/categories.hpp>
  17. #include <boost/iostreams/detail/char_traits.hpp>
  18. #include <boost/iostreams/detail/ios.hpp> // openmode, streamsize.
  19. #include <boost/iostreams/pipeline.hpp>
  20. #include <boost/iostreams/read.hpp> // check_eof
  21. #include <boost/iostreams/write.hpp>
  22. #include <boost/mpl/bool.hpp>
  23. #include <boost/type_traits/is_convertible.hpp>
  24. // Must come last.
  25. #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
  26. namespace boost { namespace iostreams {
  27. //
  28. // Template name: aggregate_filter.
  29. // Template parameters:
  30. // Ch - The character type.
  31. // Alloc - The allocator type.
  32. // Description: Utility for defining DualUseFilters which filter an
  33. // entire stream at once. To use, override the protected virtual
  34. // member do_filter.
  35. // Note: This filter should not be copied while it is in use.
  36. //
  37. template<typename Ch, typename Alloc = std::allocator<Ch> >
  38. class aggregate_filter {
  39. public:
  40. typedef Ch char_type;
  41. struct category
  42. : dual_use,
  43. filter_tag,
  44. multichar_tag,
  45. closable_tag
  46. { };
  47. aggregate_filter() : ptr_(0), state_(0) { }
  48. virtual ~aggregate_filter() { }
  49. template<typename Source>
  50. std::streamsize read(Source& src, char_type* s, std::streamsize n)
  51. {
  52. using namespace std;
  53. BOOST_ASSERT(!(state_ & f_write));
  54. state_ |= f_read;
  55. if (!(state_ & f_eof))
  56. do_read(src);
  57. std::streamsize amt =
  58. (std::min)(n, static_cast<std::streamsize>(data_.size() - ptr_));
  59. if (amt) {
  60. BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy(s, &data_[ptr_], amt);
  61. ptr_ += amt;
  62. }
  63. return detail::check_eof(amt);
  64. }
  65. template<typename Sink>
  66. std::streamsize write(Sink&, const char_type* s, std::streamsize n)
  67. {
  68. BOOST_ASSERT(!(state_ & f_read));
  69. state_ |= f_write;
  70. data_.insert(data_.end(), s, s + n);
  71. return n;
  72. }
  73. template<typename Sink>
  74. void close(Sink& sink, BOOST_IOS::openmode which)
  75. {
  76. if ((state_ & f_read) != 0 && which == BOOST_IOS::in)
  77. close_impl();
  78. if ((state_ & f_write) != 0 && which == BOOST_IOS::out) {
  79. try {
  80. vector_type filtered;
  81. do_filter(data_, filtered);
  82. do_write(
  83. sink, &filtered[0],
  84. static_cast<std::streamsize>(filtered.size())
  85. );
  86. } catch (...) {
  87. close_impl();
  88. throw;
  89. }
  90. close_impl();
  91. }
  92. }
  93. protected:
  94. typedef std::vector<Ch, Alloc> vector_type;
  95. typedef typename vector_type::size_type size_type;
  96. private:
  97. virtual void do_filter(const vector_type& src, vector_type& dest) = 0;
  98. virtual void do_close() { }
  99. template<typename Source>
  100. void do_read(Source& src)
  101. {
  102. using std::streamsize;
  103. vector_type data;
  104. while (true) {
  105. const std::streamsize size = default_device_buffer_size;
  106. Ch buf[size];
  107. std::streamsize amt;
  108. if ((amt = boost::iostreams::read(src, buf, size)) == -1)
  109. break;
  110. data.insert(data.end(), buf, buf + amt);
  111. }
  112. do_filter(data, data_);
  113. state_ |= f_eof;
  114. }
  115. template<typename Sink>
  116. void do_write(Sink& sink, const char_type* s, std::streamsize n)
  117. {
  118. typedef typename iostreams::category_of<Sink>::type category;
  119. typedef is_convertible<category, output> can_write;
  120. do_write(sink, s, n, can_write());
  121. }
  122. template<typename Sink>
  123. void do_write(Sink& sink, const char_type* s, std::streamsize n, mpl::true_)
  124. { iostreams::write(sink, s, n); }
  125. template<typename Sink>
  126. void do_write(Sink&, const char_type*, std::streamsize, mpl::false_) { }
  127. void close_impl()
  128. {
  129. data_.clear();
  130. ptr_ = 0;
  131. state_ = 0;
  132. do_close();
  133. }
  134. enum flag_type {
  135. f_read = 1,
  136. f_write = f_read << 1,
  137. f_eof = f_write << 1
  138. };
  139. // Note: typically will not be copied while vector contains data.
  140. vector_type data_;
  141. size_type ptr_;
  142. int state_;
  143. };
  144. BOOST_IOSTREAMS_PIPABLE(aggregate_filter, 1)
  145. } } // End namespaces iostreams, boost.
  146. #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
  147. #endif // #ifndef BOOST_IOSTREAMS_AGGREGATE_FILTER_HPP_INCLUDED