basic_pointerbuf.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //-----------------------------------------------------------------------------
  2. // boost detail/templated_streams.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2013 John Maddock, Antony Polukhin
  7. //
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_DETAIL_BASIC_POINTERBUF_HPP
  13. #define BOOST_DETAIL_BASIC_POINTERBUF_HPP
  14. // MS compatible compilers support #pragma once
  15. #if defined(_MSC_VER)
  16. # pragma once
  17. #endif
  18. #include <boost/config.hpp>
  19. #include <boost/integer.hpp>
  20. #include <streambuf>
  21. namespace boost { namespace detail {
  22. //
  23. // class basic_pointerbuf:
  24. // acts as a stream buffer which wraps around a pair of pointers:
  25. //
  26. template <class charT, class BufferT >
  27. class basic_pointerbuf : public BufferT {
  28. protected:
  29. typedef BufferT base_type;
  30. typedef basic_pointerbuf<charT, BufferT> this_type;
  31. typedef typename base_type::int_type int_type;
  32. typedef typename base_type::char_type char_type;
  33. typedef typename base_type::pos_type pos_type;
  34. typedef ::std::streamsize streamsize;
  35. typedef typename base_type::off_type off_type;
  36. public:
  37. basic_pointerbuf() : base_type() { this_type::setbuf(0, 0); }
  38. const charT* getnext() { return this->gptr(); }
  39. using base_type::pptr;
  40. using base_type::pbase;
  41. protected:
  42. // VC mistakenly assumes that `setbuf` and other functions are not referenced.
  43. // Marking those functions with `inline` suppresses the warnings.
  44. // There must be no harm from marking virtual functions as inline: inline virtual
  45. // call can be inlined ONLY when the compiler knows the "exact class".
  46. inline base_type* setbuf(char_type* s, streamsize n) BOOST_OVERRIDE;
  47. inline typename this_type::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which) BOOST_OVERRIDE;
  48. inline typename this_type::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which) BOOST_OVERRIDE;
  49. private:
  50. basic_pointerbuf& operator=(const basic_pointerbuf&);
  51. basic_pointerbuf(const basic_pointerbuf&);
  52. };
  53. template<class charT, class BufferT>
  54. BufferT*
  55. basic_pointerbuf<charT, BufferT>::setbuf(char_type* s, streamsize n)
  56. {
  57. this->setg(s, s, s + n);
  58. return this;
  59. }
  60. template<class charT, class BufferT>
  61. typename basic_pointerbuf<charT, BufferT>::pos_type
  62. basic_pointerbuf<charT, BufferT>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
  63. {
  64. typedef typename boost::int_t<sizeof(way) * CHAR_BIT>::least cast_type;
  65. if(which & ::std::ios_base::out)
  66. return pos_type(off_type(-1));
  67. std::ptrdiff_t size = this->egptr() - this->eback();
  68. std::ptrdiff_t pos = this->gptr() - this->eback();
  69. charT* g = this->eback();
  70. switch(static_cast<cast_type>(way))
  71. {
  72. case ::std::ios_base::beg:
  73. if((off < 0) || (off > size))
  74. return pos_type(off_type(-1));
  75. else
  76. this->setg(g, g + off, g + size);
  77. break;
  78. case ::std::ios_base::end:
  79. if((off < 0) || (off > size))
  80. return pos_type(off_type(-1));
  81. else
  82. this->setg(g, g + size - off, g + size);
  83. break;
  84. case ::std::ios_base::cur:
  85. {
  86. std::ptrdiff_t newpos = static_cast<std::ptrdiff_t>(pos + off);
  87. if((newpos < 0) || (newpos > size))
  88. return pos_type(off_type(-1));
  89. else
  90. this->setg(g, g + newpos, g + size);
  91. break;
  92. }
  93. default: ;
  94. }
  95. #ifdef BOOST_MSVC
  96. #pragma warning(push)
  97. #pragma warning(disable:4244)
  98. #endif
  99. return static_cast<pos_type>(this->gptr() - this->eback());
  100. #ifdef BOOST_MSVC
  101. #pragma warning(pop)
  102. #endif
  103. }
  104. template<class charT, class BufferT>
  105. typename basic_pointerbuf<charT, BufferT>::pos_type
  106. basic_pointerbuf<charT, BufferT>::seekpos(pos_type sp, ::std::ios_base::openmode which)
  107. {
  108. if(which & ::std::ios_base::out)
  109. return pos_type(off_type(-1));
  110. off_type size = static_cast<off_type>(this->egptr() - this->eback());
  111. charT* g = this->eback();
  112. if(off_type(sp) <= size)
  113. {
  114. this->setg(g, g + off_type(sp), g + size);
  115. }
  116. return pos_type(off_type(-1));
  117. }
  118. }} // namespace boost::detail
  119. #endif // BOOST_DETAIL_BASIC_POINTERBUF_HPP