direct_streambuf.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_DIRECT_STREAMBUF_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/assert.hpp>
  12. #include <cstddef>
  13. #include <utility> // pair.
  14. #include <boost/config.hpp> // BOOST_DEDUCED_TYPENAME,
  15. #include <boost/core/typeinfo.hpp>
  16. #include <boost/iostreams/detail/char_traits.hpp> // member template friends.
  17. #include <boost/iostreams/detail/config/wide_streams.hpp>
  18. #include <boost/iostreams/detail/error.hpp>
  19. #include <boost/iostreams/detail/execute.hpp>
  20. #include <boost/iostreams/detail/functional.hpp>
  21. #include <boost/iostreams/detail/ios.hpp>
  22. #include <boost/iostreams/detail/optional.hpp>
  23. #include <boost/iostreams/detail/streambuf.hpp>
  24. #include <boost/iostreams/detail/streambuf/linked_streambuf.hpp>
  25. #include <boost/iostreams/operations.hpp>
  26. #include <boost/iostreams/positioning.hpp>
  27. #include <boost/iostreams/traits.hpp>
  28. #include <boost/throw_exception.hpp>
  29. // Must come last.
  30. #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
  31. namespace boost { namespace iostreams {
  32. namespace detail {
  33. template< typename T,
  34. typename Tr =
  35. BOOST_IOSTREAMS_CHAR_TRAITS(
  36. BOOST_DEDUCED_TYPENAME char_type_of<T>::type
  37. ) >
  38. class direct_streambuf
  39. : public linked_streambuf<BOOST_DEDUCED_TYPENAME char_type_of<T>::type, Tr>
  40. {
  41. public:
  42. typedef typename char_type_of<T>::type char_type;
  43. BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
  44. private:
  45. typedef linked_streambuf<char_type, traits_type> base_type;
  46. typedef typename category_of<T>::type category;
  47. typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(
  48. char_type, traits_type
  49. ) streambuf_type;
  50. public: // stream needs access.
  51. void open(const T& t, std::streamsize buffer_size,
  52. std::streamsize pback_size);
  53. bool is_open() const;
  54. void close();
  55. bool auto_close() const { return auto_close_; }
  56. void set_auto_close(bool close) { auto_close_ = close; }
  57. bool strict_sync() { return true; }
  58. // Declared in linked_streambuf.
  59. T* component() { return storage_.get(); }
  60. protected:
  61. BOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
  62. direct_streambuf();
  63. //--------------Virtual functions-----------------------------------------//
  64. // Declared in linked_streambuf.
  65. void close_impl(BOOST_IOS::openmode m);
  66. const boost::core::typeinfo& component_type() const { return BOOST_CORE_TYPEID(T); }
  67. void* component_impl() { return component(); }
  68. #ifdef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
  69. public:
  70. #endif
  71. // Declared in basic_streambuf.
  72. int_type underflow();
  73. int_type pbackfail(int_type c);
  74. int_type overflow(int_type c);
  75. pos_type seekoff( off_type off, BOOST_IOS::seekdir way,
  76. BOOST_IOS::openmode which );
  77. pos_type seekpos(pos_type sp, BOOST_IOS::openmode which);
  78. private:
  79. pos_type seek_impl( stream_offset off, BOOST_IOS::seekdir way,
  80. BOOST_IOS::openmode which );
  81. void init_input(any_tag) { }
  82. void init_input(input);
  83. void init_output(any_tag) { }
  84. void init_output(output);
  85. void init_get_area();
  86. void init_put_area();
  87. bool one_head() const;
  88. bool two_head() const;
  89. optional<T> storage_;
  90. char_type *ibeg_, *iend_, *obeg_, *oend_;
  91. bool auto_close_;
  92. };
  93. //------------------Implementation of direct_streambuf------------------------//
  94. template<typename T, typename Tr>
  95. direct_streambuf<T, Tr>::direct_streambuf()
  96. : ibeg_(0), iend_(0), obeg_(0), oend_(0), auto_close_(true)
  97. { this->set_true_eof(true); }
  98. template<typename T, typename Tr>
  99. void direct_streambuf<T, Tr>::open
  100. (const T& t, std::streamsize, std::streamsize)
  101. {
  102. storage_.reset(t);
  103. init_input(category());
  104. init_output(category());
  105. setg(0, 0, 0);
  106. setp(0, 0);
  107. this->set_needs_close();
  108. }
  109. template<typename T, typename Tr>
  110. bool direct_streambuf<T, Tr>::is_open() const
  111. { return ibeg_ != 0 || obeg_ != 0; }
  112. template<typename T, typename Tr>
  113. void direct_streambuf<T, Tr>::close()
  114. {
  115. base_type* self = this;
  116. detail::execute_all( detail::call_member_close(*self, BOOST_IOS::in),
  117. detail::call_member_close(*self, BOOST_IOS::out),
  118. detail::call_reset(storage_) );
  119. }
  120. template<typename T, typename Tr>
  121. typename direct_streambuf<T, Tr>::int_type
  122. direct_streambuf<T, Tr>::underflow()
  123. {
  124. if (!ibeg_)
  125. boost::throw_exception(cant_read());
  126. if (!gptr())
  127. init_get_area();
  128. return gptr() != iend_ ?
  129. traits_type::to_int_type(*gptr()) :
  130. traits_type::eof();
  131. }
  132. template<typename T, typename Tr>
  133. typename direct_streambuf<T, Tr>::int_type
  134. direct_streambuf<T, Tr>::pbackfail(int_type c)
  135. {
  136. using namespace std;
  137. if (!ibeg_)
  138. boost::throw_exception(cant_read());
  139. if (gptr() != 0 && gptr() != ibeg_) {
  140. gbump(-1);
  141. if (!traits_type::eq_int_type(c, traits_type::eof()))
  142. *gptr() = traits_type::to_char_type(c);
  143. return traits_type::not_eof(c);
  144. }
  145. boost::throw_exception(bad_putback());
  146. }
  147. template<typename T, typename Tr>
  148. typename direct_streambuf<T, Tr>::int_type
  149. direct_streambuf<T, Tr>::overflow(int_type c)
  150. {
  151. using namespace std;
  152. if (!obeg_)
  153. boost::throw_exception(BOOST_IOSTREAMS_FAILURE("no write access"));
  154. if (!pptr()) init_put_area();
  155. if (!traits_type::eq_int_type(c, traits_type::eof())) {
  156. if (pptr() == oend_)
  157. boost::throw_exception(
  158. BOOST_IOSTREAMS_FAILURE("write area exhausted")
  159. );
  160. *pptr() = traits_type::to_char_type(c);
  161. pbump(1);
  162. return c;
  163. }
  164. return traits_type::not_eof(c);
  165. }
  166. template<typename T, typename Tr>
  167. inline typename direct_streambuf<T, Tr>::pos_type
  168. direct_streambuf<T, Tr>::seekoff
  169. (off_type off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
  170. { return seek_impl(off, way, which); }
  171. template<typename T, typename Tr>
  172. inline typename direct_streambuf<T, Tr>::pos_type
  173. direct_streambuf<T, Tr>::seekpos
  174. (pos_type sp, BOOST_IOS::openmode which)
  175. {
  176. return seek_impl(position_to_offset(sp), BOOST_IOS::beg, which);
  177. }
  178. template<typename T, typename Tr>
  179. void direct_streambuf<T, Tr>::close_impl(BOOST_IOS::openmode which)
  180. {
  181. if (which == BOOST_IOS::in && ibeg_ != 0) {
  182. setg(0, 0, 0);
  183. ibeg_ = iend_ = 0;
  184. }
  185. if (which == BOOST_IOS::out && obeg_ != 0) {
  186. sync();
  187. setp(0, 0);
  188. obeg_ = oend_ = 0;
  189. }
  190. boost::iostreams::close(*storage_, which);
  191. }
  192. template<typename T, typename Tr>
  193. typename direct_streambuf<T, Tr>::pos_type direct_streambuf<T, Tr>::seek_impl
  194. (stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
  195. {
  196. using namespace std;
  197. BOOST_IOS::openmode both = BOOST_IOS::in | BOOST_IOS::out;
  198. if (two_head() && (which & both) == both)
  199. boost::throw_exception(bad_seek());
  200. stream_offset result = -1;
  201. bool one = one_head();
  202. if (one && (pptr() != 0 || gptr()== 0))
  203. init_get_area(); // Switch to input mode, for code reuse.
  204. if (one || ((which & BOOST_IOS::in) != 0 && ibeg_ != 0)) {
  205. if (!gptr()) setg(ibeg_, ibeg_, iend_);
  206. ptrdiff_t next = 0;
  207. switch (way) {
  208. case BOOST_IOS::beg: next = off; break;
  209. case BOOST_IOS::cur: next = (gptr() - ibeg_) + off; break;
  210. case BOOST_IOS::end: next = (iend_ - ibeg_) + off; break;
  211. default: BOOST_ASSERT(0);
  212. }
  213. if (next < 0 || next > (iend_ - ibeg_))
  214. boost::throw_exception(bad_seek());
  215. setg(ibeg_, ibeg_ + next, iend_);
  216. result = next;
  217. }
  218. if (!one && (which & BOOST_IOS::out) != 0 && obeg_ != 0) {
  219. if (!pptr()) setp(obeg_, oend_);
  220. ptrdiff_t next = 0;
  221. switch (way) {
  222. case BOOST_IOS::beg: next = off; break;
  223. case BOOST_IOS::cur: next = (pptr() - obeg_) + off; break;
  224. case BOOST_IOS::end: next = (oend_ - obeg_) + off; break;
  225. default: BOOST_ASSERT(0);
  226. }
  227. if (next < 0 || next > (oend_ - obeg_))
  228. boost::throw_exception(bad_seek());
  229. pbump(static_cast<int>(next - (pptr() - obeg_)));
  230. result = next;
  231. }
  232. return offset_to_position(result);
  233. }
  234. template<typename T, typename Tr>
  235. void direct_streambuf<T, Tr>::init_input(input)
  236. {
  237. std::pair<char_type*, char_type*> p = input_sequence(*storage_);
  238. ibeg_ = p.first;
  239. iend_ = p.second;
  240. }
  241. template<typename T, typename Tr>
  242. void direct_streambuf<T, Tr>::init_output(output)
  243. {
  244. std::pair<char_type*, char_type*> p = output_sequence(*storage_);
  245. obeg_ = p.first;
  246. oend_ = p.second;
  247. }
  248. template<typename T, typename Tr>
  249. void direct_streambuf<T, Tr>::init_get_area()
  250. {
  251. setg(ibeg_, ibeg_, iend_);
  252. if (one_head() && pptr()) {
  253. gbump(static_cast<int>(pptr() - obeg_));
  254. setp(0, 0);
  255. }
  256. }
  257. template<typename T, typename Tr>
  258. void direct_streambuf<T, Tr>::init_put_area()
  259. {
  260. setp(obeg_, oend_);
  261. if (one_head() && gptr()) {
  262. pbump(static_cast<int>(gptr() - ibeg_));
  263. setg(0, 0, 0);
  264. }
  265. }
  266. template<typename T, typename Tr>
  267. inline bool direct_streambuf<T, Tr>::one_head() const
  268. { return ibeg_ && obeg_ && ibeg_ == obeg_; }
  269. template<typename T, typename Tr>
  270. inline bool direct_streambuf<T, Tr>::two_head() const
  271. { return ibeg_ && obeg_ && ibeg_ != obeg_; }
  272. //----------------------------------------------------------------------------//
  273. } // End namespace detail.
  274. } } // End namespaces iostreams, boost.
  275. #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
  276. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED