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
24namespace boost { namespace detail {
25
26//
27// class basic_pointerbuf:
28// acts as a stream buffer which wraps around a pair of pointers:
29//
30template <class charT, class BufferT >
31class basic_pointerbuf : public BufferT {
32protected:
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
41public:
42 basic_pointerbuf() : base_type() { this_type::setbuf(s: 0, n: 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
53protected:
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) BOOST_OVERRIDE;
59 inline typename this_type::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which) BOOST_OVERRIDE;
60 inline typename this_type::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which) BOOST_OVERRIDE;
61
62private:
63 basic_pointerbuf& operator=(const basic_pointerbuf&);
64 basic_pointerbuf(const basic_pointerbuf&);
65};
66
67template<class charT, class BufferT>
68BufferT*
69basic_pointerbuf<charT, BufferT>::setbuf(char_type* s, streamsize n)
70{
71 this->setg(s, s, s + n);
72 return this;
73}
74
75template<class charT, class BufferT>
76typename basic_pointerbuf<charT, BufferT>::pos_type
77basic_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 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))
87 {
88 case ::std::ios_base::beg:
89 if((off < 0) || (off > size))
90 return pos_type(off_type(-1));
91 else
92 this->setg(g, g + off, g + size);
93 break;
94 case ::std::ios_base::end:
95 if((off < 0) || (off > size))
96 return pos_type(off_type(-1));
97 else
98 this->setg(g, g + size - off, g + size);
99 break;
100 case ::std::ios_base::cur:
101 {
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));
105 else
106 this->setg(g, g + newpos, g + size);
107 break;
108 }
109 default: ;
110 }
111#ifdef BOOST_MSVC
112#pragma warning(push)
113#pragma warning(disable:4244)
114#endif
115 return static_cast<pos_type>(this->gptr() - this->eback());
116#ifdef BOOST_MSVC
117#pragma warning(pop)
118#endif
119}
120
121template<class charT, class BufferT>
122typename basic_pointerbuf<charT, BufferT>::pos_type
123basic_pointerbuf<charT, BufferT>::seekpos(pos_type sp, ::std::ios_base::openmode which)
124{
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)
130 {
131 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

source code of include/boost/detail/basic_pointerbuf.hpp