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