Line data Source code
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 : 13 : #ifndef BOOST_DETAIL_BASIC_POINTERBUF_HPP 14 : #define BOOST_DETAIL_BASIC_POINTERBUF_HPP 15 : 16 : // MS compatible compilers support #pragma once 17 : #if defined(_MSC_VER) 18 : # pragma once 19 : #endif 20 : 21 : #include "boost/config.hpp" 22 : #include <streambuf> 23 : 24 : namespace boost { namespace detail { 25 : 26 : // 27 : // class basic_pointerbuf: 28 : // acts as a stream buffer which wraps around a pair of pointers: 29 : // 30 : template <class charT, class BufferT > 31 0 : class basic_pointerbuf : public BufferT { 32 : protected: 33 : typedef BufferT base_type; 34 : typedef basic_pointerbuf<charT, BufferT> this_type; 35 : typedef typename base_type::int_type int_type; 36 : typedef typename base_type::char_type char_type; 37 : typedef typename base_type::pos_type pos_type; 38 : typedef ::std::streamsize streamsize; 39 : typedef typename base_type::off_type off_type; 40 : 41 : public: 42 0 : basic_pointerbuf() : base_type() { this_type::setbuf(0, 0); } 43 : const charT* getnext() { return this->gptr(); } 44 : 45 : #ifndef BOOST_NO_USING_TEMPLATE 46 : using base_type::pptr; 47 : using base_type::pbase; 48 : #else 49 : charT* pptr() const { return base_type::pptr(); } 50 : charT* pbase() const { return base_type::pbase(); } 51 : #endif 52 : 53 : protected: 54 : // VC mistakenly assumes that `setbuf` and other functions are not referenced. 55 : // Marking those functions with `inline` suppresses the warnings. 56 : // There must be no harm from marking virtual functions as inline: inline virtual 57 : // call can be inlined ONLY when the compiler knows the "exact class". 58 : inline base_type* setbuf(char_type* s, streamsize n); 59 : inline typename this_type::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which); 60 : inline typename this_type::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which); 61 : 62 : private: 63 : basic_pointerbuf& operator=(const basic_pointerbuf&); 64 : basic_pointerbuf(const basic_pointerbuf&); 65 : }; 66 : 67 : template<class charT, class BufferT> 68 : BufferT* 69 0 : basic_pointerbuf<charT, BufferT>::setbuf(char_type* s, streamsize n) 70 : { 71 0 : this->setg(s, s, s + n); 72 0 : return this; 73 : } 74 : 75 : template<class charT, class BufferT> 76 : typename basic_pointerbuf<charT, BufferT>::pos_type 77 0 : basic_pointerbuf<charT, BufferT>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which) 78 : { 79 : typedef typename boost::int_t<sizeof(way) * CHAR_BIT>::least cast_type; 80 : 81 0 : if(which & ::std::ios_base::out) 82 0 : return pos_type(off_type(-1)); 83 0 : std::ptrdiff_t size = this->egptr() - this->eback(); 84 0 : std::ptrdiff_t pos = this->gptr() - this->eback(); 85 0 : charT* g = this->eback(); 86 0 : switch(static_cast<cast_type>(way)) 87 : { 88 0 : case ::std::ios_base::beg: 89 0 : if((off < 0) || (off > size)) 90 0 : return pos_type(off_type(-1)); 91 : else 92 0 : this->setg(g, g + off, g + size); 93 : break; 94 0 : case ::std::ios_base::end: 95 0 : if((off < 0) || (off > size)) 96 0 : return pos_type(off_type(-1)); 97 : else 98 0 : this->setg(g, g + size - off, g + size); 99 : break; 100 0 : case ::std::ios_base::cur: 101 : { 102 0 : std::ptrdiff_t newpos = static_cast<std::ptrdiff_t>(pos + off); 103 0 : if((newpos < 0) || (newpos > size)) 104 0 : return pos_type(off_type(-1)); 105 : else 106 0 : this->setg(g, g + newpos, g + size); 107 : break; 108 : } 109 0 : default: ; 110 : } 111 : #ifdef BOOST_MSVC 112 : #pragma warning(push) 113 : #pragma warning(disable:4244) 114 : #endif 115 0 : return static_cast<pos_type>(this->gptr() - this->eback()); 116 : #ifdef BOOST_MSVC 117 : #pragma warning(pop) 118 : #endif 119 : } 120 : 121 : template<class charT, class BufferT> 122 : typename basic_pointerbuf<charT, BufferT>::pos_type 123 0 : basic_pointerbuf<charT, BufferT>::seekpos(pos_type sp, ::std::ios_base::openmode which) 124 : { 125 0 : if(which & ::std::ios_base::out) 126 0 : return pos_type(off_type(-1)); 127 0 : off_type size = static_cast<off_type>(this->egptr() - this->eback()); 128 0 : charT* g = this->eback(); 129 0 : if(off_type(sp) <= size) 130 : { 131 0 : this->setg(g, g + off_type(sp), g + size); 132 : } 133 : return pos_type(off_type(-1)); 134 : } 135 : 136 : }} // namespace boost::detail 137 : 138 : #endif // BOOST_DETAIL_BASIC_POINTERBUF_HPP 139 :