Chris@102: //----------------------------------------------------------------------------- Chris@102: // boost detail/templated_streams.hpp header file Chris@102: // See http://www.boost.org for updates, documentation, and revision history. Chris@102: //----------------------------------------------------------------------------- Chris@102: // Chris@102: // Copyright (c) 2013 John Maddock, Antony Polukhin Chris@102: // Chris@102: // Chris@102: // Distributed under the Boost Software License, Version 1.0. (See Chris@102: // accompanying file LICENSE_1_0.txt or copy at Chris@102: // http://www.boost.org/LICENSE_1_0.txt) Chris@102: Chris@102: #ifndef BOOST_DETAIL_BASIC_POINTERBUF_HPP Chris@102: #define BOOST_DETAIL_BASIC_POINTERBUF_HPP Chris@102: Chris@102: // MS compatible compilers support #pragma once Chris@102: #if defined(_MSC_VER) Chris@102: # pragma once Chris@102: #endif Chris@102: Chris@102: #include "boost/config.hpp" Chris@102: #include Chris@102: Chris@102: namespace boost { namespace detail { Chris@102: Chris@102: // Chris@102: // class basic_pointerbuf: Chris@102: // acts as a stream buffer which wraps around a pair of pointers: Chris@102: // Chris@102: template Chris@102: class basic_pointerbuf : public BufferT { Chris@102: protected: Chris@102: typedef BufferT base_type; Chris@102: typedef basic_pointerbuf this_type; Chris@102: typedef typename base_type::int_type int_type; Chris@102: typedef typename base_type::char_type char_type; Chris@102: typedef typename base_type::pos_type pos_type; Chris@102: typedef ::std::streamsize streamsize; Chris@102: typedef typename base_type::off_type off_type; Chris@102: Chris@102: public: Chris@102: basic_pointerbuf() : base_type() { setbuf(0, 0); } Chris@102: const charT* getnext() { return this->gptr(); } Chris@102: Chris@102: #ifndef BOOST_NO_USING_TEMPLATE Chris@102: using base_type::pptr; Chris@102: using base_type::pbase; Chris@102: #else Chris@102: charT* pptr() const { return base_type::pptr(); } Chris@102: charT* pbase() const { return base_type::pbase(); } Chris@102: #endif Chris@102: Chris@102: protected: Chris@102: // VC mistakenly assumes that `setbuf` and other functions are not referenced. Chris@102: // Marking those functions with `inline` suppresses the warnings. Chris@102: // There must be no harm from marking virtual functions as inline: inline virtual Chris@102: // call can be inlined ONLY when the compiler knows the "exact class". Chris@102: inline base_type* setbuf(char_type* s, streamsize n); Chris@102: inline typename this_type::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which); Chris@102: inline typename this_type::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which); Chris@102: Chris@102: private: Chris@102: basic_pointerbuf& operator=(const basic_pointerbuf&); Chris@102: basic_pointerbuf(const basic_pointerbuf&); Chris@102: }; Chris@102: Chris@102: template Chris@102: BufferT* Chris@102: basic_pointerbuf::setbuf(char_type* s, streamsize n) Chris@102: { Chris@102: this->setg(s, s, s + n); Chris@102: return this; Chris@102: } Chris@102: Chris@102: template Chris@102: typename basic_pointerbuf::pos_type Chris@102: basic_pointerbuf::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which) Chris@102: { Chris@102: typedef typename boost::int_t::least cast_type; Chris@102: Chris@102: if(which & ::std::ios_base::out) Chris@102: return pos_type(off_type(-1)); Chris@102: std::ptrdiff_t size = this->egptr() - this->eback(); Chris@102: std::ptrdiff_t pos = this->gptr() - this->eback(); Chris@102: charT* g = this->eback(); Chris@102: switch(static_cast(way)) Chris@102: { Chris@102: case ::std::ios_base::beg: Chris@102: if((off < 0) || (off > size)) Chris@102: return pos_type(off_type(-1)); Chris@102: else Chris@102: this->setg(g, g + off, g + size); Chris@102: break; Chris@102: case ::std::ios_base::end: Chris@102: if((off < 0) || (off > size)) Chris@102: return pos_type(off_type(-1)); Chris@102: else Chris@102: this->setg(g, g + size - off, g + size); Chris@102: break; Chris@102: case ::std::ios_base::cur: Chris@102: { Chris@102: std::ptrdiff_t newpos = static_cast(pos + off); Chris@102: if((newpos < 0) || (newpos > size)) Chris@102: return pos_type(off_type(-1)); Chris@102: else Chris@102: this->setg(g, g + newpos, g + size); Chris@102: break; Chris@102: } Chris@102: default: ; Chris@102: } Chris@102: #ifdef BOOST_MSVC Chris@102: #pragma warning(push) Chris@102: #pragma warning(disable:4244) Chris@102: #endif Chris@102: return static_cast(this->gptr() - this->eback()); Chris@102: #ifdef BOOST_MSVC Chris@102: #pragma warning(pop) Chris@102: #endif Chris@102: } Chris@102: Chris@102: template Chris@102: typename basic_pointerbuf::pos_type Chris@102: basic_pointerbuf::seekpos(pos_type sp, ::std::ios_base::openmode which) Chris@102: { Chris@102: if(which & ::std::ios_base::out) Chris@102: return pos_type(off_type(-1)); Chris@102: off_type size = static_cast(this->egptr() - this->eback()); Chris@102: charT* g = this->eback(); Chris@102: if(off_type(sp) <= size) Chris@102: { Chris@102: this->setg(g, g + off_type(sp), g + size); Chris@102: } Chris@102: return pos_type(off_type(-1)); Chris@102: } Chris@102: Chris@102: }} // namespace boost::detail Chris@102: Chris@102: #endif // BOOST_DETAIL_BASIC_POINTERBUF_HPP Chris@102: