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

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