indirect_streambuf.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. // This material is heavily indebted to the discussion and code samples in
  7. // A. Langer and K. Kreft, "Standard C++ IOStreams and Locales",
  8. // Addison-Wesley, 2000, pp. 228-43.
  9. // User "GMSB" provided an optimization for small seeks.
  10. #ifndef BOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED
  11. #define BOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED
  12. #include <algorithm> // min, max.
  13. #include <cassert>
  14. #include <exception>
  15. #include <typeinfo>
  16. #include <boost/config.hpp> // Member template friends.
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/iostreams/constants.hpp>
  19. #include <boost/iostreams/detail/adapter/concept_adapter.hpp>
  20. #include <boost/iostreams/detail/buffer.hpp>
  21. #include <boost/iostreams/detail/config/wide_streams.hpp>
  22. #include <boost/iostreams/detail/double_object.hpp>
  23. #include <boost/iostreams/detail/execute.hpp>
  24. #include <boost/iostreams/detail/functional.hpp>
  25. #include <boost/iostreams/detail/ios.hpp>
  26. #include <boost/iostreams/detail/optional.hpp>
  27. #include <boost/iostreams/detail/push.hpp>
  28. #include <boost/iostreams/detail/streambuf/linked_streambuf.hpp>
  29. #include <boost/iostreams/operations.hpp>
  30. #include <boost/iostreams/positioning.hpp>
  31. #include <boost/iostreams/traits.hpp>
  32. #include <boost/iostreams/operations.hpp>
  33. #include <boost/mpl/if.hpp>
  34. #include <boost/throw_exception.hpp>
  35. #include <boost/type_traits/is_convertible.hpp>
  36. // Must come last.
  37. #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC, BCC 5.x
  38. namespace boost { namespace iostreams { namespace detail {
  39. //
  40. // Description: The implementation of basic_streambuf used by chains.
  41. //
  42. template<typename T, typename Tr, typename Alloc, typename Mode>
  43. class indirect_streambuf
  44. : public linked_streambuf<BOOST_DEDUCED_TYPENAME char_type_of<T>::type, Tr>
  45. {
  46. public:
  47. typedef typename char_type_of<T>::type char_type;
  48. BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
  49. private:
  50. typedef typename category_of<T>::type category;
  51. typedef concept_adapter<T> wrapper;
  52. typedef detail::basic_buffer<char_type, Alloc> buffer_type;
  53. typedef indirect_streambuf<T, Tr, Alloc, Mode> my_type;
  54. typedef detail::linked_streambuf<char_type, traits_type> base_type;
  55. typedef linked_streambuf<char_type, Tr> streambuf_type;
  56. public:
  57. indirect_streambuf();
  58. void open(const T& t BOOST_IOSTREAMS_PUSH_PARAMS());
  59. bool is_open() const;
  60. void close();
  61. bool auto_close() const;
  62. void set_auto_close(bool close);
  63. bool strict_sync();
  64. // Declared in linked_streambuf.
  65. T* component() { return &*obj(); }
  66. protected:
  67. #if !BOOST_WORKAROUND(__GNUC__, == 2)
  68. BOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
  69. #endif
  70. //----------virtual functions---------------------------------------------//
  71. #ifndef BOOST_IOSTREAMS_NO_LOCALE
  72. void imbue(const std::locale& loc);
  73. #endif
  74. #ifdef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
  75. public:
  76. #endif
  77. int_type underflow();
  78. int_type pbackfail(int_type c);
  79. int_type overflow(int_type c);
  80. int sync();
  81. pos_type seekoff( off_type off, BOOST_IOS::seekdir way,
  82. BOOST_IOS::openmode which );
  83. pos_type seekpos(pos_type sp, BOOST_IOS::openmode which);
  84. // Declared in linked_streambuf.
  85. void set_next(streambuf_type* next);
  86. void close_impl(BOOST_IOS::openmode m);
  87. const std::type_info& component_type() const { return typeid(T); }
  88. void* component_impl() { return component(); }
  89. private:
  90. //----------Accessor functions--------------------------------------------//
  91. wrapper& obj() { return *storage_; }
  92. streambuf_type* next() const { return next_; }
  93. buffer_type& in() { return buffer_.first(); }
  94. buffer_type& out() { return buffer_.second(); }
  95. bool can_read() const { return is_convertible<Mode, input>::value; }
  96. bool can_write() const { return is_convertible<Mode, output>::value; }
  97. bool output_buffered() const { return (flags_ & f_output_buffered) != 0; }
  98. bool shared_buffer() const { return is_convertible<Mode, seekable>::value; }
  99. void set_flags(int f) { flags_ = f; }
  100. //----------State changing functions--------------------------------------//
  101. virtual void init_get_area();
  102. virtual void init_put_area();
  103. //----------Utility function----------------------------------------------//
  104. pos_type seek_impl( stream_offset off, BOOST_IOS::seekdir way,
  105. BOOST_IOS::openmode which );
  106. void sync_impl();
  107. enum flag_type {
  108. f_open = 1,
  109. f_output_buffered = f_open << 1,
  110. f_auto_close = f_output_buffered << 1
  111. };
  112. optional<wrapper> storage_;
  113. streambuf_type* next_;
  114. double_object<
  115. buffer_type,
  116. is_convertible<
  117. Mode,
  118. two_sequence
  119. >
  120. > buffer_;
  121. std::streamsize pback_size_;
  122. int flags_;
  123. };
  124. //--------------Implementation of indirect_streambuf--------------------------//
  125. template<typename T, typename Tr, typename Alloc, typename Mode>
  126. indirect_streambuf<T, Tr, Alloc, Mode>::indirect_streambuf()
  127. : next_(0), pback_size_(0), flags_(f_auto_close) { }
  128. //--------------Implementation of open, is_open and close---------------------//
  129. template<typename T, typename Tr, typename Alloc, typename Mode>
  130. void indirect_streambuf<T, Tr, Alloc, Mode>::open
  131. (const T& t, std::streamsize buffer_size, std::streamsize pback_size)
  132. {
  133. using namespace std;
  134. // Normalize buffer sizes.
  135. buffer_size =
  136. (buffer_size != -1) ?
  137. buffer_size :
  138. iostreams::optimal_buffer_size(t);
  139. pback_size =
  140. (pback_size != -1) ?
  141. pback_size :
  142. default_pback_buffer_size;
  143. // Construct input buffer.
  144. if (can_read()) {
  145. pback_size_ = (std::max)(std::streamsize(2), pback_size); // STLPort needs 2.
  146. std::streamsize size =
  147. pback_size_ +
  148. ( buffer_size ? buffer_size: 1 );
  149. in().resize(size);
  150. if (!shared_buffer())
  151. init_get_area();
  152. }
  153. // Construct output buffer.
  154. if (can_write() && !shared_buffer()) {
  155. if (buffer_size != 0)
  156. out().resize(buffer_size);
  157. init_put_area();
  158. }
  159. storage_.reset(wrapper(t));
  160. flags_ |= f_open;
  161. if (can_write() && buffer_size > 1)
  162. flags_ |= f_output_buffered;
  163. this->set_true_eof(false);
  164. this->set_needs_close();
  165. }
  166. template<typename T, typename Tr, typename Alloc, typename Mode>
  167. inline bool indirect_streambuf<T, Tr, Alloc, Mode>::is_open() const
  168. { return (flags_ & f_open) != 0; }
  169. template<typename T, typename Tr, typename Alloc, typename Mode>
  170. void indirect_streambuf<T, Tr, Alloc, Mode>::close()
  171. {
  172. using namespace std;
  173. base_type* self = this;
  174. detail::execute_all(
  175. detail::call_member_close(*self, BOOST_IOS::in),
  176. detail::call_member_close(*self, BOOST_IOS::out),
  177. detail::call_reset(storage_),
  178. detail::clear_flags(flags_)
  179. );
  180. }
  181. template<typename T, typename Tr, typename Alloc, typename Mode>
  182. bool indirect_streambuf<T, Tr, Alloc, Mode>::auto_close() const
  183. { return (flags_ & f_auto_close) != 0; }
  184. template<typename T, typename Tr, typename Alloc, typename Mode>
  185. void indirect_streambuf<T, Tr, Alloc, Mode>::set_auto_close(bool close)
  186. { flags_ = (flags_ & ~f_auto_close) | (close ? f_auto_close : 0); }
  187. //--------------Implementation virtual functions------------------------------//
  188. #ifndef BOOST_IOSTREAMS_NO_LOCALE
  189. template<typename T, typename Tr, typename Alloc, typename Mode>
  190. void indirect_streambuf<T, Tr, Alloc, Mode>::imbue(const std::locale& loc)
  191. {
  192. if (is_open()) {
  193. obj().imbue(loc);
  194. if (next_)
  195. next_->pubimbue(loc);
  196. }
  197. }
  198. #endif
  199. template<typename T, typename Tr, typename Alloc, typename Mode>
  200. typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
  201. indirect_streambuf<T, Tr, Alloc, Mode>::underflow()
  202. {
  203. using namespace std;
  204. if (!gptr()) init_get_area();
  205. buffer_type& buf = in();
  206. if (gptr() < egptr()) return traits_type::to_int_type(*gptr());
  207. // Fill putback buffer.
  208. std::streamsize keep =
  209. (std::min)( static_cast<std::streamsize>(gptr() - eback()),
  210. pback_size_ );
  211. if (keep)
  212. traits_type::move( buf.data() + (pback_size_ - keep),
  213. gptr() - keep, keep );
  214. // Set pointers to reasonable values in case read throws.
  215. setg( buf.data() + pback_size_ - keep,
  216. buf.data() + pback_size_,
  217. buf.data() + pback_size_ );
  218. // Read from source.
  219. std::streamsize chars =
  220. obj().read(buf.data() + pback_size_, buf.size() - pback_size_, next_);
  221. if (chars == -1) {
  222. this->set_true_eof(true);
  223. chars = 0;
  224. }
  225. setg(eback(), gptr(), buf.data() + pback_size_ + chars);
  226. return chars != 0 ?
  227. traits_type::to_int_type(*gptr()) :
  228. traits_type::eof();
  229. }
  230. template<typename T, typename Tr, typename Alloc, typename Mode>
  231. typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
  232. indirect_streambuf<T, Tr, Alloc, Mode>::pbackfail(int_type c)
  233. {
  234. if (gptr() != eback()) {
  235. gbump(-1);
  236. if (!traits_type::eq_int_type(c, traits_type::eof()))
  237. *gptr() = traits_type::to_char_type(c);
  238. return traits_type::not_eof(c);
  239. } else {
  240. boost::throw_exception(bad_putback());
  241. }
  242. }
  243. template<typename T, typename Tr, typename Alloc, typename Mode>
  244. typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
  245. indirect_streambuf<T, Tr, Alloc, Mode>::overflow(int_type c)
  246. {
  247. if ( (output_buffered() && pptr() == 0) ||
  248. (shared_buffer() && gptr() != 0) )
  249. {
  250. init_put_area();
  251. }
  252. if (!traits_type::eq_int_type(c, traits_type::eof())) {
  253. if (output_buffered()) {
  254. if (pptr() == epptr()) {
  255. sync_impl();
  256. if (pptr() == epptr())
  257. return traits_type::eof();
  258. }
  259. *pptr() = traits_type::to_char_type(c);
  260. pbump(1);
  261. } else {
  262. char_type d = traits_type::to_char_type(c);
  263. if (obj().write(&d, 1, next_) != 1)
  264. return traits_type::eof();
  265. }
  266. }
  267. return traits_type::not_eof(c);
  268. }
  269. template<typename T, typename Tr, typename Alloc, typename Mode>
  270. int indirect_streambuf<T, Tr, Alloc, Mode>::sync()
  271. {
  272. try { // sync() is no-throw.
  273. sync_impl();
  274. obj().flush(next_);
  275. return 0;
  276. } catch (...) { return -1; }
  277. }
  278. template<typename T, typename Tr, typename Alloc, typename Mode>
  279. bool indirect_streambuf<T, Tr, Alloc, Mode>::strict_sync()
  280. {
  281. try { // sync() is no-throw.
  282. sync_impl();
  283. return obj().flush(next_);
  284. } catch (...) { return false; }
  285. }
  286. template<typename T, typename Tr, typename Alloc, typename Mode>
  287. inline typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
  288. indirect_streambuf<T, Tr, Alloc, Mode>::seekoff
  289. (off_type off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
  290. { return seek_impl(off, way, which); }
  291. template<typename T, typename Tr, typename Alloc, typename Mode>
  292. inline typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
  293. indirect_streambuf<T, Tr, Alloc, Mode>::seekpos
  294. (pos_type sp, BOOST_IOS::openmode which)
  295. {
  296. return seek_impl(position_to_offset(sp), BOOST_IOS::beg, which);
  297. }
  298. template<typename T, typename Tr, typename Alloc, typename Mode>
  299. typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
  300. indirect_streambuf<T, Tr, Alloc, Mode>::seek_impl
  301. (stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
  302. {
  303. if ( gptr() != 0 && way == BOOST_IOS::cur && which == BOOST_IOS::in &&
  304. eback() - gptr() <= off && off <= egptr() - gptr() )
  305. { // Small seek optimization
  306. gbump(off);
  307. return obj().seek(0, BOOST_IOS::cur, BOOST_IOS::in, next_) -
  308. static_cast<off_type>(egptr() - gptr());
  309. }
  310. if (pptr() != 0)
  311. this->BOOST_IOSTREAMS_PUBSYNC(); // sync() confuses VisualAge 6.
  312. if (way == BOOST_IOS::cur && gptr())
  313. off -= static_cast<off_type>(egptr() - gptr());
  314. setg(0, 0, 0);
  315. setp(0, 0);
  316. return obj().seek(off, way, which, next_);
  317. }
  318. template<typename T, typename Tr, typename Alloc, typename Mode>
  319. inline void indirect_streambuf<T, Tr, Alloc, Mode>::set_next
  320. (streambuf_type* next)
  321. { next_ = next; }
  322. template<typename T, typename Tr, typename Alloc, typename Mode>
  323. inline void indirect_streambuf<T, Tr, Alloc, Mode>::close_impl
  324. (BOOST_IOS::openmode which)
  325. {
  326. if (which == BOOST_IOS::in && is_convertible<Mode, input>::value) {
  327. setg(0, 0, 0);
  328. }
  329. if (which == BOOST_IOS::out && is_convertible<Mode, output>::value) {
  330. sync();
  331. setp(0, 0);
  332. }
  333. if ( !is_convertible<category, dual_use>::value ||
  334. is_convertible<Mode, input>::value == (which == BOOST_IOS::in) )
  335. {
  336. obj().close(which, next_);
  337. }
  338. }
  339. //----------State changing functions------------------------------------------//
  340. template<typename T, typename Tr, typename Alloc, typename Mode>
  341. void indirect_streambuf<T, Tr, Alloc, Mode>::sync_impl()
  342. {
  343. std::streamsize avail, amt;
  344. if ((avail = static_cast<std::streamsize>(pptr() - pbase())) > 0) {
  345. if ((amt = obj().write(pbase(), avail, next())) == avail)
  346. setp(out().begin(), out().end());
  347. else {
  348. const char_type* ptr = pptr();
  349. setp(out().begin() + amt, out().end());
  350. pbump(ptr - pptr());
  351. }
  352. }
  353. }
  354. template<typename T, typename Tr, typename Alloc, typename Mode>
  355. void indirect_streambuf<T, Tr, Alloc, Mode>::init_get_area()
  356. {
  357. if (shared_buffer() && pptr() != 0) {
  358. sync_impl();
  359. setp(0, 0);
  360. }
  361. setg(in().begin(), in().begin(), in().begin());
  362. }
  363. template<typename T, typename Tr, typename Alloc, typename Mode>
  364. void indirect_streambuf<T, Tr, Alloc, Mode>::init_put_area()
  365. {
  366. using namespace std;
  367. if (shared_buffer() && gptr() != 0)
  368. setg(0, 0, 0);
  369. if (output_buffered())
  370. setp(out().begin(), out().end());
  371. else
  372. setp(0, 0);
  373. }
  374. //----------------------------------------------------------------------------//
  375. } } } // End namespaces detail, iostreams, boost.
  376. #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC, BCC 5.x
  377. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED