StreamBuffer.h
1/*
2 * Copyright (C) 2012 Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_StreamBuffer_h
30#define Pt_StreamBuffer_h
31
32#include <Pt/Api.h>
33#include <Pt/String.h>
34#include <streambuf>
35#include <cstddef>
36
37#if defined(_MSC_VER) && defined(_WIN32_WCE)
38 // alternatively compile with /FORCE:multiple
39 template class PT_API std::basic_streambuf<char>;
40#endif
41
42#if defined(_MSC_VER)
43 template class PT_API std::basic_streambuf<Pt::Char>;
44#endif
45
46namespace Pt {
47
73template <typename CharT, typename TraitsT = std::char_traits<CharT> >
74class BasicStreamBuffer : public std::basic_streambuf<CharT, TraitsT>
75{
76 public:
77 typedef CharT char_type;
78 typedef TraitsT traits_type;
79 typedef typename TraitsT::int_type int_type;
80 typedef typename TraitsT::pos_type pos_type;
81 typedef typename TraitsT::off_type off_type;
82
83 public:
88
95 std::streamsize speekn(CharT* buffer, std::streamsize size)
96 {
97 if(size <= 0)
98 return 0;
99
100 int_type next = 0;
101 if( ! this->gptr() || this->gptr() == this->egptr() )
102 {
103 next = this->underflow();
104
105 if( traits_type::eof() == next)
106 return 0;
107 }
108
109 std::size_t avail = this->gptr() ? this->egptr() - this->gptr() : 0;
110
111 // unbuffered streambufs
112 if(avail == 0)
113 {
114 *buffer = traits_type::to_char_type(next);
115 return 1;
116 }
117
118 std::size_t n = static_cast<std::size_t>(size);
119 if(avail < n)
120 n = avail;
121
122 traits_type::copy(buffer, this->gptr(), n);
123 return size;
124 }
125
128 std::streamsize out_avail()
129 {
130 if( this->pptr() )
131 return this->pptr() - this->pbase();
132
133 return showfull();
134 }
135
136 protected:
141
144 virtual std::streamsize showfull()
145 { return 0; }
146};
147
148#if defined(_MSC_VER)
149 template class PT_API BasicStreamBuffer<char>;
150 template class PT_API BasicStreamBuffer<Char>;
151#endif
152
153} // namespace Pt
154
155#endif
std::streamsize out_avail()
Returns the number of characters buffered for output.
Definition StreamBuffer.h:128
std::streamsize speekn(CharT *buffer, std::streamsize size)
Peeks characters in the stream buffer.
Definition StreamBuffer.h:95
virtual std::streamsize showfull()
Definition StreamBuffer.h:144
BasicStreamBuffer()
Default Constructor.
Definition StreamBuffer.h:139
~BasicStreamBuffer()
Destructor.
Definition StreamBuffer.h:86
Core module.
Definition Allocator.h:33