1 //-----------------------------------------------------------------------------
2 // boost detail/templated_streams.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
6 // Copyright (c) 2013 John Maddock, Antony Polukhin
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)
13 #ifndef BOOST_DETAIL_BASIC_POINTERBUF_HPP
14 #define BOOST_DETAIL_BASIC_POINTERBUF_HPP
16 // MS compatible compilers support #pragma once
21 #include "boost/config.hpp"
24 namespace boost { namespace detail {
27 // class basic_pointerbuf:
28 // acts as a stream buffer which wraps around a pair of pointers:
30 template <class charT, class BufferT >
31 class basic_pointerbuf : public BufferT {
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;
42 basic_pointerbuf() : base_type() { setbuf(0, 0); }
43 const charT* getnext() { return this->gptr(); }
45 #ifndef BOOST_NO_USING_TEMPLATE
46 using base_type::pptr;
47 using base_type::pbase;
49 charT* pptr() const { return base_type::pptr(); }
50 charT* pbase() const { return base_type::pbase(); }
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);
63 basic_pointerbuf& operator=(const basic_pointerbuf&);
64 basic_pointerbuf(const basic_pointerbuf&);
67 template<class charT, class BufferT>
69 basic_pointerbuf<charT, BufferT>::setbuf(char_type* s, streamsize n)
71 this->setg(s, s, s + n);
75 template<class charT, class BufferT>
76 typename basic_pointerbuf<charT, BufferT>::pos_type
77 basic_pointerbuf<charT, BufferT>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
79 typedef typename boost::int_t<sizeof(way) * CHAR_BIT>::least cast_type;
81 if(which & ::std::ios_base::out)
82 return pos_type(off_type(-1));
83 std::ptrdiff_t size = this->egptr() - this->eback();
84 std::ptrdiff_t pos = this->gptr() - this->eback();
85 charT* g = this->eback();
86 switch(static_cast<cast_type>(way))
88 case ::std::ios_base::beg:
89 if((off < 0) || (off > size))
90 return pos_type(off_type(-1));
92 this->setg(g, g + off, g + size);
94 case ::std::ios_base::end:
95 if((off < 0) || (off > size))
96 return pos_type(off_type(-1));
98 this->setg(g, g + size - off, g + size);
100 case ::std::ios_base::cur:
102 std::ptrdiff_t newpos = static_cast<std::ptrdiff_t>(pos + off);
103 if((newpos < 0) || (newpos > size))
104 return pos_type(off_type(-1));
106 this->setg(g, g + newpos, g + size);
112 #pragma warning(push)
113 #pragma warning(disable:4244)
115 return static_cast<pos_type>(this->gptr() - this->eback());
121 template<class charT, class BufferT>
122 typename basic_pointerbuf<charT, BufferT>::pos_type
123 basic_pointerbuf<charT, BufferT>::seekpos(pos_type sp, ::std::ios_base::openmode which)
125 if(which & ::std::ios_base::out)
126 return pos_type(off_type(-1));
127 off_type size = static_cast<off_type>(this->egptr() - this->eback());
128 charT* g = this->eback();
129 if(off_type(sp) <= size)
131 this->setg(g, g + off_type(sp), g + size);
133 return pos_type(off_type(-1));
136 }} // namespace boost::detail
138 #endif // BOOST_DETAIL_BASIC_POINTERBUF_HPP